如果你有一部非小米手机,怎样连入米家,使用小爱同学语音控制家电呢
此文相关看不懂的知识点
1、esp8266无法下载程序:参考我的另外一篇文章
链接
2、非小米手机小爱同学语音接入
链接
实现功能
小爱同学语音控制客厅灯开关、模式切换、亮度调节
准备工作
1、arduino IDE开发环境
2、一块esp8266 开发板
3、一部手机(我用的是oppo)
Blinker界面配置
第一步blinker配置
于是你就得到一个新的设备
复制这个密钥到代码中
下面展示一些 内联代码片
。
char auth[] = "fe5af1cadf0d";//密钥复制到这里
char ssid[] = "L";//你手机的热点名
char pswd[] = "123456789";//wifi密码
#define BLINKER_PRINT Serial
#define BLINKER_WIFI#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
//#include <Adafruit_NeoPixel.h>
#include "Adafruit_NeoPixel.h" //灯条库char auth[] = "fe5af1cadf0d";//设备号
char ssid[] = "L";//无线账号
char pswd[] = "123456789";//无线密码int LED_OPEN=0;//氛围灯标志位
int Control_OPEN=0;//氛围灯颜色控制标志位int count=0;//LED翻转计数器
int send_data=0;//发送命令标志位
uint8_t colorR1 ;//用于颜色存储
uint8_t colorG1 ;
uint8_t colorB1 ;
uint8_t colorR2 ;//用于颜色存储2
uint8_t colorG2 ;
uint8_t colorB2 ;
uint8_t colorR_Set= map( 0, 0, 255, 0, 255);//颜色默认值
uint8_t colorG_Set= map(255, 0, 255, 0, 255);//颜色默认值
uint8_t colorB_Set= map(242, 0, 255, 0, 255);;//颜色默认值int LED_R=0,LED_G=0,LED_B=0,LED_Bright=255;// RGB和亮度
char wsMode; //模式#define kled 16//厨房 v
#define roomled 14//卧室灯
#define PIN 15//客厅RGB
#define NUMPIXELS 8//灯珠数量
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);#define RGB_1 "RGBKey"// 新建组件对象
BlinkerButton Button1("LED_Button");//RGB客厅灯
BlinkerButton Button2("Control_Button");//RGB颜色控制
BlinkerRGB RGB1(RGB_1);//RGB调色板BlinkerNumber Number1("num-abc");
int counter = 0;
bool oState = false;//APP端颜色设置回调函数
void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));BLINKER_LOG("R value: ", r_value);BLINKER_LOG("G value: ", g_value);BLINKER_LOG("B value: ", b_value);BLINKER_LOG("Rrightness value: ", bright_value);uint8_t colorR = map(r_value, 0, 255, 0, bright_value);uint8_t colorG = map(g_value, 0, 255, 0, bright_value);uint8_t colorB = map(b_value, 0, 255, 0, bright_value);colorR2=colorR1 ;//存储colorG2=colorG1 ;colorB2=colorB1 ;colorR1=colorR ;//存储colorG1=colorG ;colorB1=colorB ;Button2.print("on");Control_OPEN=1;//氛围灯颜色调节send_data=1;//运行标志位为1
}// 按下按键1即会执行该函数按下按键1,LED1亮
void button1_callback(const String & state) {BLINKER_LOG("get button state: ", state);digitalWrite(kled, !digitalRead(kled));if(digitalRead(kled)==0){Button1.print("off");LED_OPEN=0;//关闭}else{Button1.print("on");LED_OPEN=1;//开启}send_data=1;//运行标志位为1
}
// 按下按键2即会执行该函数
void button2_callback(const String & state) {BLINKER_LOG("get button state: ", state);digitalWrite(roomled, !digitalRead(roomled));if(digitalRead(roomled)==0){Button2.print("off");Control_OPEN=0;//关闭}else{Button2.print("on");Control_OPEN=1;//开启}send_data=1;//运行标志位为1
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{BLINKER_LOG("Blinker readString: ", data);}
//小爱同学处理函数
void miotPowerState(const String & state)
{BLINKER_LOG("need set power state: ", state);if (state == BLINKER_CMD_ON) {digitalWrite(roomled, HIGH);LED_OPEN=1;BlinkerMIOT.powerState("on");BlinkerMIOT.print();send_data=1;//运行标志位为1}else if (state == BLINKER_CMD_OFF) {digitalWrite(roomled, LOW);LED_OPEN=0;BlinkerMIOT.powerState("off");BlinkerMIOT.print();send_data=1;//运行标志位为1}
}// 查询设备状态回调函数
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;}
}//控制rgb灯
void SET_RGB(int R,int G,int B,int bright)
{for (uint16_t i = 0; i < NUMPIXELS; i++) //把灯条变色{pixels.setPixelColor(i,R,G,B);}pixels.setBrightness(bright);//亮度pixels.show(); //送出显示
}
// 灯光亮度控制回调函数
void miotBright(const String & bright)
{BLINKER_LOG("need set brightness: ", bright);int colorW = bright.toInt();BLINKER_LOG("now set brightness: ", colorW);LED_Bright = colorW;SET_RGB(LED_R,LED_G,LED_B,LED_Bright);Serial.printf("亮度调节中...%d",colorW);BlinkerMIOT.brightness(colorW);//反馈小爱控制状态BlinkerMIOT.print();
}// 灯光色温控制回调函数
void miotColor(int32_t color)
{BLINKER_LOG("need set color: ", color);int colorR,colorG,colorB;colorR = color >> 16 & 0xFF;colorG = color >> 8 & 0xFF;colorB = color & 0xFF;BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);SET_RGB(colorR,colorG,colorB,LED_Bright);BlinkerMIOT.color(color);//反馈小爱控制状态BlinkerMIOT.print();
}// 模式控制回调函数
void miotMode(uint8_t mode)
{BLINKER_LOG("need set mode: ", mode);//日光if (mode == BLINKER_CMD_MIOT_DAY) { SET_RGB(255,255,255,LED_Bright); }//月光else if (mode == BLINKER_CMD_MIOT_NIGHT) {SET_RGB(255,181,0,LED_Bright); }//彩光else if (mode == BLINKER_CMD_MIOT_COLOR) {pixels.setPixelColor(0,255,0,20);pixels.setPixelColor(1,0,255,184);pixels.setPixelColor(2,255,231,192);pixels.setPixelColor(3,0,59,255);pixels.setPixelColor(4,209,22,255);pixels.setBrightness(LED_Bright);//亮度pixels.show(); //送出显示}//温馨else if (mode == BLINKER_CMD_MIOT_WARMTH) {SET_RGB(255,38,0,LED_Bright); }//电视模式else if (mode == BLINKER_CMD_MIOT_TV) {// Your mode functionSET_RGB(255,0,154,LED_Bright); }//阅读模式else if (mode == BLINKER_CMD_MIOT_READING) {// Your mode functionSET_RGB(0,255,164,LED_Bright); }//电脑模式else if (mode == BLINKER_CMD_MIOT_COMPUTER) {// Your mode functionSET_RGB(255,0,0,LED_Bright); }wsMode = mode;BlinkerMIOT.mode(mode);BlinkerMIOT.print();
}void setup() {// 初始化串口Serial.begin(115200);BLINKER_DEBUG.stream(Serial);BLINKER_DEBUG.debugAll();#if defined(BLINKER_PRINT)BLINKER_DEBUG.stream(BLINKER_PRINT);#endif// 初始化有LED的IOpinMode(LED_BUILTIN, OUTPUT);digitalWrite(LED_BUILTIN, HIGH);pinMode(kled, OUTPUT);digitalWrite(kled, LOW);pinMode(roomled, OUTPUT);digitalWrite(roomled, LOW);// 初始化blinkerBlinker.begin(auth, ssid, pswd);pixels.begin();Blinker.attachData(dataRead);Button1.attach(button1_callback);//按键1Button2.attach(button2_callback);//按键2RGB1.attach(rgb1_callback);//小爱回调函数BlinkerMIOT.attachPowerState(miotPowerState); // 电源控制回调BlinkerMIOT.attachQuery(miotQuery); // 查询设备状态回调BlinkerMIOT.attachBrightness(miotBright); // 灯光亮度控制回调BlinkerMIOT.attachColor(miotColor); // 灯光色温控制回调BlinkerMIOT.attachMode(miotMode); // 模式控制回调}void loop() {Blinker.run();count++;//led计数器if(send_data==1)//运行标志位为1{if(LED_OPEN==1 )//开启{if(Control_OPEN==1)//控制开启{// int r1=colorR2,g1=colorG2,b1=colorB2;for(int n = 0; n < NUMPIXELS; n++){pixels.setPixelColor(n, pixels.Color(colorR1,colorG1,colorB1));pixels.show();} }else{int r=0,g=0,b=0;for(int i = 0; i < NUMPIXELS; i++){pixels.setPixelColor(i, pixels.Color(colorR_Set,colorG_Set,colorB_Set));pixels.show();}} }else if (LED_OPEN==0)//关闭{uint8_t colorR = map(0, 0, 255, 0, 0);uint8_t colorG = map(0, 0, 255, 0, 0);uint8_t colorB = map(0, 0, 255, 0, 0);for(int i = 0; i < NUMPIXELS; i++){pixels.setPixelColor(i, pixels.Color(colorR,colorG,colorB));pixels.show();}}send_data=0;//运行标志位清0 }else{send_data=0;//运行标志位为0 }if(count==100){digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));count=0;}
}
烧录代码到esp8266
也可以是arduino开发板,不过我没试过arduino
等待一会连上网可以看到设备在线
进行界面配置
将下面此段代码复制到blinker界面配置
{¨version¨¨2.0.0¨¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/bg/2.jpg¨}}¨dashboard¨|{¨type¨¨btn¨¨ico¨¨fal fa-power-off¨¨mode¨Ê¨t0¨¨客厅灯¨¨t1¨¨文本2¨¨bg¨É¨cols¨Ë¨rows¨Ë¨key¨¨LED_Button¨´x´Ï´y´É¨lstyle¨Ê¨clr¨¨#EA0909¨}{ßB¨num¨ßG¨温度¨ßD¨fal fa-question¨ßQßR¨min¨É¨max¨¢1c¨uni¨´℃´ßKÉßLÍßMËßN¨temp¨´x´Ë´y´ÉßP˨rt¨»}{ßBßSßG¨湿度¨ßDßUßQ¨#076EEF¨ßVÉßWº0ßX¨RH¨ßKÉßLÍßMËßN¨humi¨´x´Ë´y´ËßPÌßZ»}{ßB¨deb¨ßFÉßKÉßLÑßMÌßN¨debug¨´x´É´y´¤G}{ßB¨cha¨ßKɨsty¨¨line¨ßQßR¨sty1¨ßi¨clr1¨ßb¨sty2¨ßi¨clr2¨¨#389BEE¨ßLÑßMÌßN´´´x´É´y´ÍßGßTßIßa¨key0¨ßY¨key1¨´´ßPÉ}{ßBßgßKÉßhßißQßbßjßißkßbßlßißmßbßLÑßMÌßN´´´x´É´y´ÐßoßdßGßa}{ßBßSßGßTßD¨fad fa-thermometer-three-quarters¨ßQßRßVÉßWº0ßX´℃´ßKÉßLËßMËßNßY´x´É´y´ÉßPÉßZ»}{ßBßSßGßaßD¨fad fa-humidity¨ßQßnßVÉßWº0ßXßcßKÉßLËßMËßNßd´x´É´y´ËßZ»}{ßBßCßDßEßFÉßG¨控制¨ßIßJßKÉßLËßMËßN¨Control_Button¨´x´Ï´y´ËßPÊßQ¨#00A90C¨}{ßB¨col¨ßG¨颜色拾取¨ßQßnßKËßLÏßMÏßN¨RGBKey¨´x´É´y´¤AßPÉ}÷¨actions¨|÷¨triggers¨|÷ßZ|ßYßdßYßd÷}
退出再进来就可以得到这个界面
一切准备就绪,如果你是小米手机,配置训练指令就可以了,如果你是非小米,请看下一篇文章