|
esp8266+dht11+bmp180温湿度气压数据使用blinke数据记录
使用blinker自带的实例两个合并为一个,esp8266的i2c接口是D1,D2, D1=SCL D2=SDA BMP180使用的是这两接口,bmp180使用3.3v,DHT11可以在定义为D7接口
代码有两个问题,一个是天猫精灵关联后无法获取温湿度数据,第二个历史数据无法记录,单独dht11模块可以存历史数据。两个合并后就无法存储了。
- #define BLINKER_WIFI
- #define BLINKER_ALIGENIE_SENSOR
-
- #include <Blinker.h>
- #include <DHT.h>
-
- char auth[] = "a9135b780e41";
- char ssid[] = "abc";
- char pswd[] = "chenxu123";
-
- BlinkerNumber HUMI("humi"); //DHT11
- BlinkerNumber TEMP11("temp"); //DHT11
- BlinkerNumber TEMP180("temp180"); // Temperature
- BlinkerNumber PRES("pres"); // Pressure
- BlinkerNumber ALTI("alti"); // Altitude
-
- #define DHTPIN D7
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- #include <Wire.h>
- #include <Adafruit_BMP085.h>
- Adafruit_BMP085 bmp;
-
- DHT dht(DHTPIN, DHTTYPE);
-
- float humi_read = 0,temp180_read = 0, temp_read = 0 , pres_read = 0, alti_read = 0 ;
-
- void heartbeat()
- {
- HUMI.print(humi_read);
- TEMP11.print(temp_read);
- TEMP180.print(bmp.readTemperature());
- PRES.print(bmp.readPressure());
- ALTI.print(bmp.readAltitude());
-
- }
- void dataStorage()
- {
- pres_read=bmp.readPressure();
- Blinker.dataStorage("temp", temp_read);
- Blinker.dataStorage("humi", humi_read);
- Blinker.dataStorage("pres", pres_read);
- }
- void aligenieQuery(int32_t queryCode)
- {
- switch (queryCode)
- {
- case BLINKER_CMD_QUERY_ALL_NUMBER :
- BLINKER_LOG("AliGenie Query All");
- BlinkerAliGenie.temp(temp_read);
- BlinkerAliGenie.humi(humi_read);
- BlinkerAliGenie.print();
- break;
- case BLINKER_CMD_QUERY_TEMP_NUMBER :
- BlinkerAliGenie.temp(temp_read);
- BlinkerAliGenie.print();
- break;
- case BLINKER_CMD_QUERY_HUMI_NUMBER :
- BlinkerAliGenie.humi(humi_read);
- BlinkerAliGenie.print();
- break;
- default :
- BlinkerAliGenie.temp(20);
- BlinkerAliGenie.humi(20);
- BlinkerAliGenie.print();
- break;
- }
- }
-
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
-
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachHeartbeat(heartbeat);
- BlinkerAliGenie.attachQuery(aligenieQuery);
- Blinker.attachDataStorage(dataStorage);
- dht.begin();
- if (!bmp.begin()) {
- BLINKER_LOG("Could not find a valid BMP085 sensor, check wiring!");
- while (1) {delay(100);}
- }
- }
-
- void loop()
- {
- Blinker.run();
-
- float h = dht.readHumidity();
- float t = dht.readTemperature();
-
- if (isnan(h) || isnan(t))
- {
- BLINKER_LOG("Failed to read from DHT sensor!");
- }
- else
- {
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- humi_read = h;
- temp_read = t;
- }
-
- Blinker.delay(2000);
- }
复制代码
|
|