|
本帖最后由 MMMMMCU 于 2021-6-16 12:47 编辑
ERROR: MQTT NOT ALIVE OR MSG LIMIT
单独使用按钮或者单独使用小爱同学控制开关都没问题,但是使用小爱同学控制开关的时候我有写代码同步手机上按钮的状态,
这个状态同步就时灵时不灵,主要就是开机灵一阵子,然后就不同步了,不灵的时候串口监视器会报错:ERROR: MQTT NOT ALIVE OR MSG LIMIT
代码如下:
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#define DELAY 0
#define BUTTON_1 "btn-m46"
#include <Blinker.h>
char auth[] = "************";
char ssid[] = "*************";
char pswd[] = "************";
BlinkerButton Button1(BUTTON_1);
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP) {
BLINKER_LOG("Button tap!");
digitalWrite(DELAY, !digitalRead(DELAY));
}
else if (state == BLINKER_CMD_BUTTON_PRESSED) {
BLINKER_LOG("Button pressed!");
}
else if (state == BLINKER_CMD_BUTTON_RELEASED) {
BLINKER_LOG("Button released!");
digitalWrite(DELAY, !digitalRead(DELAY));
}
else if (state == BLINKER_CMD_ON) {
BLINKER_LOG("Toggle on!");
digitalWrite(DELAY, LOW);
}
else if (state == BLINKER_CMD_OFF) {
BLINKER_LOG("Toggle off!");
digitalWrite(DELAY, HIGH);
}
else {
BLINKER_LOG("Get user setting: ", state);
}
if (digitalRead(DELAY) == LOW) {
Serial.print("LOW\n");
Button1.color("#00FF00");
Button1.text("ON");
BlinkerMIOT.powerState("on");
Button1.print();
BlinkerMIOT.print();
}
else {
Serial.print("HIGH\n");
Button1.color("#000000");
Button1.text("OFF");
BlinkerMIOT.powerState("off");
Button1.print();
BlinkerMIOT.print();
}
}
void miotPowerState(const String& state)
{
BLINKER_LOG("need set power state: ", state);
button1_callback(state);
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER:
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(digitalRead(DELAY) == LOW ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER:
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(digitalRead(DELAY) == LOW ? "on" : "off");
BlinkerMIOT.print();
break;
default:
BlinkerMIOT.powerState(digitalRead(DELAY) == LOW ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(DELAY, OUTPUT);
digitalWrite(DELAY, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}
|
|