diff --git a/include/hasp_conf.h b/include/hasp_conf.h index 8d9aabbc..ac5cc828 100644 --- a/include/hasp_conf.h +++ b/include/hasp_conf.h @@ -172,6 +172,13 @@ static WiFiSpiClass WiFi; #if HASP_USE_ETHERNET > 0 #if defined(ARDUINO_ARCH_ESP32) + +#if HASP_USE_W5500 > 0 +#include "sys/net/hasp_ethernet_lib.h" + +#warning Using ESP32 Ethernet W5500 + +#else #include #define ETH_ADDR 0 @@ -184,8 +191,10 @@ static WiFiSpiClass WiFi; #include "sys/net/hasp_ethernet_esp32.h" #warning Using ESP32 Ethernet LAN8720 +#endif + +#else // Not ARDUINO_ARCH_ESP32 -#else #if USE_BUILTIN_ETHERNET > 0 #include #include @@ -200,7 +209,8 @@ static WiFiSpiClass WiFi; #endif #include "sys/net/hasp_ethernet_stm32.h" #endif -#endif + +#endif // ARDUINO_ARCH_ESP32 #if HASP_USE_MQTT > 0 #include "mqtt/hasp_mqtt.h" diff --git a/src/hasp_oobe.cpp b/src/hasp_oobe.cpp index 6a298840..b6f8292f 100644 --- a/src/hasp_oobe.cpp +++ b/src/hasp_oobe.cpp @@ -304,7 +304,8 @@ void oobeSetAutoCalibrate(bool cal) bool oobeSetup() { #if HASP_USE_ETHERNET > 0 - if(eth_connected) return false; + // if(eth_connected) + return false; #endif #if HASP_USE_WIFI > 0 char ssid[32]; diff --git a/src/sys/net/hasp_ethernet_esp32.cpp b/src/sys/net/hasp_ethernet_esp32.cpp index b9cb872b..38f49079 100644 --- a/src/sys/net/hasp_ethernet_esp32.cpp +++ b/src/sys/net/hasp_ethernet_esp32.cpp @@ -8,7 +8,7 @@ #include "hal/hasp_hal.h" #include "dev/device.h" -#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) +#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) && !defined(HASP_USE_ETHERNET_LIB) IPAddress ip; diff --git a/src/sys/net/hasp_ethernet_lib.cpp b/src/sys/net/hasp_ethernet_lib.cpp new file mode 100644 index 00000000..f75bd7fb --- /dev/null +++ b/src/sys/net/hasp_ethernet_lib.cpp @@ -0,0 +1,145 @@ +/* MIT License - Copyright (c) 2019-2021 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#include "hasp_conf.h" + +#include "hasp_debug.h" +#include "hal/hasp_hal.h" + +#if HASP_USE_ETHERNET > 0 && HASP_USE_ETHERNET_LIB > 0 + +EthernetClient EthClient; +IPAddress ip; + +void ethernetSetup() +{ +#if USE_BUILTIN_ETHERNET > 0 + // start Ethernet and UDP + LOG_TRACE(TAG_ETH, F("LAN8720 " D_SERVICE_STARTING)); + if(Ethernet.begin() == 0) { + LOG_TRACE(TAG_ETH, F("Failed to configure Ethernet using DHCP")); + eth_connected = false; + } else { + ip = Ethernet.localIP(); + LOG_TRACE(TAG_ETH, F("DHCP Success got IP %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); + eth_connected = true; + } + + LOG_TRACE(TAG_ETH, F("MAC Address %s"), halGetMacAddress(0, ":")); + +#else // Not USE_BUILTIN_ETHERNET + byte mac[6]; +#ifdef STM32 + uint32_t baseUID = (uint32_t)UID_BASE; + mac[0] = 0x00; + mac[1] = 0x80; + mac[2] = 0xE1; + mac[3] = (baseUID & 0x00FF0000) >> 16; + mac[4] = (baseUID & 0x0000FF00) >> 8; + mac[5] = (baseUID & 0x000000FF); + + char ethHostname[12]; + memset(ethHostname, 0, sizeof(ethHostname)); + snprintf_P(ethHostname, sizeof(ethHostname), PSTR("HASP-%02x%02x%02x"), mac[3], mac[4], mac[5]); + + Ethernet.setCsPin(W5500_CS); + Ethernet.setRstPin(W5500_RST); + Ethernet.setHostname(ethHostname); + +#elif defined(ARDUINO_ARCH_ESP32) + esp_read_mac(mac, ESP_MAC_ETH); + Ethernet.init(ETH_CS); // 27 on huzzah32 +#endif + + LOG_TRACE(TAG_ETH, F("W5500 " D_SERVICE_STARTING)); + if(Ethernet.begin(mac) == 0) { + LOG_TRACE(TAG_ETH, F("Failed to configure Ethernet using DHCP")); + } else { + ip = Ethernet.localIP(); + LOG_TRACE(TAG_ETH, F("DHCP Success got IP %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); + } +#endif +} + +void ethernetLoop(void) +{ + switch(Ethernet.maintain()) { + case 1: + // renewed fail + LOG_ERROR(TAG_ETH, F("Error: renewed fail")); + break; + + case 2: + // renewed success + ip = Ethernet.localIP(); + LOG_TRACE(TAG_ETH, F("DHCP Renew Success got IP=%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); + break; + + case 3: + // rebind fail + LOG_ERROR(TAG_ETH, F("Error: rebind fail")); + break; + + case 4: + // rebind success + ip = Ethernet.localIP(); + LOG_TRACE(TAG_ETH, F("DHCP Rebind Success got IP=%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); + break; + + default: + // nothing happened + break; + } +} + +bool ethernetEvery5Seconds() +{ + bool state; +#if USE_BUILTIN_ETHERNET > 0 + state = Ethernet.linkStatus() == LinkON; +#elif HASP_USE_ETHERNET_LIB > 0 + state = Ethernet.linkStatus() == 1; +#else + state = Ethernet.link() == 1; +#endif + LOG_WARNING(TAG_ETH, state ? F(D_NETWORK_ONLINE) : F(D_NETWORK_OFFLINE)); + return state; +} + +void ethernet_get_statusupdate(char* buffer, size_t len) +{ + bool state; +#if USE_BUILTIN_ETHERNET > 0 + state = Ethernet.linkStatus() == LinkON; +#elif HASP_USE_ETHERNET_LIB > 0 + state = Ethernet.linkStatus() == 1; +#else + state = Ethernet.link() == 1; +#endif + + IPAddress ip = Ethernet.localIP(); + snprintf_P(buffer, len, PSTR("\"eth\":\"%s\",\"link\":%d,\"ip\":\"%d.%d.%d.%d\","), state ? F("on") : F("off"), 10, + ip[0], ip[1], ip[2], ip[3]); +} + +void ethernet_get_info(JsonDocument& doc) +{ + char size_buf[32]; + String buffer((char*)0); + buffer.reserve(64); + + JsonObject info = doc.createNestedObject(F(D_INFO_ETHERNET)); + + // buffer = ETH.linkSpeed(); + // buffer += F(" Mbps"); + // if(ETH.fullDuplex()) { + // buffer += F(" " D_INFO_FULL_DUPLEX); + // } + + // info[F(D_INFO_LINK_SPEED)] = buffer; + info[F(D_INFO_IP_ADDRESS)] = Ethernet.localIP().toString(); + info[F(D_INFO_GATEWAY)] = Ethernet.gatewayIP().toString(); + info[F(D_INFO_DNS_SERVER)] = Ethernet.dnsServerIP().toString(); + // info[F(D_INFO_MAC_ADDRESS)] = ETH.macAddress(); +} +#endif \ No newline at end of file diff --git a/src/sys/net/hasp_ethernet_lib.h b/src/sys/net/hasp_ethernet_lib.h new file mode 100644 index 00000000..606643e1 --- /dev/null +++ b/src/sys/net/hasp_ethernet_lib.h @@ -0,0 +1,24 @@ +/* MIT License - Copyright (c) 2019-2021 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_ETHERNET_LIB_H +#define HASP_ETHERNET_LIB_H + +#include +#include "Ethernet.h" +#include "EthernetUdp.h" + +#include "ArduinoJson.h" + +static bool eth_connected = false; + +void ethernetSetup(); +void ethernetLoop(void); + +bool ethernetEverySecond(); +bool ethernetEvery5Seconds(); +void ethernet_get_statusupdate(char* buffer, size_t len); + +void ethernet_get_info(JsonDocument& doc); + +#endif \ No newline at end of file diff --git a/src/sys/svc/hasp_http.cpp b/src/sys/svc/hasp_http.cpp index 136d912f..466b7cb0 100644 --- a/src/sys/svc/hasp_http.cpp +++ b/src/sys/svc/hasp_http.cpp @@ -732,6 +732,16 @@ void webHandleInfo() #endif #if HASP_USE_ETHERNET > 0 #if defined(ARDUINO_ARCH_ESP32) +#if HASP_USE_ETHERNET_LIB > 0 + httpMessage += F("
IP Address: "); + httpMessage += String(Ethernet.localIP().toString()); + httpMessage += F("
Gateway: "); + httpMessage += String(Ethernet.gatewayIP().toString()); + httpMessage += F("
DNS Server: "); + httpMessage += String(Ethernet.dnsServerIP().toString()); + httpMessage += F("
MAC Address: "); + // httpMessage += String(Ethernet.macAddress()); +#else httpMessage += F("

Ethernet: "); httpMessage += String(ETH.linkSpeed()); httpMessage += F(" Mbps"); @@ -747,6 +757,8 @@ void webHandleInfo() httpMessage += F("
MAC Address: "); httpMessage += String(ETH.macAddress()); #endif + +#endif #endif /* Mqtt Stats */ #if HASP_USE_MQTT > 0 @@ -2194,7 +2206,7 @@ void httpStart() #endif #else IPAddress ip; -#if defined(ARDUINO_ARCH_ESP32) +#if defined(ARDUINO_ARCH_ESP32) && !defined(HASP_USE_ETHERNET_LIB) ip = ETH.localIP(); #else ip = Ethernet.localIP(); diff --git a/user_setups/esp32/huzzah32-featherwing-35-poe.ini b/user_setups/esp32/huzzah32-featherwing-35-poe.ini new file mode 100644 index 00000000..83e28a55 --- /dev/null +++ b/user_setups/esp32/huzzah32-featherwing-35-poe.ini @@ -0,0 +1,45 @@ +;***************************************************; +; HUZZAH32 ESP32 with Featherwing TFT 3.5" ; +; - HUZZAH32 esp32 board ; +; - HX8357D TFT Featherwing 3.5" ; +; - STMPE610 touch controller ; +;***************************************************; + +[env:huzzah32-featherwing-35-poe] +extends = esp32 +board = featheresp32 + +build_flags = + ${env.build_flags} + ${esp32.build_flags} + -D HASP_MODEL="Adafruit Featherwing 3.2" + +;region -- TFT_eSPI build options ------------------------ + ${lcd.featherwing-35} + -D TFT_MISO=19 + -D TFT_MOSI=18 + -D TFT_SCLK=5 + -D TFT_DC=33 + -D TFT_CS=15 + -D TFT_RST=-1 ; RST + -D TFT_BCKL=21 ; Solder the LITE pad to a PWM enabled pin of the ESP, like GPIO 21 + -D STMPE_CS=32 +;endregion + +;region -- Hasp build options ---------------------------- + -D HASP_USE_ETHERNET=1 + -D HASP_USE_ETHERNET_LIB=1 + -D HASP_USE_WIFI=0 + -D HASP_USE_W5500=1 + -D ETH_CS=27 +;endregion + +lib_deps = + ${env.lib_deps} + ${esp32.lib_deps} + adafruit/Adafruit STMPE610@^1.1.3 ;STMPE610 touch controller + paulstoffregen/Ethernet + +lib_ignore = + ${env.lib_ignore} + ${esp32.lib_ignore} \ No newline at end of file