今天给大家说一下各个语言助手来控制ESP8266来点灯,这是我们用的还是blinker平台,前面有写过关于小爱同学点灯的文章,今天说一下小度和天猫精灵的控制
#define BLINKER_MIOT_LIGHT
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
//小爱电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
#define BLINKER_ALIGENIE_LIGHT //设置天猫灯类库
BlinkerAliGenie.attachPowerState(aligeniePowerState); //天猫语音操作注册函数
//天猫精灵电源类回调
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
}
}
#define BLINKER_DUEROS_LIGHT //设置小度灯类库
BlinkerDuerOS.attachPowerState(duerPowerState); //小度语音操作注册函数
//小度电源类回调
void duerPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
}
}
大家可以根据自己的需求来添加程序,我将三种控制合成一个程序
#define BLINKER_PRINT Serial //用于打开串口
#define BLINKER_WIFI //用于指定设备接入方式 wifi 接入
#define BLINKER_MIOT_LIGHT //设置小爱同学控制为语音控制灯设备
#define BLINKER_DUEROS_LIGHT //设置小度灯类库
#define BLINKER_ALIGENIE_LIGHT //设置天猫灯类库
#include
char auth[] = "a3c11b64eb38"; //上一步中在app中获取到的Secret Key(新建设备的秘钥)
char ssid[] = "ZHAI"; //你的WiFi热点名称
char pswd[] = "123456789"; //你的WiFi密码
BlinkerButton Button1("btn-abc"); //btn-abc 名称要和app新建组件一致
// app 端按下按键即会执行该函数 回调函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state=="on") {
digitalWrite(LED_BUILTIN, LOW);
// 反馈开关状态
Button1.print("on");
} else if(state=="off"){
digitalWrite(LED_BUILTIN, HIGH);
// 反馈开关状态
Button1.print("off");
}
}
//小爱电源类回调
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
//小度电源类回调
void duerPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerDuerOS.powerState("on");
BlinkerDuerOS.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerDuerOS.powerState("off");
BlinkerDuerOS.print();
}
}
//天猫精灵电源类回调
void aligeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
}
}
void setup() {
// 初始化串口,并开启调试信息
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial); //串口打印调试信息
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT); //LED_BUILTIN 宏就是开发板指示灯的io口
digitalWrite(LED_BUILTIN, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback); //绑定按键执行回调函数
BlinkerDuerOS.attachPowerState(duerPowerState); //小度语音操作注册函数
BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
BlinkerAliGenie.attachPowerState(aligeniePowerState); //天猫语音操作注册函数
}
void loop() {
Blinker.run(); /*每次运行都会将设备收到的数据进行一次解析。
在使用WiFi接入时,该语句也负责保持网络连接*/
}