|
本帖最后由 Guofuqiang01 于 2021-4-21 09:00 编辑
- #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[] = "key-----"; //blinker账户生产的密码,小爱同学必须选用阿里云
- //代码配网用下面这两段
- //char ssid[] = "WIFI ssid"; //WiFi账号
- //char pswd[] = "WiFi pswd"; //WiFi密码
- //定义温湿度传感器信号针脚
- #define DHTPIN D3
- //选择传感器类型
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- DHT dht(DHTPIN, DHTTYPE);
- float humi_read, temp_read;
- //定义blinker软件内数据组件键名分别为:humi、temp
- BlinkerNumber HUMI("humi");
- BlinkerNumber TEMP("temp");
- //定义blinker软件中重置esp设置按钮
- BlinkerButton Reset("恢复出厂设置");
- void reset_callback(const String & state){
- BLINKER_LOG("get button state:", state);
- //当长按"恢复出厂设置"释放后清楚配网信息
- if(state == "pressup"){
- Blinker.reset();
- }
- }
- //小米小爱状态回调函数
- 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;
- default :
- BlinkerMIOT.temp(100);
- BlinkerMIOT.humi(100);
- BlinkerMIOT.print();
- break;
- }
- }
- void heartbeat()
- {
- HUMI.print(humi_read);
- TEMP.print(temp_read);
- }
- void dataStorage()
- {
- Blinker.dataStorage("temp", temp_read);
- Blinker.dataStorage("humi", humi_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.attachHeartbeat(heartbeat);
- Blinker.attachDataStorage(dataStorage);
- //注册小爱同学状态回调函数
- BlinkerMIOT.attachQuery(miotQuery);
- Reset.attach(reset_callback);
-
- //初始化温湿度程序
- dht.begin();
- }
- 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);
- }
复制代码
DHT温湿度计程序,配合另外一个帖子解读吧。
|
|