本帖最后由 xpmcn 于 2021-9-19 06:37 编辑
学习修改了一个APP+本地按钮的开关,但是1、打开APP中设备时不按开关按钮没有状态反馈(按调试中的刷新也不反馈);2、按开关按钮后大多数时候也不及时反馈,按调试中的刷新才反馈状态。重点想解决第1个问题,当然第2个问题也能解决更好。特求教,谢谢!
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
char auth[] = "74592f80a84e";//APP中获取到的Secret Key(密钥)
//手机配网
#define RELAY 0
#define KEY 2
int relay_State = 0;
int KEY_NUM = 0;
// 新建组件对象
BlinkerButton Button1("btn-abc"); //注意:要和APP组件’数据键名’一致 // 按下BlinkerAPP按键即会执行该函数
void button1_callback(const String & state)
{
if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
Button1.text("断电");
relay_State = 1;
Button1.print("on");
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
Button1.color("#FF0000");
Button1.text("插座已送电");
relay_State = 0;
Button1.print("off");
}
Blinker.vibrate();
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
//Text1.print("os time", BlinkerTime);
}
void setup() {
// 初始化串口,并开启调试信息,仅调试用,可以删除
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化IO
pinMode(RELAY, OUTPUT);
pinMode(KEY, INPUT);
digitalWrite(RELAY,HIGH);//上电继电器释放
// 初始化blinker
Blinker.begin(auth);
Button1.attach(button1_callback);
Blinker.attachData(dataRead);
}
void loop() {
Blinker.run();
KEY_NUM = 0;
if (digitalRead(KEY) == LOW)
{
delay(10);
if (digitalRead(KEY) == LOW)
{
KEY_NUM = 1;
while (digitalRead(KEY) == LOW);
}
}
if (KEY_NUM == 1)
{
relay_State = relay_State + 1;
}
if (relay_State >= 2)
{
relay_State = 0;
}
if (relay_State == 1)
{
digitalWrite(RELAY, LOW);
}
else
{
digitalWrite(RELAY, HIGH);
}
}
|