|
根据群内分享改编的,四路输出四个物理按键开关,支持小爱同学控制,小爱控制可以统一开关,就是小爱的语言操作怪怪的,设备名开关为总开关,设备名+插孔一、二、三、四可以单独操作,app和按键没有增加总开关功能,需要独立开关,继电器为低
电平触发,开关也是低电平触发,app页面没有特殊处理就是简单的开关,想进一步美化自己改吧。
- {¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨dark¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨deb¨¨mode¨É¨bg¨É¨cols¨Ñ¨rows¨Ì¨key¨¨debug¨´x´É´y´É}{ßA¨btn¨¨ico¨¨fad fa-lightbulb-on¨ßCʨt0¨¨继电器1¨¨t1¨¨文本2¨ßDÉßEÍßFÍßG¨btn-on1¨´x´É´y´Í¨speech¨|÷¨lstyle¨Ë¨clr¨¨#FBA613¨}{ßAßIßJßKßCÊßL¨继电器2¨ßNßOßDÉßEÍßFÍßG¨btn-on2¨´x´Í´y´ÍßQ|÷ßRËßS¨#076EEF¨}{ßAßIßJßKßCÊßL¨继电器3¨ßNßOßDÉßEÍßFÍßG¨btn-on3¨´x´É´y´ÑßQ|÷ßRËßS¨#00A90C¨}{ßAßIßJßKßCÊßL¨继电器4¨ßNßOßDÉßEÍßFÍßG¨btn-on4¨´x´Í´y´ÑßQ|÷ßRËßS¨#EA0909¨}÷¨actions¨|¦¨cmd¨¦¨switch¨‡¨text¨‡¨on¨¨打开?name¨¨off¨¨关闭?name¨—÷¨triggers¨|{¨source¨ßf¨source_zh¨¨开关状态¨¨state¨|ßhßj÷¨state_zh¨|¨打开¨¨关闭¨÷}÷}
复制代码 页面配置,下面直接上代码
- //参考点灯社区GuoFuqiang与284003505两位网友的代码
- //
- //#define BLINKER_MIOT_LIGHT //小爱库灯光
- #define BLINKER_MIOT_MULTI_OUTLET //小爱插座
- #define BLINKER_WIFI //WiFi
- #define BLINKER_PRINT Serial //串口协议
- #include <WiFiManager.h> //wifimanager库自动配网库
- #include <Blinker.h> //点灯库
- char auth[] = "*************";//点灯科技app里的key
- //char ssid[] = "不需要";//wifi名字
- //char pswd[] = "不需要";//wifi密码
- // 新建组件对象
- BlinkerButton ButtonOn1("btn-on1");//按键的名称 开灯
- BlinkerButton ButtonOn2("btn-on2");//按键的名称 开灯
- BlinkerButton ButtonOn3("btn-on3");//按键的名称 开灯
- BlinkerButton ButtonOn4("btn-on4");//按键的名称 开灯
- int counter = 0;
- int GPIO1=16;//定义GPIO1口用于控制继电器
- int GPIO2=14;//定义GPIO2口用于控制继电器
- int GPIO3=12;//定义GPIO3口用于控制继电器
- int GPIO4=13;//定义GPIO4口用于控制继电器
- int KEY1=5;//遥控按键1定义高电平有效
- int KEY2=4;//遥控按键2定义高电平有效
- int KEY3=0;//遥控按键3定义高电平有效
- int KEY4=2;//遥控按键4定义高电平有效
- //定义插座状态,用于小爱同学状态反馈
- bool oState[5] = { false };
- #define OUTLET_ALL 0 //所有插孔
- #define OUTLET_NO_1 1 //插座插孔一
- #define OUTLET_NO_2 2 //插座插孔二
- #define OUTLET_NO_3 3 //插座插孔三
- #define OUTLET_NO_4 4 //插座插孔四
- int gpiobj1,gpiobj2,gpiobj3,gpiobj4;
- //使用web配置网络
- WiFiManager wifiManager;
- char devname[] = "ESP8266-4p"; //定义WIFI名称和主机名
- void WebConfig() {
- Serial.println("Entered config mode...");
- // 自动连接WiFi,参数是连接ESP8266时的WiFi名称
- wifiManager.setConfigPortalTimeout(60); //设置连接超时时间60s
- wifiManager.autoConnect(devname);
- WiFi.hostname(devname); //设置主机名
- // WiFi连接成功后将通过串口监视器输出连接成功信息
- Serial.println("");
- Serial.print("ESP8266 Connected to ");
- Serial.println(WiFi.SSID()); // WiFi名称
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // IP
- }
- // 按下按键1即会执行该函数
- void button1_callback(const String & state) {
- if (state == "on") {
- BLINKER_LOG("get button state: 开灯");
- Blinker.print("switch", "on");
- digitalWrite(GPIO1, HIGH);
- delay(200);
- ButtonOn1.color("red"); //设置app按键是红色
- ButtonOn1.print("on");
-
- gpiobj1=HIGH;
- }
- if (state == "off") {
- BLINKER_LOG("get button state: 关灯");
- digitalWrite(GPIO1, LOW);
- delay(200);
- // 反馈开关状态
- ButtonOn1.color("gray"); //设置app按键是灰色
- ButtonOn1.print("off");
- gpiobj1=LOW;
- }
- Blinker.vibrate();
- }
- // 按下按键2即会执行该函数
- void button2_callback(const String & state) {
- if (state == "on") {
- BLINKER_LOG("get button state: 开灯");
- Blinker.print("switch", "on");
- digitalWrite(GPIO2, HIGH);
- delay(200);
- ButtonOn2.color("red"); //设置app按键是红色
- ButtonOn2.print("on");
- gpiobj2=HIGH;
- }
- if (state == "off") {
- BLINKER_LOG("get button state: 关灯");
- digitalWrite(GPIO2, LOW);
- delay(200);
- // 反馈开关状态
- ButtonOn2.color("gray"); //设置app按键是灰色
- ButtonOn2.print("off");
- gpiobj2=LOW;
- }
- Blinker.vibrate();
- }
- // 按下按键3即会执行该函数
- void button3_callback(const String & state) {
- if (state == "on") {
- BLINKER_LOG("get button state: 开灯");
- Blinker.print("switch", "on");
- digitalWrite(GPIO3, HIGH);
- delay(200);
- ButtonOn3.color("red"); //设置app按键是红色
- ButtonOn3.print("on");
- gpiobj3=HIGH;
- }
- if (state == "off") {
- BLINKER_LOG("get button state: 关灯");
- digitalWrite(GPIO3, LOW);
- delay(200);
- // 反馈开关状态
- ButtonOn3.color("gray"); //设置app按键是灰色
- ButtonOn3.print("off");
- gpiobj3=LOW;
- }
- Blinker.vibrate();
- }
- // 按下按键4即会执行该函数
- void button4_callback(const String & state) {
- if (state == "on") {
- BLINKER_LOG("get button state: 开灯");
- Blinker.print("switch", "on");
- digitalWrite(GPIO4, HIGH);
- delay(200);
- ButtonOn4.color("red"); //设置app按键是红色
- ButtonOn4.print("on");
- gpiobj4=HIGH;
- }
- if (state == "off") {
- BLINKER_LOG("get button state: 关灯");
- digitalWrite(GPIO4, LOW);
- delay(200);
- // 反馈开关状态
- ButtonOn4.color("gray"); //设置app按键是灰色
- ButtonOn4.print("off");
- gpiobj4=LOW;
- }
- Blinker.vibrate();
- }
- // 如果未绑定的组件被触发,则会执行其中内容
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- counter++;
- }
- ///如果本地开关有动作执行下面手动模式
- void sdms()
- {
- int bj;
- //继电器1手动操作
- if (digitalRead(KEY1)==LOW||digitalRead(KEY2)==LOW||digitalRead(KEY3)==LOW||digitalRead(KEY4)==LOW)
- {
- if(digitalRead(KEY1)==LOW)bj=1;
- else if(digitalRead(KEY2)==LOW)bj=2;
- else if(digitalRead(KEY3)==LOW)bj=3;
- else if(digitalRead(KEY4)==LOW)bj=4;
- else{}
- Blinker.delay(400); //延时400ms,最少需150ms
-
- if (digitalRead(KEY1)==HIGH&&bj==1&&gpiobj1==LOW)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器1开!"); //串口打印
- digitalWrite(GPIO1,HIGH);
- ButtonOn1.color("blue"); //设置app按键是蓝色
- ButtonOn1.print("on");
- gpiobj1=HIGH;
- }
- else if (digitalRead(KEY2)==HIGH&&bj==2&&gpiobj2==LOW)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器2开!"); //串口打印
- digitalWrite(GPIO2,HIGH);
- ButtonOn2.color("blue"); //设置app按键是蓝色
- ButtonOn2.print("on");
- gpiobj2=HIGH;
- }
- else if (digitalRead(KEY3)==HIGH&&bj==3&&gpiobj3==LOW)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器3开!"); //串口打印
- digitalWrite(GPIO3,HIGH);
- ButtonOn3.color("blue"); //设置app按键是蓝色
- ButtonOn3.print("on");
- gpiobj3=HIGH;
- }
- else if (digitalRead(KEY4)==HIGH&&bj==4&&gpiobj4==LOW)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器4开!"); //串口打印
- digitalWrite(GPIO4,HIGH);
- ButtonOn4.color("blue"); //设置app按键是蓝色
- ButtonOn4.print("on");
- gpiobj4=HIGH;
- }
- else if (digitalRead(KEY1)==HIGH&&bj==1&&gpiobj1==HIGH)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器1关!"); //串口打印
- digitalWrite(GPIO1,LOW);
- ButtonOn1.color("gray"); //设置app按键是灰色
- ButtonOn1.print("off");
- gpiobj1=LOW;
- }
- else if (digitalRead(KEY2)==HIGH&&bj==2&&gpiobj2==HIGH)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器2关!"); //串口打印
- digitalWrite(GPIO2,LOW);
- ButtonOn2.color("gray"); //设置app按键是灰色
- ButtonOn2.print("off");
- gpiobj2=LOW;
- }
- else if (digitalRead(KEY3)==HIGH&&bj==3&&gpiobj3==HIGH)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器3关!"); //串口打印
- digitalWrite(GPIO3,LOW);
- ButtonOn3.color("gray"); //设置app按键是灰色
- ButtonOn3.print("off");
- gpiobj3=LOW;
- }
- else if (digitalRead(KEY4)==HIGH&&bj==4&&gpiobj4==HIGH)
- {
- BLINKER_LOG("本地开关动作!");
- BLINKER_LOG("继电器4关!"); //串口打印
- digitalWrite(GPIO4,LOW); //继电器操作
- ButtonOn4.color("gray"); //设置app按键是灰色
- //ButtonOn4.text("继电器关闭"); //设置app按键文子
- ButtonOn4.print("off"); //返回开关状态
- gpiobj4=LOW;
- }
- else {}
- }
- }
- //小爱同学控制插座多个插孔
- void ctrl_multi_outlet(uint8_t num, uint8_t io_level)
- {
- switch (num)
- {
- case 0: //所有插孔
- digitalWrite(GPIO1, io_level);//控制继电器1
- digitalWrite(GPIO2, io_level);//控制继电器2
- digitalWrite(GPIO3, io_level);//控制继电器3
- digitalWrite(GPIO4, io_level);//控制继电器4
- break;
- case 1: //插座插孔一
- digitalWrite(GPIO1, io_level);//控制继电器1
- break;
- case 2: //插座插孔二
- digitalWrite(GPIO2, io_level);//控制继电器2
- break;
- case 3: //插座插孔三
- digitalWrite(GPIO3, io_level);//控制继电器3
- break;
- case 4: //插座插孔四
- digitalWrite(GPIO4, io_level);//控制继电器4
- break;
- default:
- break;
- }
- }
- //小爱电源类回调,例如:“打开插座”、“打开插座插孔一”、“打开插座插孔二”
- void miotPowerState(const String & state, uint8_t num)
- {
- BLINKER_LOG("need set outlet: ", num, ", power state: ", state); //打印数据
- if (state == BLINKER_CMD_ON)
- {
- ctrl_multi_outlet(num, HIGH);//打开继电器,num表示是多少路(继电器低电平出发)
- BlinkerMIOT.powerState("on", num); //回馈操作状态
- BlinkerMIOT.print();
- //RelayState(num); //反馈继电器状态
- oState[num] = true; //状态变量赋值
- }
- else if (state == BLINKER_CMD_OFF)
- {
- ctrl_multi_outlet(num, LOW);//关闭继电器,num表示是多少路
- BlinkerMIOT.powerState("off", num);
- BlinkerMIOT.print();
- //RelayState(num);
- oState[num] = false;
- }
- }
- //小爱设备查询的回调函数,查询设备状态,例如:“插座插孔一状态”
- void miotQuery(int32_t queryCode,uint8_t num)
- {
- BLINKER_LOG("插孔",num,"状态",",codes:", queryCode);
- switch (num)
- {
- case 0 :
- BLINKER_LOG("状态:");
- BlinkerMIOT.powerState(oState[1] ? "on" : "off");
- BlinkerMIOT.powerState(oState[2] ? "on" : "off");
- BlinkerMIOT.powerState(oState[3] ? "on" : "off");
- BlinkerMIOT.powerState(oState[4] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- case 1 :
- BLINKER_LOG("插孔1状态:");
- BlinkerMIOT.powerState(oState[1] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- case 2 :
- BLINKER_LOG("插孔2状态:");
- BlinkerMIOT.powerState(oState[2] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- case 3 :
- BLINKER_LOG("插孔3状态:");
- BlinkerMIOT.powerState(oState[3] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- case 4 :
- BLINKER_LOG("插孔4状态:");
- BlinkerMIOT.powerState(oState[4] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- default :
- BlinkerMIOT.powerState(oState[1] ? "on" : "off");
- BlinkerMIOT.powerState(oState[2] ? "on" : "off");
- BlinkerMIOT.powerState(oState[3] ? "on" : "off");
- BlinkerMIOT.powerState(oState[4] ? "on" : "off");
- BlinkerMIOT.print();
- break;
- }
- }
- void setup() {
-
- Serial.begin(115200); // 初始化串口
- pinMode(GPIO1,OUTPUT); //继电器1初始化为输出模式
- pinMode(GPIO2,OUTPUT); //继电器2初始化为输出状态
- pinMode(GPIO3,OUTPUT); //继电器3初始化为输出状态
- pinMode(GPIO4,OUTPUT); //继电器4初始化为输出模式
- pinMode(KEY1,INPUT_PULLUP); //LED_G初始化为输出状态
- pinMode(KEY2,INPUT_PULLUP); //LED_G初始化为输出状态
- pinMode(KEY3,INPUT_PULLUP); //LED_G初始化为输出状态
- pinMode(KEY4,INPUT_PULLUP); //LED_G初始化为输出状态
- digitalWrite(GPIO1,LOW); //初始化,由于继电器是低电平触发。所以刚开始设为高电平
- digitalWrite(GPIO2,LOW);//初始化,由于LED_R是低电平触发。所以刚开始设为高电平
- digitalWrite(GPIO3,LOW);//初始化,由于LED_G是低电平触发。所以刚开始设为高电平
- digitalWrite(GPIO4,LOW); //初始化,由于继电器是低电平触发。所以刚开始设为高电平
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
-
- // 初始化blinker
- //Blinker.begin(auth, ssid, pswd);
- WebConfig(); //AP配置网络
- Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str()); //初始化blinker
- Blinker.attachData(dataRead); //状态回调
- ButtonOn1.attach(button1_callback); //APP回调函数
- ButtonOn2.attach(button2_callback); //APP回调函数
- ButtonOn3.attach(button3_callback); //APP回调函数
- ButtonOn4.attach(button4_callback); //APP回调函数
- //小爱同学注册回调
- BlinkerMIOT.attachPowerState(miotPowerState); //注册小爱电源回调
- BlinkerMIOT.attachQuery(miotQuery); //小爱设备查询的回调函数
-
- // BlinkerMIOT.attachPowerState(miotPowerState);//这段代码一定要加,不加小爱同学控制不了,务必在回调函数中反馈该控制状态
- }
- void loop() {
- Blinker.run();
- sdms();//本地开关扫描
- }
复制代码 代码一般有那可以改进望大神指点
|
-
app
|