Improve wifi connection management (#471)
-avoid issues with WIFI automatic reconnection on ESP32 -avoid reset of ESP32 when wifi is disconnected -enable to erase the wifi/mqtt settings if; the mqtt broker wasn't connected once or if the TRIGGER_PIN is pushed for a long time >10s
This commit is contained in:
1 parent
d7deb682e7
commit
d196f387ae
4 files changed
+113
-62
No files matched your search
+10
-2
@@ -28,7 +28,7 @@
|
|||||||
#ifndef user_config_h
|
#ifndef user_config_h
|
||||||
#define user_config_h
|
#define user_config_h
|
||||||
/*-------------------VERSION----------------------*/
|
/*-------------------VERSION----------------------*/
|
||||||
#define OMG_VERSION "0.9.2"
|
#define OMG_VERSION "0.9.2beta"
|
||||||
|
|
||||||
/*-------------CONFIGURE WIFIMANAGER-------------(only ESP8266 & SONOFF RFBridge)*/
|
/*-------------CONFIGURE WIFIMANAGER-------------(only ESP8266 & SONOFF RFBridge)*/
|
||||||
/*
|
/*
|
||||||
@@ -106,7 +106,7 @@ char mqtt_port[6] = "1883";
|
|||||||
|
|
||||||
/*-------------DEFINE YOUR ADVANCED NETWORK PARAMETERS BELOW----------------*/
|
/*-------------DEFINE YOUR ADVANCED NETWORK PARAMETERS BELOW----------------*/
|
||||||
//#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work
|
//#define MDNS_SD //uncomment if you want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work
|
||||||
#define maxMQTTretry 4 //maximum MQTT connection attempts before going to wifi setup
|
#define maxMQTTretry 10 //maximum MQTT connection attempts before going to wifimanager setup if never connected once
|
||||||
|
|
||||||
//set minimum quality of signal so it ignores AP's under that quality
|
//set minimum quality of signal so it ignores AP's under that quality
|
||||||
#define MinimumWifiSignalQuality 8
|
#define MinimumWifiSignalQuality 8
|
||||||
@@ -160,6 +160,14 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
|||||||
#define led_info 44
|
#define led_info 44
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef TRIGGER_PIN
|
||||||
|
#ifdef ESP8266
|
||||||
|
#define TRIGGER_PIN 14 // pin D5 as full reset button (long press >10s)
|
||||||
|
#elif ESP32
|
||||||
|
#define TRIGGER_PIN 0 // boot button as full reset button (long press >10s)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
||||||
// LED Resistor 270-510R
|
// LED Resistor 270-510R
|
||||||
|
|
||||||
|
|||||||
+94
-58
@@ -146,7 +146,8 @@ int failure_number = 0; // number of failure connecting to MQTT
|
|||||||
PubSubClient client(eClient);
|
PubSubClient client(eClient);
|
||||||
|
|
||||||
//MQTT last attemps reconnection date
|
//MQTT last attemps reconnection date
|
||||||
unsigned long lastReconnectAttempt = 0;
|
unsigned long lastMQTTReconnectAttempt = 0;
|
||||||
|
unsigned long lastNTWKReconnectAttempt = 0;
|
||||||
|
|
||||||
void revert_hex_data(char * in, char * out, int l){
|
void revert_hex_data(char * in, char * out, int l){
|
||||||
//reverting array 2 by 2 to get the data in good order
|
//reverting array 2 by 2 to get the data in good order
|
||||||
@@ -278,7 +279,7 @@ void pub(char * topic, char * payload, bool retainFlag){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void pub(char * topicori, JsonObject& data){
|
void pub(char * topicori, JsonObject& data){
|
||||||
|
if(client.connected()) {
|
||||||
digitalWrite(led_receive, HIGH);
|
digitalWrite(led_receive, HIGH);
|
||||||
|
|
||||||
String topic = topicori;
|
String topic = topicori;
|
||||||
@@ -328,7 +329,9 @@ void pub(char * topicori, JsonObject& data){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
}else{
|
||||||
|
trc("client not connected can't pub");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void pub(char * topic, char * payload){
|
void pub(char * topic, char * payload){
|
||||||
@@ -401,14 +404,17 @@ void pub(String topic, unsigned long payload){
|
|||||||
client.publish((char *)topic.c_str(),val);
|
client.publish((char *)topic.c_str(),val);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool reconnect() {
|
void reconnect() {
|
||||||
|
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
checkButton(); // check if a reset of wifi/mqtt settings is asked
|
||||||
|
#endif
|
||||||
|
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
trc(F("MQTT connection...")); //F function enable to decrease sram usage
|
trc(F("MQTT connection...")); //F function enable to decrease sram usage
|
||||||
if (client.connect(Gateway_Name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) {
|
if (client.connect(Gateway_Name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) {
|
||||||
trc(F("Connected to broker"));
|
trc(F("Connected to broker"));
|
||||||
failure_number = 0;
|
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
pub(will_Topic,Gateway_AnnouncementMsg,will_Retain);
|
pub(will_Topic,Gateway_AnnouncementMsg,will_Retain);
|
||||||
// publish version
|
// publish version
|
||||||
@@ -431,19 +437,17 @@ bool reconnect() {
|
|||||||
failure_number ++; // we count the failure
|
failure_number ++; // we count the failure
|
||||||
trc(F("failure_number"));
|
trc(F("failure_number"));
|
||||||
trc(failure_number);
|
trc(failure_number);
|
||||||
|
if (failure_number > maxMQTTretry && !connectedOnce){
|
||||||
|
trc(F("failed connecting first time to mqtt, reset wifi manager"));
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
setup_wifimanager(true);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
trc(F("failed, rc="));
|
trc(F("failed, rc="));
|
||||||
trc(client.state());
|
trc(client.state());
|
||||||
trc(F("try again in 5s"));
|
|
||||||
// Wait 5 seconds before retrying
|
|
||||||
delay(5000);
|
delay(5000);
|
||||||
|
|
||||||
if (failure_number > maxMQTTretry){
|
|
||||||
trc(F("failed connecting to mqtt"));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return client.connected();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Callback function, when the gateway receive an MQTT value on the topics subscribed this function is called
|
// Callback function, when the gateway receive an MQTT value on the topics subscribed this function is called
|
||||||
@@ -558,7 +562,8 @@ void setup()
|
|||||||
|
|
||||||
delay(1500);
|
delay(1500);
|
||||||
|
|
||||||
lastReconnectAttempt = 0;
|
lastMQTTReconnectAttempt = 0;
|
||||||
|
lastNTWKReconnectAttempt = 0;
|
||||||
|
|
||||||
#ifdef ZsensorBME280
|
#ifdef ZsensorBME280
|
||||||
setupZsensorBME280();
|
setupZsensorBME280();
|
||||||
@@ -667,8 +672,33 @@ void saveConfigCallback () {
|
|||||||
shouldSaveConfig = true;
|
shouldSaveConfig = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void checkButton(){ // code from tzapu/wifimanager examples
|
||||||
|
// check for button press
|
||||||
|
if ( digitalRead(TRIGGER_PIN) == LOW ) {
|
||||||
|
// poor mans debounce/press-hold, code not ideal for production
|
||||||
|
delay(50);
|
||||||
|
if( digitalRead(TRIGGER_PIN) == LOW ){
|
||||||
|
trc(F("Trigger button Pressed"));
|
||||||
|
// still holding button for 3000 ms, reset settings, code not ideaa for production
|
||||||
|
delay(3000); // reset delay hold
|
||||||
|
if( digitalRead(TRIGGER_PIN) == LOW ){
|
||||||
|
trc(F("Button Held"));
|
||||||
|
trc(F("Erasing ESP Config, restarting"));
|
||||||
|
setup_wifimanager(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup_wifimanager(bool reset_settings){
|
void setup_wifimanager(bool reset_settings){
|
||||||
if(reset_settings) SPIFFS.format();
|
|
||||||
|
pinMode(TRIGGER_PIN, INPUT_PULLUP);
|
||||||
|
if(reset_settings) {
|
||||||
|
trc("Formatting requested, result:");
|
||||||
|
trc(SPIFFS.format());
|
||||||
|
}
|
||||||
|
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
//read configuration from FS json
|
//read configuration from FS json
|
||||||
trc(F("mounting FS..."));
|
trc(F("mounting FS..."));
|
||||||
@@ -734,8 +764,16 @@ void setup_wifimanager(bool reset_settings){
|
|||||||
wifiManager.addParameter(&custom_mqtt_user);
|
wifiManager.addParameter(&custom_mqtt_user);
|
||||||
wifiManager.addParameter(&custom_mqtt_pass);
|
wifiManager.addParameter(&custom_mqtt_pass);
|
||||||
|
|
||||||
if(reset_settings) wifiManager.resetSettings();
|
if(reset_settings){
|
||||||
//set minimu quality of signal so it ignores AP's under that quality
|
wifiManager.resetSettings();
|
||||||
|
wifiManager.erase();
|
||||||
|
#if defined(ESP8266)
|
||||||
|
ESP.reset();
|
||||||
|
#else
|
||||||
|
ESP.restart();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
//set minimum quality of signal so it ignores AP's under that quality
|
||||||
wifiManager.setMinimumSignalQuality(MinimumWifiSignalQuality);
|
wifiManager.setMinimumSignalQuality(MinimumWifiSignalQuality);
|
||||||
|
|
||||||
//fetches ssid and pass and tries to connect
|
//fetches ssid and pass and tries to connect
|
||||||
@@ -835,55 +873,26 @@ void setup_ethernet() {
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
checkButton(); //check reset button state
|
||||||
|
#endif
|
||||||
|
|
||||||
digitalWrite(led_receive, LOW);
|
digitalWrite(led_receive, LOW);
|
||||||
digitalWrite(led_info, LOW);
|
digitalWrite(led_info, LOW);
|
||||||
digitalWrite(led_send, LOW);
|
digitalWrite(led_send, LOW);
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
//MQTT client connexion management
|
|
||||||
if (!client.connected()) { // not connected
|
|
||||||
if (now - lastReconnectAttempt > 5000) {
|
|
||||||
trc("client disconnected");
|
|
||||||
lastReconnectAttempt = now;
|
|
||||||
// Attempt to reconnect
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
if (WiFi.status() == WL_CONNECTED){
|
if (WiFi.status() == WL_CONNECTED){
|
||||||
if (reconnect()) {
|
|
||||||
lastReconnectAttempt = 0;
|
|
||||||
} else {
|
|
||||||
#if defined(ESPWifiManualSetup)
|
|
||||||
trc(F("restarting ESP"));
|
|
||||||
#ifdef ESP32
|
|
||||||
ESP.restart();
|
|
||||||
#endif
|
|
||||||
#ifdef ESP8266
|
|
||||||
ESP.reset();
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
if (!connectedOnce) {
|
|
||||||
trc(F("reseting wifi manager"));
|
|
||||||
setup_wifimanager(true); // if we didn't connected once to mqtt we reset and start in AP mode again to have a chance to change the parameters
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
trc("wifi disconnected");
|
|
||||||
}
|
|
||||||
#else //other boards
|
|
||||||
if (Ethernet.linkStatus() == LinkON){
|
if (Ethernet.linkStatus() == LinkON){
|
||||||
if (reconnect()) {
|
|
||||||
lastReconnectAttempt = 0;
|
|
||||||
} else {
|
|
||||||
trc(F("brk disconnected"));
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
trc("eth disconnected");
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
}
|
lastNTWKReconnectAttempt = 0;
|
||||||
} else { //connected
|
if (client.connected()) {
|
||||||
// MQTT loop
|
// MQTT loop
|
||||||
connectedOnce = true;
|
connectedOnce = true;
|
||||||
|
lastMQTTReconnectAttempt = 0;
|
||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
@@ -962,7 +971,36 @@ void loop()
|
|||||||
#ifdef ZactuatorFASTLED
|
#ifdef ZactuatorFASTLED
|
||||||
FASTLEDLoop();
|
FASTLEDLoop();
|
||||||
#endif
|
#endif
|
||||||
|
}else{
|
||||||
|
if (now - lastMQTTReconnectAttempt > 5000) {
|
||||||
|
lastMQTTReconnectAttempt = now;
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { // disconnected from network
|
||||||
|
if ( now - lastNTWKReconnectAttempt > 10000) {
|
||||||
|
lastNTWKReconnectAttempt = now;
|
||||||
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
|
trc("wifi disconnected");
|
||||||
|
delay(10000);// add a delay to avoid ESP32 crash and reset
|
||||||
|
#if defined(ESPWifiManualSetup)
|
||||||
|
trc(F("restarting ESP"));
|
||||||
|
#ifdef ESP32
|
||||||
|
ESP.restart();
|
||||||
|
#endif
|
||||||
|
#ifdef ESP8266
|
||||||
|
ESP.reset();
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
if (!connectedOnce) {
|
||||||
|
trc(F("reseting wifi manager"));
|
||||||
|
setup_wifimanager(true); // if we didn't connected once to mqtt we reset and start in AP mode again to have a chance to change the parameters
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
trc("eth disconnected");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1065,9 +1103,7 @@ void stateMeasures(){
|
|||||||
|
|
||||||
SYSdata["modules"] = modules;
|
SYSdata["modules"] = modules;
|
||||||
trc(SYSdata);
|
trc(SYSdata);
|
||||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
pub(subjectSYStoMQTT,SYSdata);
|
||||||
SYSdata.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
|
||||||
pub(subjectSYStoMQTT,JSONmessageBuffer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-1
@@ -89,7 +89,7 @@ monitor_speed = 115200
|
|||||||
|
|
||||||
[com]
|
[com]
|
||||||
esp8266_platform = espressif8266@1.8.0
|
esp8266_platform = espressif8266@1.8.0
|
||||||
esp32_platform = espressif32@1.9.0
|
esp32_platform = espressif32@1.11.0
|
||||||
atmelavr_platform = atmelavr@1.13.0
|
atmelavr_platform = atmelavr@1.13.0
|
||||||
|
|
||||||
[com-esp]
|
[com-esp]
|
||||||
@@ -102,6 +102,7 @@ build_flags =
|
|||||||
'-DsimpleReceiving=true'
|
'-DsimpleReceiving=true'
|
||||||
'-DZmqttDiscovery="HADiscovery"'
|
'-DZmqttDiscovery="HADiscovery"'
|
||||||
'-DTRACE=1'
|
'-DTRACE=1'
|
||||||
|
;'-DCORE_DEBUG_LEVEL=4'
|
||||||
|
|
||||||
[com-arduino]
|
[com-arduino]
|
||||||
lib_deps =
|
lib_deps =
|
||||||
|
|||||||
+7
-1
@@ -123,7 +123,13 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
|||||||
|
|
||||||
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
||||||
// LED Resistor 270-510R
|
// LED Resistor 270-510R
|
||||||
|
#ifndef TRIGGER_PIN
|
||||||
|
#ifdef ESP8266
|
||||||
|
#define TRIGGER_PIN 14 // pin D5 as full reset button (long press >10s)
|
||||||
|
#elif ESP32
|
||||||
|
#define TRIGGER_PIN 0 // boot button as full reset button (long press >10s)
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
/*----------------------------OTHER PARAMETERS-----------------------------*/
|
/*----------------------------OTHER PARAMETERS-----------------------------*/
|
||||||
#ifdef ZgatewaySRFB
|
#ifdef ZgatewaySRFB
|
||||||
|
|||||||
Reference in new issue
Block a user