|
给大家分享一个ESP8266反馈温室的程序
自己根据例程修改
硬件:ESP8266 (nodeMCU、01、01S等均可)、DHT11(DHT22、DHT21均可,可以和01模块匹配的节点器也可以)、辅料杜邦线数据线若干
注意事项:1、Blinker中一定选择阿里云服务器
2、小爱同学状态回调函数中湿度必须是int型,否则反馈出错
- #define BLINKER_WIFI
- #define BLINKER_ESP_SMARTCONFIG //Smartconfig手机配网用这段,代码写入注释这一条
- #define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈
- #define BLINKER_MIOT_SENSOR
- #include <DHT.h>
- #include <Blinker.h>
- char auth[] = "blinker中的key"; //blinker账户生产的密码,小爱同学必须选用阿里云
- //代码配网用下面这两段
- //char ssid[] = "WIFI ssid"; //WiFi账号
- //char pswd[] = "WiFi pswd"; //WiFi密码
- //定义blinker软件内数据组件键名分别为:humi、temp
- BlinkerNumber HUMI("humi");
- BlinkerNumber TEMP("temp");
- //定义温湿度传感器信号针脚
- #define DHTPIN 2
- //选择传感器类型
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- DHT dht(DHTPIN, DHTTYPE);
- uint32_t read_time = 0;
- float humi_read, temp_read;
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
-
- uint32_t BlinkerTime = millis();
-
- Blinker.print("millis", BlinkerTime);
- }
- //小米小爱状态回调函数
- void miotQuery(int32_t queryCode)
- {
- BLINKER_LOG("MIOT Query codes: ", queryCode);
- int hVal = dht.readHumidity(); //多次测试湿度必须为整数型
- switch (queryCode)
- {
- case BLINKER_CMD_QUERY_ALL_NUMBER :
- BLINKER_LOG("MIOT Query All");
- BlinkerMIOT.temp(temp_read);
- BlinkerMIOT.humi(hVal);
- BlinkerMIOT.print();
- break;
- case BLINKER_CMD_QUERY_TEMP_NUMBER :
- BLINKER_LOG("MIOT Query TEMP");
- BlinkerMIOT.temp(temp_read);
- BlinkerMIOT.print();
- break;
- case BLINKER_CMD_QUERY_HUMI_NUMBER :
- BLINKER_LOG("MIOT Query HUMI");
- BlinkerMIOT.humi(hVal);
- BlinkerMIOT.print();
- break;
- default :
- BlinkerMIOT.temp(100);
- BlinkerMIOT.humi(100);
- BlinkerMIOT.print();
- break;
- }
- }
- void heartbeat()
- {
- HUMI.print(humi_read);
- TEMP.print(temp_read);
- }
- void setup()
- {
- //调试程序
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
- Blinker.begin(auth); //手机配网用这段
- //Blinker.begin(auth, ssid, pswd); //代码配网用这段
- Blinker.attachData(dataRead);
- Blinker.attachHeartbeat(heartbeat);
- //注册小爱同学状态回调函数
- BlinkerMIOT.attachQuery(miotQuery);
- dht.begin();
- }
- void loop()
- {
- Blinker.run();
- if (read_time == 0 || (millis() - read_time) >= 2000)
- {
- read_time = millis();
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- BLINKER_LOG("Failed to read from DHT sensor!");
- return;
- }
- humi_read = h;
- temp_read = t;
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- }
- }
复制代码
|
|