|
本帖最后由 38151987 于 2022-3-20 22:36 编辑
每次运行一会就,app上温湿度就获取不到数据了,继电器开关正常用,是不是代码有问题?哪位大神给看看?
我用的是esp01s+esp-01s relay模块+dht11
- #define BLINKER_WIFI
- #define BLINKER_WIFI_MULTI
- #define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈
- #define BLINKER_MIOT_MULTI_OUTLET //支持多路插座,最多4个插孔
- #include <DHT.h>
- #include <Blinker.h>
- #include <Ticker.h>
- #include <EEPROM.h>
- char auth[] = "******";
- char ssid[] = "******";
- char pswd[] = "******";
- Ticker flipper;
- BlinkerButton Button("zkg"); //总开关
- BlinkerButton Button1("jdq"); //继电器开关按钮
- BlinkerButton Button2("zdkg"); //自动模式开关按钮
- BlinkerText Text1("text1");
- BlinkerSlider Slider3("lowtemp"); //滑动条最低温度
- BlinkerSlider Slider4("hightemp"); //滑动条最高温度
- #define DHTPIN 2 //定义温湿度传感器针脚
- int kg=0;//继电器输出针脚
- #define DHTTYPE DHT11 // DHT 11
- DHT dht(DHTPIN, DHTTYPE);
- float humi_read, temp_read;
- BlinkerNumber HUMI("humi"); //湿度调用按钮
- BlinkerNumber TEMP("temp"); //温度调用按钮
- // 在APP控制,按下zkg按键即会执行该函数
- void button_callback(const String & state)
- {
- BLINKER_LOG("操作了zkg: ", state);//APP中的Monitor控件打印的信息
- if (state == BLINKER_CMD_ON)
- {
- Button.print("on"); //总开关状态是开
- digitalWrite(kg,LOW);
- Button1.print("on"); //继电器是开
- zd=1;
- Button2.print("on"); //自动模式是开
- } else if(state == BLINKER_CMD_OFF)
- {
- Button.print("off"); //总开关状态是关
- digitalWrite(kg, HIGH);
- Button1.print("off"); //继电器是关
- zd=0;
- Button2.print("off"); //自动模式是关
- }
- }
- void button1_callback(const String & state)
- {
- BLINKER_LOG("app操作了!: ", state); //串口打印
- if (state == BLINKER_CMD_OFF) { //如果state是on状态
- BLINKER_LOG("关!"); //串口打印
- digitalWrite(kg, HIGH);
- Button1.print("off"); //继电器关
- }
- else if (state == BLINKER_CMD_ON) { //如果state是off状态
- BLINKER_LOG("开!"); //串口打印
- digitalWrite(kg,LOW);
- Button1.print("on"); //继电器开
- }
- }
- void Button2_callback(const String & state3)
- {
- BLINKER_LOG("app操作了!: ", state3); //串口打印
- if (state3 == BLINKER_CMD_OFF) { //如果state是on状态
- BLINKER_LOG("自动模式关"); //串口打印
- zd=0;
- Button2.print("off"); //自动模式关
- }
- else if (state3 == BLINKER_CMD_ON) { //如果state是off状态
- BLINKER_LOG("自动模式开"); //串口打印
- zd=1;
- Button2.print("on"); //自动模式开
- }
- EEPROM.begin(512);
- EEPROM.write(3, zd);
- EEPROM.commit();
- }
- float zuidi=3;
- float zuigao=20;
- void slider3_callback(int32_t value3)
- {
- BLINKER_LOG("get slider value: ", value3);
- zuidi=value3;
- EEPROM.begin(512);
- EEPROM.write(4, zuidi);
- EEPROM.commit();
- }
- void slider4_callback(int32_t value4)
- {
- BLINKER_LOG("get slider value: ", value4);
- zuigao=value4;
- EEPROM.begin(512);
- EEPROM.write(2, zuigao);
- EEPROM.commit();
- }
- void zdms(){
- if(zd==0){
- BLINKER_LOG("自动模式关");
- Button2.print("off");
- }
- if(zd==1){
- BLINKER_LOG("自动模式开");
- Button2.print("on");
- zidong();
- }
-
- }
- void zidong(){
- if(temp_read<zuidi)
- {
- digitalWrite(kg,LOW);
- BLINKER_LOG("低于最低温度,继电器开!");
- BlinkerMIOT.print();
- Button1.print("on"); //反馈回按键状态是开
- }
- if(temp_read>zuigao)
- {
- digitalWrite(kg,HIGH);
- BLINKER_LOG("高于最高温度,继电器关!");
- BlinkerMIOT.print();
- Button1.print("off"); //反馈回按键状态是开
- }
- }
- void heartbeat()
- {
- BLINKER_LOG("状态同步!");
-
- if (digitalRead(kg)==LOW){
- Button.print("on"); //总开关开
- }
- if (digitalRead(kg)==HIGH){
- Button.print("off"); //总开关关
- }
- if (digitalRead(kg)==LOW)
- {
- Button1.print("on"); //继电器开
- }
- if(digitalRead(kg)==HIGH)
- {
- Button1.print("off"); //继电器关
- }
- if (zd==1)
- {
- Button2.print("on"); //自动模式开
- }
- if (zd==0)
- {
- Button2.print("off"); //自动模式关
- }
-
- HUMI.print(humi_read);
- TEMP.print(temp_read);
-
- Slider3.print(zuidi);
- delay(50);
- Slider4.print(zuigao);
- }
- void dataStorage()
- {
- Blinker.dataStorage("temp", temp_read);
- Blinker.dataStorage("humi", humi_read);
- }
- void setup()
- {
- //调试程序
- EEPROM.begin(512);
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- pinMode(kg, OUTPUT);
- digitalWrite(kg, LOW); //默认上电是开 HIGH 时是接通继电器。
- zd = int(EEPROM.read(3));
- delay(50);
- zuidi = int(EEPROM.read(4));
- delay(50);
- zuigao = int(EEPROM.read(2));
- Blinker.begin(auth, ssid, pswd); //代码配网用这段
- Blinker.attachHeartbeat(heartbeat);
- Blinker.attachDataStorage(dataStorage);
- // Blinker.attachRTData(rtData);
- Button1.attach(button1_callback);
- Button2.attach(Button2_callback);
- Button.attach(button_callback); //绑定按键回调
-
- Slider3.attach(slider3_callback);
- Slider4.attach(slider4_callback);
- dht.begin();
- flipper.attach(5, zdms);
- }
- void loop()
- {
- Blinker.run();
-
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t))
- {
- BLINKER_LOG("No DHT!");
- }else
- {
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- humi_read = h;
- temp_read = t;
- }
- Blinker.delay(2000);
- }
复制代码
|
|