diff --git a/src/app.h b/src/app.h index b3e5cffa..05b11469 100644 --- a/src/app.h +++ b/src/app.h @@ -39,7 +39,11 @@ #if defined(ETHERNET) #include "network/AhoyEthernet.h" #else /* defined(ETHERNET) */ - #include "network/AhoyWifiEsp32.h" + #if defined(ESP32) + #include "network/AhoyWifiEsp32.h" + #else + #include "network/AhoyWifiEsp8266.h" + #endif #include "utils/improv.h" #endif /* defined(ETHERNET) */ diff --git a/src/network/AhoyNetwork.h b/src/network/AhoyNetwork.h index 952206f0..7c51c38d 100644 --- a/src/network/AhoyNetwork.h +++ b/src/network/AhoyNetwork.h @@ -7,18 +7,11 @@ #define __AHOY_NETWORK_H__ #include "AhoyNetworkHelper.h" -#include #include "../config/settings.h" #include "../utils/helper.h" #include "AhoyWifiAp.h" #include "AsyncJson.h" -#if defined(ESP32) -#include -#else -#include -#endif - #define NTP_PACKET_SIZE 48 class AhoyNetwork { @@ -46,15 +39,15 @@ class AhoyNetwork { #else wifiConnectHandler = WiFi.onStationModeConnected( [this](const WiFiEventStationModeConnected& event) -> void { - OnEvent(SYSTEM_EVENT_STA_CONNECTED); + OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_CONNECTED); }); wifiGotIPHandler = WiFi.onStationModeGotIP( [this](const WiFiEventStationModeGotIP& event) -> void { - OnEvent(SYSTEM_EVENT_STA_GOT_IP); + OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_GOT_IP); }); wifiDisconnectHandler = WiFi.onStationModeDisconnected( [this](const WiFiEventStationModeDisconnected& event) -> void { - OnEvent(SYSTEM_EVENT_STA_DISCONNECTED); + OnEvent((WiFiEvent_t)SYSTEM_EVENT_STA_DISCONNECTED); }); #endif } @@ -194,8 +187,14 @@ class AhoyNetwork { NetworkState mStatus = NetworkState::DISCONNECTED; AhoyWifiAp mAp; - AsyncUDP mUdp; // for time server DNSServer mDns; + + #if defined(ESP32) + AsyncUDP mUdp; // for time server + #else + WiFiUDP mUdp; // for time server + WiFiEventHandler wifiConnectHandler, wifiDisconnectHandler, wifiGotIPHandler; + #endif }; #endif /*__AHOY_NETWORK_H__*/ diff --git a/src/network/AhoyNetworkHelper.h b/src/network/AhoyNetworkHelper.h index f814f1db..33f71029 100644 --- a/src/network/AhoyNetworkHelper.h +++ b/src/network/AhoyNetworkHelper.h @@ -8,8 +8,28 @@ #include "../utils/dbg.h" #include -#include -#include +#if defined(ESP32) + #include + #include + #include +#else + #include + #include + //#include + #include "ESPAsyncUDP.h" + + enum { + SYSTEM_EVENT_STA_CONNECTED = 1, + ARDUINO_EVENT_ETH_CONNECTED, + SYSTEM_EVENT_STA_GOT_IP, + ARDUINO_EVENT_ETH_GOT_IP, + ARDUINO_EVENT_WIFI_STA_LOST_IP, + ARDUINO_EVENT_WIFI_STA_STOP, + SYSTEM_EVENT_STA_DISCONNECTED, + ARDUINO_EVENT_ETH_STOP, + ARDUINO_EVENT_ETH_DISCONNECTED + }; +#endif #include namespace ah { diff --git a/src/network/AhoyWifiEsp32.h b/src/network/AhoyWifiEsp32.h index 53d39af3..f9fc064a 100644 --- a/src/network/AhoyWifiEsp32.h +++ b/src/network/AhoyWifiEsp32.h @@ -3,8 +3,8 @@ // Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed //----------------------------------------------------------------------------- -#ifndef __AHOY_Wifi_ESP32_H__ -#define __AHOY_Wifi_ESP32_H__ +#ifndef __AHOY_WIFI_ESP32_H__ +#define __AHOY_WIFI_ESP32_H__ #if defined(ESP32) && !defined(ETHERNET) #include @@ -109,4 +109,4 @@ class AhoyWifi : public AhoyNetwork { }; #endif /*ESP32 & !ETHERNET*/ -#endif /*__AHOY_Wifi_ESP32_H__*/ +#endif /*__AHOY_WIFI_ESP32_H__*/ diff --git a/src/network/AhoyWifiEsp8266.h b/src/network/AhoyWifiEsp8266.h new file mode 100644 index 00000000..1a068797 --- /dev/null +++ b/src/network/AhoyWifiEsp8266.h @@ -0,0 +1,110 @@ +//----------------------------------------------------------------------------- +// 2024 Ahoy, https://ahoydtu.de +// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed +//----------------------------------------------------------------------------- + +#ifndef __AHOY_WIFI_ESP8266_H__ +#define __AHOY_WIFI_ESP8266_H__ + +#if defined(ESP8266) +#include +#include +#include "AhoyNetwork.h" +#include "ESPAsyncWebServer.h" + +class AhoyWifi : public AhoyNetwork { + public: + void begin() override { + mAp.enable(); + + // static IP + setupIp([this](IPAddress ip, IPAddress gateway, IPAddress mask, IPAddress dns1, IPAddress dns2) -> bool { + return WiFi.config(ip, gateway, mask, dns1, dns2); + }); + + WiFi.setHostname(mConfig->sys.deviceName); + WiFi.begin(mConfig->sys.stationSsid, mConfig->sys.stationPwd); + + DBGPRINT(F("connect to network '")); Serial.flush(); + DBGPRINT(mConfig->sys.stationSsid); + } + + void tickNetworkLoop() override { + if(mAp.isEnabled()) + mAp.tickLoop(); + + switch(mStatus) { + case NetworkState::DISCONNECTED: + if(mConnected) { + mConnected = false; + mOnNetworkCB(false); + mAp.enable(); + } + + if (WiFi.softAPgetStationNum() > 0) { + DBGPRINTLN(F("AP client connected")); + } + break; + + case NetworkState::CONNECTED: + break; + + case NetworkState::GOT_IP: + if(!mConnected) { + mAp.disable(); + mConnected = true; + ah::welcome(WiFi.localIP().toString(), F("Station")); + MDNS.begin(mConfig->sys.deviceName); + mOnNetworkCB(true); + } + break; + } + } + + String getIp(void) override { + return WiFi.localIP().toString(); + } + + void scanAvailNetworks(void) override { + if(!mScanActive) { + mScanActive = true; + WiFi.scanNetworks(true); + } + } + + bool getAvailNetworks(JsonObject obj) override { + JsonArray nets = obj.createNestedArray(F("networks")); + + int n = WiFi.scanComplete(); + if (n < 0) + return false; + if(n > 0) { + int sort[n]; + sortRSSI(&sort[0], n); + for (int i = 0; i < n; ++i) { + nets[i][F("ssid")] = WiFi.SSID(sort[i]); + nets[i][F("rssi")] = WiFi.RSSI(sort[i]); + } + } + mScanActive = false; + WiFi.scanDelete(); + + return true; + } + + private: + void sortRSSI(int *sort, int n) { + for (int i = 0; i < n; i++) + sort[i] = i; + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (WiFi.RSSI(sort[j]) > WiFi.RSSI(sort[i])) + std::swap(sort[i], sort[j]); + } + + private: + bool mScanActive = false; +}; + +#endif /*ESP8266*/ +#endif /*__AHOY_WIFI_ESP8266_H__*/ diff --git a/src/platformio.ini b/src/platformio.ini index 20f8005a..de9028d0 100644 --- a/src/platformio.ini +++ b/src/platformio.ini @@ -33,6 +33,7 @@ lib_deps = https://github.com/JChristensen/Timezone @ ^1.2.4 olikraus/U8g2 @ ^2.35.9 https://github.com/zinggjm/GxEPD2#1.5.3 + build_flags = -std=c++17 -std=gnu++17 @@ -44,6 +45,9 @@ build_unflags = platform = espressif8266 board = esp12e board_build.f_cpu = 80000000L +lib_deps = + ${env.lib_deps} + https://github.com/me-no-dev/ESPAsyncUDP build_flags = ${env.build_flags} -DEMC_MIN_FREE_MEMORY=4096 -DENABLE_MQTT @@ -156,33 +160,28 @@ build_flags = ${env.build_flags} monitor_filters = esp8266_exception_decoder -[env:esp32-wroom32] +[env:esp32-wroom32-minimal] platform = espressif32@6.5.0 board = lolin_d32 build_flags = ${env.build_flags} -DUSE_HSPI_FOR_EPD - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY monitor_filters = esp32_exception_decoder -[env:esp32-wroom32-minimal] +[env:esp32-wroom32] platform = espressif32@6.5.0 board = lolin_d32 -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD +build_flags = ${env:esp32-wroom32-minimal.build_flags} + -DENABLE_MQTT + -DPLUGIN_DISPLAY + -DENABLE_HISTORY monitor_filters = esp32_exception_decoder [env:esp32-wroom32-de] platform = espressif32@6.5.0 board = lolin_d32 -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY +build_flags = ${env:esp32-wroom32.build_flags} -DLANG_DE monitor_filters = esp32_exception_decoder @@ -190,38 +189,24 @@ monitor_filters = [env:esp32-wroom32-prometheus] platform = espressif32@6.5.0 board = lolin_d32 -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD +build_flags = ${env:esp32-wroom32.build_flags} -DENABLE_PROMETHEUS_EP - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY monitor_filters = esp32_exception_decoder [env:esp32-wroom32-prometheus-de] platform = espressif32@6.5.0 board = lolin_d32 -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD +build_flags = ${env:esp32-wroom32-prometheus.build_flags} -DLANG_DE - -DENABLE_PROMETHEUS_EP - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY monitor_filters = esp32_exception_decoder [env:esp32-wroom32-ethernet] platform = espressif32 board = lolin_d32 -build_flags = ${env.build_flags} +build_flags = ${env:esp32-wroom32.build_flags} -DETHERNET - -DRELEASE - -DUSE_HSPI_FOR_EPD - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY -DDEF_ETH_CS_PIN=15 -DDEF_ETH_SCK_PIN=14 -DDEF_ETH_MISO_PIN=12 @@ -240,26 +225,8 @@ monitor_filters = [env:esp32-wroom32-ethernet-de] platform = espressif32 board = lolin_d32 -build_flags = ${env.build_flags} - -DETHERNET - -DRELEASE - -DUSE_HSPI_FOR_EPD +build_flags = ${env:esp32-wroom32-ethernet.build_flags} -DLANG_DE - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY - -DDEF_ETH_CS_PIN=15 - -DDEF_ETH_SCK_PIN=14 - -DDEF_ETH_MISO_PIN=12 - -DDEF_ETH_MOSI_PIN=13 - -DDEF_ETH_IRQ_PIN=4 - -DDEF_ETH_RST_PIN=2 - -DDEF_NRF_CS_PIN=5 - -DDEF_NRF_CE_PIN=17 - -DDEF_NRF_IRQ_PIN=16 - -DDEF_NRF_MISO_PIN=19 - -DDEF_NRF_MOSI_PIN=23 - -DDEF_NRF_SCLK_PIN=18 monitor_filters = esp32_exception_decoder @@ -288,22 +255,7 @@ monitor_filters = [env:esp32-s2-mini-de] platform = espressif32@6.5.0 board = lolin_s2_mini -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY - -DDEF_NRF_CS_PIN=12 - -DDEF_NRF_CE_PIN=3 - -DDEF_NRF_IRQ_PIN=5 - -DDEF_NRF_MISO_PIN=9 - -DDEF_NRF_MOSI_PIN=11 - -DDEF_NRF_SCLK_PIN=7 - -DDEF_CMT_CSB=16 - -DDEF_CMT_FCSB=18 - -DDEF_CMT_IRQ=33 - -DDEF_CMT_SDIO=35 - -DDEF_CMT_SCLK=37 +build_flags = ${env:esp32-s2-mini.build_flags} -DLANG_DE monitor_filters = esp32_exception_decoder @@ -333,34 +285,16 @@ monitor_filters = [env:esp32-c3-mini-de] platform = espressif32@6.5.0 board = lolin_c3_mini -build_flags = ${env.build_flags} - -DUSE_HSPI_FOR_EPD - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY - -DDEF_NRF_CS_PIN=5 - -DDEF_NRF_CE_PIN=0 - -DDEF_NRF_IRQ_PIN=1 - -DDEF_NRF_MISO_PIN=3 - -DDEF_NRF_MOSI_PIN=4 - -DDEF_NRF_SCLK_PIN=2 - -DDEF_CMT_CSB=255 - -DDEF_CMT_FCSB=255 - -DDEF_CMT_IRQ=255 - -DDEF_CMT_SDIO=255 - -DDEF_CMT_SCLK=255 +build_flags = ${env:esp32-c3-mini.build_flags} -DLANG_DE monitor_filters = esp32_exception_decoder -[env:opendtufusion] +[env:opendtufusion-minimal] platform = espressif32@6.5.0 board = esp32-s3-devkitc-1 upload_protocol = esp-builtin build_flags = ${env.build_flags} - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY -DSPI_HAL -DDEF_NRF_CS_PIN=37 -DDEF_NRF_CE_PIN=38 @@ -381,75 +315,32 @@ build_flags = ${env.build_flags} monitor_filters = esp32_exception_decoder, colorize -[env:opendtufusion-de] +[env:opendtufusion] platform = espressif32@6.5.0 board = esp32-s3-devkitc-1 upload_protocol = esp-builtin -build_flags = ${env.build_flags} - -DLANG_DE +build_flags = ${env:opendtufusion-minimal.build_flags} -DENABLE_MQTT -DPLUGIN_DISPLAY -DENABLE_HISTORY - -DSPI_HAL - -DDEF_NRF_CS_PIN=37 - -DDEF_NRF_CE_PIN=38 - -DDEF_NRF_IRQ_PIN=47 - -DDEF_NRF_MISO_PIN=48 - -DDEF_NRF_MOSI_PIN=35 - -DDEF_NRF_SCLK_PIN=36 - -DDEF_CMT_CSB=4 - -DDEF_CMT_FCSB=21 - -DDEF_CMT_IRQ=8 - -DDEF_CMT_SDIO=5 - -DDEF_CMT_SCLK=6 - -DDEF_LED0=18 - -DDEF_LED1=17 - -DLED_ACTIVE_HIGH - -DARDUINO_USB_MODE=1 monitor_filters = esp32_exception_decoder, colorize -[env:opendtufusion-minimal] +[env:opendtufusion-de] platform = espressif32@6.5.0 board = esp32-s3-devkitc-1 upload_protocol = esp-builtin -build_flags = ${env.build_flags} - -DSPI_HAL - -DDEF_NRF_CS_PIN=37 - -DDEF_NRF_CE_PIN=38 - -DDEF_NRF_IRQ_PIN=47 - -DDEF_NRF_MISO_PIN=48 - -DDEF_NRF_MOSI_PIN=35 - -DDEF_NRF_SCLK_PIN=36 - -DDEF_CMT_CSB=4 - -DDEF_CMT_FCSB=21 - -DDEF_CMT_IRQ=8 - -DDEF_CMT_SDIO=5 - -DDEF_CMT_SCLK=6 - -DDEF_LED0=18 - -DDEF_LED1=17 - -DLED_ACTIVE_HIGH - -DARDUINO_USB_MODE=1 +build_flags = ${env:opendtufusion.build_flags} + -DLANG_DE monitor_filters = esp32_exception_decoder, colorize [env:opendtufusion-ethernet] platform = espressif32@6.5.0 board = esp32-s3-devkitc-1 -#lib_deps = -# khoih-prog/AsyncWebServer_ESP32_W5500 -# khoih-prog/AsyncUDP_ESP32_W5500 -# https://github.com/nrf24/RF24 @ ^1.4.8 -# paulstoffregen/Time @ ^1.6.1 -# https://github.com/bertmelis/espMqttClient#v1.6.0 -# bblanchon/ArduinoJson @ ^6.21.3 -# https://github.com/JChristensen/Timezone @ ^1.2.4 -# olikraus/U8g2 @ ^2.35.9 -# https://github.com/zinggjm/GxEPD2#1.5.3 upload_protocol = esp-builtin -build_flags = ${env.build_flags} +build_flags = ${env:opendtufusion-minimal.build_flags} -DETHERNET - -DSPI_HAL -DENABLE_MQTT -DPLUGIN_DISPLAY -DENABLE_HISTORY @@ -459,67 +350,14 @@ build_flags = ${env.build_flags} -DDEF_ETH_MOSI_PIN=40 -DDEF_ETH_IRQ_PIN=44 -DDEF_ETH_RST_PIN=43 - -DDEF_NRF_CS_PIN=37 - -DDEF_NRF_CE_PIN=38 - -DDEF_NRF_IRQ_PIN=47 - -DDEF_NRF_MISO_PIN=48 - -DDEF_NRF_MOSI_PIN=35 - -DDEF_NRF_SCLK_PIN=36 - -DDEF_CMT_CSB=4 - -DDEF_CMT_FCSB=21 - -DDEF_CMT_IRQ=8 - -DDEF_CMT_SDIO=5 - -DDEF_CMT_SCLK=6 - -DDEF_LED0=18 - -DDEF_LED1=17 - -DLED_ACTIVE_HIGH - -DARDUINO_USB_MODE=1 - #-DARDUINO_USB_CDC_ON_BOOT=1 monitor_filters = esp32_exception_decoder, colorize [env:opendtufusion-ethernet-de] platform = espressif32@6.5.0 board = esp32-s3-devkitc-1 -#lib_deps = -# khoih-prog/AsyncWebServer_ESP32_W5500 -# khoih-prog/AsyncUDP_ESP32_W5500 -# https://github.com/nrf24/RF24 @ ^1.4.8 -# paulstoffregen/Time @ ^1.6.1 -# https://github.com/bertmelis/espMqttClient#v1.6.0 -# bblanchon/ArduinoJson @ ^6.21.3 -# https://github.com/JChristensen/Timezone @ ^1.2.4 -# olikraus/U8g2 @ ^2.35.9 -# https://github.com/zinggjm/GxEPD2#1.5.3 upload_protocol = esp-builtin -build_flags = ${env.build_flags} - -DETHERNET - -DSPI_HAL +build_flags = ${env:opendtufusion-ethernet.build_flags} -DLANG_DE - -DENABLE_MQTT - -DPLUGIN_DISPLAY - -DENABLE_HISTORY - -DDEF_ETH_CS_PIN=42 - -DDEF_ETH_SCK_PIN=39 - -DDEF_ETH_MISO_PIN=41 - -DDEF_ETH_MOSI_PIN=40 - -DDEF_ETH_IRQ_PIN=44 - -DDEF_ETH_RST_PIN=43 - -DDEF_NRF_CS_PIN=37 - -DDEF_NRF_CE_PIN=38 - -DDEF_NRF_IRQ_PIN=47 - -DDEF_NRF_MISO_PIN=48 - -DDEF_NRF_MOSI_PIN=35 - -DDEF_NRF_SCLK_PIN=36 - -DDEF_CMT_CSB=4 - -DDEF_CMT_FCSB=21 - -DDEF_CMT_IRQ=8 - -DDEF_CMT_SDIO=5 - -DDEF_CMT_SCLK=6 - -DDEF_LED0=18 - -DDEF_LED1=17 - -DLED_ACTIVE_HIGH - -DARDUINO_USB_MODE=1 - #-DARDUINO_USB_CDC_ON_BOOT=1 monitor_filters = esp32_exception_decoder, colorize