diff --git a/.gitignore b/.gitignore index 43cee78db73..13b79421383 100644 --- a/.gitignore +++ b/.gitignore @@ -15,8 +15,20 @@ web.tar .platformio .local .cache +.history +# macOS .DS_Store +.DS_Store? +._* +.AppleDouble +.LSOverride +.Spotlight-V100 +.Trashes +.fseventsd +.AppleDB +.AppleDesktop + Thumbs.db .autotools .built diff --git a/src/DebugConfiguration.h b/src/DebugConfiguration.h index eac6260fcee..b79caa8cd28 100644 --- a/src/DebugConfiguration.h +++ b/src/DebugConfiguration.h @@ -147,14 +147,16 @@ extern "C" void logLegacy(const char *level, const char *fmt, ...); // Default Bluetooth PIN #define defaultBLEPin 123456 -#if HAS_ETHERNET && !defined(USE_WS5500) -#include -#endif // HAS_ETHERNET - -#if HAS_ETHERNET && defined(USE_WS5500) +#if HAS_ETHERNET +#if defined(USE_WS5500) #include #define ETH ETH2 -#endif // HAS_ETHERNET +#elif defined(ESP32) && defined(ETH_PHY_TYPE) +#include +#else +#include +#endif +#endif #if HAS_WIFI #include diff --git a/src/PowerFSM.cpp b/src/PowerFSM.cpp index b11f37cf0a5..fe01832abb0 100644 --- a/src/PowerFSM.cpp +++ b/src/PowerFSM.cpp @@ -19,7 +19,7 @@ #include "sleep.h" #include "target_specific.h" -#if HAS_WIFI && !defined(ARCH_PORTDUINO) || defined(MESHTASTIC_EXCLUDE_WIFI) +#if (HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK) && !defined(ARCH_PORTDUINO) || defined(MESHTASTIC_EXCLUDE_WIFI) #include "mesh/wifi/WiFiAPClient.h" #endif @@ -376,7 +376,7 @@ void PowerFSM_setup() // Don't add power saving transitions if we are a power saving tracker or sensor or have Wifi enabled. Sleep will be initiated // through the modules -#if HAS_WIFI && !defined(MESHTASTIC_EXCLUDE_WIFI) +#if (HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK) && !defined(MESHTASTIC_EXCLUDE_WIFI) bool isTrackerOrSensor = config.device.role == meshtastic_Config_DeviceConfig_Role_TRACKER || config.device.role == meshtastic_Config_DeviceConfig_Role_TAK_TRACKER || config.device.role == meshtastic_Config_DeviceConfig_Role_SENSOR; diff --git a/src/configuration.h b/src/configuration.h index cc623455f6e..8eee8b6ed8d 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -521,6 +521,13 @@ along with this program. If not, see . // Allow code that needs internet to just check HAS_NETWORKING rather than HAS_WIFI || HAS_ETHERNET #define HAS_NETWORKING (HAS_WIFI || HAS_ETHERNET) +// ESP32 uses Arduino Wi-Fi stack when using native/WS5500 Ethernet +#if (HAS_ETHERNET && (defined(ETH_PHY_TYPE) || defined(USE_WS5500))) +#define HAS_ETHERNET_ON_WIFI_STACK 1 +#else +#define HAS_ETHERNET_ON_WIFI_STACK 0 +#endif + // // Turn off Bluetooth #ifdef MESHTASTIC_EXCLUDE_BLUETOOTH #undef HAS_BLUETOOTH diff --git a/src/main.cpp b/src/main.cpp index 8a46b3f5bf6..c5136d1c9c4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,15 +59,17 @@ NimbleBluetooth *nimbleBluetooth = nullptr; NRF52Bluetooth *nrf52Bluetooth = nullptr; #endif -#if HAS_WIFI || defined(USE_WS5500) +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include "mesh/api/WiFiServerAPI.h" #include "mesh/wifi/WiFiAPClient.h" #endif -#if HAS_ETHERNET && !defined(USE_WS5500) +#if HAS_ETHERNET #include "mesh/api/ethServerAPI.h" +#if !defined(USE_WS5500) && !defined(ETH_PHY_TYPE) #include "mesh/eth/ethClient.h" #endif +#endif #if !MESHTASTIC_EXCLUDE_MQTT #include "mqtt/MQTT.h" diff --git a/src/mesh/InterfacesTemplates.cpp b/src/mesh/InterfacesTemplates.cpp index 57abbf0ee40..9bc58eb542c 100644 --- a/src/mesh/InterfacesTemplates.cpp +++ b/src/mesh/InterfacesTemplates.cpp @@ -1,3 +1,4 @@ +#include "configuration.h" #include "LR11x0Interface.cpp" #include "LR11x0Interface.h" #include "SX126xInterface.cpp" @@ -25,10 +26,12 @@ template class LR11x0Interface; template class SX126xInterface; #endif -#if HAS_ETHERNET && !defined(USE_WS5500) +#if HAS_ETHERNET #include "api/ethServerAPI.h" -template class ServerAPI; -template class APIServerPort; +#if !(defined(ESP32) && HAS_WIFI) +template class ServerAPI; +#endif +template class APIServerPort; #endif #if HAS_WIFI diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 529f500031a..7ff67ba6b33 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -31,7 +31,7 @@ #include #ifdef ARCH_ESP32 -#if HAS_WIFI +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include "mesh/wifi/WiFiAPClient.h" #endif #include "SPILock.h" diff --git a/src/mesh/api/ethServerAPI.cpp b/src/mesh/api/ethServerAPI.cpp index 10ff06df23a..39caf6d9860 100644 --- a/src/mesh/api/ethServerAPI.cpp +++ b/src/mesh/api/ethServerAPI.cpp @@ -1,23 +1,23 @@ #include "configuration.h" -#include - -#if HAS_ETHERNET && !defined(USE_WS5500) - #include "ethServerAPI.h" +#include + +#if HAS_ETHERNET +#if !HAS_WIFI static ethServerPort *apiPort; void initApiServer(int port) { - // Start API server on port 4403 if (!apiPort) { apiPort = new ethServerPort(port); LOG_INFO("API server listening on TCP port %d", port); apiPort->init(); } } +#endif -ethServerAPI::ethServerAPI(EthernetClient &_client) : ServerAPI(_client) +ethServerAPI::ethServerAPI(MeshEthernetClient &_client) : ServerAPI(_client) { LOG_INFO("Incoming ethernet connection"); api_type = TYPE_ETH; diff --git a/src/mesh/api/ethServerAPI.h b/src/mesh/api/ethServerAPI.h index c616c87be7d..575fd901dbf 100644 --- a/src/mesh/api/ethServerAPI.h +++ b/src/mesh/api/ethServerAPI.h @@ -1,27 +1,40 @@ #pragma once - +#include "configuration.h" #include "ServerAPI.h" -#ifndef USE_WS5500 + +#if HAS_ETHERNET +#if defined(ESP32) && HAS_ETHERNET_ON_WIFI_STACK +#if defined(ETH_PHY_TYPE) +#include +#else +#include +#endif +typedef WiFiClient MeshEthernetClient; +typedef WiFiServer MeshEthernetServer; +#else #include +typedef EthernetClient MeshEthernetClient; +typedef EthernetServer MeshEthernetServer; +#endif /** * Provides both debug printing and, if the client starts sending protobufs to us, switches to send/receive protobufs * (and starts dropping debug printing - FIXME, eventually those prints should be encapsulated in protobufs). */ -class ethServerAPI : public ServerAPI +class ethServerAPI : public ServerAPI { public: - explicit ethServerAPI(EthernetClient &_client); + explicit ethServerAPI(MeshEthernetClient &_client); }; /** * Listens for incoming connections and does accepts and creates instances of EthernetServerAPI as needed */ -class ethServerPort : public APIServerPort +class ethServerPort : public APIServerPort { public: explicit ethServerPort(int port); }; -void initApiServer(int port = SERVER_API_DEFAULT_PORT); -#endif +void initApiServer(int port); +#endif \ No newline at end of file diff --git a/src/mesh/eth/ethClient.cpp b/src/mesh/eth/ethClient.cpp index a811ec16cd5..fce7b698e1f 100644 --- a/src/mesh/eth/ethClient.cpp +++ b/src/mesh/eth/ethClient.cpp @@ -1,8 +1,8 @@ +#include "configuration.h" #include "mesh/eth/ethClient.h" #include "NodeDB.h" #include "RTC.h" #include "concurrency/Periodic.h" -#include "configuration.h" #include "main.h" #include "mesh/api/ethServerAPI.h" #include "target_specific.h" @@ -65,7 +65,7 @@ static int32_t reconnectETH() #if !MESHTASTIC_EXCLUDE_SOCKETAPI if (config.display.displaymode != meshtastic_Config_DisplayConfig_DisplayMode_COLOR) { - initApiServer(); + initApiServer(SERVER_API_DEFAULT_PORT); } #endif #if HAS_UDP_MULTICAST diff --git a/src/mesh/http/ContentHandler.cpp b/src/mesh/http/ContentHandler.cpp index 6b3aa4859ff..ff53e239fd6 100644 --- a/src/mesh/http/ContentHandler.cpp +++ b/src/mesh/http/ContentHandler.cpp @@ -6,7 +6,7 @@ #include "main.h" #include "mesh/http/ContentHelper.h" #include "mesh/http/WebServer.h" -#if HAS_WIFI +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include "mesh/wifi/WiFiAPClient.h" #endif #include "SPILock.h" diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 5d05c7fc6df..4f65bc74838 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -1,5 +1,5 @@ #include "configuration.h" -#if HAS_WIFI +#if HAS_WIFI || (HAS_ETHERNET && defined(ETH_PHY_TYPE)) #include "NodeDB.h" #include "RTC.h" #include "concurrency/Periodic.h" @@ -13,7 +13,7 @@ #if HAS_ETHERNET && defined(USE_WS5500) #include #define ETH ETH2 -#endif // HAS_ETHERNET +#endif #include #ifdef ARCH_ESP32 @@ -50,7 +50,7 @@ char ourHost[16]; static unsigned long wifiReconnectStartMillis = 0; static bool wifiReconnectPending = false; -bool APStartupComplete = 0; +bool APStartupComplete = false; unsigned long lastrun_ntp = 0; @@ -62,19 +62,31 @@ meshtastic::Syslog syslog(syslogClient); Periodic *wifiReconnect; -#ifdef USE_WS5500 -// Startup Ethernet +#if HAS_ETHERNET +// Startup Ethernet (Universal: Native or SPI) bool initEthernet() { - if ((config.network.eth_enabled) && (ETH.begin(ETH_PHY_W5500, 1, ETH_CS_PIN, ETH_INT_PIN, ETH_RST_PIN, SPI3_HOST, - ETH_SCLK_PIN, ETH_MISO_PIN, ETH_MOSI_PIN))) { - WiFi.onEvent(WiFiEvent); + bool ok = false; +#if defined(ESP32) && defined(ETH_PHY_TYPE) + // Native ESP32 Ethernet (LAN8720, etc.) + LOG_INFO("Starting Native Ethernet..."); + WiFi.onEvent(WiFiEvent); + ok = ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE); +#elif defined(USE_WS5500) + // SPI Ethernet (W5500) + LOG_INFO("Starting W5500 Ethernet..."); + WiFi.onEvent(WiFiEvent); + ok = ETH.begin(ETH_PHY_W5500, 1, ETH_CS_PIN, ETH_INT_PIN, ETH_RST_PIN, SPI3_HOST, ETH_SCLK_PIN, ETH_MISO_PIN, ETH_MOSI_PIN); +#endif + + if (ok) { #if !MESHTASTIC_EXCLUDE_WEBSERVER createSSLCert(); // For WebServer #endif return true; } + LOG_ERROR("Ethernet failed to start"); return false; } #endif @@ -231,21 +243,20 @@ static int32_t reconnectWiFi() bool isWifiAvailable() { - if (config.network.wifi_enabled && (config.network.wifi_ssid[0])) { return true; -#ifdef USE_WS5500 - } else if (config.network.eth_enabled) { + } +#if HAS_ETHERNET + else if (config.network.eth_enabled) { return true; + } #endif #ifndef ARCH_PORTDUINO - } else if (WiFi.status() == WL_CONNECTED) { - // it's likely we have wifi now, but user intends to turn it off in config! + else if (WiFi.status() == WL_CONNECTED) { return true; -#endif - } else { - return false; } +#endif + return false; } // Disable WiFi @@ -494,7 +505,10 @@ static void WiFiEvent(WiFiEvent_t event) LOG_INFO("Ethernet disconnected"); break; case ARDUINO_EVENT_ETH_GOT_IP: -#ifdef USE_WS5500 +#if defined(ETH_PHY_TYPE) + LOG_INFO("Ethernet Obtained IP address: %s", ETH.localIP().toString().c_str()); + onNetworkConnected(); +#elif defined(USE_WS5500) LOG_INFO("Obtained IP address: %s, %u Mbps, %s", ETH.localIP().toString().c_str(), ETH.linkSpeed(), ETH.fullDuplex() ? "FULL_DUPLEX" : "HALF_DUPLEX"); onNetworkConnected(); diff --git a/src/mesh/wifi/WiFiAPClient.h b/src/mesh/wifi/WiFiAPClient.h index 078c4019321..bcc2864d51d 100644 --- a/src/mesh/wifi/WiFiAPClient.h +++ b/src/mesh/wifi/WiFiAPClient.h @@ -9,15 +9,19 @@ #include #endif -#if HAS_ETHERNET && defined(USE_WS5500) +#if HAS_ETHERNET +#if defined(ESP32) && defined(ETH_PHY_TYPE) +#include +#elif defined(USE_WS5500) #include #define ETH ETH2 -#endif // HAS_ETHERNET +#endif +#endif extern bool needReconnect; extern concurrency::Periodic *wifiReconnect; -/// @return true if wifi is now in use +/// @return true if Wi-Fi is now in use bool initWifi(); void deinitWifi(); @@ -26,7 +30,6 @@ bool isWifiAvailable(); uint8_t getWifiDisconnectReason(); -#ifdef USE_WS5500 -// Startup Ethernet +#if HAS_ETHERNET_ON_WIFI_STACK bool initEthernet(); #endif \ No newline at end of file diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index 6ffde70db7b..bc6fca2b1a9 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -26,6 +26,10 @@ #include "MeshRadio.h" #include "TypeConversions.h" +#if HAS_ETHERNET && defined(ETH_PHY_TYPE) +#include +#endif + #if !MESHTASTIC_EXCLUDE_MQTT #include "mqtt/MQTT.h" #endif @@ -1285,12 +1289,27 @@ void AdminModule::handleGetDeviceConnectionStatus(const meshtastic_MeshPacket &r } #endif -#if HAS_ETHERNET && !defined(USE_WS5500) +#if HAS_ETHERNET conn.has_ethernet = true; conn.ethernet.has_status = true; - if (Ethernet.linkStatus() == LinkON) { + bool isConnected = false; + uint32_t ipAddr = 0; + +#if HAS_ETHERNET_ON_WIFI_STACK + isConnected = ETH.linkUp(); +#else + isConnected = (Ethernet.linkStatus() == LinkON); +#endif + + if (isConnected) { +#if HAS_ETHERNET_ON_WIFI_STACK + ipAddr = (uint32_t)ETH.localIP(); +#else + ipAddr = (uint32_t)Ethernet.localIP(); +#endif + conn.ethernet.status.is_connected = true; - conn.ethernet.status.ip_address = Ethernet.localIP(); + conn.ethernet.status.ip_address = ipAddr; #if !MESHTASTIC_EXCLUDE_MQTT conn.ethernet.status.is_mqtt_connected = mqtt && mqtt->isConnectedDirectly(); #endif diff --git a/src/modules/AdminModule.h b/src/modules/AdminModule.h index c446887b3ea..17296f67154 100644 --- a/src/modules/AdminModule.h +++ b/src/modules/AdminModule.h @@ -4,7 +4,7 @@ #endif #include "ProtobufModule.h" #include -#if HAS_WIFI +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include "mesh/wifi/WiFiAPClient.h" #endif diff --git a/src/mqtt/MQTT.h b/src/mqtt/MQTT.h index 7d571560263..4fbb6540ba8 100644 --- a/src/mqtt/MQTT.h +++ b/src/mqtt/MQTT.h @@ -9,13 +9,15 @@ #if !defined(ARCH_NRF52) || NRF52_USE_JSON #include "serialization/JSON.h" #endif -#if HAS_WIFI + +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include #if __has_include() #include #endif #endif -#if HAS_ETHERNET && !defined(USE_WS5500) + +#if HAS_ETHERNET && !defined(ETH_PHY_TYPE) && !defined(USE_WS5500) #include #endif @@ -80,7 +82,8 @@ class MQTT : private concurrency::OSThread #ifndef PIO_UNIT_TESTING private: #endif -#if HAS_WIFI + +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK using MQTTClient = WiFiClient; #if __has_include() using MQTTClientTLS = WiFiClientSecure; diff --git a/src/sleep.cpp b/src/sleep.cpp index 9c044eaf7ac..145f631c92f 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -21,7 +21,7 @@ // "esp_pm_config_esp32_t is deprecated, please include esp_pm.h and use esp_pm_config_t instead" #include "esp32/pm.h" #include "esp_pm.h" -#if HAS_WIFI +#if HAS_WIFI || HAS_ETHERNET_ON_WIFI_STACK #include "mesh/wifi/WiFiAPClient.h" #endif #include "rom/rtc.h" diff --git a/variants/esp32/wt32-eth01-e22/platformio.ini b/variants/esp32/wt32-eth01-e22/platformio.ini new file mode 100644 index 00000000000..9440beaafe9 --- /dev/null +++ b/variants/esp32/wt32-eth01-e22/platformio.ini @@ -0,0 +1,10 @@ +[env:wt32-eth01-e22] +extends = esp32_base +board = esp32dev +upload_speed = 115200 +build_flags = + ${esp32_base.build_flags} + -I variants/esp32/wt32-eth01-e22 + -D WT32_ETH01_E22 + -D HAS_ETHERNET=1 + -D ETH_TYPE=0 \ No newline at end of file diff --git a/variants/esp32/wt32-eth01-e22/variant.h b/variants/esp32/wt32-eth01-e22/variant.h new file mode 100644 index 00000000000..72ff0c1cc32 --- /dev/null +++ b/variants/esp32/wt32-eth01-e22/variant.h @@ -0,0 +1,49 @@ +#pragma once + +// Meshtastic +#define HAS_SCREEN 0 + +// GPS +#define HAS_GPS 0 // This variant does not have GPS support +#undef GPS_RX_PIN +#undef GPS_TX_PIN + +// Ethernet +#define HAS_ETHERNET 1 // This variant has Ethernet support +#define ETH_PHY_TYPE ETH_PHY_LAN8720 // LAN8720 PHY +#define ETH_PHY_ADDR 1 // PHY address +#define ETH_PHY_MDC 23 // MDC pin +#define ETH_PHY_MDIO 18 // MDIO pin +#define ETH_PHY_POWER 16 // Power pin +#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN // Clock mode + +// LoRa +#define USE_SX1262 +#define LORA_SCK 14 // Serial Clock +#define LORA_MISO 12 // Master In Slave Out +#define LORA_MOSI 15 // Master Out Slave In +#define LORA_CS 2 // Chip Select +#define LORA_DIO1 39 // DIO1 pin +#define LORA_RESET 4 // Reset pin +#define LORA_BUSY 36 // Busy pin + +// SX1262 +#define SX126X_CS LORA_CS +#define SX126X_DIO1 LORA_DIO1 +#define SX126X_RESET LORA_RESET +#define SX126X_BUSY LORA_BUSY + +// E22-900M30S +#define SX126X_RXEN 17 +#define SX126X_TXEN RADIOLIB_NC +#define SX126X_DIO2_AS_RF_SWITCH 1 // Uses DIO2 as RF switch control +#define SX126X_DIO3_TCXO_VOLTAGE 1.8 // DIO3 used for TCXO voltage control (1.8V) +#define SX126X_MAX_POWER 22 + +// i2c +#define I2C_SDA 32 // I2C Data +#define I2C_SCL 33 // I2C Clock + +// Button +#define BUTTON_PIN 35 // Button pin +#define BUTTON_NEED_PULLUP 1 // Button needs pull-up resistor \ No newline at end of file