From 2b049168f9896e42496c8d2fb8664cb1f294ccb7 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Sun, 18 Feb 2024 17:38:48 +0000 Subject: [PATCH] Add basic support for WT32-ETH01 board --- platformio.ini | 16 +++++++++++++ src/main.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++----- src/setup.h | 5 ++++ 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index a16631f9..d77bf513 100644 --- a/platformio.ini +++ b/platformio.ini @@ -68,6 +68,22 @@ lib_deps = build_flags = "-D ARDUINO_M5Stick_C_Plus" +[env:wt32-eth01] +platform = espressif32 +board = wt32-eth01 +framework = arduino +monitor_speed = 115200 +upload_speed = 115200 +; Uncomment this line to allow for remote upgrade. If name resolution does not work for you, replace with the IP of ESPAltherma +; upload_port = ESPAltherma.local +; Uncomment this line if you want to define the protocol. Autodetected otherwise. +; upload_protocol = espota + +lib_deps = + PubSubClient + +build_flags = "-D WT32_ETH01" + [env:native] # Used to run unit test; g++ must be in PATH. platform = native diff --git a/src/main.cpp b/src/main.cpp index 2b2fa03a..66c0e97a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,10 @@ #endif #include +#ifdef WT32_ETH01 +#include +#endif + #include #include @@ -33,6 +37,8 @@ Converter converter; char registryIDs[32]; //Holds the registries to query bool busy = false; +static bool eth_connected = false; + #if defined(ARDUINO_M5Stick_C) || defined(ARDUINO_M5Stick_C_Plus) long LCDTimeout = 40000;//Keep screen ON for 40s then turn off. ButtonA will turn it On again. #endif @@ -157,7 +163,7 @@ void get_wifi_bssid(const char *ssid, uint8_t *bssid, uint32_t *wifi_channel) void checkWifi() { int i = 0; - while (WiFi.status() != WL_CONNECTED) + while (!WiFi.isConnected()) { delay(500); Serial.print("."); @@ -169,11 +175,52 @@ void checkWifi() } } -void setup_wifi() +#ifdef WT32_ETH01 +void WiFiEvent(WiFiEvent_t event) +{ + switch (event) { + case ARDUINO_EVENT_ETH_START: + Serial.println("ETH Started"); + // The hostname must be set after the interface is started, but needs + // to be set before DHCP, so set it from the event handler thread. + ETH.setHostname(HOSTNAME); + break; + case ARDUINO_EVENT_ETH_CONNECTED: + Serial.println("ETH Connected"); + break; + case ARDUINO_EVENT_ETH_GOT_IP: + Serial.println("ETH Got IP"); + eth_connected = true; + break; + case ARDUINO_EVENT_ETH_DISCONNECTED: + Serial.println("ETH Disconnected"); + eth_connected = false; + break; + case ARDUINO_EVENT_ETH_STOP: + Serial.println("ETH Stopped"); + eth_connected = false; + break; + default: + break; + } +} + +void setupEthernet() +{ + WiFi.onEvent(WiFiEvent); + ETH.begin(); + + if (ETH.linkUp()) { + Serial.printf("Connected. IP Address: %s\n", ETH.localIP().toString().c_str()); + } +} +#endif + +void setupWifi() { delay(10); // We start by connecting to a WiFi network - mqttSerial.printf("Connecting to %s\n", WIFI_SSID); + Serial.printf("Connecting to %s\n", WIFI_SSID); #if defined(WIFI_IP) && defined(WIFI_GATEWAY) && defined(WIFI_SUBNET) IPAddress local_IP(WIFI_IP); @@ -215,7 +262,7 @@ void setup_wifi() WiFi.begin(WIFI_SSID, WIFI_PWD, 0, 0, true); } checkWifi(); - mqttSerial.printf("Connected. IP Address: %s\n", WiFi.localIP().toString().c_str()); + Serial.printf("Connected. IP Address: %s\n", WiFi.localIP().toString().c_str()); } void initRegistries(){ @@ -287,8 +334,13 @@ void setup() EEPROM.begin(10); readEEPROM();//Restore previous state +#ifdef WT32_ETH01 + mqttSerial.print("Setting up ethernet..."); + setupEthernet(); +#else mqttSerial.print("Setting up wifi..."); - setup_wifi(); + setupWifi(); +#endif ArduinoOTA.setHostname(HOSTNAME); ArduinoOTA.onStart([]() { busy = true; @@ -324,7 +376,7 @@ void waitLoop(uint ms){ void loop() { unsigned long start = millis(); - if (WiFi.status() != WL_CONNECTED) + if (!eth_connected && !WiFi.isConnected()) { //restart board if needed checkWifi(); } diff --git a/src/setup.h b/src/setup.h index 83693fc4..4159eb4d 100644 --- a/src/setup.h +++ b/src/setup.h @@ -26,9 +26,14 @@ #define TX_PIN 26// Pin connected to the RX pin of X10A #else //Default GPIO PINs for Serial2: +#if defined(WT32_ETH01) +#define RX_PIN 5// Pin connected to the TX pin of X10A +#define TX_PIN 17// Pin connected to the RX pin of X10A +#else #define RX_PIN 16// Pin connected to the TX pin of X10A #define TX_PIN 17// Pin connected to the RX pin of X10A #endif +#endif #define PIN_THERM 0// Pin connected to the thermostat relay (normally open)