diff --git a/Basecamp.cpp b/Basecamp.cpp old mode 100644 new mode 100755 index 7c6b0ec..98022d5 --- 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); @@ -108,7 +114,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 +145,24 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) // Get WiFi MAC mac = wifi.getSoftwareMacAddress(":"); #endif + +#ifndef BASECAMP_NOETH + eth.begin(hostname); + 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); + } + + + macEth = eth.getMac(); +#endif + #ifndef BASECAMP_NOMQTT // Check if MQTT has been disabled by the user if (!configuration.get(ConfigurationKey::mqttActive).equalsIgnoreCase("false")) { @@ -248,7 +271,8 @@ bool Basecamp::begin(String fixedWiFiApEncryptionPassword) web.setInterfaceElementAttribute("configform", "onsubmit", "collectConfiguration()"); 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"); @@ -256,6 +280,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(ConfigurationKey::mqttActive).equalsIgnoreCase("false")) { @@ -272,9 +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 MAC-Address: " + mac; + 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"); @@ -318,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. @@ -336,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;) } @@ -431,8 +478,14 @@ 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; + #endif if (configuration.isKeySet(ConfigurationKey::accessPointSecret)) { info << "*******************************************" << std::endl; diff --git a/Basecamp.hpp b/Basecamp.hpp index 869c079..00acc7f 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 TimerHandle_t mqttReconnectTimer; diff --git a/Configuration.hpp b/Configuration.hpp old mode 100644 new mode 100755 index aa7ab1d..c0720e3 --- a/Configuration.hpp +++ b/Configuration.hpp @@ -19,6 +19,7 @@ enum class ConfigurationKey { deviceName, accessPointSecret, + ethConfigured, wifiConfigured, wifiEssid, wifiPassword, @@ -43,7 +44,8 @@ static const String getKeyName(ConfigurationKey key) case ConfigurationKey::accessPointSecret: return "APSecret"; - + case ConfigurationKey::ethConfigured: + return "EthConfigured"; case ConfigurationKey::wifiConfigured: return "WifiConfigured"; diff --git a/EthControl.cpp b/EthControl.cpp new file mode 100755 index 0000000..5663431 --- /dev/null +++ b/EthControl.cpp @@ -0,0 +1,76 @@ +#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(); +} + +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: "); + 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: + 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; + } +} diff --git a/EthControl.hpp b/EthControl.hpp new file mode 100755 index 0000000..8d35e8f --- /dev/null +++ b/EthControl.hpp @@ -0,0 +1,45 @@ +#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 + +typedef enum eth_state_t { + ETH_STARTED, + ETH_CONNECTED, + ETH_GOT_IP, + ETH_DISCONNECTED, + ETH_STOPPED +}; + +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 ); + 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 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 diff --git a/external/AsyncTCP b/external/AsyncTCP index 4dbbf10..ac55171 160000 --- a/external/AsyncTCP +++ b/external/AsyncTCP @@ -1 +1 @@ -Subproject commit 4dbbf1060923fd3940c6478bc2ac887ec389397f +Subproject commit ac551716aa655672cd3e0f1f2c137a710bccdc42 diff --git a/external/ESPAsyncWebServer b/external/ESPAsyncWebServer index 232b87a..95dedf7 160000 --- a/external/ESPAsyncWebServer +++ b/external/ESPAsyncWebServer @@ -1 +1 @@ -Subproject commit 232b87aeb12dd212af21b62a9f68e5e2c89a3a9c +Subproject commit 95dedf7a2df5a0d0ab01725baaacb4f982dedcb2