直接上代码
💡 Tips:需要修改点灯设备key和钉钉通知的token
#include <ESP8266WiFi.h>#include <DNSServer.h>#include <ESP8266WebServer.h>#include <WiFiManager.h>#include <ESP8266HTTPClient.h>#include <ArduinoJson.h>#include <EEPROM.h>#define BLINKER_WIFI //定义wifi模块#define BLINKER_MIOT_SENSOR //小爱同学定义为传感器设备#define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈#include <Blinker.h>//包含Blinker头文件#include <DHT.h>//包含DHT头文件#include <TM1637Display.h> //数显库#define CLK 4 //数显的CLK D2#define DIO 0 //数显的DIN D3TM1637Display display(CLK,DIO);uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };int temp_int = 0;int humi_int = 0;int AlarmRate = 50;int AlarmInt = 1;union int_value{int i; //int类型成员变量byte b[2]; //byte类型数组成员};//温度阈值int_value temp_threshold;//湿度阈值int_value humi_threshold;BlinkerSlider SliderTemp("ran-temp");BlinkerSlider SliderHumi("ran-humi");#define DHTPIN 2 //定义DHT11模块连接管脚io2 (D4)#define DHTTYPE DHT11 // 使用温度湿度模块的类型为DHT11//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321//#define DHTTYPE DHT21 // DHT 21 (AM2301)//char auth[] = "********"; //点灯app中获取到的Secret Key(新建设备的秘钥)String DingDingURL = "https://oapi.dingtalk.com/robot/send?access_token=*****";boolean DingDingSwitch = true;//新建数据类型组件对象,作用:将数据传输到手机blinker appBlinkerNumber HUMI("humi"); //定义湿度数据键名BlinkerNumber TEMP("temp"); //定义温度数据键名DHT dht(DHTPIN, DHTTYPE); //定义dhtfloat humi_read = 0, temp_read = 0; //定义浮点型全局变量 储存传感器读取的温湿度数据//小米小爱状态回调函数void miotQuery(int32_t queryCode){BLINKER_LOG("MIOT Query codes: ", queryCode);int hVal = humi_read; //多次测试湿度须为整数型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_HUMI_NUMBER :BLINKER_LOG("MIOT Query HUMI");BlinkerMIOT.humi(hVal);BlinkerMIOT.print();break;case BLINKER_CMD_QUERY_TEMP_NUMBER :BLINKER_LOG("MIOT Query TEMP");BlinkerMIOT.temp(temp_read);BlinkerMIOT.print();break;default :BlinkerMIOT.temp(20);BlinkerMIOT.humi(20);BlinkerMIOT.pm25(20);BlinkerMIOT.co2(20);BlinkerMIOT.print();break;}}void heartbeat(){//反馈温度数据HUMI.print(humi_read);//设置ui组件图标和颜色HUMI.icon("fas fa-thermometer-2");HUMI.color("#fddb10");//反馈湿度数据TEMP.print(temp_read);TEMP.icon("fas fa-heart");HUMI.color("#fddb01");SliderTemp.print(temp_threshold.i);SliderHumi.print(humi_threshold.i);}void dataStorage(){Blinker.dataStorage("temp", temp_read);Blinker.dataStorage("humi", humi_read);}void setup(){//初始化串口Serial.begin(115200);WiFiManager wifiManager;wifiManager.autoConnect("AutoConnectAP");// 如果您希望该WiFi添加密码,可以使用以下语句:// wifiManager.autoConnect("AutoConnectAP", "12345678");// 以上语句中的12345678是连接AutoConnectAP的密码// WiFi连接成功后将通过串口监视器输出连接成功信息Serial.println("");Serial.print("ESP8266 Connected to ");Serial.println(WiFi.SSID()); // WiFi名称Serial.print("IP address:\t");Serial.println(WiFi.localIP()); // IPBlinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());BLINKER_DEBUG.stream(Serial);BLINKER_DEBUG.debugAll();//LED灯闪动pinMode(LED_BUILTIN, OUTPUT);digitalWrite(LED_BUILTIN, LOW);dht.begin();//初始化DHT传感器//Blinker.begin(auth, ssid, pswd); // // 初始化blinkerBlinker.attachHeartbeat(heartbeat);//将传感器获取的数据传给blinker app上Blinker.attachDataStorage(dataStorage);//调用云函数//在回调函数中反馈该控制状态BlinkerMIOT.attachQuery(miotQuery);//每次呼出小爱同学,就会调用miotQuery()函数//注册滑块回调SliderTemp.attach(slidertemp_callback);SliderHumi.attach(sliderhumi_callback);display.setBrightness(0x0f);// All segments ondisplay.setSegments(data);delay(2000);EEPROM.begin(1024); //开启EEPROM,开辟1024个位空间byte a1=EEPROM.read(0); //a1获取EEPROM 0 位的值byte a2=EEPROM.read(1); //a2获取EEPROM 1 位的值int_value tt;tt.b[0] = a1;tt.b[1] = a2;if(tt.i < 1 || tt.i>50){temp_threshold.i = 27;}else{temp_threshold.i = tt.i;}byte b1=EEPROM.read(2); //a1获取EEPROM 0 位的值byte b2=EEPROM.read(3); //a2获取EEPROM 1 位的值int_value hh;hh.b[0] = b1;hh.b[1] = b2;if(hh.i>100 || hh.i<1 ){humi_threshold.i = 70;}else{humi_threshold.i = hh.i;}Serial.println("End read");}void slidertemp_callback(int32_t value){Blinker.vibrate();///震动BLINKER_LOG("Get temp slider value: ", value);int_value tt;tt.i = value;EEPROM.begin(1024);EEPROM.write(0,tt.b[0]); //给EEPROM 第0位写入temp_threshold.b[0]的值EEPROM.write(1,tt.b[1]); //给EEPROM 第1位写入temp_threshold.b[1]的值EEPROM.commit();temp_threshold.i = tt.i;}void sliderhumi_callback(int32_t value){Blinker.vibrate();///震动BLINKER_LOG("Get humi slider value: ", value);int_value hh;hh.i = value;EEPROM.begin(1024);EEPROM.write(2,hh.b[0]); //给EEPROM 第0位写入temp_threshold.b[0]的值EEPROM.write(3,hh.b[1]); //给EEPROM 第1位写入temp_threshold.b[1]的值EEPROM.commit();humi_threshold.i = hh.i;}//通过循环不断读取温湿度传感器获取的数据void loop(){Blinker.run();float h = dht.readHumidity(true);//读取DHT11传感器的湿度 并赋值给hfloat t = dht.readTemperature(true);//读取传感器的温度 并赋值给tif (isnan(h) || isnan(t))//判断是否成功读取到温湿度数据{BLINKER_LOG("Failed to read from DHT sensor!");//读取温湿度失败!}else{t = dht.convertFtoC(t);//将华氏度转换为摄氏度BLINKER_LOG("Humidity: ", h, " %");BLINKER_LOG("Temperature: ", t, " *C");AlarmInt += 1;if(AlarmInt>AlarmRate){AlarmInt = 1;}if(AlarmInt == 5){if(DingDingSwitch){if( t>temp_threshold.i || h>humi_threshold.i){BLINKER_LOG("发送钉钉告警通知");httpClientRequest(h,t);}}}humi_read = h;//将读取到的湿度赋值给全局变量humi_readtemp_read = t;//将读取到的温度赋值给全局变量temp_read}//TM1637_SHOW(temp_read,humi_read);Blinker.delay(1000);//1秒延时(超过2s 小爱同学可能取不到数据)}void httpClientRequest(float h,float t){WiFiClientSecure client;client.setInsecure();client.connect("oapi.dingtalk.com",443);String jsonRaw = rootJson(h,t);client.print(String("POST ") + DingDingURL + " HTTP/1.1\r\n" +"Host: " + "oapi.dingtalk.com" + "\r\n" +"Content-Type: application/json\r\n" +"Content-Length: " +String(jsonRaw.length()) + "\r\n\r\n");client.print(jsonRaw);delay(6000);//操作结束,断开服务器连接client.stop();Serial.println("closing connection");}String rootJson(float h,float t){//const size_t capacity = JSON_OBJECT_SIZE(1) + 3*JSON_OBJECT_SIZE(3)+140;const size_t capacity = 1*JSON_OBJECT_SIZE(1)+140;DynamicJsonDocument doc(capacity);doc["msgtype"] = "text";JsonObject info = doc.createNestedObject("text");String content = "【温湿度】,温度:";content.concat(t);content.concat(" 湿度:");content.concat(h);info["content"] = content;String jsonCode;serializeJson(doc,jsonCode);return jsonCode;}void TM1637_SHOW(float t,float h){if(temp_int == int(t*10)){BLINKER_LOG("数据未变");return;}else{display.clear();temp_int = int(t*10);int bai = temp_int/100;int shi = (temp_int-100*bai)/10;int ge = temp_int%10;BLINKER_LOG("temp_int:", temp_int, " ");BLINKER_LOG("数据变:", bai, " 百");data[0] = display.encodeDigit(bai);data[1] = display.encodeDigit(shi);data[2] = display.encodeDigit(ge);data[3] = display.encodeDigit(12);display.setSegments(data);}//humi_int = int(h);//display.showNumberDec(t, true, 3, 2); // Expect: 04__//display.showNumberHexEx(0x2c); // Expect: __2C}
点灯界面配置
{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨num¨¨t0¨¨湿度¨¨ico¨¨fad fa-humidity¨¨clr¨¨#076EEF¨¨min¨É¨max¨¢1c¨uni¨´%´¨bg¨É¨cols¨Í¨rows¨Ë¨key¨¨humi¨´x´É´y´É¨speech¨|÷¨lstyle¨Ë}{ßAßBßC¨温度¨ßE¨fas fa-thermometer-three-quarters¨ßGßHßIÉßJº0ßK´℃´ßLÉßMÍßNËßO¨temp¨´x´Í´y´ÉßQ|÷ßRÊ}{ßA¨cha¨ßLɨsty¨¨line¨ßGßH¨sty1¨ßX¨clr1¨¨#EA0909¨¨sty2¨ßX¨clr2¨¨#389BEE¨ßMÑßNÌßOßD´x´É´y´ËßQ|÷ßRɨkey0¨ßPßC¨湿度%¨¨key1¨´´¨t1¨´´}{ßAßVßLÉßWßXßG¨#00A90C¨ßYßXßZßdßbßXßcßdßMÑßNÌßOßS´x´É´y´ÎßQ|÷ßeßUßC¨温度℃¨ßRÉ}{ßA¨deb¨¨mode¨ÉßLÉßMÑßNÌßO¨debug¨´x´É´y´¤C}{ßA¨ran¨ßC¨湿度阈值¨ßGßdßJº0ßIÉßLÉßMÑßNËßO¨ran-humi¨´x´É´y´ÑßQ|÷}{ßAßnßC¨温度阈值¨ßGßdßJ¤oßIÉßLÉßMÑßNËßO¨ran-temp¨´x´É´y´¤AßQ|÷}÷¨actions¨|÷¨triggers¨|÷}
