读者按照一下图片自行配置app
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>#define LED 0 //定义继电器输入引脚为GPIO0//填入自己的信息
char auth[] = " ";
char ssid[] = " ";
char pswd[] = " ";bool wsState;// 新建组件对象
BlinkerButton Button1("light"); //注意:要和APP组件’数据键名’一致// 按下BlinkerAPP按键即会执行该函数
void button1_callback(const String & state )
{BLINKER_LOG("串串灯状态: ", state);if (state=="on") {digitalWrite(LED, LOW);Button1.color("#00FF11"); //按钮按下时,app按键颜色状态显示是绿色Button1.text("已开灯"); // 反馈开关状态Button1.print("on");wsState=true;} else if(state=="off"){digitalWrite(LED, HIGH);Button1.color("#000000"); //按钮没有按下时,app按键颜色状态显示是黑色Button1.text("已关灯"); // 反馈开关状态Button1.print("off");wsState=false;}Blinker.vibrate();
}//小爱同学 的回调查询函数,照抄即可。主要是查询 当前灯的状态
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(wsState ? "on" : "off");BlinkerMIOT.color(0);BlinkerMIOT.mode(0);BlinkerMIOT.colorTemp(1000);BlinkerMIOT.brightness(1);BlinkerMIOT.print();break;case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :BLINKER_LOG("MIOT Query Power State");BlinkerMIOT.powerState(wsState ? "on" : "off");BlinkerMIOT.print();break; default :BlinkerMIOT.powerState(wsState ? "on" : "off");BlinkerMIOT.color(0);BlinkerMIOT.mode(0);BlinkerMIOT.colorTemp(1000);BlinkerMIOT.brightness(1);BlinkerMIOT.print();break;}
}//小爱电源类操作的回调函数:
//当小爱同学向设备发起控制, 设备端需要有对应控制处理函数
void miotPowerState(const String & state)
{BLINKER_LOG("need set power state: ", state);if (state == BLINKER_CMD_ON) {digitalWrite(LED, LOW);Button1.color("#00FF11"); //按钮按下时,app按键颜色状态显示是绿色Button1.text("已开灯"); // 反馈开关状态Button1.print("on");BlinkerMIOT.powerState("on");BlinkerMIOT.print();wsState=true;}else if (state == BLINKER_CMD_OFF) {digitalWrite(LED, HIGH);Button1.color("#000000"); //按钮没有按下时,app按键颜色状态显示是黑色Button1.text("已关灯"); // 反馈开关状态Button1.print("off");BlinkerMIOT.powerState("off");BlinkerMIOT.print();wsState=false;}
}void setup() {// 初始化串口,并开启调试信息,调试用可以删除Serial.begin(115200);BLINKER_DEBUG.stream(Serial);// 初始化IOpinMode(LED, OUTPUT);digitalWrite(LED, HIGH);Button1.color("#000000"); //按钮没有按下时,app按键颜色状态显示是黑色Button1.text("已关灯"); // 反馈开关状态Button1.print("off");//初始化blinkerBlinker.begin(auth, ssid, pswd);Button1.attach(button1_callback);//注册回调函数BlinkerMIOT.attachPowerState(miotPowerState);BlinkerMIOT.attachQuery(miotQuery);}void loop() {Blinker.run();
}