diff --git a/platformio.ini b/platformio.ini index a16631f9..ae253cb1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -68,6 +68,22 @@ lib_deps = build_flags = "-D ARDUINO_M5Stick_C_Plus" +[env:ESPOE32] +platform = espressif32 +board = m5stack-core-esp32 +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 ESPOE32" + [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 31603c27..6293edb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,14 @@ #endif #include +#ifdef ESPOE32 +#include +#define ETH_ADDR 1 +#define ETH_POWER 5 +#define ETH_MDC 23 +#define ETH_MDIO 18 +#endif + #include #include @@ -33,6 +41,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 +167,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 +179,54 @@ void checkWifi() } } +#ifdef ESPOE32 +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); + #ifdef ESPOE32 + ETH.begin(ETH_ADDR, ETH_POWER, ETH_MDC, ETH_MDIO, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN); + #endif + + if (ETH.linkUp()) { + Serial.printf("Connected. IP Address: %s\n", ETH.localIP().toString().c_str()); + } +} +#endif + void setup_wifi() { 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 +268,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,9 +340,14 @@ void setup() EEPROM.begin(10); readEEPROM();//Restore previous state +#ifdef ESPOE32 + mqttSerial.print("Setting up ethernet..."); + setupEthernet(); +#else mqttSerial.print("Setting up wifi..."); - setup_wifi(); - ArduinoOTA.setHostname("ESPAltherma"); + setupWifi(); +#endif + ArduinoOTA.setHostname(HOSTNAME); ArduinoOTA.onStart([]() { busy = true; }); @@ -324,7 +382,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 227ad81b..83693fc4 100644 --- a/src/setup.h +++ b/src/setup.h @@ -1,3 +1,6 @@ +//Default device hostname: +#define HOSTNAME "ESPAltherma" + //Setup your credentials and mqtt info here: //only change the value between the " " leave the rest of the line untouched. #define WIFI_SSID "SSID"//**Your SSID here**