ESP8266+点灯APP+小爱同学控制灯开关
/*点灯APP+小爱控制灯 20201年8月28日
功能:
1、用点灯APP控制灯开关
2、用小爱同学控制灯开关
材料:
ESP8266
5V继电器模块
220V转5V供电模块
作者:九洲创客
QQ:353529281
*/
#define BLINKER_WIFI //调用WIFI库
#define BLINKER_MIOT_LIGHT//调用开关库
#include <Blinker.h> //点灯库
char auth[] = "XXXX"; //点灯KEY
char ssid[] = "XXX"; //家里WIFI账号
char pswd[] = "XXXX";//这里WIFI密码
#define DengPin D4//控制继电器的IO脚
// 新建组件对象
BlinkerButton Deng1("btn-deng"); //点灯APP新建按钮开关键名
// 按下按键即会执行该函数
void deng1_callback(const String & state) {
BLINKER_LOG("need set power state: ", state);
if(state=="on"){
digitalWrite(DengPin,LOW); //设置继电器状态
Deng1.print("on"); //反馈按钮状态
}
else if(state == "off"){
digitalWrite(DengPin,HIGH);
Deng1.print("off");
}
}
void miotPowerState(const String & state) //语音控制
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(DengPin,LOW);//设置继电器状态
Deng1.print("on");//反馈点灯APP按钮状态
BlinkerMIOT.powerState("on"); //反馈开关状态
BlinkerMIOT.print();//发送到APP
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(DengPin,HIGH);
Deng1.print("off");
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void setup() {
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial); //串口输出状态
// 初始化有继电器的IO(我的继电器是低电平输出,所以默认输出高电平)
pinMode(DengPin,OUTPUT);
digitalWrite(DengPin, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Deng1.attach(deng1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
}
void loop() {
Blinker.run();//点灯科技运行
}
#在这里快速回#include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> ESP8266WiFiMulti wifiMulti;bool autoConfig() { WiFi.begin(); for (int i = 0; i < 20; i++) { int wstatus = WiFi.status(); if (wstatus == WL_CONNECTED) { Serial.println("AutoConfig Success"); Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str()); Serial.printf("PSW:%s\r\n", WiFi.psk().c_str()); WiFi.printDiag(Serial); return true; //break; } else { Serial.print("AutoConfig Waiting......"); Serial.println(wstatus); delay(1000); } } Serial.println("AutoConfig Faild!" ); return false; //WiFi.printDiag(Serial); } void smartConfig() { WiFi.mode(WIFI_STA); Serial.println("\r\nWait for Smartconfig"); WiFi.beginSmartConfig(); while (1) { Serial.print("."); if (WiFi.smartConfigDone()) { Serial.println("SmartConfig Success"); Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str()); Serial.printf("PSW:%s\r\n", WiFi.psk().c_str()); WiFi.setAutoConnect(true);// 设置自动连接 break; } delay(1000); // 这个地方一定要加延时,否则极易崩溃重启 } }void setup(){ Serial.begin(9600); Serial.println("Wait for Smartconfig"); if (!autoConfig()) { Serial.println("Start module"); smartConfig(); } pinMode(2, OUTPUT); }void loop(){ digitalWrite(2,HIGH); delay(1000); digitalWrite(2,LOW); delay(1000);}复#
页:
[1]