From d4b7e3d246285bcec54d9d3b1a250c3303151ab6 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Sun, 22 Nov 2020 19:40:46 +0100 Subject: [PATCH] Update networking --- src/hasp_ethernet_esp32.cpp | 3 + src/hasp_network.cpp | 79 ++++++++++++++++++++ src/hasp_network.h | 22 ++++++ src/hasp_wifi.cpp | 15 +--- src/main.cpp | 25 +++---- user_setups/esp32/ttgo-esp32-poe_ili9341.ini | 12 +-- 6 files changed, 123 insertions(+), 33 deletions(-) create mode 100644 src/hasp_network.cpp create mode 100644 src/hasp_network.h diff --git a/src/hasp_ethernet_esp32.cpp b/src/hasp_ethernet_esp32.cpp index fbfa33cc..2fb61a34 100644 --- a/src/hasp_ethernet_esp32.cpp +++ b/src/hasp_ethernet_esp32.cpp @@ -8,6 +8,7 @@ #include "hasp_conf.h" #include "hasp_hal.h" #include "hasp_debug.h" +#include "hasp_network.h" #if HASP_USE_ETHERNET > 0 @@ -34,10 +35,12 @@ void EthernetEvent(WiFiEvent_t event) } Log.notice(TAG_ETH, F("LINK_SPEED %d Mbps"), ETH.linkSpeed()); eth_connected = true; + networkStart();// Start network services break; case SYSTEM_EVENT_ETH_DISCONNECTED: Log.notice(TAG_ETH, F("Disconnected")); eth_connected = false; + networkStop(); // Stop network services break; case SYSTEM_EVENT_ETH_STOP: Log.notice(TAG_ETH, F("Stopped")); diff --git a/src/hasp_network.cpp b/src/hasp_network.cpp new file mode 100644 index 00000000..785c19c1 --- /dev/null +++ b/src/hasp_network.cpp @@ -0,0 +1,79 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#include +#include "ArduinoLog.h" + +#include "hasp_conf.h" +#include "hasp_hal.h" +#include "hasp_debug.h" +#include "hasp.h" + +#if HASP_USE_ETHERNET > 0 || HASP_USE_WIFI > 0 + +void networkStart(void) +{ + haspProgressVal(255); // hide + + haspReconnect(); + debugStartSyslog(); + // mqttStart(); + httpStart(); + mdnsStart(); +} + +void networkStop(void) +{ + haspProgressMsg(F("Network Disconnected")); + + debugStopSyslog(); + mqttStop(); + httpStop(); + mdnsStop(); +} + +void networkSetup() +{ +#if HASP_USE_ETHERNET > 0 + ethernetSetup(); +#endif + +#if HASP_USE_WIFI > 0 + wifiSetup(); +#endif +} + +void networkLoop(void) +{ +#if HASP_USE_ETHERNET > 0 + ethernetSetup(); +#endif + +#if HASP_USE_WIFI > 0 + wifiSetup(); +#endif +} + +bool networkEvery5Seconds(void) +{ +#if HASP_USE_ETHERNET > 0 + ethernetEvery5Seconds(); +#endif + +#if HASP_USE_WIFI > 0 + wifiEvery5Seconds(); +#endif +} + +bool networkEverySecond(void) +{ +#if HASP_USE_ETHERNET > 0 + // ethernetEverySecond(); +#endif + +#if HASP_USE_WIFI > 0 + return wifiEverySecond(); +#endif +} + +#endif \ No newline at end of file diff --git a/src/hasp_network.h b/src/hasp_network.h new file mode 100644 index 00000000..1b8994e0 --- /dev/null +++ b/src/hasp_network.h @@ -0,0 +1,22 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_NETWORK_H +#define HASP_NETWORK_H + +/* ===== Default Event Processors ===== */ +void networkSetup(); +void networkLoop(void); +void networkEvery5Seconds(void); +void networkEverySecond(void); +void networkStart(void); +void networkStop(void); + +/* ===== Special Event Processors ===== */ + +/* ===== Getter and Setter Functions ===== */ + +/* ===== Read/Write Configuration ===== */ + + +#endif \ No newline at end of file diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index 79d8ddac..a0bb51ac 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -64,13 +64,7 @@ static void wifiConnected(IPAddress ipaddress) #endif Log.verbose(TAG_WIFI, F("Connected = %s"), WiFi.status() == WL_CONNECTED ? PSTR("yes") : PSTR("no")); - haspProgressVal(255); - - haspReconnect(); - debugStartSyslog(); - //mqttStart(); - httpStart(); - mdnsStart(); + networkEvent(true); } static void wifiDisconnected(const char * ssid, uint8_t reason) @@ -78,12 +72,7 @@ static void wifiDisconnected(const char * ssid, uint8_t reason) wifiReconnectCounter++; haspProgressVal(wifiReconnectCounter * 3); - haspProgressMsg(F("Wifi Disconnected")); - - debugStopSyslog(); - mqttStop(); - httpStop(); - mdnsStop(); + networkEvent(false); if(wifiReconnectCounter > 33) { Log.error(TAG_WIFI, F("Retries exceed %u: Rebooting..."), wifiReconnectCounter); diff --git a/src/main.cpp b/src/main.cpp index 6fc2e37a..4ddae1c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include "hasp_gui.h" #include "hasp_oobe.h" #include "hasp_dispatch.h" +#include "hasp_network.h" #include "hasp.h" bool isConnected; @@ -27,17 +28,17 @@ void setup() #endif // #if HASP_USE_SPIFFS > 0 || HASP_USE_LITTLEFS > 0 -// filesystemSetup(); // Done in configSetup() +// filesystemSetup(); // FS mount is done in configSetup() // #endif -#if HASP_USE_SDCARD > 0 - sdcardSetup(); -#endif +// #if HASP_USE_SDCARD > 0 +// sdcardSetup(); +// #endif /**************************** * Read & Apply User Configuration ***************************/ - configSetup(); + configSetup(); // also runs debugPreSetup(), debugSetup() and debugStart() dispatchSetup(); @@ -45,19 +46,15 @@ void setup() * Apply User Configuration ***************************/ -#if HASP_USE_ETHERNET > 0 - ethernetSetup(); -#endif - #if HASP_USE_MQTT > 0 mqttSetup(); // Load Hostname before starting WiFi #endif -#if HASP_USE_WIFI > 0 - wifiSetup(); +#if HASP_USE_WIFI > 0 || HASP_USE_ETHERNET > 0 + networkSetup(); #endif - debugSetup(); + // debugSetup(); guiSetup(); if(!oobeSetup()) { @@ -155,11 +152,11 @@ void loop() /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { /* Runs Every Second */ + guiEverySecond(); // sleep timer + debugEverySecond(); // statusupdate #if HASP_USE_OTA > 0 otaEverySecond(); // progressbar #endif - guiEverySecond(); // sleep timer - debugEverySecond(); // statusupdate /* Runs Every 5 Seconds */ if(mainLoopCounter == 0 || mainLoopCounter == 5) { diff --git a/user_setups/esp32/ttgo-esp32-poe_ili9341.ini b/user_setups/esp32/ttgo-esp32-poe_ili9341.ini index f2921d4e..4a5ecd38 100644 --- a/user_setups/esp32/ttgo-esp32-poe_ili9341.ini +++ b/user_setups/esp32/ttgo-esp32-poe_ili9341.ini @@ -6,16 +6,16 @@ ; - xpt2606 touch controller ; ;***************************************************; -[env:ttgo_esp32-lolintft24] +[env:ttgo_esp32_poe-lolintft24] platform = espressif32@^2.0.0 board = esp32dev -upload_protocol = espota ; Use ArduinoOTA after flashing over serial -upload_port = 10.4.70.37 ; 10.4.0.198 ; IP of the ESP -upload_flags = - --port=3232 +;upload_protocol = espota ; Use ArduinoOTA after flashing over serial +;upload_port = 10.4.70.37 ; 10.4.0.198 ; IP of the ESP +;upload_flags = +; --port=3232 ;upload_port = COM9 ; To change the port, use platform_override.ini -monitor_port = COM9 ; To change the port, use platform_override.ini +;monitor_port = COM9 ; To change the port, use platform_override.ini monitor_filters = esp32_exception_decoder board_build.partitions = user_setups/esp32_partition_app1300k_spiffs1216k.csv