Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ESP8266とBME280でAmbientにデータを5分おきに送信するコード
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include "BME280_MOD-1022.h"
#include "Ambient.h"
extern "C" {
#include "user_interface.h"
}
const char* ssid = "xxxxxxxxxx"; // Wifiの SSID
const char* password = "xxxxxxxxxxxx"; // Wifiのパスワード
unsigned int channelId = xxxx;
const char* writeKey = "xxxxxxxxxxxx";
void printFormattedFloat(float val) {
char buffer[10];
dtostrf(val, 4, 2, buffer);
Serial.print(buffer);
}
void setup() {
Serial.begin(115200);
Wire.begin(13, 14); delay(10); // SDA=GPIO_13,SCL=GPIO_14
BME280.readCompensationParams(); // read the NVM compensation parameters
BME280.writeOversamplingTemperature(os1x); // 1x over sampling
BME280.writeOversamplingHumidity(os1x); // 1x over sampling
BME280.writeOversamplingPressure(os1x); // 1x over sampling
Serial.println();Serial.println();Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500);Serial.print(".");}
Serial.println("");Serial.println("WiFi connected");Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
delay(5000);
WiFiClient client; // Use WiFiClient class to create TCP connections
Ambient ambient;
BME280.writeMode(smForced); // After taking the measurement the chip goes back to sleep
while (BME280.isMeasuring()){Serial.println("Measuring...");delay(50);}
Serial.println("Done!");
BME280.readMeasurements(); // read out the data
float temperature = BME280.getTemperature(); // Temp
float humidity = BME280.getHumidity(); // Humidity
float pressure = BME280.getPressure(); // Pressure
Serial.print("Temperature: ");
printFormattedFloat(temperature);
Serial.println("");
Serial.print("Humidity: ");
printFormattedFloat(humidity);
Serial.println("");
Serial.print("Pressure: ");
printFormattedFloat(pressure);
Serial.println("");
Serial.println("");
ambient.begin(channelId, writeKey, &client);
Serial.println("Ambient start.");
ambient.set(1, temperature);
ambient.set(2, humidity);
ambient.set(3, pressure);
ambient.send();
delay(10);
Serial.println();Serial.println("closing connection. going to sleep...");
delay(1000);
system_deep_sleep_set_option(0); // go to deepsleep for 5 minutes
system_deep_sleep(5 * 60 * 1000000); // 5 min
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment