BLINKER_ESP_SMARTCONFIG的连接逻辑不太符合我的需求,想实现的逻辑是:自动连接保存的WiFi>如果失败>尝试使用程序预置的SSID和密钥连接 =>双击按键启动smartConfig
先放一下报错内容
- In file included from D:\Documents\Arduino\SmartConfig\SmartConfig.ino:3:0:
- d:\Documents\Arduino\libraries\blinker-library-master\src/Blinker.h:245:6: error: #error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
- #error Please set a mode BLINKER_BLE/BLINKER_WIFI/BLINKER_MQTT ! Please check your mode setting.
- ^
- exit status 1
复制代码
个人粗鄙的看法是因为"define BLINKER_WIFI"跟"Blinker.begin(auth)"的原因,BLINKER_WIFI需要(auth, ssid, pswd)。但我已经通过其他方式完成了网络连接,并不需要ssid和pswd。不知道我的分析是否正确。
请大佬帮忙看看,感谢!
程序内容
- #define BLINKER_WIFI
- #include <Blinker.h>
- #include <DHT.h>
- #include <ESP8266WiFi.h>
- #include <OneButton.h>
- unsigned char num = 0;
- #define STASSID "iopewq"
- #define STAPSK "12345678"
- char auth[] = "1*********b";
- const char* ssid = STASSID;
- const char* password = STAPSK;
- OneButton button1(D3, true);
- BlinkerNumber HUMI("humi");
- BlinkerNumber TEMP("temp");
- #define DHTPIN D7
- #define LED_PIN D4 //板载LED
- #define DHTTYPE DHT11 // DHT 11
- DHT dht(DHTPIN, DHTTYPE);
- float humi_read = 0, temp_read = 0;
- void heartbeat()
- {
- HUMI.print(humi_read);
- TEMP.print(temp_read);
- }
- void deviceReset()
- {
- // Reset device ,erase WiFi config.
- Blinker.reset();
- }
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- Blinker.vibrate();
-
- uint32_t BlinkerTime = millis();
-
- Blinker.print("millis", BlinkerTime);
- }
- bool autoConfig()
- {
- WiFi.begin();
- for (int i = 0; i < 20; i++)
- {
- int wstatus = WiFi.status();
- if (wstatus == WL_CONNECTED)
- {
- Serial.println("自动连接上一次配置成功");
- //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);
- }
- bool defaultwifi()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- for (int i=0; 1 < 20; i++)
- {
- int wstatus = WiFi.status();
- if (wstatus == WL_CONNECTED)
- {
- Serial.println("连接默认配置成功");
- //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("使用默认配置连接中...");
- Serial.println(wstatus);
- delay(1000);
- }
- }
- }
- void smartConfig()
- {
- WiFi.mode(WIFI_STA);
- Serial.println("等待SmartConfig!");
- WiFi.beginSmartConfig(); //开启SmartConfig服务
- while(1)
- {
- Serial.print(".");
- num++;
- if(num >= 15)
- {
- Serial.print("\n");
- num = 0;
- }
- delay(500);
- if(WiFi.smartConfigDone()) //连接成功标志
- {
- Serial.println("SmartConfig成功!");
- delay(2000);
- Serial.println();
- Serial.printf("SSID:%s\t\n",WiFi.SSID().c_str()); //显示WiFi SSID
- Serial.printf("PASSWORD:%s\t\n",WiFi.psk().c_str()); //显示WiFi密码
- delay(2000);
- Serial.printf("IP address: ");
- WiFi.setAutoConnect(true);
- Serial.println(WiFi.localIP());
- WiFi.printDiag(Serial);
- break;
- }
- else //连接失败后的提示
- Serial.println("SmartConfig,Please reboot ESP8266 and try agin!");
- }
- }
- void click1() {
- Serial.println("按键1单击");
- digitalWrite(LED_PIN, !digitalRead(LED_PIN));
- }
-
- void DoubleClick1() {
- Serial.println("按键1双击");
- smartConfig();
- } // DoubleClick1
- void longPress1() {
- Serial.println("按键1长按");
- deviceReset();
- } // longPress1
-
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println("Serial is ok!");
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW);
-
- Blinker.begin(auth);
- Blinker.attachData(dataRead);
- button1.attachClick(click1);//按键1单击
- button1.attachDoubleClick(doubleclick1);//按键1双击
- button1.attachDuringLongPress(longPress1);//按钮1长按
- if (!autoConfig())
- {
- //Serial.println("Defaultconfig module");
- defaultwifi();
- if (!defaultwifi())
- {
- //Serial.println("smartConfig module");
- smartConfig();
- }
- }
- Blinker.attachHeartbeat(heartbeat);
- dht.begin();
- }
-
- void loop() {
- Blinker.run();
- button1.tick();
-
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t))
- {
- BLINKER_LOG("Failed to read from DHT sensor!");
- }
- else
- {
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- humi_read = h;
- temp_read = t;
- }
- Blinker.delay(2000);
- // put your main code here, to run repeatedly:
- }
-
复制代码
|