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
+198
-147
No files matched your search
+10
-2
@@ -28,7 +28,7 @@
|
||||
#ifndef user_config_h
|
||||
#define user_config_h
|
||||
/*-------------------VERSION----------------------*/
|
||||
#define OMG_VERSION "0.9.2"
|
||||
#define OMG_VERSION "0.9.2beta"
|
||||
|
||||
/*-------------CONFIGURE WIFIMANAGER-------------(only ESP8266 & SONOFF RFBridge)*/
|
||||
/*
|
||||
@@ -106,7 +106,7 @@ char mqtt_port[6] = "1883";
|
||||
|
||||
/*-------------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 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
|
||||
#define MinimumWifiSignalQuality 8
|
||||
@@ -160,6 +160,14 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
||||
#define led_info 44
|
||||
#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
|
||||
// LED Resistor 270-510R
|
||||
|
||||
|
||||
+179
-143
@@ -146,7 +146,8 @@ int failure_number = 0; // number of failure connecting to MQTT
|
||||
PubSubClient client(eClient);
|
||||
|
||||
//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){
|
||||
//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){
|
||||
|
||||
if(client.connected()) {
|
||||
digitalWrite(led_receive, HIGH);
|
||||
|
||||
String topic = topicori;
|
||||
@@ -328,7 +329,9 @@ void pub(char * topicori, JsonObject& data){
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
}else{
|
||||
trc("client not connected can't pub");
|
||||
}
|
||||
}
|
||||
|
||||
void pub(char * topic, char * payload){
|
||||
@@ -401,14 +404,17 @@ void pub(String topic, unsigned long payload){
|
||||
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
|
||||
while (!client.connected()) {
|
||||
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)) {
|
||||
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)) {
|
||||
trc(F("Connected to broker"));
|
||||
failure_number = 0;
|
||||
// Once connected, publish an announcement...
|
||||
pub(will_Topic,Gateway_AnnouncementMsg,will_Retain);
|
||||
// publish version
|
||||
@@ -427,23 +433,21 @@ bool reconnect() {
|
||||
#endif
|
||||
trc(F("Subscription OK to the subjects"));
|
||||
}
|
||||
} else {
|
||||
} else {
|
||||
failure_number ++; // we count the failure
|
||||
trc(F("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(client.state());
|
||||
trc(F("try again in 5s"));
|
||||
// Wait 5 seconds before retrying
|
||||
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
|
||||
@@ -558,7 +562,8 @@ void setup()
|
||||
|
||||
delay(1500);
|
||||
|
||||
lastReconnectAttempt = 0;
|
||||
lastMQTTReconnectAttempt = 0;
|
||||
lastNTWKReconnectAttempt = 0;
|
||||
|
||||
#ifdef ZsensorBME280
|
||||
setupZsensorBME280();
|
||||
@@ -667,8 +672,33 @@ void saveConfigCallback () {
|
||||
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){
|
||||
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
|
||||
trc(F("mounting FS..."));
|
||||
@@ -734,8 +764,16 @@ void setup_wifimanager(bool reset_settings){
|
||||
wifiManager.addParameter(&custom_mqtt_user);
|
||||
wifiManager.addParameter(&custom_mqtt_pass);
|
||||
|
||||
if(reset_settings) wifiManager.resetSettings();
|
||||
//set minimu quality of signal so it ignores AP's under that quality
|
||||
if(reset_settings){
|
||||
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);
|
||||
|
||||
//fetches ssid and pass and tries to connect
|
||||
@@ -835,135 +873,135 @@ void setup_ethernet() {
|
||||
|
||||
void loop()
|
||||
{
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
checkButton(); //check reset button state
|
||||
#endif
|
||||
|
||||
digitalWrite(led_receive, LOW);
|
||||
digitalWrite(led_info, LOW);
|
||||
digitalWrite(led_send, LOW);
|
||||
|
||||
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 (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
|
||||
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 (reconnect()) {
|
||||
lastReconnectAttempt = 0;
|
||||
} else {
|
||||
trc(F("brk disconnected"));
|
||||
}
|
||||
}else{
|
||||
trc("eth disconnected");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} else { //connected
|
||||
// MQTT loop
|
||||
connectedOnce = true;
|
||||
client.loop();
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
ArduinoOTA.handle();
|
||||
#endif
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
if (WiFi.status() == WL_CONNECTED){
|
||||
#else
|
||||
if (Ethernet.linkStatus() == LinkON){
|
||||
#endif
|
||||
lastNTWKReconnectAttempt = 0;
|
||||
if (client.connected()) {
|
||||
// MQTT loop
|
||||
connectedOnce = true;
|
||||
lastMQTTReconnectAttempt = 0;
|
||||
client.loop();
|
||||
|
||||
#ifdef ZsensorBME280
|
||||
MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280
|
||||
#endif
|
||||
#ifdef ZsensorHCSR04
|
||||
MeasureDistance(); //Addon to measure distance with a HC-SR04
|
||||
#endif
|
||||
#ifdef ZsensorBH1750
|
||||
MeasureLightIntensity(); //Addon to measure Light Intensity with a BH1750
|
||||
#endif
|
||||
#ifdef ZsensorTSL2561
|
||||
MeasureLightIntensityTSL2561();
|
||||
#endif
|
||||
#ifdef ZsensorDHT
|
||||
MeasureTempAndHum(); //Addon to measure the temperature with a DHT
|
||||
#endif
|
||||
#ifdef ZsensorINA226
|
||||
MeasureINA226();
|
||||
#endif
|
||||
#ifdef ZsensorHCSR501
|
||||
MeasureHCSR501();
|
||||
#endif
|
||||
#ifdef ZsensorGPIOInput
|
||||
MeasureGPIOInput();
|
||||
#endif
|
||||
#ifdef ZsensorGPIOKeyCode
|
||||
MeasureGPIOKeyCode();
|
||||
#endif
|
||||
#ifdef ZsensorADC
|
||||
MeasureADC(); //Addon to measure the analog value of analog pin
|
||||
#endif
|
||||
#ifdef ZgatewayLORA
|
||||
LORAtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF
|
||||
RFtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF315
|
||||
RF315toMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF2
|
||||
RF2toMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayPilight
|
||||
PilighttoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayBT
|
||||
#ifndef ESP32
|
||||
if(BTtoMQTT())
|
||||
trc(F("BTtoMQTT OK"));
|
||||
#if defined(ESP8266) || defined(ESP32)
|
||||
ArduinoOTA.handle();
|
||||
#endif
|
||||
#endif
|
||||
#ifdef ZgatewaySRFB
|
||||
SRFBtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayIR
|
||||
IRtoMQTT();
|
||||
#endif
|
||||
#ifdef Zgateway2G
|
||||
if(_2GtoMQTT()){
|
||||
trc(F("2GtoMQTT OK"));
|
||||
}
|
||||
#endif
|
||||
#ifdef ZgatewayRFM69
|
||||
if(RFM69toMQTT())
|
||||
trc(F("RFM69toMQTT OK"));
|
||||
#endif
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
stateMeasures();
|
||||
#endif
|
||||
#ifdef ZactuatorFASTLED
|
||||
FASTLEDLoop();
|
||||
#endif
|
||||
|
||||
}
|
||||
#ifdef ZsensorBME280
|
||||
MeasureTempHumAndPressure(); //Addon to measure Temperature, Humidity, Pressure and Altitude with a Bosch BME280
|
||||
#endif
|
||||
#ifdef ZsensorHCSR04
|
||||
MeasureDistance(); //Addon to measure distance with a HC-SR04
|
||||
#endif
|
||||
#ifdef ZsensorBH1750
|
||||
MeasureLightIntensity(); //Addon to measure Light Intensity with a BH1750
|
||||
#endif
|
||||
#ifdef ZsensorTSL2561
|
||||
MeasureLightIntensityTSL2561();
|
||||
#endif
|
||||
#ifdef ZsensorDHT
|
||||
MeasureTempAndHum(); //Addon to measure the temperature with a DHT
|
||||
#endif
|
||||
#ifdef ZsensorINA226
|
||||
MeasureINA226();
|
||||
#endif
|
||||
#ifdef ZsensorHCSR501
|
||||
MeasureHCSR501();
|
||||
#endif
|
||||
#ifdef ZsensorGPIOInput
|
||||
MeasureGPIOInput();
|
||||
#endif
|
||||
#ifdef ZsensorGPIOKeyCode
|
||||
MeasureGPIOKeyCode();
|
||||
#endif
|
||||
#ifdef ZsensorADC
|
||||
MeasureADC(); //Addon to measure the analog value of analog pin
|
||||
#endif
|
||||
#ifdef ZgatewayLORA
|
||||
LORAtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF
|
||||
RFtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF315
|
||||
RF315toMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayRF2
|
||||
RF2toMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayPilight
|
||||
PilighttoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayBT
|
||||
#ifndef ESP32
|
||||
if(BTtoMQTT())
|
||||
trc(F("BTtoMQTT OK"));
|
||||
#endif
|
||||
#endif
|
||||
#ifdef ZgatewaySRFB
|
||||
SRFBtoMQTT();
|
||||
#endif
|
||||
#ifdef ZgatewayIR
|
||||
IRtoMQTT();
|
||||
#endif
|
||||
#ifdef Zgateway2G
|
||||
if(_2GtoMQTT()){
|
||||
trc(F("2GtoMQTT OK"));
|
||||
}
|
||||
#endif
|
||||
#ifdef ZgatewayRFM69
|
||||
if(RFM69toMQTT())
|
||||
trc(F("RFM69toMQTT OK"));
|
||||
#endif
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
stateMeasures();
|
||||
#endif
|
||||
#ifdef ZactuatorFASTLED
|
||||
FASTLEDLoop();
|
||||
#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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
@@ -1065,9 +1103,7 @@ void stateMeasures(){
|
||||
|
||||
SYSdata["modules"] = modules;
|
||||
trc(SYSdata);
|
||||
char JSONmessageBuffer[JSON_MSG_BUFFER];
|
||||
SYSdata.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
||||
pub(subjectSYStoMQTT,JSONmessageBuffer);
|
||||
pub(subjectSYStoMQTT,SYSdata);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
+2
-1
@@ -89,7 +89,7 @@ monitor_speed = 115200
|
||||
|
||||
[com]
|
||||
esp8266_platform = espressif8266@1.8.0
|
||||
esp32_platform = espressif32@1.9.0
|
||||
esp32_platform = espressif32@1.11.0
|
||||
atmelavr_platform = atmelavr@1.13.0
|
||||
|
||||
[com-esp]
|
||||
@@ -102,6 +102,7 @@ build_flags =
|
||||
'-DsimpleReceiving=true'
|
||||
'-DZmqttDiscovery="HADiscovery"'
|
||||
'-DTRACE=1'
|
||||
;'-DCORE_DEBUG_LEVEL=4'
|
||||
|
||||
[com-arduino]
|
||||
lib_deps =
|
||||
|
||||
+7
-1
@@ -123,7 +123,13 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
|
||||
|
||||
// VCC ------------D|-----------/\/\/\/\ ----------------- Arduino PIN
|
||||
// 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-----------------------------*/
|
||||
#ifdef ZgatewaySRFB
|
||||
|
||||
Reference in new issue
Block a user