Улучшил работу в случае отсутствия WiFi
This commit is contained in:
+21
-10
@@ -3,6 +3,10 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
// network
|
// network
|
||||||
|
// After how much time without connection we will
|
||||||
|
// reboot (in millis)
|
||||||
|
#define NON_CONNECTED_REBOOT_TIMEOUT 60000
|
||||||
|
#define RECONNECT_TIMEOUT 10000
|
||||||
|
|
||||||
IPAddress my_ip(MY_IP);
|
IPAddress my_ip(MY_IP);
|
||||||
IPAddress gateway(MY_GW);
|
IPAddress gateway(MY_GW);
|
||||||
@@ -47,19 +51,13 @@ void setup()
|
|||||||
WiFi.setAutoReconnect(true);
|
WiFi.setAutoReconnect(true);
|
||||||
WiFi.begin(WIFI_NAME, WIFI_PASS);
|
WiFi.begin(WIFI_NAME, WIFI_PASS);
|
||||||
Serial.print("Connecting");
|
Serial.print("Connecting");
|
||||||
while (WiFi.status() != WL_CONNECTED)
|
delay(500);
|
||||||
{
|
|
||||||
delay(500);
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
Serial.println();
|
Serial.println();
|
||||||
|
|
||||||
Serial.print("Connected, IP address: ");
|
|
||||||
Serial.println(WiFi.localIP());
|
|
||||||
uint8_t macAddr[6];
|
uint8_t macAddr[6];
|
||||||
WiFi.macAddress(macAddr);
|
WiFi.macAddress(macAddr);
|
||||||
sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
sprintf(id, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
||||||
Serial.printf("Connected, mac address: %s\n", id);
|
Serial.printf("mac address: %s\n", id);
|
||||||
last_poll = millis();
|
last_poll = millis();
|
||||||
|
|
||||||
mb.server(15000);
|
mb.server(15000);
|
||||||
@@ -101,6 +99,9 @@ void UpdateCurrent()
|
|||||||
last_update = millis();
|
last_update = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long last_connected = 0;
|
||||||
|
unsigned long last_connecting = 0;
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
unsigned long current = millis();
|
unsigned long current = millis();
|
||||||
@@ -138,8 +139,18 @@ void loop()
|
|||||||
mb.Hreg(REGISTER_SECONDARY_TEMP, cur_t);
|
mb.Hreg(REGISTER_SECONDARY_TEMP, cur_t);
|
||||||
if (WiFi.status() != WL_CONNECTED)
|
if (WiFi.status() != WL_CONNECTED)
|
||||||
{
|
{
|
||||||
// no wifi
|
if (current < last_connecting || current - last_connecting > RECONNECT_TIMEOUT)
|
||||||
Serial.println("No WiFi connection");
|
{
|
||||||
|
WiFi.reconnect();
|
||||||
|
last_connecting = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last_connected = current;
|
||||||
|
}
|
||||||
|
if (current < last_connected || current - last_connected > NON_CONNECTED_REBOOT_TIMEOUT)
|
||||||
|
{
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user