关于点灯开关按钮状态同步
点灯app打开后 开关状态和真实状态一致,
但是第一次点击按钮时,状态不改变,第二次点击才正常。刷新监视器时,状态也不同步。代码如下,请大侠指点
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#include <Blinker.h>
char auth[] = "*****";
char ssid[] = "m*****";
char pswd[] = "********";
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
BlinkerButton b1("b1");
BlinkerButton Button("btn");
#include <DHT.h>
#define DHTPIN 4
int OUT1 = 12;
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
float humi_read, temp_read;
void button_callback(const String & state) //手机控制LED的亮灭//
{
BLINKER_LOG("AAA button state: ", state);
if (state == BLINKER_CMD_ON)
{
BLINKER_LOG("Toggle on!");
digitalWrite(OUT1, HIGH);//开灯
Button.print("on");
Button.color("#FF0000"); //red
Button.text("已关b");
}
else if(state==BLINKER_CMD_OFF)
{
BLINKER_LOG("Toggle off!");
digitalWrite(OUT1, LOW);
Button.print("off");
Button.color("#008000"); //green
Button.text("已开b");
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("间隔", BlinkerTime/1000);
}
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
if (digitalRead(OUT1)==HIGH)
{
Button.print("off");
Button.color("#008000"); //green
Button.text("已开x");
}
else
{
Button.print("on");
Button.color("#FF0000"); //red
Button.text("已关x");
}
}
void dataStorage() {
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(OUT1, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// Blinker.delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Button.attach(button_callback);
Blinker.attachDataStorage(dataStorage);
dht.begin();
}
void loop()
{
Blinker.run();
digitalWrite(LED_BUILTIN, HIGH);
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
humi_read = 33.33;
temp_read = 66.666;
digitalWrite(LED_BUILTIN, LOW);
}
}
说明本程序是读取温湿度,同时控制开关灯的,为了验证开关,温湿度的地方注释了一部分 看调试信息 奈何col 发表于 2020-12-10 18:53
看调试信息
调试信息太多了,看不出来。感觉逻辑也很简单,就是状态不同步 if (state == BLINKER_CMD_ON) //按钮当前的状态是ON
{
BLINKER_LOG("Toggle on!");
digitalWrite(OUT1, HIGH);//开灯
Button.print("on"); //这里要改为 OFF 通知 APP按钮 变为 关,后面的代码 类似
Button.color("#FF0000"); //red
Button.text("已关b");
} BLINKER_LOG("Toggle on!");
digitalWrite(OUT1, HIGH);//开灯
Button.print("on");
Button.color("#FF0000"); //red
Button.text("已关b");
把print放在最后,调用print即会发送数据 skygz 发表于 2020-12-11 23:18
if (state == BLINKER_CMD_ON) //按钮当前的状态是ON
{
BLINKER_LOG("Toggle on!");
多谢大侠!! 奈何col 发表于 2020-12-13 10:41
把print放在最后,调用print即会发送数据
也就是说print之后的可能不执行了?
mangge08 发表于 2020-12-13 14:30
也就是说print之后的可能不执行了?
PRINT会发送之前的,https://www.arduino.cn/thread-83176-1-1.html
教程有讲,请先过一遍教程 奈何col 发表于 2020-12-13 14:37
PRINT会发送之前的,https://www.arduino.cn/thread-83176-1-1.html
教程有讲,请先过一遍教程 ...
多谢指点
页:
[1]
2