From 886c76a7b7f1867a0403c31d70d34de389d9ded5 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Fri, 5 Jul 2024 15:34:03 +0200 Subject: [PATCH] added ethernet support --- .../Bentuino/.vscode/c_cpp_properties.json | 2 + Firmware/Bentuino/.vscode/settings.json | 7 +- Firmware/Bentuino/platformio.ini | 2 + .../components/wifi/WifiComponent.cpp | 111 ++++++++++++++++-- .../Component/components/wifi/WifiComponent.h | 46 ++++++++ 5 files changed, 158 insertions(+), 10 deletions(-) diff --git a/Firmware/Bentuino/.vscode/c_cpp_properties.json b/Firmware/Bentuino/.vscode/c_cpp_properties.json index 55c8a728..ae1b1730 100644 --- a/Firmware/Bentuino/.vscode/c_cpp_properties.json +++ b/Firmware/Bentuino/.vscode/c_cpp_properties.json @@ -525,6 +525,8 @@ "USE_WIFI", "USE_OSC", "BENTUINO_CREATORTAB", + "USE_ETHERNET=true", + "WIFI_DEFAULT_MODE=MODE_ETH", "USE_POWER", "POWER_KEEP_PIN=12", "POWER_WAKEUP_BUTTON=34", diff --git a/Firmware/Bentuino/.vscode/settings.json b/Firmware/Bentuino/.vscode/settings.json index 0e015188..4f5f1c04 100644 --- a/Firmware/Bentuino/.vscode/settings.json +++ b/Firmware/Bentuino/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { + "*.embeddedhtml": "html", "typeinfo": "cpp", "functional": "cpp", "array": "cpp", @@ -47,7 +48,11 @@ "regex": "cpp", "ratio": "cpp", "thread": "cpp", - "list": "cpp" + "list": "cpp", + "format": "cpp", + "xhash": "cpp", + "xstring": "cpp", + "xutility": "cpp" }, "cmake.sourceDirectory": "D:/Documents/Perso/Projects/BenTo/Firmware/Bentuino/.pio/libdeps/creatorclub/FastAccelStepper" } \ No newline at end of file diff --git a/Firmware/Bentuino/platformio.ini b/Firmware/Bentuino/platformio.ini index d54c2071..bce71ed0 100644 --- a/Firmware/Bentuino/platformio.ini +++ b/Firmware/Bentuino/platformio.ini @@ -175,6 +175,8 @@ board = esp32-poe build_flags = ${bentuino.build_flags} -D BENTUINO_CREATORTAB + -D USE_ETHERNET=true + -D WIFI_DEFAULT_MODE=MODE_ETH -D USE_POWER -D POWER_KEEP_PIN=12 -D POWER_WAKEUP_BUTTON=34 diff --git a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp index ae179f5e..1c580905 100644 --- a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp +++ b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.cpp @@ -10,6 +10,11 @@ ImplementSingleton(WifiComponent) AddStringParamConfig(ssid); AddStringParamConfig(pass); +#ifdef USE_ETHERNET + AddIntParamConfig(mode); + + WiFi.onEvent(std::bind(&WifiComponent::WiFiEvent, this, std::placeholders::_1)); +#endif connect(); @@ -18,7 +23,7 @@ ImplementSingleton(WifiComponent) void WifiComponent::updateInternal() { - +#ifndef USE_ETHERNET long curTime = millis(); if (curTime > lastConnectTime + timeBetweenTries) { @@ -33,7 +38,7 @@ void WifiComponent::updateInternal() #endif { setState(Connected); - NDBG("Connected, local IP is " + getIP()); + timeAtConnect = -1; } @@ -63,6 +68,7 @@ void WifiComponent::updateInternal() break; } } +#endif } void WifiComponent::clearInternal() @@ -76,6 +82,22 @@ void WifiComponent::setState(ConnectionState s) return; state = s; + switch (state) + { + case Connected: +#ifdef USE_ETHERNET + NDBG("Connected to ethernet with IP " + getIP()); + +#else + NDBG("Connected to wifi " + ssid + " : " + pass + " with IP " + getIP()); +#endif + break; + + case ConnectionError: + NDBG("Connection error"); + break; + } + timeAtStateChange = millis(); sendEvent(ConnectionStateChanged); } @@ -88,17 +110,51 @@ void WifiComponent::connect() if (state == Connected || state == Hotspot) WiFi.disconnect(); - WiFi.mode(WIFI_STA); + wifi_mode_t wMode = WIFI_STA; + switch (mode) + { + case MODE_STA: + wMode = WIFI_STA; + break; + case MODE_AP: + wMode = WIFI_AP; + break; + case MODE_AP_STA: + wMode = WIFI_AP_STA; + break; + +#ifdef USE_ETHERNET + case MODE_ETH: + wMode = WIFI_OFF; + break; + case MODE_ETH_STA: + wMode = WIFI_STA; + break; +#endif + } + + WiFi.mode(wMode); #ifdef ESP32 - WiFi.setAutoConnect(true); - WiFi.setAutoReconnect(true); - WiFi.setSleep(false); - WiFi.setTxPower(WIFI_POWER_19dBm); #endif - NDBG("Connecting to " + ssid + " : " + pass + "..."); - WiFi.begin(ssid.c_str(), pass.c_str()); +#ifdef USE_ETHERNET + if (mode == MODE_ETH || mode == MODE_ETH_STA) + { + NDBG("Connecting to ethernet..."); + ETH.begin(); + } +#endif + + if (wMode != WIFI_OFF) + { + NDBG("Connecting to wifi " + ssid + " : " + pass + "..."); + WiFi.setAutoConnect(true); + WiFi.setAutoReconnect(true); + WiFi.setSleep(false); + WiFi.setTxPower(WIFI_POWER_19dBm); + WiFi.begin(ssid.c_str(), pass.c_str()); + } setState(Connecting); } @@ -115,10 +171,47 @@ void WifiComponent::disconnect() setState(Off); } +#ifdef USE_ETHERNET +void WifiComponent::WiFiEvent(WiFiEvent_t event) +{ + switch (event) + { + + case ARDUINO_EVENT_ETH_START: + ETH.setHostname(DeviceID.c_str()); + setState(Connecting); + break; + + case ARDUINO_EVENT_ETH_CONNECTED: + setState(Connected); + break; + + case ARDUINO_EVENT_ETH_GOT_IP: + setState(Connected); + break; + + case ARDUINO_EVENT_ETH_DISCONNECTED: + setState(ConnectionError); + break; + + case ARDUINO_EVENT_ETH_STOP: + setState(ConnectionError); + break; + + default: + break; + } +} +#endif + String WifiComponent::getIP() const { if (state == Connected) +#ifdef USE_ETHERNET + return StringHelpers::ipToString(ETH.localIP()); +#else return StringHelpers::ipToString(WiFi.localIP()); +#endif else if (state == Hotspot) return StringHelpers::ipToString(WiFi.softAPIP()); diff --git a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h index 0f285ad6..9a2bed60 100644 --- a/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h +++ b/Firmware/Bentuino/src/Component/components/wifi/WifiComponent.h @@ -1,11 +1,25 @@ #pragma once #ifdef ESP32 + +#ifdef USE_ETHERNET + +#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT +#define ETH_PHY_POWER 12 + +#include +#endif + #include + #elif defined ESP8266 #include #endif +#ifndef WIFI_DEFAULT_MODE +#define WIFI_DEFAULT_MODE WIFI_MODE_STA +#endif + DeclareComponentSingleton(Wifi, "wifi", ) enum ConnectionState { Off, @@ -28,8 +42,27 @@ long timeAtStateChange; ConnectionState state; +enum WifiMode +{ + MODE_STA, + MODE_AP, + MODE_AP_STA, +#ifdef USE_ETHERNET + MODE_ETH, + MODE_ETH_STA, +#endif + MODE_MAX +}; +const String wifiModeNames[MODE_MAX]{ + "Wifi", "AP", "Wifi+AP", +#ifdef USE_ETHERNET + "Ethernet", "Wifi+Ethernet" +#endif +}; + DeclareStringParam(ssid, ""); DeclareStringParam(pass, ""); +DeclareIntParam(mode, WIFI_DEFAULT_MODE); bool initInternal(JsonObject o) override; void updateInternal() override; @@ -42,21 +75,34 @@ void setAP(); void disable(); void setState(ConnectionState s); +#ifdef USE_ETHERNET +void WiFiEvent(WiFiEvent_t event); +#endif + String getIP() const; HandleSetParamInternalStart CheckAndSetParam(ssid); CheckAndSetParam(pass); +#ifdef USE_ETHERNET +CheckAndSetParam(mode); +#endif HandleSetParamInternalEnd; FillSettingsInternalStart FillSettingsParam(ssid); FillSettingsParam(pass); +#ifdef USE_ETHERNET +FillSettingsParam(mode); +#endif FillSettingsInternalEnd; FillOSCQueryInternalStart FillOSCQueryStringParam(ssid); FillOSCQueryStringParam(pass); +#ifdef USE_ETHERNET +FillOSCQueryEnumParam(mode, wifiModeNames, WIFI_MODE_MAX); +#endif FillOSCQueryInternalEnd; DeclareComponentEventTypes(ConnectionStateChanged);