From 3317f3d2289fd6ca1cb08994cc80c643c75f4b8a Mon Sep 17 00:00:00 2001 From: geeks-r-us Date: Fri, 1 Jun 2018 02:05:58 +0200 Subject: [PATCH 1/3] Added basic support for wired connections using eth phys --- Basecamp.cpp | 43 ++++++++++++++++++++++++++++--- Basecamp.hpp | 9 +++++++ Configuration.hpp | 4 +++ EthControl.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++ EthControl.hpp | 39 ++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 3 deletions(-) mode change 100644 => 100755 Basecamp.cpp mode change 100644 => 100755 Configuration.hpp create mode 100755 EthControl.cpp create mode 100755 EthControl.hpp diff --git a/Basecamp.cpp b/Basecamp.cpp old mode 100644 new mode 100755 index 77435f2..449ffdc --- a/Basecamp.cpp +++ b/Basecamp.cpp @@ -108,7 +108,6 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) checkResetReason(); #ifndef BASECAMP_NOWIFI - // If there is no access point secret set yet, generate one and save it. // It will survive the default config reset. if (!configuration.isKeySet(ConfigurationKey::accessPointSecret) || @@ -140,6 +139,23 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) // Get WiFi MAC mac = wifi.getSoftwareMacAddress(":"); #endif + +#ifndef BASECAMP_NOETH + if(!configuration.isKeySet(ConfigurationKey::ethConfigured)) + if(configuration.get(ConfigurationKey::ethConfigured) != "false"){ + const auto ðIP = configuration.get("EthIP"); + const auto ðGateway = configuration.get("EthGateway"); + const auto ðSubnetMask = configuration.get("EthSubnetMask"); + const auto ðDNS1 = configuration.get("EthDNS1"); + const auto ðDNS2 = configuration.get("EthDNS2"); + + eth.config(ethIP, ethGateway, ethSubnetMask, ethDNS1, ethDNS2); + } + eth.begin(hostname); + + macEth = eth.getMac(); +#endif + #ifndef BASECAMP_NOMQTT // Check if MQTT has been disabled by the user if (configuration.get("MQTTActive") != "false") { @@ -214,7 +230,8 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.setInterfaceElementAttribute("configform", "action", "saveConfig"); web.addInterfaceElement("DeviceName", "input", "Device name","#configform" , "DeviceName"); - + + #ifndef BASECAMP_NOWIFI // 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"); @@ -222,6 +239,19 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.addInterfaceElement("WifiConfigured", "input", "", "#configform", "WifiConfigured"); web.setInterfaceElementAttribute("WifiConfigured", "type", "hidden"); web.setInterfaceElementAttribute("WifiConfigured", "value", "true"); + #endif + + #ifndef BASECAMP_NOETH + // Add an input field for the eth data and link it to the corresponding configuration data + web.addInterfaceElement("EthIP", "input", "ETH IP:","#configform" , "EthIP"); + web.addInterfaceElement("EthGateway", "input", "ETH Gateway:", "#configform", "EthGateway"); + web.addInterfaceElement("EthSubnet", "input", "ETH Subnet:", "#configform", "EthSubnet"); + web.addInterfaceElement("EthDNS1", "input", "ETH DNS 1:", "#configform", "EthDNS1"); + web.addInterfaceElement("EthDNS2", "input", "ETH DNS 2:", "#configform", "EthDNS2"); + web.addInterfaceElement("EthConfigured", "input", "", "#configform", "EthConfigured"); + 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("MQTTActive") != "false") { @@ -241,9 +271,12 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.setInterfaceElementAttribute("saveform", "onclick", "collectConfiguration()"); // Show the devices MAC in the Webinterface - String infotext2 = "This device has the MAC-Address: " + mac; + String infotext2 = "This device has the WiFi MAC-Address: " + mac; web.addInterfaceElement("infotext2", "p", infotext2,"#wrapper"); + String infotext3 = "This device has the ETH MAC-Address: " + macEth; + web.addInterfaceElement("infotext3", "p", infotext3,"#wrapper"); + web.addInterfaceElement("footer", "footer", "Powered by ", "body"); web.addInterfaceElement("footerlink", "a", "Basecamp", "footer"); web.setInterfaceElementAttribute("footerlink", "href", "https://github.com/merlinschumacher/Basecamp"); @@ -449,6 +482,10 @@ String Basecamp::showSystemInfo() { info << "MAC-Address: " << mac.c_str(); info << ", Hardware MAC: " << wifi.getHardwareMacAddress(":").c_str() << std::endl; + #ifndef BASECAMP_NOETH + info << "ETH MAC-Address:" << macEth.c_str() << std::endl; + #endif + if (configuration.isKeySet(ConfigurationKey::accessPointSecret)) { info << "*******************************************" << std::endl; info << "* ACCESS POINT PASSWORD: "; diff --git a/Basecamp.hpp b/Basecamp.hpp index 56fbd7b..df5a831 100644 --- a/Basecamp.hpp +++ b/Basecamp.hpp @@ -15,6 +15,10 @@ #include "WifiControl.hpp" #endif +#ifndef BASECAMP_NOETH +#include "EthControl.hpp" +#endif + #ifndef BASECAMP_NOWEB #ifdef BASECAMP_USEDNS #include @@ -82,6 +86,11 @@ class Basecamp WifiControl wifi; #endif +#ifndef BASECAMP_NOETH + String macEth; + EthControl eth; +#endif + #ifndef BASECAMP_NOMQTT AsyncMqttClient mqtt; static void MqttHandling(void *); diff --git a/Configuration.hpp b/Configuration.hpp old mode 100644 new mode 100755 index 4224f63..01f83c8 --- a/Configuration.hpp +++ b/Configuration.hpp @@ -18,6 +18,7 @@ // TODO: Extend with all known keys enum class ConfigurationKey { accessPointSecret, + ethConfigured, }; // TODO: Extend with all known keys @@ -30,6 +31,9 @@ static const String getKeyName(ConfigurationKey key) case ConfigurationKey::accessPointSecret: return "APSecret"; break; + case ConfigurationKey::ethConfigured: + return "EthConfigured"; + break; } return ""; } diff --git a/EthControl.cpp b/EthControl.cpp new file mode 100755 index 0000000..03f350a --- /dev/null +++ b/EthControl.cpp @@ -0,0 +1,65 @@ +#include "EthControl.hpp" + + +void EthControl::begin(String hostname) { + ETH.begin(); + ETH.setHostname(hostname.c_str()); + WiFi.onEvent(WiFiEvent); +} + +bool EthControl::config(const String& ip, const String& gateway, const String& subnet, const String& dns1, const String& dns2 ) { + IPAddress mip, mgateway, msubnet, mdns1, mdns2; + mip.fromString(ip); + mgateway.fromString(gateway); + msubnet.fromString(subnet); + mdns1.fromString(dns1); + mdns2.fromString(dns2); + + return config(mip, mgateway, msubnet, mdns1, mdns1); +} + + +bool EthControl::config(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2 ) +{ + return ETH.config(ip, gateway, subnet, dns1, dns2); +} + + +IPAddress EthControl::getIP() { + return ETH.localIP(); +} + +String EthControl::getMac() { + return ETH.macAddress(); +} + +void EthControl::WiFiEvent(WiFiEvent_t event){ + switch (event) { + case SYSTEM_EVENT_ETH_START: + DEBUG_PRINTLN("ETH Started"); + break; + case SYSTEM_EVENT_ETH_CONNECTED: + DEBUG_PRINTLN("ETH Connected"); + break; + case SYSTEM_EVENT_ETH_GOT_IP: + DEBUG_PRINT("ETH MAC: "); + DEBUG_PRINT(ETH.macAddress()); + DEBUG_PRINT(", IPv4: "); + DEBUG_PRINT(ETH.localIP()); + if (ETH.fullDuplex()) { + DEBUG_PRINT(", FULL_DUPLEX"); + } + DEBUG_PRINT(", "); + DEBUG_PRINT(ETH.linkSpeed()); + DEBUG_PRINTLN("Mbps"); + break; + case SYSTEM_EVENT_ETH_DISCONNECTED: + DEBUG_PRINTLN("ETH Disconnected"); + break; + case SYSTEM_EVENT_ETH_STOP: + DEBUG_PRINTLN("ETH Stopped"); + break; + default: + break; + } +} \ No newline at end of file diff --git a/EthControl.hpp b/EthControl.hpp new file mode 100755 index 0000000..2e39d01 --- /dev/null +++ b/EthControl.hpp @@ -0,0 +1,39 @@ +#ifndef EthControl_h +#define EthControl_h + +// CONFIG FOR WAVESHARE LAN8720 WITH POWER EN MOD BY SAUTTER +//#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN +//#define ETH_POWER_PIN 17 +//#define ETH_TYPE ETH_PHY_LAN8720 +//#define ETH_ADDR 1 +//#define ETH_MDC_PIN 23 +//#define ETH_MDIO_PIN 18 + +#include "debug.hpp" +#include +#include +#include +#include +#include +#include + + + +class EthControl { + public: + EthControl(){}; + + void begin(String hostname = "BasecampDevice"); + bool config(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2 ); + bool config(const String& ip, const String& gateway, const String& subnet, const String& dns1, const String& dns2 ); + + IPAddress getIP(); + String getMac(); + + + static void WiFiEvent(WiFiEvent_t event); + private: + +}; + +#endif //EthControl_h \ No newline at end of file From a188287cfcd205b934adc5b1da8f051e86ff3ab4 Mon Sep 17 00:00:00 2001 From: geeks-r-us Date: Fri, 12 Apr 2019 01:04:00 +0200 Subject: [PATCH 2/3] updated ArduinoJson --- external/ArduinoJson | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/ArduinoJson b/external/ArduinoJson index 7e4fcb0..ad4b13c 160000 --- a/external/ArduinoJson +++ b/external/ArduinoJson @@ -1 +1 @@ -Subproject commit 7e4fcb0868c9da02f49283ea06dadeeee498765d +Subproject commit ad4b13c8f044e67f1610fba96e8dc108ebc90cd5 From f82c0ae3574bba7faf77d15c58d014c6403f4453 Mon Sep 17 00:00:00 2001 From: geeks-r-us Date: Sat, 13 Apr 2019 00:31:09 +0200 Subject: [PATCH 3/3] ETH works also without wifi --- Basecamp.cpp | 26 +++++++++++++++++++++----- EthControl.cpp | 13 ++++++++++++- EthControl.hpp | 16 +++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Basecamp.cpp b/Basecamp.cpp index afec94c..98022d5 100755 --- a/Basecamp.cpp +++ b/Basecamp.cpp @@ -61,7 +61,11 @@ bool Basecamp::isSetupModeWifiEncrypted(){ * Returns the SSID of the setup WiFi network */ String Basecamp::getSetupModeWifiName(){ - return wifi.getAPName(); + #ifndef BASECAMP_NOWIFI + return wifi.getAPName(); + #else + return ""; + #endif } /** @@ -76,6 +80,7 @@ String Basecamp::getSetupModeWifiSecret(){ */ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) { + #ifndef BASECAMP_NOWIFI // Make sure we only accept valid passwords for ap if (fixedWiFiApEncryptionPassword.length() != 0) { if (fixedWiFiApEncryptionPassword.length() >= wifi.getMinimumSecretLength()) { @@ -84,6 +89,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) Serial.println("Error: Given fixed ap secret is too short. Refusing."); } } + #endif // Enable serial output Serial.begin(115200); @@ -141,6 +147,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) #endif #ifndef BASECAMP_NOETH + eth.begin(hostname); if(!configuration.isKeySet(ConfigurationKey::ethConfigured)) if(configuration.get(ConfigurationKey::ethConfigured) != "false"){ const auto ðIP = configuration.get("EthIP"); @@ -151,7 +158,7 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) eth.config(ethIP, ethGateway, ethSubnetMask, ethDNS1, ethDNS2); } - eth.begin(hostname); + macEth = eth.getMac(); #endif @@ -302,12 +309,16 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.addInterfaceElement("saveform", "button", "Save","#configform"); web.setInterfaceElementAttribute("saveform", "type", "submit"); + #ifndef BASECAMP_NOWIFI // Show the devices MAC in the Webinterface String infotext2 = "This device has the WiFi MAC-Address: " + mac; web.addInterfaceElement("infotext2", "p", infotext2,"#wrapper"); + #endif + #ifndef BASECAMP_NOETH String infotext3 = "This device has the ETH MAC-Address: " + macEth; web.addInterfaceElement("infotext3", "p", infotext3,"#wrapper"); + #endif web.addInterfaceElement("footer", "footer", "Powered by ", "body"); web.addInterfaceElement("footerlink", "a", "Basecamp", "footer"); @@ -351,8 +362,11 @@ void Basecamp::handle (void) bool Basecamp::shouldEnableConfigWebserver() const { - return (configurationUi_ == ConfigurationUI::always || - (configurationUi_ == ConfigurationUI::accessPoint && wifi.getOperationMode() == WifiControl::Mode::accessPoint)); + return (configurationUi_ == ConfigurationUI::always + #ifndef BASECAMP_NOWIFI + || (configurationUi_ == ConfigurationUI::accessPoint && wifi.getOperationMode() == WifiControl::Mode::accessPoint) + #endif + ); } // This is a task that is called if MQTT client has lost connection. After 2 seconds it automatically trys to reconnect. @@ -369,7 +383,7 @@ void Basecamp::connectToMqtt(TimerHandle_t xTimer) { AsyncMqttClient *mqtt = (AsyncMqttClient *) pvTimerGetTimerID(xTimer); - if (WiFi.status() == WL_CONNECTED) { + if (WiFi.status() == WL_CONNECTED || EthControl::status() == ETH_GOT_IP ) { Serial.println("Trying to connect ..."); mqtt->connect(); // has no effect if already connected ( if (_connected) return;) } @@ -464,8 +478,10 @@ void Basecamp::checkResetReason() // TODO: expand infos String Basecamp::showSystemInfo() { std::ostringstream info; + #ifndef BASECAMP_NOWIFI info << "MAC-Address: " << mac.c_str(); info << ", Hardware MAC: " << wifi.getHardwareMacAddress(":").c_str() << std::endl; + #endif #ifndef BASECAMP_NOETH info << "ETH MAC-Address:" << macEth.c_str() << std::endl; diff --git a/EthControl.cpp b/EthControl.cpp index 03f350a..5663431 100755 --- a/EthControl.cpp +++ b/EthControl.cpp @@ -33,15 +33,24 @@ String EthControl::getMac() { return ETH.macAddress(); } +eth_state_t EthControl::_state = ETH_STOPPED; + +eth_state_t EthControl::status() { + return EthControl::_state; +} + void EthControl::WiFiEvent(WiFiEvent_t event){ switch (event) { case SYSTEM_EVENT_ETH_START: + EthControl::_state = ETH_STARTED; DEBUG_PRINTLN("ETH Started"); break; case SYSTEM_EVENT_ETH_CONNECTED: + EthControl::_state = ETH_CONNECTED; DEBUG_PRINTLN("ETH Connected"); break; case SYSTEM_EVENT_ETH_GOT_IP: + EthControl::_state = ETH_GOT_IP; DEBUG_PRINT("ETH MAC: "); DEBUG_PRINT(ETH.macAddress()); DEBUG_PRINT(", IPv4: "); @@ -54,12 +63,14 @@ void EthControl::WiFiEvent(WiFiEvent_t event){ DEBUG_PRINTLN("Mbps"); break; case SYSTEM_EVENT_ETH_DISCONNECTED: + EthControl::_state = ETH_DISCONNECTED; DEBUG_PRINTLN("ETH Disconnected"); break; case SYSTEM_EVENT_ETH_STOP: + EthControl::_state = ETH_STOPPED; DEBUG_PRINTLN("ETH Stopped"); break; default: break; } -} \ No newline at end of file +} diff --git a/EthControl.hpp b/EthControl.hpp index 2e39d01..8d35e8f 100755 --- a/EthControl.hpp +++ b/EthControl.hpp @@ -17,7 +17,13 @@ #include #include - +typedef enum eth_state_t { + ETH_STARTED, + ETH_CONNECTED, + ETH_GOT_IP, + ETH_DISCONNECTED, + ETH_STOPPED +}; class EthControl { public: @@ -26,14 +32,14 @@ class EthControl { void begin(String hostname = "BasecampDevice"); bool config(IPAddress ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2 ); bool config(const String& ip, const String& gateway, const String& subnet, const String& dns1, const String& dns2 ); - + bool connected(); IPAddress getIP(); String getMac(); - + static eth_state_t status(); static void WiFiEvent(WiFiEvent_t event); private: - + static eth_state_t _state; }; -#endif //EthControl_h \ No newline at end of file +#endif //EthControl_h