|
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "1c52eb65d32a"; //设备key号
char ssid[] = "B-LINK_D0D686"; //wifi名称
char pswd[] = ""; //wifi密码
#define LED LED_BUILTIN
bool oState = false;
BlinkerButton Button1("btn-abc");
//*******app有操作即会执行该函数。
void button1_callback(const String & state) //用state来存储组键的值按键 : "tap"(点按); "pre"(长按); "pup"(释放)开关 : "on"(打开); "off"(关闭)
{
BLINKER_LOG("app操作了!: ", state); //串口打印
if (digitalRead(LED)==LOW) { //如果state是on状态
BLINKER_LOG("灭灯!"); //串口打印
digitalWrite(LED, HIGH);
Button1.color("#00FFFF"); //设置app按键是深蓝色
Button1.text("灭");
Button1.print("off"); //反馈回按键状态是开
}
else if (digitalRead(LED)==HIGH) { //如果state是off状态
BLINKER_LOG("亮灯!"); //串口打印
digitalWrite(LED,LOW);
Button1.color("#0000FF"); //设置app按键是浅蓝色
Button1.text("亮");
Button1.print("on"); //反馈回按键状态是关
}
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Button1.color("#00FFFF"); //设置app按键是深蓝色
Button1.text("灭");
Button1.print("off"); //反馈回按键状态是开
oState = true;
} else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
Button1.color("#0000FF"); //设置app按键是浅蓝色
Button1.text("亮");
Button1.print("on"); //反馈回按键状态是关
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);
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(LED)==LOW)
{ Button1.print("on");
Button1.color("#0000FF"); //设置app按键是浅蓝色
Button1.text("亮");
}
else
{
Button1.print("off");
Button1.color("#00FFFF"); //设置app按键是深蓝色
Button1.text("灭");
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(2, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态进行语音操作和app操作同步。
}
void loop()
{
Blinker.run();
if(digitalRead(LED)){
}
while(digitalRead(LED)){
delay(10);
}
digitalwrite(2,(!digitalRead(2)
}
|
|