From 0ad53d56d8ce27c24635444dbc3adcaf372a3584 Mon Sep 17 00:00:00 2001 From: tastendruecker123 <111116980+tastendruecker123@users.noreply.github.com> Date: Fri, 19 Aug 2022 17:35:10 +0200 Subject: [PATCH] ESP32 support added * ESP32 adjustments, compiles and runs * Changed gitignore to ignore debug log files --- tools/esp8266/CircularBuffer.h | 3 +++ tools/esp8266/{wifi.cpp => ahoywifi.cpp} | 24 +++++++++++-------- tools/esp8266/{wifi.h => ahoywifi.h} | 23 +++++++++++------- tools/esp8266/app.cpp | 20 +++++++++------- tools/esp8266/app.h | 23 ++++++++++++------ tools/esp8266/eep.h | 14 ++++++++++- tools/esp8266/hmInverter.h | 5 ++++ tools/esp8266/include/dbg.h | 4 ++++ tools/esp8266/mqtt.h | 17 ++++++++++++-- tools/esp8266/platformio.ini | 30 ++++++++++++++++++++---- tools/esp8266/scripts/getVersion.py | 2 +- tools/esp8266/web.cpp | 20 +++++++++++++--- tools/esp8266/web.h | 18 ++++++++++---- 13 files changed, 153 insertions(+), 50 deletions(-) rename tools/esp8266/{wifi.cpp => ahoywifi.cpp} (93%) rename tools/esp8266/{wifi.h => ahoywifi.h} (76%) diff --git a/tools/esp8266/CircularBuffer.h b/tools/esp8266/CircularBuffer.h index 8ad3d5df..b784f639 100644 --- a/tools/esp8266/CircularBuffer.h +++ b/tools/esp8266/CircularBuffer.h @@ -24,6 +24,9 @@ #ifdef ESP8266 #define DISABLE_IRQ noInterrupts() #define RESTORE_IRQ interrupts() +#elif defined(ESP32) +#define DISABLE_IRQ noInterrupts() +#define RESTORE_IRQ interrupts() #else #define DISABLE_IRQ \ uint8_t sreg = SREG; \ diff --git a/tools/esp8266/wifi.cpp b/tools/esp8266/ahoywifi.cpp similarity index 93% rename from tools/esp8266/wifi.cpp rename to tools/esp8266/ahoywifi.cpp index f0cc8f84..af1729fa 100644 --- a/tools/esp8266/wifi.cpp +++ b/tools/esp8266/ahoywifi.cpp @@ -3,7 +3,11 @@ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //----------------------------------------------------------------------------- -#include "wifi.h" +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif +#include "ahoywifi.h" // NTP CONFIG @@ -12,7 +16,7 @@ //----------------------------------------------------------------------------- -wifi::wifi(app *main, sysConfig_t *sysCfg, config_t *config) { +ahoywifi::ahoywifi(app *main, sysConfig_t *sysCfg, config_t *config) { mMain = main; mSysCfg = sysCfg; mConfig = config; @@ -29,7 +33,7 @@ wifi::wifi(app *main, sysConfig_t *sysCfg, config_t *config) { //----------------------------------------------------------------------------- -void wifi::setup(uint32_t timeout, bool settingValid) { +void ahoywifi::setup(uint32_t timeout, bool settingValid) { mWifiStationTimeout = timeout; #ifndef AP_ONLY if(false == mApActive) @@ -58,7 +62,7 @@ void wifi::setup(uint32_t timeout, bool settingValid) { //----------------------------------------------------------------------------- -bool wifi::loop(void) { +bool ahoywifi::loop(void) { if(mApActive) { mDns->processNextRequest(); #ifndef AP_ONLY @@ -98,7 +102,7 @@ bool wifi::loop(void) { //----------------------------------------------------------------------------- -void wifi::setupAp(const char *ssid, const char *pwd) { +void ahoywifi::setupAp(const char *ssid, const char *pwd) { DPRINTLN(DBG_VERBOSE, F("app::setupAp")); IPAddress apIp(192, 168, 1, 1); @@ -118,7 +122,7 @@ void wifi::setupAp(const char *ssid, const char *pwd) { //----------------------------------------------------------------------------- -bool wifi::setupStation(uint32_t timeout) { +bool ahoywifi::setupStation(uint32_t timeout) { DPRINTLN(DBG_VERBOSE, F("app::setupStation")); int32_t cnt; bool startAp = false; @@ -166,12 +170,12 @@ bool wifi::setupStation(uint32_t timeout) { //----------------------------------------------------------------------------- -bool wifi::getApActive(void) { +bool ahoywifi::getApActive(void) { return mApActive; } //----------------------------------------------------------------------------- -time_t wifi::getNtpTime(void) { +time_t ahoywifi::getNtpTime(void) { //DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime")); time_t date = 0; IPAddress timeServer; @@ -209,7 +213,7 @@ time_t wifi::getNtpTime(void) { //----------------------------------------------------------------------------- -void wifi::sendNTPpacket(IPAddress& address) { +void ahoywifi::sendNTPpacket(IPAddress& address) { //DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket")); uint8_t buf[NTP_PACKET_SIZE] = {0}; @@ -232,7 +236,7 @@ void wifi::sendNTPpacket(IPAddress& address) { //----------------------------------------------------------------------------- // calculates the daylight saving time for middle Europe. Input: Unixtime in UTC // from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536 -time_t wifi::offsetDayLightSaving (uint32_t local_t) { +time_t ahoywifi::offsetDayLightSaving (uint32_t local_t) { //DPRINTLN(DBG_VERBOSE, F("wifi::offsetDayLightSaving")); int m = month (local_t); if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez diff --git a/tools/esp8266/wifi.h b/tools/esp8266/ahoywifi.h similarity index 76% rename from tools/esp8266/wifi.h rename to tools/esp8266/ahoywifi.h index 4a1bc32e..5e79568f 100644 --- a/tools/esp8266/wifi.h +++ b/tools/esp8266/ahoywifi.h @@ -3,12 +3,17 @@ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //----------------------------------------------------------------------------- -#ifndef __WIFI_H__ -#define __WIFI_H__ +#ifndef __AHOYWIFI_H__ +#define __AHOYWIFI_H__ #include "dbg.h" -#include -#include +#ifdef ESP8266 + #include + #include +#elif defined(ESP32) + #include + #include +#endif // NTP #include @@ -21,10 +26,10 @@ class app; -class wifi { +class ahoywifi { public: - wifi(app *main, sysConfig_t *sysCfg, config_t *config); - ~wifi() {} + ahoywifi(app *main, sysConfig_t *sysCfg, config_t *config); + ~ahoywifi() {} void setup(uint32_t timeout, bool settingValid); bool loop(void); @@ -32,7 +37,7 @@ class wifi { bool setupStation(uint32_t timeout); bool getApActive(void); time_t getNtpTime(void); - + private: void sendNTPpacket(IPAddress& address); time_t offsetDayLightSaving (uint32_t local_t); @@ -52,4 +57,4 @@ class wifi { bool wifiWasEstablished; }; -#endif /*__WIFI_H__*/ +#endif /*__AHOYWIFI_H__*/ diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index b60c004e..ccb0d037 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -3,23 +3,24 @@ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //----------------------------------------------------------------------------- +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif + #include "app.h" #include //----------------------------------------------------------------------------- app::app() { + Serial.begin(115200); DPRINTLN(DBG_VERBOSE, F("app::app")); mEep = new eep(); - Serial.begin(115200); - - mWifi = new wifi(this, &mSysConfig, &mConfig); - - mWebInst = new web(this, &mSysConfig, &mConfig, mVersion); - mWebInst->setup(); + mWifi = new ahoywifi(this, &mSysConfig, &mConfig); resetSystem(); - loadDefaultConfig(); + loadDefaultConfig(); mSys = new HmSystemType(); } @@ -39,6 +40,9 @@ void app::setup(uint32_t timeout) { setupMqtt(); #endif mSys->setup(&mConfig); + + mWebInst = new web(this, &mSysConfig, &mConfig, mVersion); + mWebInst->setup(); } //----------------------------------------------------------------------------- @@ -430,7 +434,7 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) { const char *token = strtok(topic, "/"); while (token != NULL) { - if (std::strcmp(token,"devcontrol")==0){ + if (strcmp(token,"devcontrol")==0){ token = strtok(NULL, "/"); uint8_t iv_id = std::stoi(token); if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){ diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 00967f76..0e09701a 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -21,7 +21,7 @@ #include "CircularBuffer.h" #include "hmSystem.h" #include "mqtt.h" -#include "wifi.h" +#include "ahoywifi.h" #include "web.h" // hier läst sich das Verhalten der app in Bezug auf MQTT @@ -56,7 +56,7 @@ typedef struct { } invPayload_t; -class wifi; +class ahoywifi; class web; class app { @@ -200,10 +200,19 @@ class app { void stats(void) { DPRINTLN(DBG_VERBOSE, F("main.h:stats")); - uint32_t free; - uint16_t max; - uint8_t frag; - ESP.getHeapStats(&free, &max, &frag); + #ifdef ESP8266 + uint32_t free; + uint16_t max; + uint8_t frag; + ESP.getHeapStats(&free, &max, &frag); + #elif defined(ESP32) + uint32_t free; + uint32_t max; + uint8_t frag; + free = ESP.getFreeHeap(); + max = ESP.getMaxAllocHeap(); + frag = 0; + #endif DPRINT(DBG_VERBOSE, F("free: ") + String(free)); DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%"); DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag)); @@ -224,7 +233,7 @@ class app { bool mShowRebootRequest; - wifi *mWifi; + ahoywifi *mWifi; web *mWebInst; sysConfig_t mSysConfig; config_t mConfig; diff --git a/tools/esp8266/eep.h b/tools/esp8266/eep.h index b66d2cf7..69aff7f5 100644 --- a/tools/esp8266/eep.h +++ b/tools/esp8266/eep.h @@ -8,11 +8,23 @@ #include "Arduino.h" #include +#ifdef ESP32 + #include +#endif class eep { public: eep() { - EEPROM.begin(4096); + + #ifdef ESP32 + if(!EEPROM.begin(4096)) { + nvs_flash_init(); + EEPROM.begin(4096); + } + #else + EEPROM.begin(4096); + #endif + } ~eep() { EEPROM.end(); diff --git a/tools/esp8266/hmInverter.h b/tools/esp8266/hmInverter.h index 3b443873..be67bc9d 100644 --- a/tools/esp8266/hmInverter.h +++ b/tools/esp8266/hmInverter.h @@ -6,6 +6,11 @@ #ifndef __HM_INVERTER_H__ #define __HM_INVERTER_H__ +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif + #include "hmDefines.h" /** diff --git a/tools/esp8266/include/dbg.h b/tools/esp8266/include/dbg.h index 1b11f959..8eb41f7c 100644 --- a/tools/esp8266/include/dbg.h +++ b/tools/esp8266/include/dbg.h @@ -5,6 +5,10 @@ #ifndef __DBG_H__ #define __DBG_H__ +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif //----------------------------------------------------------------------------- // available levels diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 38999dc4..74193afa 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -6,7 +6,16 @@ #ifndef __MQTT_H__ #define __MQTT_H__ -#include +#ifdef ESP8266 + #include +#elif defined(ESP32) + #include +#endif + +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif #include #include "defines.h" @@ -70,7 +79,11 @@ class mqtt { void reconnect(void) { DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect")); DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) ); - DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) ); + + #ifdef ESP8266 + DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) ); + #endif + boolean resub = false; if(!mClient->connected()) { if(strlen(mDevName) > 0) { diff --git a/tools/esp8266/platformio.ini b/tools/esp8266/platformio.ini index 10d2a679..ee0ad242 100644 --- a/tools/esp8266/platformio.ini +++ b/tools/esp8266/platformio.ini @@ -12,10 +12,7 @@ src_dir = . [env] -platform = espressif8266 framework = arduino -board = esp12e -board_build.f_cpu = 80000000L ; ;;;;; Possible Debug options ;;;;;; ; https://docs.platformio.org/en/latest/platforms/espressif8266.html#debug-level @@ -46,18 +43,41 @@ lib_deps = ;esp8266/Ticker@^1.0 [env:esp8266-release] +platform = espressif8266 +board = esp12e +board_build.f_cpu = 80000000L build_flags = -D RELEASE monitor_filters = ;default ; Remove typical terminal control codes from input time ; Add timestamp with milliseconds for each new line ;log2file ; Log data to a file “platformio-device-monitor-*.log” located in the current working directory - [env:esp8266-debug] +platform = espressif8266 +board = esp12e +board_build.f_cpu = 80000000L build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_OOM -DDEBUG_ESP_PORT=Serial build_type = debug - monitor_filters = ;default ; Remove typical terminal control codes from input time ; Add timestamp with milliseconds for each new line log2file ; Log data to a file “platformio-device-monitor-*.log” located in the current working directory + +[env:esp32-wroom32-release] +platform = espressif32 +board = lolin_d32 +build_flags = -D RELEASE +monitor_filters = + ;default ; Remove typical terminal control codes from input + time ; Add timestamp with milliseconds for each new line + ;log2file ; Log data to a file “platformio-device-monitor-*.log” located in the current working directory + +[env:esp32-wroom32-debug] +platform = espressif32 +board = lolin_d32 +build_flags = -DDEBUG_ESP_CORE -DDEBUG_ESP_WIFI -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP_HTTP_SERVER -DDEBUG_ESP_OOM -DDEBUG_ESP_PORT=Serial +build_type = debug +monitor_filters = + ;default ; Remove typical terminal control codes from input + time ; Add timestamp with milliseconds for each new line + log2file ; Log data to a file “platformio-device-monitor-*.log” located in the current working directory \ No newline at end of file diff --git a/tools/esp8266/scripts/getVersion.py b/tools/esp8266/scripts/getVersion.py index cbdfba6e..9907eb55 100644 --- a/tools/esp8266/scripts/getVersion.py +++ b/tools/esp8266/scripts/getVersion.py @@ -27,4 +27,4 @@ def readVersion(path, infile): print("::set-output name=name::" + versionnumber[:-1] ) -readVersion("../", "defines.h") \ No newline at end of file +readVersion("../", "defines.h") diff --git a/tools/esp8266/web.cpp b/tools/esp8266/web.cpp index b926411a..1fdaf76b 100644 --- a/tools/esp8266/web.cpp +++ b/tools/esp8266/web.cpp @@ -3,6 +3,11 @@ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //----------------------------------------------------------------------------- +#if defined(ESP32) && defined(F) + #undef F + #define F(sl) (sl) +#endif + #include "web.h" #include "html/h/index_html.h" @@ -17,15 +22,22 @@ web::web(app *main, sysConfig_t *sysCfg, config_t *config, char version[]) { mSysCfg = sysCfg; mConfig = config; mVersion = version; - mWeb = new ESP8266WebServer(80); - mUpdater = new ESP8266HTTPUpdateServer(); + #ifdef ESP8266 + mWeb = new ESP8266WebServer(80); + mUpdater = new ESP8266HTTPUpdateServer(); + #elif defined(ESP32) + mWeb = new WebServer(80); + mUpdater = new HTTPUpdateServer(); + #endif mUpdater->setup(mWeb); } //----------------------------------------------------------------------------- void web::setup(void) { + DPRINTLN(DBG_VERBOSE, F("app::setup-begin")); mWeb->begin(); + DPRINTLN(DBG_VERBOSE, F("app::setup-on")); mWeb->on("/", std::bind(&web::showIndex, this)); mWeb->on("/style.css", std::bind(&web::showCss, this)); mWeb->on("/favicon.ico", std::bind(&web::showFavicon, this)); @@ -441,7 +453,9 @@ void web::showWebApi(void) // process payload from web request corresponding to the cmd if (mMain->mSys->NextInfoCmd == AlarmData) iv->alarmMesIndex = response["payload"]; - DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(mMain->mSys->InfoCmd) + F(" and payload ") + String(response["payload"])); + DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(mMain->mSys->InfoCmd) + F(" and payload ") + String((uint16_t) response["payload"])); + //DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(mMain->mSys->InfoCmd) + F(" and payload ")); + } diff --git a/tools/esp8266/web.h b/tools/esp8266/web.h index 0199063d..ad9aa7c6 100644 --- a/tools/esp8266/web.h +++ b/tools/esp8266/web.h @@ -7,8 +7,13 @@ #define __WEB_H__ #include "dbg.h" -#include -#include +#ifdef ESP8266 + #include + #include +#elif defined(ESP32) + #include + #include +#endif #include "app.h" @@ -40,8 +45,13 @@ class web { void showWebApi(void); private: - ESP8266WebServer *mWeb; - ESP8266HTTPUpdateServer *mUpdater; + #ifdef ESP8266 + ESP8266WebServer *mWeb; + ESP8266HTTPUpdateServer *mUpdater; + #elif defined(ESP32) + WebServer *mWeb; + HTTPUpdateServer *mUpdater; + #endif config_t *mConfig; sysConfig_t *mSysCfg;