From 90c21ef12538ad55c59a7bbe02c3b2a7ff303906 Mon Sep 17 00:00:00 2001 From: livello Date: Fri, 29 Jun 2018 10:35:32 +0300 Subject: [PATCH] wifimanager esp8266, RESTART_LAN_ON_MQTT_ERRORS option, option RESET_PIN for restart on lan init error --- README.md | 4 ++++ lighthub/main.cpp | 59 ++++++++++++++++++++++++++++------------------ lighthub/main.h | 7 ++++++ lighthub/options.h | 2 ++ platformio.ini | 1 + 5 files changed, 50 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 6d46dca..d7b3606 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,9 @@ platformio device monitor -b 115200 * LAN_INIT_DELAY=2000 // set lan init delay for Wiznet ethernet shield * ESP_WIFI_AP=MYAP // esp wifi access point name * ESP_WIFI_PWD=MYPWD // esp wifi access point password +* WIFI_MANAGER_DISABLE //Disable wifi manager for esp8266 * DHT_DISABLE //disable DHT Input support +* RESTART_LAN_ON_MQTT_ERRORS //reinit LAN if many mqtt errors occured @@ -172,5 +174,7 @@ platformio device monitor -b 115200 * Default MQTT topic to publish device status: /myhome/s_out * Default Alarm output topic /alarm * DHT support enabled +* Wifi manager for esp8266 enabled +* RESTART_LAN_ON_MQTT_ERRORS disabled If you've using Arduino IDE to compile & flash firmware, it will use Default options above and you will not able to configure additional compilers options except edit "options.h" file diff --git a/lighthub/main.cpp b/lighthub/main.cpp index 4167221..4b050ec 100644 --- a/lighthub/main.cpp +++ b/lighthub/main.cpp @@ -181,7 +181,6 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) { } //valid item } - void printIPAddress(IPAddress ipAddress) { for (byte thisByte = 0; thisByte < 4; thisByte++) { Serial.print(ipAddress[thisByte], DEC); @@ -377,16 +376,19 @@ void ip_ready_config_loaded_connecting_to_broker() { Serial.print(mqttClient.state()); Serial.println(F(" try again in 5 seconds")); nextLanCheckTime = millis() + 5000; -#ifdef RESET_PIN +#ifdef RESTART_LAN_ON_MQTT_ERRORS mqttErrorRate++; if(mqttErrorRate>50){ Serial.print(F("Too many MQTT connection errors. Restart LAN")); mqttErrorRate=0; +#ifdef RESET_PIN resetHard(); +#endif lanStatus=INITIAL_STATE; return; } #endif + lanStatus = RECONNECT;//12; } } @@ -394,6 +396,7 @@ void ip_ready_config_loaded_connecting_to_broker() { void onInitialStateInitLAN() { #ifdef __ESP__ +#ifdef WIFI_MANAGER_DISABLE if(!wifiInitialized) { WiFi.mode(WIFI_STA); Serial.print(F("WIFI AP/Password:")); @@ -404,16 +407,16 @@ void onInitialStateInitLAN() { WiFi.begin(QUOTE(ESP_WIFI_AP), QUOTE(ESP_WIFI_PWD)); wifiInitialized = true; } - +#endif if (WiFi.status() == WL_CONNECTED) { Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); lanStatus=HAVE_IP_ADDRESS;//1; } +#endif - -#else +#if not defined(__ESP__) IPAddress ip, dns, gw, mask; int res = 1; Serial.println(F("Starting lan")); @@ -464,12 +467,12 @@ void (*softRebootFunc)(void) = 0; void resetHard() { #ifdef RESET_PIN - Serial.print(F("Reset Wiznet5100 shield with digital pin ")); + Serial.print(F("Reset Arduino with digital pin ")); Serial.println(QUOTE(RESET_PIN)); - delay(50); + delay(500); pinMode(RESET_PIN, OUTPUT); digitalWrite(RESET_PIN,LOW); - delay(25); + delay(500); digitalWrite(RESET_PIN,HIGH); delay(500); #endif @@ -1006,7 +1009,7 @@ void postTransmission() { } void setup_main() { - setupCmdArduino(); + setupCmdArduino(); printFirmwareVersionAndBuildOptions(); #ifdef SD_CARD_INSERTED @@ -1017,17 +1020,17 @@ void setup_main() { #ifdef _modbus #ifdef CONTROLLINO -//set PORTJ pin 5,6 direction (RE,DE) -DDRJ |= B01100000; -//set RE,DE on LOW -PORTJ &= B10011111; + //set PORTJ pin 5,6 direction (RE,DE) + DDRJ |= B01100000; + //set RE,DE on LOW + PORTJ &= B10011111; #else -pinMode(TXEnablePin, OUTPUT); + pinMode(TXEnablePin, OUTPUT); #endif - modbusSerial.begin(MODBUS_SERIAL_BAUD); - node.idle(&modbusIdle); - node.preTransmission(preTransmission); - node.postTransmission(postTransmission); + modbusSerial.begin(MODBUS_SERIAL_BAUD); + node.idle(&modbusIdle); + node.preTransmission(preTransmission); + node.postTransmission(postTransmission); #endif delay(20); @@ -1042,10 +1045,16 @@ pinMode(TXEnablePin, OUTPUT); #ifdef _artnet ArtnetSetup(); #endif -/* -SPI.begin(); -while (Ethernet.maintain() == NO_LINK && millis()<3000UL) {delay(500);Serial.print(F("."));} -*/ + +#ifndef WIFI_MANAGER_DISABLE + WiFiManager wifiManager; +#if defined(ESP_WIFI_AP) and defined(ESP_WIFI_PWD) + wifiManager.autoConnect(QUOTE(ESP_WIFI_AP), QUOTE(ESP_WIFI_PWD)); +#else + wifiManager.autoConnect(); +#endif +#endif + delay(LAN_INIT_DELAY);//for LAN-shield initializing //TODO: checkForRemoteSketchUpdate(); } @@ -1120,7 +1129,11 @@ void printFirmwareVersionAndBuildOptions() { Serial.println("(-)HARDRESET, using soft"); #endif - +#ifdef RESTART_LAN_ON_MQTT_ERRORS + Serial.println(F("(+)RESTART_LAN_ON_MQTT_ERRORS")); +#else + Serial.println("(-)RESTART_LAN_ON_MQTT_ERRORS"); +#endif } void setupMacAddress() { diff --git a/lighthub/main.h b/lighthub/main.h index 6a93980..67088d1 100644 --- a/lighthub/main.h +++ b/lighthub/main.h @@ -67,6 +67,13 @@ #include //this needs to be first, or it all crashes and burns... #include #include + +#ifndef WIFI_MANAGER_DISABLE +#include +#include +#include +#endif + #endif #ifdef _owire diff --git a/lighthub/options.h b/lighthub/options.h index 7b8a2b8..0509d12 100644 --- a/lighthub/options.h +++ b/lighthub/options.h @@ -129,6 +129,7 @@ #undef _artnet #endif +#ifdef WIFI_MANAGER_DISABLE #ifndef ESP_WIFI_AP #define ESP_WIFI_AP mywifiap #endif @@ -136,5 +137,6 @@ #ifndef ESP_WIFI_PWD #define ESP_WIFI_PWD mywifipass #endif +#endif #define DHT_POLL_DELAY_DEFAULT 15000 \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 258b490..8a32709 100644 --- a/platformio.ini +++ b/platformio.ini @@ -92,6 +92,7 @@ lib_deps = Adafruit Unified Sensor DHT sensor library for ESPx DHT sensor library + WifiManager [env:megaatmega2560-5500]