通过ESP8266模块与Blinker对接,并接入小爱同学,实现语音控制
准备
硬件准备
- ESP8266开发板
- 继电器
- 烧入设备CH340
- 220V转5V电灯
- 一条充电器线
软件准备
-
安装
Arduino IDE
和ESP8266扩展包
Arduino IDE默认没有集成ESP8266扩展包,因此需要单独安装。扩展包可以通过Arduino IDE中开发板管理器安装,也可以使用离线安装包。安装扩展包后,即可在Arduino IDE菜单 > 工具 > 开发板中选择对应的开发板
其中,Arduino IDE下载需要1.8.x及以上版本,ESP8266扩展需要2.5.0版本
-
手机下载点灯科技APP
开发
生成Key
-
手机打开
Blinker
,点击+
-
添加
独立设备
-
通过WIFI接入,并选择阿里云.生成
key
,并复制到剪贴板以备用
烧录
-
安装
Arduino IDE
-
打开
light.ino
#define BLINKER_WIFI #define BLINKER_MIOT_LIGHT//小爱同学 #include <Blinker.h> char auth[] = "f00c2c8069d2"; //Key char ssid[] = "H3C_3AAF1E"; char pswd[] = "trj19611120"; bool wsState; uint8_t wsMode = BLINKER_CMD_MIOT_DAY; BlinkerButton ButtonAll("btn-lightAll"); //定义按钮数据 BlinkerButton ButtonR("btn-lightR"); //定义按钮数据 BlinkerButton ButtonG("btn-lightG"); //定义按钮数据 BlinkerButton ButtonB("btn-lightB"); //定义按钮数据 BlinkerButton ButtonOffAll("btn-unlightAll"); BlinkerButton ButtonOffR("btn-unlightR"); BlinkerButton ButtonOffG("btn-unlightG"); BlinkerButton ButtonOffB("btn-unlightB"); BlinkerButton Button3("btn-aaa"); bool oState = false; int counter = 0; void miotPowerState(const String & state) { BLINKER_LOG("need set power state: ", state); if (state == BLINKER_CMD_ON) { //小爱同学控制开命令 digitalWrite(5, LOW); digitalWrite(4, LOW); digitalWrite(0, LOW); BlinkerMIOT.powerState("on"); BlinkerMIOT.print(); oState = true; } else if (state == BLINKER_CMD_OFF) { //小爱同学控制关命令 digitalWrite(5,HIGH); digitalWrite(4,HIGH); digitalWrite(0,HIGH); BlinkerMIOT.powerState("off"); BlinkerMIOT.print(); oState = false; } } void miotMode(uint8_t mode) { BLINKER_LOG("need set mode: ", mode); if (mode == BLINKER_CMD_MIOT_DAY) { //红色 digitalWrite(5, LOW); digitalWrite(4, HIGH); digitalWrite(0, HIGH); } else if (mode == BLINKER_CMD_MIOT_NIGHT) { //绿色 digitalWrite(5, HIGH); digitalWrite(4, LOW); digitalWrite(0, HIGH); // Your mode function } else if (mode == BLINKER_CMD_MIOT_COLOR) { //蓝色 digitalWrite(5, HIGH); digitalWrite(4, HIGH); digitalWrite(0, LOW); } else if (mode == BLINKER_CMD_MIOT_WARMTH) { //黄色 digitalWrite(5, LOW); digitalWrite(4, LOW); digitalWrite(0, HIGH); } else if (mode == BLINKER_CMD_MIOT_TV) { //紫色 digitalWrite(5, LOW); digitalWrite(4, HIGH); digitalWrite(0, LOW); } else if (mode == BLINKER_CMD_MIOT_READING) { //青色 digitalWrite(5, HIGH); digitalWrite(4, LOW); digitalWrite(0, LOW); } else if (mode == BLINKER_CMD_MIOT_COMPUTER) { digitalWrite(5, HIGH); digitalWrite(4, HIGH); digitalWrite(0, LOW); } wsMode = mode; BlinkerMIOT.mode(mode); BlinkerMIOT.print(); } 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 buttonAll_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(5,LOW); digitalWrite(4,LOW); digitalWrite(0,LOW); BLINKER_LOG("get button state:on", state); } void buttonR_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(5,LOW); BLINKER_LOG("get button state:on", state); } void buttonG_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(4,LOW); BLINKER_LOG("get button state:on", state); } void buttonB_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(0,LOW); BLINKER_LOG("get button state:on", state); } void buttonOffAll_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(5,HIGH); digitalWrite(4,HIGH); digitalWrite(0,HIGH); BLINKER_LOG("get button state:off", state); } void buttonOffR_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(5,HIGH); BLINKER_LOG("get button state:off", state); } void buttonOffG_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(4,HIGH); BLINKER_LOG("get button state:off", state); } void buttonOffB_callback(const String & state) //点灯app内控制按键触发 { digitalWrite(0,HIGH); BLINKER_LOG("get button state:off", state); } void button3_callback(const String & state) //点灯app内控制按键触发 { BLINKER_LOG("get button state:on", state); Blinker.print(state); if(state=="on"){ digitalWrite(5,LOW); // Button3.print("on"); }else if(state =="off"){ digitalWrite(5,HIGH); // Button3.print("off"); } } void setup() { Serial.begin(115200); BLINKER_DEBUG.stream(Serial); pinMode(5, OUTPUT); //定义io口为输出 pinMode(4, OUTPUT); //定义io口为输出 pinMode(0, OUTPUT); //定义io口为输出 digitalWrite(5, HIGH); //定义io默认为高电平 digitalWrite(4, HIGH); //定义io默认为高电平 digitalWrite(0, HIGH); //定义io默认为高电平 wsState = true; Blinker.begin(auth, ssid, pswd); Blinker.attachData(dataRead); BlinkerMIOT.attachPowerState(miotPowerState); BlinkerMIOT.attachMode(miotMode); BlinkerMIOT.attachQuery(miotQuery); ButtonAll.attach(buttonAll_callback); ButtonR.attach(buttonR_callback); ButtonG.attach(buttonG_callback); ButtonB.attach(buttonB_callback); ; ButtonOffAll.attach(buttonOffAll_callback); ButtonOffR.attach(buttonOffR_callback); ButtonOffG.attach(buttonOffG_callback); ButtonOffB.attach(buttonOffB_callback); Button3.attach(button3_callback); } void loop() { Blinker.run(); }
-
点击开发者管理器搜索8266选择安装 (不建议选最新版本)
-
安装后退出,烧入器插入电脑,选择端口
-
配置并烧录
以下内容需个人配置
char auth[] = "之前获取的Key"; char ssid[] = "WIF名字"; char pswd[] = "WIFI密码";
注:需填2.4GHz的WIFI,不要填5GHz的WIFI
### 移动端
-
烧入成功后,打开电灯科技APP,设备在线
-
点击右上角的
+
配置按钮 -
电灯与继电器接线
通过以上方式,就可以在APP上进行基本的控制开关了
语音控制
通过接入小爱同学实现语音控制 (小米手机)
-
下载
米家APP
-
进入
其他平台设备
,找到点灯科技
,进行绑定账号同步设备 -
打开
小爱APP
训练计划 以上,便实现了全部的软件控制与语音控制