|
如何能配网加上超时,就是在一定时间内没有配网或配网失败,就进入loop循环,进入离线运行状态.请大神不吝赐教.谢谢!!!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266HTTPClient.h>
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h>
- #define sensor D7 //红外传感器输入口
- int LED1 = 12;
- int LED2 = 5;
- int buzzer = 14;
- int smokeA0 = 13;
- int sensorThreshold = 400;
- /******************************************************************************/
- String uid = "ed5c4b420f53e61f30921"; // 用户私钥,巴法云控制台获取
- String type = "1"; // 1表示是预警消息,默认即可
- String device = "烟雾监测"; // 设备名称
- String msg = "烟雾浓度超标"; //发送的消息
- String msg2 = "车间";
- int delaytime = 4;
- String ApiUrl = "http://ai.bemfa.com/api/wechat/v1/"; //默认 api 网址
- /******************************************************************************/
- static uint32_t lastWiFiCheckTick = 0;
- //=======================================================================
- // WIFI重新连接函数
- //=======================================================================
- void startSTA(){
- WiFi.disconnect();
- WiFi.mode(WIFI_STA);
- WiFi.begin();
- }
- //=======================================================================
- // WIFI状态检测函数,如果WIFI断开自动重连
- //=======================================================================
- void doWiFiTick(){
- if ( WiFi.status() != WL_CONNECTED ) {
- //未连接1s重连
- if (millis() - lastWiFiCheckTick > 1000) {
- lastWiFiCheckTick = millis();
- startSTA();
- }
- }
- }
- //=======================================================================
- // 初始化
- //=======================================================================
- void setup() {
- {
- pinMode(LED1, OUTPUT);
- pinMode(LED2, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(smokeA0, INPUT);
- }
- {
- Serial.begin(115200);
- // 建立WiFiManager对象
- WiFiManager wifiManager;
-
- // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
- wifiManager.autoConnect("AutoConnectAP");
-
- // 如果您希望该WiFi添加密码,可以使用以下语句:
- // wifiManager.autoConnect("AutoConnectAP", "12345678");
- // 以上语句中的12345678是连接AutoConnectAP的密码
-
- // WiFi连接成功后将通过串口监视器输出连接成功信息
- Serial.println("");
- Serial.print("ESP8266 Connected to ");
- Serial.println(WiFi.SSID()); // WiFi名称
- Serial.print("IP address:\t");
- Serial.println(WiFi.localIP()); // IP
- }
- pinMode(sensor, INPUT); // declare sensor as input
- delay(1000);
- Serial.begin(115200);
- WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
- delay(1000);
- WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot
-
- WiFi.begin(); //Connect to your WiFi router
- Serial.println("");
- Serial.print("Connecting");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- //If connection successful show IP address in serial monitor
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println();
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP()); //IP address assigned to your ESP
- }
- //=======================================================================
- // 主循环
- //=======================================================================
- void loop() {
- {
- int analogSensor = analogRead(smokeA0);
- Serial.print("Pin A0: ");
- Serial.println(analogSensor);
- // Checks if it has reached the threshold value
- if (analogSensor > sensorThreshold)
- {
- digitalWrite(LED1, HIGH);
- digitalWrite(LED2, LOW);
- noTone(buzzer);
- }
- else
- {
- digitalWrite(LED1, LOW);
- digitalWrite(LED2, HIGH);
- tone(buzzer, 1000, 200);
- }
- delay(100);
- }
- doWiFiTick();
- long state = digitalRead(sensor);
- if(state == LOW) {
- Serial.println("people here");
- doHttpStick();//在想推送消息的地方执行推送函数即可
- delay(1000);
- }
- else {
- Serial.println("no people");
- delay(1000);
- }
- }
- //******微信消息推送函数********//
- void doHttpStick(){ //微信消息推送函数
- HTTPClient http; //Declare object of class HTTPClient
- String postData;
- //Post Data
- postData = "uid="+uid+"&type=" + type +"&time="+delaytime+"&device="+device+"&msg="+msg+"&msg2="+msg2;
- http.begin(ApiUrl); //Specify request destination
- http.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Specify content-type header
- int httpCode = http.POST(postData); //Send the request
- String payload = http.getString(); //Get the response payload
- Serial.println(httpCode); //Print HTTP return code
- Serial.println(payload); //Print request response payload
- http.end(); //Close connection
- Serial.println("send success");
- }
- //=======================================================================
复制代码
|
|