diff --git a/Network.cpp b/Network.cpp index f456928..5d96618 100644 --- a/Network.cpp +++ b/Network.cpp @@ -29,6 +29,8 @@ Network::Network(Preferences *preferences, const String& maintenancePathPrefix) void Network::setupDevice() { + int hardwareDetect = _preferences->getInt(preference_network_hardware); + int hardwareDetectGpio = _preferences->getInt(preference_network_hardware_gpio); if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0) { @@ -37,35 +39,38 @@ void Network::setupDevice() } else { - int hardwareDetect = _preferences->getInt(preference_network_hardware_detect); + if(hardwareDetectGpio == 0) + { + hardwareDetectGpio = 26; + _preferences->putInt(preference_network_hardware_gpio, hardwareDetectGpio); + } if(hardwareDetect == 0) { - hardwareDetect = 26; - _preferences->putInt(preference_network_hardware_detect, hardwareDetect); - } - - if(hardwareDetect == -1) - { - Log->println(F("W5500 hardware is disable, using Wifi.")); + Log->println(F("W5500 hardware is disabled, using Wifi.")); _networkDeviceType = NetworkDeviceType::WiFi; } - else + else if(hardwareDetect == 1) { Log->print(F("Using PIN ")); - Log->print(hardwareDetect); + Log->print(hardwareDetectGpio); Log->println(F(" for network device selection")); pinMode(hardwareDetect, INPUT_PULLUP); _networkDeviceType = digitalRead(hardwareDetect) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500; } + else if(hardwareDetect == 2) + { + Log->print(F("W5500 on M5Sack Atom POE")); + _networkDeviceType = NetworkDeviceType::W5500; + } } - _networkDeviceType = NetworkDeviceType::W5500; + switch(_networkDeviceType) { case NetworkDeviceType::W5500: Log->println(F("Network device: W5500")); - _device = new W5500Device(_hostname, _preferences); + _device = new W5500Device(_hostname, _preferences, hardwareDetect); break; case NetworkDeviceType::WiFi: Log->println(F("Network device: Builtin WiFi")); diff --git a/Pins.h b/Pins.h index 032a9da..e7f9d90 100644 --- a/Pins.h +++ b/Pins.h @@ -1,8 +1,5 @@ #pragma once -#define ETHERNET_SCK_PIN 22 -#define ETHERNET_MISO_PIN 23 -#define ETHERNET_MOSI_PIN 33 #define ETHERNET_CS_PIN 19 #define ETHERNET_RESET_PIN -1 #define TRIGGER_LOCK_PIN 32 diff --git a/PreferencesKeys.h b/PreferencesKeys.h index bf06518..806bf44 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -16,7 +16,8 @@ #define preference_mqtt_crt "mqttcrt" #define preference_mqtt_key "mqttkey" #define preference_mqtt_hass_discovery "hassdiscovery" -#define preference_network_hardware_detect "nwhwdt" +#define preference_network_hardware "nwhw" +#define preference_network_hardware_gpio "nwhwdt" #define preference_rssi_publish_interval "rssipb" #define preference_hostname "hostname" #define preference_network_timeout "nettmout" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index f74fe30..3a0f22d 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -239,9 +239,14 @@ bool WebCfgServer::processArgs(String& message) _preferences->putString(preference_mqtt_key, value); configChanged = true; } + else if(key == "NWHW") + { + _preferences->putInt(preference_network_hardware, value.toInt()); + configChanged = true; + } else if(key == "NWHWDT") { - _preferences->putInt(preference_network_hardware_detect, value.toInt()); + _preferences->putInt(preference_network_hardware_gpio, value.toInt()); configChanged = true; } else if(key == "RSSI") @@ -607,7 +612,8 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE, _network->encryptionSupported()); printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE, _network->encryptionSupported()); printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE, _network->encryptionSupported()); - printDropDown(response, "NWHWDT", "Network hardware detection", String(_preferences->getInt(preference_network_hardware_detect)), getNetworkDetectionOptions()); + printDropDown(response, "NWHW", "Network hardware", String(_preferences->getInt(preference_network_hardware)), getNetworkDetectionOptions()); + printDropDown(response, "NWHWDT", "Network hardware detection", String(_preferences->getInt(preference_network_hardware_gpio)), getNetworkGpioOptions()); printInputField(response, "RSSI", "RSSI Publish interval (seconds; -1 to disable)", _preferences->getInt(preference_rssi_publish_interval), 6); printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect)); @@ -956,7 +962,16 @@ const std::vector> WebCfgServer::getNetworkDetectionOp { std::vector> options; - options.push_back(std::make_pair("-1", "Disable W5500")); + options.push_back(std::make_pair("0", "Disable W5500 (Wifi only)")); + options.push_back(std::make_pair("1", "W5500 (GPIO CS=5; SCK=18; MISO=19; MOSI=23; RST=33)")); + options.push_back(std::make_pair("2", "M5Stack Atom POE (W5500)")); + + return options; +} + +const std::vector> WebCfgServer::getNetworkGpioOptions() const +{ + std::vector> options; for(int i=16; i <= 33; i++) { @@ -966,4 +981,4 @@ const std::vector> WebCfgServer::getNetworkDetectionOp } return options; -} +} \ No newline at end of file diff --git a/WebCfgServer.h b/WebCfgServer.h index 4ca7693..cf1be2b 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -50,6 +50,7 @@ private: void buildNavigationButton(String& response, const char* caption, const char* targetPath); const std::vector> getNetworkDetectionOptions() const; + const std::vector> getNetworkGpioOptions() const; void printParameter(String& response, const char* description, const char* value); diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index 8f7ce1d..6a84af4 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -6,9 +6,10 @@ #include "../Logger.h" #include "../MqttTopics.h" -W5500Device::W5500Device(const String &hostname, Preferences* preferences) +W5500Device::W5500Device(const String &hostname, Preferences* preferences, int variant) : NetworkDevice(hostname), - _preferences(preferences) + _preferences(preferences), + _variant((W5500Variant)variant) { initializeMacAddress(_mac); @@ -37,7 +38,15 @@ void W5500Device::initialize() resetDevice(); - Ethernet.init(ETHERNET_CS_PIN, ETHERNET_SCK_PIN, ETHERNET_MISO_PIN, ETHERNET_MOSI_PIN); + switch(_variant) + { + case W5500Variant::M5StackAtomPoe: + Ethernet.init(ETHERNET_CS_PIN, 22, 23, 33); + break; + default: + Ethernet.init(ETHERNET_CS_PIN); + break; + } if(_preferences->getBool(preference_mqtt_log_enabled)) { diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index 953a6ed..4272760 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -6,10 +6,16 @@ #include #include +enum class W5500Variant +{ + Generic = 1, + M5StackAtomPoe = 2 +}; + class W5500Device : public NetworkDevice { public: - explicit W5500Device(const String& hostname, Preferences* _preferences); + explicit W5500Device(const String& hostname, Preferences* _preferences, int variant); ~W5500Device(); virtual void initialize(); @@ -61,6 +67,7 @@ private: int _maintainResult = 0; bool _hasDHCPAddress = false; char* _path; + W5500Variant _variant; byte _mac[6]; }; \ No newline at end of file