From 85ec9dbb44d9c9fee4af6debc60e1db80400c523 Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sun, 6 Jan 2019 17:27:10 +0100 Subject: [PATCH 1/6] Initial version with ethernet support --- Basecamp.cpp | 2 +- WifiControl.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++- WifiControl.hpp | 1 + 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/Basecamp.cpp b/Basecamp.cpp index 7c6b0ec..20a5682 100644 --- a/Basecamp.cpp +++ b/Basecamp.cpp @@ -336,7 +336,7 @@ void Basecamp::connectToMqtt(TimerHandle_t xTimer) { AsyncMqttClient *mqtt = (AsyncMqttClient *) pvTimerGetTimerID(xTimer); - if (WiFi.status() == WL_CONNECTED) { + if (WifiControl::isConnected()) { Serial.println("Trying to connect ..."); mqtt->connect(); // has no effect if already connected ( if (_connected) return;) } diff --git a/WifiControl.cpp b/WifiControl.cpp index 92c7691..80d9c81 100644 --- a/WifiControl.cpp +++ b/WifiControl.cpp @@ -5,17 +5,21 @@ */ #include "WifiControl.hpp" +#ifdef BASECAMP_WIRED_NETWORK +#include +#endif namespace { // Minumum access point secret length to be generated (8 is min for ESP32) const constexpr unsigned minApSecretLength = 8; + static bool eth_connected = false; } void WifiControl::begin(String essid, String password, String configured, String hostname, String apSecret) { +#ifndef BASECAMP_WIRED_NETWORK DEBUG_PRINTLN("Connecting to Wifi"); - String _wifiConfigured = std::move(configured); _wifiEssid = std::move(essid); _wifiPassword = std::move(password); @@ -49,6 +53,30 @@ void WifiControl::begin(String essid, String password, String configured, WiFi.softAP(_wifiAPName.c_str()); } } +#else + DEBUG_PRINTLN("Connecting to Ethernet"); + operationMode_ = Mode::client; + WiFi.onEvent(WiFiEvent); + ETH.begin() ; + ETH.setHostname(hostname.c_str()); + DEBUG_PRINTLN ("Ethernet initialized") ; + DEBUG_PRINTLN ("Waiting for connection") ; + while (!eth_connected) { + DEBUG_PRINT (".") ; + delay(100) ; + } +#endif + +} + + +bool WifiControl::isConnected() +{ +#ifndef BASECAMP_WIRED_NETWORK + return WiFi.isConnected() ; +#else + return eth_connected ; +#endif } WifiControl::Mode WifiControl::getOperationMode() const @@ -61,7 +89,11 @@ int WifiControl::status() { } IPAddress WifiControl::getIP() { +#ifndef BASECAMP_WIRED_NETWORK return WiFi.localIP(); +#else + return ETH.localIP() ; +#endif } IPAddress WifiControl::getSoftAPIP() { return WiFi.softAPIP(); @@ -83,6 +115,7 @@ void WifiControl::WiFiEvent(WiFiEvent_t event) // In case somebody wants to know this.. DEBUG_PRINTF("[WiFi-event] event. Bootcounter is %d\n", bootCounter); DEBUG_PRINTF("[WiFi-event] event: %d\n", event); +#ifndef BASECAMP_WIRED_NETWORK switch(event) { case SYSTEM_EVENT_STA_GOT_IP: DEBUG_PRINT("Wifi IP address: "); @@ -97,6 +130,41 @@ void WifiControl::WiFiEvent(WiFiEvent_t event) // INFO: Default = do nothing break; } +#else + switch (event) { + case SYSTEM_EVENT_ETH_START: + Serial.println("ETH Started"); + //set eth hostname here + ETH.setHostname("esp32-ethernet"); + break; + case SYSTEM_EVENT_ETH_CONNECTED: + Serial.println("ETH Connected"); + break; + case SYSTEM_EVENT_ETH_GOT_IP: + Serial.print("ETH MAC: "); + Serial.print(ETH.macAddress()); + Serial.print(", IPv4: "); + Serial.print(ETH.localIP()); + if (ETH.fullDuplex()) { + Serial.print(", FULL_DUPLEX"); + } + Serial.print(", "); + Serial.print(ETH.linkSpeed()); + Serial.println("Mbps"); + eth_connected = true; + break; + case SYSTEM_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + break; + case SYSTEM_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + break; + default: + break; + } +#endif } namespace { @@ -120,16 +188,25 @@ namespace { // See https://github.com/espressif/esp-idf/blob/master/components/esp32/include/esp_system.h String WifiControl::getHardwareMacAddress(const String& delimiter) { +#ifndef BASECAMP_WIRED_NETWORK uint8_t rawMac[6]; esp_efuse_mac_get_default(rawMac); return format6Bytes(rawMac, delimiter); +#else + return ETH.macAddress() ; +#endif } String WifiControl::getSoftwareMacAddress(const String& delimiter) { +#ifndef BASECAMP_WIRED_NETWORK uint8_t rawMac[6]; WiFi.macAddress(rawMac); return format6Bytes(rawMac, delimiter); +#else + return ETH.macAddress() ; +#endif + } unsigned WifiControl::getMinimumSecretLength() const diff --git a/WifiControl.hpp b/WifiControl.hpp index 3b54cd7..f7b6b50 100644 --- a/WifiControl.hpp +++ b/WifiControl.hpp @@ -25,6 +25,7 @@ class WifiControl { WifiControl(){}; bool connect(); bool disconnect(); + static bool isConnected() ; Mode getOperationMode() const; From b94360f1cd779f426bc745875ed8a66f87ac072d Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sun, 6 Jan 2019 20:19:24 +0100 Subject: [PATCH 2/6] replaced Serial.print with DEBUG_PRINT --- WifiControl.cpp | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/WifiControl.cpp b/WifiControl.cpp index 80d9c81..02d6cdf 100644 --- a/WifiControl.cpp +++ b/WifiControl.cpp @@ -133,32 +133,30 @@ void WifiControl::WiFiEvent(WiFiEvent_t event) #else switch (event) { case SYSTEM_EVENT_ETH_START: - Serial.println("ETH Started"); - //set eth hostname here - ETH.setHostname("esp32-ethernet"); + DEBUG_PRINTLN("ETH Started"); break; case SYSTEM_EVENT_ETH_CONNECTED: - Serial.println("ETH Connected"); + DEBUG_PRINTLN("ETH Connected"); break; case SYSTEM_EVENT_ETH_GOT_IP: - Serial.print("ETH MAC: "); - Serial.print(ETH.macAddress()); - Serial.print(", IPv4: "); - Serial.print(ETH.localIP()); + DEBUG_PRINT("ETH MAC: "); + DEBUG_PRINT(ETH.macAddress()); + DEBUG_PRINT(", IPv4: "); + DEBUG_PRINT(ETH.localIP()); if (ETH.fullDuplex()) { - Serial.print(", FULL_DUPLEX"); + DEBUG_PRINT(", FULL_DUPLEX"); } - Serial.print(", "); - Serial.print(ETH.linkSpeed()); - Serial.println("Mbps"); + DEBUG_PRINT(", "); + DEBUG_PRINT(ETH.linkSpeed()); + DEBUG_PRINTLN("Mbps"); eth_connected = true; break; case SYSTEM_EVENT_ETH_DISCONNECTED: - Serial.println("ETH Disconnected"); + DEBUG_PRINTLN("ETH Disconnected"); eth_connected = false; break; case SYSTEM_EVENT_ETH_STOP: - Serial.println("ETH Stopped"); + DEBUG_PRINTLN("ETH Stopped"); eth_connected = false; break; default: From 38e6ebdadcdb3357e3d1a01a68522a3d598f2ce0 Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sun, 6 Jan 2019 20:24:36 +0100 Subject: [PATCH 3/6] removed wifi config from web UI in case wired network is used --- Basecamp.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Basecamp.cpp b/Basecamp.cpp index 20a5682..f49d110 100644 --- a/Basecamp.cpp +++ b/Basecamp.cpp @@ -249,6 +249,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.addInterfaceElement("DeviceName", "input", "Device name","#configform" , "DeviceName"); +#ifdef BASECAMP_WIRED_NETWORK // Add an input field for the WIFI data and link it to the corresponding configuration data web.addInterfaceElement("WifiEssid", "input", "WIFI SSID:","#configform" , "WifiEssid"); web.addInterfaceElement("WifiPassword", "input", "WIFI Password:", "#configform", "WifiPassword"); @@ -256,7 +257,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.addInterfaceElement("WifiConfigured", "input", "", "#configform", "WifiConfigured"); web.setInterfaceElementAttribute("WifiConfigured", "type", "hidden"); web.setInterfaceElementAttribute("WifiConfigured", "value", "true"); - +#endif // Add input fields for MQTT configurations if it hasn't been disabled if (!configuration.get(ConfigurationKey::mqttActive).equalsIgnoreCase("false")) { web.addInterfaceElement("MQTTHost", "input", "MQTT Host:","#configform" , "MQTTHost"); From c430486b5f88a31eb8d7f65cb7b9c90db457f999 Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sun, 6 Jan 2019 20:29:04 +0100 Subject: [PATCH 4/6] reordered code in #ifdef BASECAMP_WIRED from negative to positive --- WifiControl.cpp | 82 ++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/WifiControl.cpp b/WifiControl.cpp index 02d6cdf..08a64b9 100644 --- a/WifiControl.cpp +++ b/WifiControl.cpp @@ -18,7 +18,19 @@ namespace { void WifiControl::begin(String essid, String password, String configured, String hostname, String apSecret) { -#ifndef BASECAMP_WIRED_NETWORK +#ifdef BASECAMP_WIRED_NETWORK + DEBUG_PRINTLN("Connecting to Ethernet"); + operationMode_ = Mode::client; + WiFi.onEvent(WiFiEvent); + ETH.begin() ; + ETH.setHostname(hostname.c_str()); + DEBUG_PRINTLN ("Ethernet initialized") ; + DEBUG_PRINTLN ("Waiting for connection") ; + while (!eth_connected) { + DEBUG_PRINT (".") ; + delay(100) ; + } +#else DEBUG_PRINTLN("Connecting to Wifi"); String _wifiConfigured = std::move(configured); _wifiEssid = std::move(essid); @@ -53,18 +65,6 @@ void WifiControl::begin(String essid, String password, String configured, WiFi.softAP(_wifiAPName.c_str()); } } -#else - DEBUG_PRINTLN("Connecting to Ethernet"); - operationMode_ = Mode::client; - WiFi.onEvent(WiFiEvent); - ETH.begin() ; - ETH.setHostname(hostname.c_str()); - DEBUG_PRINTLN ("Ethernet initialized") ; - DEBUG_PRINTLN ("Waiting for connection") ; - while (!eth_connected) { - DEBUG_PRINT (".") ; - delay(100) ; - } #endif } @@ -72,10 +72,10 @@ void WifiControl::begin(String essid, String password, String configured, bool WifiControl::isConnected() { -#ifndef BASECAMP_WIRED_NETWORK - return WiFi.isConnected() ; -#else +#ifdef BASECAMP_WIRED_NETWORK return eth_connected ; +#else + return WiFi.isConnected() ; #endif } @@ -89,10 +89,10 @@ int WifiControl::status() { } IPAddress WifiControl::getIP() { -#ifndef BASECAMP_WIRED_NETWORK - return WiFi.localIP(); -#else +#ifdef BASECAMP_WIRED_NETWORK return ETH.localIP() ; +#else + return WiFi.localIP(); #endif } IPAddress WifiControl::getSoftAPIP() { @@ -115,22 +115,7 @@ void WifiControl::WiFiEvent(WiFiEvent_t event) // In case somebody wants to know this.. DEBUG_PRINTF("[WiFi-event] event. Bootcounter is %d\n", bootCounter); DEBUG_PRINTF("[WiFi-event] event: %d\n", event); -#ifndef BASECAMP_WIRED_NETWORK - switch(event) { - case SYSTEM_EVENT_STA_GOT_IP: - DEBUG_PRINT("Wifi IP address: "); - DEBUG_PRINTLN(WiFi.localIP()); - preferences.putUInt("bootcounter", 0); - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - DEBUG_PRINTLN("WiFi lost connection"); - WiFi.reconnect(); - break; - default: - // INFO: Default = do nothing - break; - } -#else +#ifdef BASECAMP_WIRED_NETWORK switch (event) { case SYSTEM_EVENT_ETH_START: DEBUG_PRINTLN("ETH Started"); @@ -162,6 +147,21 @@ void WifiControl::WiFiEvent(WiFiEvent_t event) default: break; } +#else + switch(event) { + case SYSTEM_EVENT_STA_GOT_IP: + DEBUG_PRINT("Wifi IP address: "); + DEBUG_PRINTLN(WiFi.localIP()); + preferences.putUInt("bootcounter", 0); + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + DEBUG_PRINTLN("WiFi lost connection"); + WiFi.reconnect(); + break; + default: + // INFO: Default = do nothing + break; + } #endif } @@ -186,23 +186,23 @@ namespace { // See https://github.com/espressif/esp-idf/blob/master/components/esp32/include/esp_system.h String WifiControl::getHardwareMacAddress(const String& delimiter) { -#ifndef BASECAMP_WIRED_NETWORK +#ifdef BASECAMP_WIRED_NETWORK + return ETH.macAddress() ; +#else uint8_t rawMac[6]; esp_efuse_mac_get_default(rawMac); return format6Bytes(rawMac, delimiter); -#else - return ETH.macAddress() ; #endif } String WifiControl::getSoftwareMacAddress(const String& delimiter) { -#ifndef BASECAMP_WIRED_NETWORK +#ifdef BASECAMP_WIRED_NETWORK + return ETH.macAddress() ; +#else uint8_t rawMac[6]; WiFi.macAddress(rawMac); return format6Bytes(rawMac, delimiter); -#else - return ETH.macAddress() ; #endif } From 9a2c5f0bdb4c9a17296c6cf4bca88e512f0a69cd Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sat, 6 Jul 2019 11:27:46 +0200 Subject: [PATCH 5/6] fixed not including Wifi config on wireless network --- Basecamp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Basecamp.cpp b/Basecamp.cpp index f49d110..59bdee2 100644 --- a/Basecamp.cpp +++ b/Basecamp.cpp @@ -249,7 +249,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.addInterfaceElement("DeviceName", "input", "Device name","#configform" , "DeviceName"); -#ifdef BASECAMP_WIRED_NETWORK +#ifndef BASECAMP_WIRED_NETWORK // Add an input field for the WIFI data and link it to the corresponding configuration data web.addInterfaceElement("WifiEssid", "input", "WIFI SSID:","#configform" , "WifiEssid"); web.addInterfaceElement("WifiPassword", "input", "WIFI Password:", "#configform", "WifiPassword"); From 66b95ca2b9f4dea7512f4e82b067e86766615d4e Mon Sep 17 00:00:00 2001 From: Dominik Bernhardt Date: Sun, 20 Oct 2019 20:14:37 +0200 Subject: [PATCH 6/6] Fix init order compiler warning --- Configuration.hpp | 4 ++-- WebServer.cpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Configuration.hpp b/Configuration.hpp index aa7ab1d..e325e58 100644 --- a/Configuration.hpp +++ b/Configuration.hpp @@ -139,11 +139,11 @@ class Configuration { private: static void CheckConfigStatus(void *); + // Set to true if configuration is memory-only + bool _memOnlyConfig; String _jsonFile; bool _configurationTainted = false; String noResult_ = {}; - // Set to true if configuration is memory-only - bool _memOnlyConfig; }; #endif diff --git a/WebServer.cpp b/WebServer.cpp index bdcae68..ef8510b 100644 --- a/WebServer.cpp +++ b/WebServer.cpp @@ -15,8 +15,7 @@ namespace { } WebServer::WebServer() - : events("/events") - , server(80) + : server(80), events("/events") { server.addHandler(&events); #ifdef BASECAMP_USEDNS