- #include <ESP8266WiFi.h>
- #define BLINKER_ALIGENIE_LIGHT //天猫精灵
- #define BLINKER_MIOT_LIGHT //设置小爱同学控制为语音控制灯设备//kaiguan
- #define BLINKER_DUEROS_LIGHT //设置小度灯类库
- #define BLINKER_WIFI
- #define BLINKER_MQTT
- #include <ESP8266WebServer.h>
- #include <FS.h> //闪存文件系统
- #include <ArduinoJson.h>//json数据处理库(第三方)
- #include <Blinker.h>
- char auth[] = "22222222";
- char ssid1[] = "2222222";//你家的WiFi名字
- char pswd[] = "22222222";//你家WiFi的密码
- String ssid, password, location0, apikey0;//定义blinker用到的参数
- int pinRelay = 0; //定义输出端口号 IO0号端口
- BlinkerButton Button1("btn-abc");//在点灯科技app里面新建一个开关按钮 名字命名为btn-abc
- BlinkerButton Button2("btn-abd");
- BlinkerNumber Number1("num-abc");//在点灯科技app里面新建一个开关按钮 名字命名为num-abc 显示文本1:信号强度 单位:db
- ESP8266WebServer server(80); //创建Web服务端口为80
- IPAddress apIP(192, 168, 4, 1);//esp8266-AP-IP地址
- void setup() {
- pinMode(pinRelay, OUTPUT); //设置pinRelay脚为输出状态
- pinMode(LED_BUILTIN, OUTPUT);//设置Led_Builtin为输出状态
- digitalWrite(pinRelay, HIGH);/*输出HIGH电平,继电器模块闭合*/
- digitalWrite(LED_BUILTIN, LOW);//设置Led_Builtin为低电平
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);//blinker的调试状态回显
- BLINKER_DEBUG.debugAll();
- peiwang();
- //Blinker.begin(auth, ssid1, pswd);
- Blinker.attachHeartbeat(heartbeat);//心跳包
- Button1.attach(button1_callback); //绑定按键执行回调函数
- Button2.attach(button2_callback);
- BlinkerDuerOS.attachPowerState(duerPowerState); //小度语音操作注册函数
- BlinkerMIOT.attachPowerState(miotPowerState); //小爱语音操作注册函数
- BlinkerAliGenie.attachPowerState(aligeniePowerState);//天猫语音操作注册函数
- button1_callback(BLINKER_CMD_OFF);
- }
- void peiwang() {
- if (SPIFFS.begin()) { // 打开闪存文件系统
- Serial.println("");
- Serial.println("闪存文件系统打开成功");
- }
- else {
- Serial.println("");
- Serial.println("闪存文件系统打开失败");
- }
- if (SPIFFS.exists("/config.json")) // exists 判断有没有config.json这个文件
- {
- Serial.println("存在配置信息,正在自动连接");
- const size_t capacity = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + 156; //分配一个内存空间
- DynamicJsonDocument doc(capacity);// 声明json处理对象
- File configJson = SPIFFS.open("/config.json", "r");
- deserializeJson(doc, configJson); // json数据序列化
- const char* ssid = doc["ssid"]; Serial.println(ssid);
- const char* password = doc["password"]; Serial.println(password);
- const char* location = doc["location0"]; Serial.println(location);
- const char* apikey = doc["apikey0"]; Serial.println(apikey);
- WiFi.mode(WIFI_STA); // 更换wifi模式 //WiFi.mode(WIFI_AP_STA);//设置模式为AP+STA
- Serial.println("开始联网!");
- Blinker.begin(auth, ssid, password);
- //Blinker.begin(auth, ssid1, pswd);
- int s = 1;
- while (!Blinker.connect()) {
- digitalWrite(LED_BUILTIN, HIGH);
- delay(500);
- digitalWrite(LED_BUILTIN, LOW);
- delay(500);
- Serial.println(s++);
- if (s > 10) {
- Serial.println("联网失败!重新配网");
- peiwang1();
- removeConfig();
- break;
- }
- }
- if (Blinker.connect() == 1) {
- Serial.println("联网成功!");
- digitalWrite(LED_BUILTIN, LOW);
- Serial.println("获取的IP地址:");
- Serial.println(WiFi.localIP());
- configJson.close();//关闭缓存
- }
- }
- else {
- Serial.println("不存在配置信息,正在打开web配网模式");
- peiwang1();
- }
- }
- void peiwang1() {
- WiFi.mode(WIFI_AP);
- WiFi.softAP("智能开关配网"); //这里是配网模式下热点的名字和密码,热点配网。
- Serial.println(WiFi.softAPIP());
- server.on("/", handleRoot);//web首页监听
- server.on("/set", handleConnect); // 配置ssid密码监听,感觉跟express的路由好像
- server.begin();
- }
- void loop() {
- server.handleClient();
- KeySwitch();
- Blinker.run();
- }
- void handleRoot() { //展示网页的关键代码
- if (SPIFFS.exists("/index.html")) {//先判断文件系统中是否有文件
- File index = SPIFFS.open("/index.html", "r");//打开文件
- Serial.println("发现网页!");
- server.streamFile(index, "text/html");//发送响应信息
- index.close();
- } else {
- Serial.println("网页不存在!");
- }
- }
- void handleConnect() { //处理配置信息的函数
- ssid = server.arg("ssid"); //arg是获取请求参数
- password = server.arg("password");
- location0 = server.arg("location"); //从JavaScript发送的数据中找laction的值
- apikey0 = server.arg("apikey"); //从JavaScript发送的数据中找apikey的值
- server.send(200, "text/html", "<meta charset='UTF-8'>保存成功");
- delay(500);//等待保存成功回显时间
- //Blinker.begin(location0.c_str(), ssid.c_str(), password.c_str());
- Blinker.begin(auth, ssid.c_str(), password.c_str());
- int a = 0;
- while (!Blinker.connect()) {
- digitalWrite(LED_BUILTIN, HIGH);
- Blinker.delay(500);
- digitalWrite(LED_BUILTIN, LOW);
- Blinker.delay(500);
- Serial.println(a++);
- Blinker.run();
- if (a > 20) {
- Serial.println("重启esp8266配网!");
- delay(50);
- ESP.restart();
- }
- }
- Serial.println("联网成功!");
- digitalWrite(LED_BUILTIN, LOW);
- Serial.println("获取的IP地址:");
- Serial.println(WiFi.localIP());
- removeConfig(); // 不管有没有配置先删除一次再说。
- String payload; // 拼接构造一段字符串形式的json数据长{"ssid":"xxxxx","password":"xxxxxxxxxxx","location0":"xxxx","apikey0":"xxxx"}
- payload += "{"ssid":"";
- payload += ssid;
- payload += "","password":"";
- payload += password;
- payload += "","location0":"";
- payload += location0;
- payload += "","apikey0":"";
- payload += apikey0;
- payload += ""}";
- File wifiConfig = SPIFFS.open("/config.json", "w");
- wifiConfig.println(payload);//将数据写入config.json文件中
- Serial.println("配置文件写入成功!");
- wifiConfig.close();
- }
- void removeConfig() { //移除缓存中配置信息文件
- if (SPIFFS.exists("/config.json")) { // 判断有没有config.json这个文件
- if (SPIFFS.remove("/config.json")) {
- Serial.println("删除旧配置");
- }
- else {
- Serial.println("删除旧配置失败");
- }
- }
- }
- void KeySwitch() //机械开关动作判断
- {
- //Blinker.delay(500);
- static bool is_btn = digitalRead(LED_BUILTIN);//按钮的标志位,用来逻辑处理对比,判断按钮有没有改变状态
- bool is = digitalRead(LED_BUILTIN); //按钮状态
- if ( is != is_btn)
- {
- bool is_led = digitalRead(pinRelay);
- digitalWrite(pinRelay, !digitalRead(pinRelay));
- if (is_led == 0)
- {
- button1_callback(BLINKER_CMD_OFF);
- }
- else
- {
- button1_callback(BLINKER_CMD_ON);
- }
- is_btn = digitalRead(LED_BUILTIN); //更新按钮状态
- }
- }
- // 按下按键即会执行该函数
- void button1_callback(const String & state) //按钮回调函数
- {
- BLINKER_LOG("get button state:", state); //获取按钮状态
- //digitalWrite(pinRelay, !digitalRead(pinRelay));
-
- if (state == BLINKER_CMD_ON) //如果按钮状态为开
- {
- digitalWrite(pinRelay, LOW); //低电平,继电器吸合
- kaideng();
-
- //开灯
- }
- else if (state == BLINKER_CMD_OFF) //如果按钮状态为关
- {
- // 反馈开关状态
- digitalWrite(pinRelay, HIGH); //高电平,继电器断开
- guandeng();
- //关灯
- }
-
- Blinker.delay(20);
- Blinker.vibrate();
- }
- // 按下按键即会重新配网
- void button2_callback(const String & state) //按钮回调函数
- {
- Serial.println("重启esp8266配网!");
- removeConfig();
- peiwang();
- }
- void kaideng() {
-
- Button1.icon("fas fa-lightbulb");
- Button1.color("#fddb00");
- Button1.text("已经开灯");
- Button1.print("on"); //把按钮状态更改为开
- Number1.print(100 - abs(WiFi.RSSI())); //信号强度
- }
- void guandeng() {
-
- Button1.icon("fas fa-lightbulb");
- Button1.color("#000000");
- Button1.text("已经关灯");
- Button1.print("off"); //把按钮状态更改为关
- Number1.print(100 - abs(WiFi.RSSI())); //信号强度
- }
- void heartbeat() //心跳包
- {
-
- if (digitalRead(pinRelay) == HIGH)
- {
-
- Button1.icon("fas fa-lightbulb");
- Button1.color("#000000");
- Button1.text("已经关灯");
- Button1.print("off"); //把按钮状态更改为关
- }
- else if (digitalRead(pinRelay) == LOW)
- {
-
- Button1.icon("fas fa-lightbulb");
- Button1.color("#fddb00");
- Button1.text("已经开灯");
- Button1.print("on"); //把按钮状态更改为开
- }
-
- //Button1.icon("fas fa-lightbulb");
- Number1.print(100 - abs(WiFi.RSSI())); //信号强度
- }
- //天猫精灵
- void aligeniePowerState(const String & state)
- {
- BLINKER_LOG("need set power state: ", state);
- if (state == BLINKER_CMD_ON)
- {
- digitalWrite(pinRelay, LOW);
- BlinkerAliGenie.powerState("off");
- BlinkerAliGenie.print();
- kaideng();
- }
- else if (state == BLINKER_CMD_OFF) {
- digitalWrite(pinRelay, HIGH);
- BlinkerAliGenie.powerState("off");
- BlinkerAliGenie.print();
- guandeng();
- }
- }
- //小爱电源类回调
- void miotPowerState(const String & state)
- {
- BLINKER_LOG("need set power state: ", state);
- if (state == BLINKER_CMD_ON) {
- digitalWrite(pinRelay, LOW);
- BlinkerMIOT.powerState("on");
- BlinkerMIOT.print();
- kaideng();
- }
- else if (state == BLINKER_CMD_OFF) {
- digitalWrite(pinRelay, HIGH);
- BlinkerMIOT.powerState("off");
- BlinkerMIOT.print();
- guandeng();
- }
- }
- //小度电源类回调
- void duerPowerState(const String & state)
- {
- BLINKER_LOG("need set power state: ", state);
- if (state == BLINKER_CMD_ON) {
- BlinkerDuerOS.powerState("on");
- BlinkerDuerOS.print();
- kaideng();
- }
- else if (state == BLINKER_CMD_OFF) {
- BlinkerDuerOS.powerState("off");
- BlinkerDuerOS.print();
- guandeng();
- }
- }
复制代码
|