一、前期准备
三、程序
/*******************************************
#include
//设备连接BLINKER及家中的WIFI
char auth[] = “Blink app生成的密钥”;
char ssid[] = “wifi名字”;
char pswd[] = “wifi密码”;
bool oState = false;
//Blinker APP中控制继电器的按钮
BlinkerButton btn_TVP(“btn-TVP”);
int TV_Pin = D5;
//与小度APP进行电源状态交互
void duerPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(TV_Pin, HIGH);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(TV_Pin, LOW);
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
oState = false;
}
}
//用户自定义设备查询的回调函数:
void duerQuery(int32_t queryCode)
{
BLINKER_LOG("DuerOS Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("DuerOS Query All");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
default :
BlinkerDuerOS.powerState(oState ? "on" : "off");
BlinkerDuerOS.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
//Blinker APP 按钮控制命令
void btn_TVP_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(TV_Pin, !digitalRead(TV_Pin));
}
void setup()
{
Serial.begin(115200);
//debug 命令
BLINKER_DEBUG.stream(BLINKER_PRINT);
BLINKER_DEBUG.debugAll();
//继电器接入公共端和常开端口,电压初始化为低电压,接入高电压后吸合供电
pinMode(TV_Pin, OUTPUT);
digitalWrite(TV_Pin, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerDuerOS.attachPowerState(duerPowerState);
BlinkerDuerOS.attachQuery(duerQuery);
btn_TVP.attach(btn_TVP_callback);
}
void loop()
{
Blinker.run();
}
以上程序基于Arduino IDE–实例–Blinker–Blinker_AliGenie–AliGenie_OUTLET修改而来。
Blinker APP 中设置一个按钮,命名为“btn_TVP”,语音设置:打开电视机–on,关闭电视机–off