[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "666666";
char ssid[] = "666666";
char pswd[] = "z666666";
#define LED 0 //0对应GPIO0 2对应GPIO2
bool oState = false;
BlinkerButton Button1("btn");
void heartbeat()
{
if (oState)
{ Button1.print("on");
}
else
{
Button1.print("off");
}
}
//按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, HIGH);
oState = true;
//Button1.text("已开启");
} else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, LOW);
oState = false;
//Button1.text("已关闭");
}
Button1.print(state);
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Button1.text("已开启");
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
Button1.text("已关闭");
oState = false;
}
}
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(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "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(LED, OUTPUT);
digitalWrite(LED, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Blinker.attachHeartbeat(heartbeat);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
}
void loop()
{
Blinker.run();
}[/mw_shl_code]
用这个,心跳包是i分钟一次,所以,肯定会有1分钟一次的状态更新。
另外,我的按钮用的中间那个按钮,是可以实时显示开关状态的。 |