|
昨天我的esp8266nodemcu v3到货了,研究了一晚上从环境搭建到程序上传
终于做出了自己的第一个作品,虽然很多代码不太理解,但是功能还是实现了
《现在有一个小问题,就是APP操作的多了,偶尔串口调试窗口会显示 ERROR: MQTT MSG LIMIT 不知道什么原因
这个加入了WIFI配网 需要添加WiFiManager 库文件
- #define BLINKER_PRINT Serial#define BLINKER_ALIGENIE_MULTI_OUTLET
- #define BLINKER_WIFI //WiFi
- #include <WiFiManager.h> //wifimanager库自动配网库
- #include <Blinker.h>
- BlinkerButton Button1("btn-lt1"); //设置点灯app内数据键名
- BlinkerButton Button2("btn-lt2");
- BlinkerButton Button3("btn-lt3");
- BlinkerButton Button4("btn-lt4");
- char i; //定义变量
- char auth[] = "这个是点灯APP上新添加设备的SN"; //填上刚刚记下来的密钥
- bool oState[5] = { false }; //记录天猫精灵控制状态量
- //使用web配置网络
- WiFiManager wifiManager;
- char devname[] = "ESP8266-12"; //定义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
- }
- void aligeniePowerState(const String & state, uint8_t num)//天猫精灵电源状态函数
- {
- BLINKER_LOG("need set outlet: ", num, ", power state: ", state);//获取电源状态,串口中输出
- if (num == 1) {
- i = 0; //指定每一路开关对应在开发板上的通道接口
- } else if (num == 2) {
- i = 2;
- } else if (num == 3) {
- i = 4;
- } else if (num == 4) {
- i = 5;
- }
- if (state == BLINKER_CMD_ON) //状态为开
- {
- digitalWrite(i, HIGH); //状态为开的端口对应动作
- BlinkerAliGenie.powerState("on", num); //返回开状态
- BlinkerAliGenie.print();
- oState[num] = true;
- }
- else if (state == BLINKER_CMD_OFF) {
- digitalWrite(i, LOW);
- BlinkerAliGenie.powerState("off", num);
- BlinkerAliGenie.print();
- oState[num] = false;
- if (num == 0)
- {
- for (uint8_t o_num = 0; o_num < 5; o_num++)
- {
- oState[o_num] = false;
- }
- }
- }
- }
- void aligenieQuery(int32_t queryCode, uint8_t num) //天猫精灵查询状态
- {
- BLINKER_LOG("AliGenie Query outlet: ", num, ", codes: ", queryCode);
- switch (queryCode)
- {
- case BLINKER_CMD_QUERY_ALL_NUMBER :
- BLINKER_LOG("AliGenie Query All");
- BlinkerAliGenie.powerState(oState[num] ? "on" : "off", num);
- BlinkerAliGenie.print();
- break;
- case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
- BLINKER_LOG("AliGenie Query Power State");
- BlinkerAliGenie.powerState(oState[num] ? "on" : "off", num);
- BlinkerAliGenie.print();
- break;
- default :
- BlinkerAliGenie.powerState(oState[num] ? "on" : "off", num);
- BlinkerAliGenie.print();
- break;
- }
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
- uint32_t BlinkerTime = millis();
- Blinker.print(BlinkerTime);
- Blinker.print("millis", BlinkerTime);
- }
- //*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
- void heartbeat()
- {
- BLINKER_LOG("状态同步!");
- if (digitalRead(0)==HIGH) //状态为开
- {
- Button1.print("on");
- Button1.color("red"); //设置app按键是红色
- }
- else
- {
- Button1.print("off");
- Button1.color("gray"); //设置app按键是灰色
- }
- delay(200);
- if (digitalRead(2)==HIGH) //状态为开
- {
- Button2.print("on");
- Button2.color("red"); //设置app按键是红色
- }
- else
- {
- Button2.print("off");
- Button2.color("gray"); //设置app按键是灰色
- }
- delay(200);
- if (digitalRead(4)==HIGH) //状态为开
- {
- Button3.print("on");
- Button3.color("red"); //设置app按键是红色
- }
- else
- {
- Button3.print("off");
- Button3.color("gray"); //设置app按键是灰色
- }
- delay(200);
- if (digitalRead(5)==HIGH) //状态为开
- {
- Button4.print("on");
- Button4.color("red"); //设置app按键是红色
- }
- else
- {
- Button4.print("off");
- Button4.color("gray"); //设置app按键是灰色
- }
- delay(200);
- }
- // 按下按键1即会执行该函数
- void button1_callback(const String & state)
- {
- if (state == "on") {
- BLINKER_LOG("1: 打开状态");
- Blinker.print("switch", "on");
- digitalWrite(0, HIGH);
- delay(200);
- Button1.color("red"); //设置app按键是红色
- Button1.print("on");
- oState[1] = true; //同步到天猫精灵,不加此代码APP操作后,语音查询状态时不同步。其实也可以加入到心跳包
- }
- if (state == "off") {
- BLINKER_LOG("1: 关闭状态");
- digitalWrite(0, LOW);
- delay(200);
- // 反馈开关状态
- Button1.color("gray"); //设置app按键是灰色
- Button1.print("off");
- oState[1] = false; //同步到天猫精灵,不加此代码APP操作后,语音查询状态时不同步
- }
- Blinker.vibrate();
- }
- // 按下按键2即会执行该函数
- void button2_callback(const String & state)
- {
- if (state == "on") {
- BLINKER_LOG("2: 打开状态");
- Blinker.print("switch", "on");
- digitalWrite(2, HIGH);
- delay(200);
- Button2.color("red"); //设置app按键是红色
- Button2.print("on");
- oState[2] = true;
- }
- if (state == "off") {
- BLINKER_LOG("2: 关闭状态");
- digitalWrite(2, LOW);
- delay(200);
- // 反馈开关状态
- Button2.color("gray"); //设置app按键是灰色
- Button2.print("off");
- oState[2] = false;
- }
- Blinker.vibrate(); //app内操作按下,振动反馈
- }
- // 按下按键3即会执行该函数
- void button3_callback(const String & state)
- {
- if (state == "on") {
- BLINKER_LOG("3: 打开状态");
- Blinker.print("switch", "on");
- digitalWrite(4, HIGH);
- delay(200);
- Button3.color("red"); //设置app按键是红色
- Button3.print("on");
- oState[3] = true;
- }
- if (state == "off") {
- BLINKER_LOG("3: 关闭状态");
- digitalWrite(4, LOW);
- delay(200);
- // 反馈开关状态
- Button3.color("gray"); //设置app按键是灰色
- Button3.print("off");
- oState[3] = false;
- }
- Blinker.vibrate();
- }
- // 按下按键4即会执行该函数
- void button4_callback(const String & state)
- {
- if (state == "on") {
- BLINKER_LOG("4: 打开状态");
- Blinker.print("switch", "on");
- digitalWrite(5, HIGH);
- delay(200);
- Button4.color("red"); //设置app按键是红色
- Button4.print("on");
- oState[4] = true;
- }
- if (state == "off") {
- BLINKER_LOG("4: 关闭状态");
- digitalWrite(5, LOW);
- delay(200);
- // 反馈开关状态
- Button4.color("gray"); //设置app按键是灰色
- Button4.print("off");
- oState[4] = false;
- }
- Blinker.vibrate();
- }
- void setup()
- {
- Serial.begin(115200);//初始化串口
- #if defined(BLINKER_PRINT)
- BLINKER_DEBUG.stream(BLINKER_PRINT);
- #endif
- pinMode(0, OUTPUT);
- digitalWrite(0, LOW);
- pinMode(2, OUTPUT);
- digitalWrite(2, LOW);
- pinMode(4, OUTPUT);
- digitalWrite(4, LOW);
- pinMode(5, OUTPUT);
- digitalWrite(5, LOW);
- pinMode(LED_BUILTIN, OUTPUT); //LED设定为输出模式
- digitalWrite(LED_BUILTIN, LOW); //LED低电平
- //Blinker.begin(auth, ssid, pswd); //连接网络并接入物联网平台
- WebConfig(); //AP配置网络
- Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str()); //初始化blinker
- Blinker.attachData(dataRead);
- BlinkerAliGenie.attachPowerState(aligeniePowerState); //启动天猫精灵电源状态函数
- BlinkerAliGenie.attachQuery(aligenieQuery); //启动天猫精灵查询函数
- Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态进行语音操作和app操作同步。
- Button1.attach(button1_callback); //app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback"
- Button2.attach(button2_callback);
- Button3.attach(button3_callback);
- Button4.attach(button4_callback);
- }
- void loop()
- {
- Blinker.run();
- }
|
|