集小爱同学语音控制、本地按键、无线按键、离线语音的灯光控制
电路图
代码
#define BLINKER_WIFI
#define BLINKER_MIOT_MULTI_OUTLET //设置为小爱多个插座的模式
#include <Blinker.h>
#define BLINKER_PRINT Serial //串口协议库
#define BLINKER_WITHOUT_SSL
char i;
char auth[] = "xxx"; //填上刚刚记下来的密钥
/*******直连(直连时取消注释)*******/
char ssid[] = "526"; //填上你家WiFi名字
char pswd[] = "XXXX"; //填上你家WiFi密码码
//定义本地开关信号
#define KG_1 5 //定义开关1信号接入GPIO5,D1 /控制D3 io3 0
#define KG_2 4 //定义开关2信号接入GPIO4,D2 /控制D5 io2 14
#define KG_3 2 //定义开关3信号接入GPIO2,D4 /控制D6 io1 12
#define KG_4 3 //定义开关4信号接入GPIO3,D9 RX /控制D7 24 13 (GPIO15/D8持续低电平,后更改为GPIO3/RX接口)
#define KG_5 1 //定义开关3信号接入GPIO1 D10 /控制D8 23 15
bool oState[6] = { false };
//io口定义
int sj=1000;//延迟时间
int kg11=D0;
int kg3=D3;
int kg5=D5;
int kg6=D6;
int kg7=D7;
int kg8=D8;
//****新建组件对象****
BlinkerButton Button1("btn-kg1"); //设置app按键的键名
BlinkerButton Button3("btn-kg3"); //设置app按键的键名
BlinkerButton Button5("btn-kg5"); //设置app按键的键名
BlinkerButton Button6("btn-kg6"); //设置app按键的键名
BlinkerButton Button7("btn-kg7"); //设置app按键的键名
BlinkerButton Button8("btn-kg8"); //设置app按键的键名
BlinkerButton Button11("btn-kg11"); //设置app按键的键名
//反馈继电器状态函数
void RelayState(int num)
{switch(num){case 1: //插座插孔一状态if(digitalRead(kg7)==HIGH){Button7.color("#FF0000"); //设置app按键是纯黄色,16进制颜色码Button7.text("插座1开"); //设置app按键注释“开”Button7.print("on");oState[1] = true;}else if(digitalRead(kg7)==LOW){Button7.text("插座1关"); //设置app按键注释“关”Button7.print("off");oState[1] = false;}break;case 2:if(digitalRead(kg6)==HIGH){Button6.color("#FF0000"); //设置app按键是纯黄色,16进制颜色码Button6.text("插座2开"); //设置app按键注释“开”Button6.print("on");oState[2] = true;}else if(digitalRead(kg6)==LOW){Button6.text("插座2关"); //设置app按键注释“关”Button6.print("off");oState[2] = false;}break;case 3:if(digitalRead(kg5)==HIGH){Button5.color("#FF0000"); //设置app按键是纯黄色,16进制颜色码Button5.text("插座3开"); //设置app按键注释“开”Button5.print("on");oState[3] = true;}else if(digitalRead(kg5)==LOW){Button5.text("插座3关"); //设置app按键注释“关”Button5.print("off");oState[3] = false;}break;case 4:if(digitalRead(kg11)==HIGH){Button11.color("#FF0000"); //设置app按键是纯黄色,16进制颜色码Button11.text("插座4开"); //设置app按键注释“开”Button11.print("on");oState[4] = true;}else if(digitalRead(kg11)==LOW){Button11.text("插座4关"); //设置app按键注释“关”Button11.print("off");oState[4] = false;}break;case 5:if(digitalRead(kg3)==HIGH){Button3.color("#FF0000"); //设置app按键是纯黄色,16进制颜色码Button3.text("插座4开"); //设置app按键注释“开”Button3.print("on");oState[5] = true;}else if(digitalRead(kg3)==LOW){Button3.text("插座4关"); //设置app按键注释“关”Button3.print("off");oState[5] = false;}break;default:break;}
}void miotPowerState(const String & state, uint8_t num)
{BLINKER_LOG("need set outlet: ", num, ", power state: ", state); i=0; //初始化 重要!!if(num==1){i=D7; }else if(num==2){i=D6;}else if(num==3){i=D5;}else if(num==4){i=D0;}else if(num==5){i=D3;}if (state == BLINKER_CMD_ON) { digitalWrite(i, HIGH); BlinkerMIOT.powerState("on", num);BlinkerMIOT.print();oState[num] = true; }else if (state == BLINKER_CMD_OFF) { digitalWrite(i, LOW);BlinkerMIOT.powerState("off", num);BlinkerMIOT.print();oState[num] = false;/if (num == 0){for (uint8_t o_num = 0; o_num <12; o_num++){oState[o_num] = false;}}}
}//心跳包刷新状态void heartbeat(){for(int i=0;i<5;i++){RelayState(i);}}//本地开关控制继电器程序void bdms(){if(digitalRead(KG_1)==LOW){digitalWrite(kg3, !digitalRead(kg3));//反馈继电器1状态RelayState(1); //调用继电器反馈程序Blinker.delay(1000); //必须加延时,否则灯一直切换状态}if(digitalRead(KG_2)==LOW){digitalWrite(kg5, !digitalRead(kg5));RelayState(2); //调用继电器反馈程序Blinker.delay(1000); }if(digitalRead(KG_3)==LOW){digitalWrite(kg6, !digitalRead(kg6));RelayState(3); //调用继电器反馈程序Blinker.delay(1000);}if(digitalRead(KG_4)==LOW){digitalWrite(kg7, !digitalRead(kg7));RelayState(4); //调用继电器反馈程序Blinker.delay(1000);}if(digitalRead(KG_5)==LOW){digitalWrite(kg8, !digitalRead(kg8));RelayState(5); //调用继电器反馈程序Blinker.delay(1000);}}void button1_callback(const String & state) {BLINKER_LOG("ALL#灯,状态: ", state);if (state=="on") {digitalWrite(kg3, HIGH );digitalWrite(kg5, HIGH );digitalWrite(kg6, HIGH );digitalWrite(kg7, HIGH );digitalWrite(kg8, HIGH );digitalWrite(kg11, HIGH );Button1.color("#FF0000"); //3#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button1.text("已开灯");Button1.print("on");BLINKER_LOG ("ALL#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg3, LOW);digitalWrite(kg5, LOW);digitalWrite(kg6, LOW);digitalWrite(kg7, LOW);digitalWrite(kg8, LOW);digitalWrite(kg11, LOW);// 反馈开关状态Button1.text("已关灯");Button1.print("off");BLINKER_LOG ("ALL#灯已关灯off");}
}
void button3_callback(const String & state) {BLINKER_LOG("3#灯,状态: ", state);if (state=="on") {digitalWrite(kg3, HIGH );Button3.color("#FF0000"); //3#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button3.text("已开灯");Button3.print("on");BLINKER_LOG ("3#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg3, LOW);// 反馈开关状态Button3.text("已关灯");Button3.print("off");BLINKER_LOG ("3#灯已关灯off");}
}
void button8_callback(const String & state) {BLINKER_LOG("1#灯,状态: ", state);if (state=="on") {digitalWrite(kg8, HIGH);Button8.color("#FF0000"); //3#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button8.text("已开灯");Button8.print("on");BLINKER_LOG ("1#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg8, LOW);// 反馈开关状态Button8.text("已关灯");Button8.print("off");BLINKER_LOG ("3#灯已关灯off");}
}
void button5_callback(const String & state) {BLINKER_LOG("5#灯,状态: ", state);if (state=="on") {digitalWrite(kg5, HIGH);Button5.color("#FF0000"); //4#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button5.text("已开灯");Button5.print("on");BLINKER_LOG ("5#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg5, LOW);// 反馈开关状态Button5.text("已关灯");Button5.print("off");BLINKER_LOG ("5#灯已关灯off");}
}
void button6_callback(const String & state) {BLINKER_LOG("6#灯,状态: ", state);if (state=="on") {digitalWrite(kg6, HIGH);Button6.color("#FF0000"); //4#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button6.text("已开灯");Button6.print("on");BLINKER_LOG ("6#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg6, LOW);// 反馈开关状态Button6.text("已关灯");Button6.print("off");BLINKER_LOG ("6#灯已关灯off");}
}
void button7_callback(const String & state) {BLINKER_LOG("7#灯,状态: ", state);if (state=="on") {digitalWrite(kg7, HIGH);Button7.color("#FF0000"); //4#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button7.text("已开灯");Button7.print("on");BLINKER_LOG ("7#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg7, LOW);// 反馈开关状态Button7.text("已关灯");Button7.print("off");BLINKER_LOG ("7#灯已关灯off");}
}void button11_callback(const String & state) {BLINKER_LOG("11#灯,状态: ", state);if (state=="on") {digitalWrite(kg11, HIGH);Button11.color("#FF0000"); //4#按钮按下时,app按键颜色状态显示是红色// 反馈开关状态Button11.text("已开灯");Button11.print("on");BLINKER_LOG ("11#灯已开灯on"); //串口打印} else if(state=="off"){digitalWrite(kg11, LOW);// 反馈开关状态Button11.text("已关灯");Button11.print("off");BLINKER_LOG ("11#灯已关灯off");}
}
//void miotQuery(int32_t queryCode, uint8_t num)
{BLINKER_LOG("AliGenie Query outlet: ", num,", codes: ", queryCode);switch (queryCode){case BLINKER_CMD_QUERY_ALL_NUMBER :BLINKER_LOG("MIOT Query All");BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);BlinkerMIOT.print();break;case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :BLINKER_LOG("MIOT Query Power State");BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);BlinkerMIOT.print();break;default :BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);BlinkerMIOT.print();break;}
}void dataRead(const String & data)
{BLINKER_LOG("Blinker readString: ", data);Blinker.vibrate();uint32_t BlinkerTime = millis();Blinker.print("millis", BlinkerTime);
}void setup()
{// 初始化串口,用于调试,后期可删除Serial.begin(115200);BLINKER_DEBUG.stream(Serial);BLINKER_DEBUG.debugAll();pinMode(D3, OUTPUT);pinMode(D5, OUTPUT);pinMode(D6, OUTPUT);pinMode(D7, OUTPUT);pinMode(D8, OUTPUT);pinMode(D8, OUTPUT);pinMode(D0, OUTPUT);digitalWrite(D3, LOW);digitalWrite(D5, LOW);digitalWrite(D6, LOW);digitalWrite(D7, LOW);digitalWrite(D8, LOW);digitalWrite(D0, LOW);//初始化本地按键状态pinMode(KG_1, INPUT_PULLUP); //本地开关输入上拉pinMode(KG_2, INPUT_PULLUP); //本地开关输入上拉pinMode(KG_3, INPUT_PULLUP); //本地开关输入上拉pinMode(KG_4, INPUT_PULLUP); //本地开关输入上拉pinMode(KG_5, INPUT_PULLUP); //本地开关输入上拉
/******** 直连(直连时 取消注释) ******/ Blinker.begin(auth, ssid, pswd);Blinker.attachData(dataRead);
/**************/ BlinkerMIOT.attachPowerState(miotPowerState);BlinkerMIOT.attachQuery(miotQuery);//****app关联函数****
//app上操作必须的注册回调函数关联按键名“Button1-Button4”和判断程序“button1_callback"Button1.attach(button1_callback); Button3.attach(button3_callback); Button5.attach(button5_callback); Button6.attach(button6_callback); Button7.attach(button7_callback);Button8.attach(button8_callback);Button11.attach(button11_callback);
}
void loop()
{bdms(); //本地开关模式Blinker.run();}