|
楼主 |
发表于 2020-3-20 17:58
|
显示全部楼层
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET //小爱同学
#include <Blinker.h>
char auth[] = "********"; //设备key
char ssid[] = "*********"; //路由器wifi ssid
char pswd[] = "********"; //路由器wifi 密码
BlinkerButton Button1("btn-on"); //定义按钮键名
BlinkerButton Button2("btn-off"); //这里加了第二个按钮
bool oState = false;
int counter = 0;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) { //小爱同学控制开命令 此处修改为点动模式,适合按钮操作,
digitalWrite(0, LOW);
delay(200);
digitalWrite(0, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
if (state == BLINKER_CMD_OFF) { //小爱同学控制关命令 此处修改为点动模式,适合按钮操作,
digitalWrite(0,LOW);
delay(5000);
digitalWrite(0, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
oState = true;
}
}
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 button1_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(0,LOW);
delay(200);
digitalWrite(0, HIGH);
}
void button2_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(0,LOW);
delay(5000);
digitalWrite(0, HIGH); //这是拷贝上面的一段把 1 改为 2 。
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(0, OUTPUT); //定义io口为输出
digitalWrite(0, HIGH); //定义io默认为高电平
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback); // 这里按照上面一行加多一行并改为2
}
void loop()
{
Blinker.run();
} |
|