本帖最后由 bigfou 于 2020-3-4 23:38 编辑
非常感谢 奈何col,弄好了。代码分享一下,欢迎交流
另外感谢 jyc001 的恢复,但连接里的注释真的太少了。
完整代码如下:增加了心跳回调
我的继电器模块是低电平触发,所以初始化就给低电平。
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "########";
char ssid[] = "#######";
char pswd[] = "#######";
#define BUTTON_1 "btn-abc"
#define jdq_io 0
BlinkerButton Button1(BUTTON_1);
void SetButton(char state)
{
if (state == 1)
{
Button1.color("#6DBF47");
Button1.text("开");
Button1.print("on");
}
else
{
Button1.color("#7C7C7C");
Button1.text("关");
Button1.print("off");
}
}
void button1_callback(const String & state)
{
//BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(jdq_io, LOW);
BLINKER_LOG("Toggle on!");
SetButton(1);
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(jdq_io, HIGH);
BLINKER_LOG("Toggle off!");
SetButton(0);
}
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(jdq_io)==LOW) SetButton(1);
else SetButton(0);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(jdq_io, OUTPUT);
digitalWrite(jdq_io, HIGH);
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Blinker.attachHeartbeat(heartbeat);
}
void loop()
{
Blinker.run();
}
|