Browse Source

Merge PR #181 from tastendruecker123/esp32

ESP32 support added
pull/180/head
Andreas Schiffler 2 years ago
committed by GitHub
parent
commit
76f2de853c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      tools/esp8266/CircularBuffer.h
  2. 24
      tools/esp8266/ahoywifi.cpp
  3. 21
      tools/esp8266/ahoywifi.h
  4. 20
      tools/esp8266/app.cpp
  5. 23
      tools/esp8266/app.h
  6. 14
      tools/esp8266/eep.h
  7. 5
      tools/esp8266/hmInverter.h
  8. 4
      tools/esp8266/include/dbg.h
  9. 17
      tools/esp8266/mqtt.h
  10. 28
      tools/esp8266/platformio.ini
  11. 20
      tools/esp8266/web.cpp
  12. 18
      tools/esp8266/web.h

3
tools/esp8266/CircularBuffer.h

@ -24,6 +24,9 @@
#ifdef ESP8266 #ifdef ESP8266
#define DISABLE_IRQ noInterrupts() #define DISABLE_IRQ noInterrupts()
#define RESTORE_IRQ interrupts() #define RESTORE_IRQ interrupts()
#elif defined(ESP32)
#define DISABLE_IRQ noInterrupts()
#define RESTORE_IRQ interrupts()
#else #else
#define DISABLE_IRQ \ #define DISABLE_IRQ \
uint8_t sreg = SREG; \ uint8_t sreg = SREG; \

24
tools/esp8266/wifi.cpp → tools/esp8266/ahoywifi.cpp

@ -3,7 +3,11 @@
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ // 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 // 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; mMain = main;
mSysCfg = sysCfg; mSysCfg = sysCfg;
mConfig = config; 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; mWifiStationTimeout = timeout;
#ifndef AP_ONLY #ifndef AP_ONLY
if(false == mApActive) 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) { if(mApActive) {
mDns->processNextRequest(); mDns->processNextRequest();
#ifndef AP_ONLY #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")); DPRINTLN(DBG_VERBOSE, F("app::setupAp"));
IPAddress apIp(192, 168, 1, 1); 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")); DPRINTLN(DBG_VERBOSE, F("app::setupStation"));
int32_t cnt; int32_t cnt;
bool startAp = false; bool startAp = false;
@ -166,12 +170,12 @@ bool wifi::setupStation(uint32_t timeout) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool wifi::getApActive(void) { bool ahoywifi::getApActive(void) {
return mApActive; return mApActive;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
time_t wifi::getNtpTime(void) { time_t ahoywifi::getNtpTime(void) {
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime")); //DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
time_t date = 0; time_t date = 0;
IPAddress timeServer; 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")); //DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket"));
uint8_t buf[NTP_PACKET_SIZE] = {0}; 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 // calculates the daylight saving time for middle Europe. Input: Unixtime in UTC
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536 // 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")); //DPRINTLN(DBG_VERBOSE, F("wifi::offsetDayLightSaving"));
int m = month (local_t); int m = month (local_t);
if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez

21
tools/esp8266/wifi.h → tools/esp8266/ahoywifi.h

@ -3,12 +3,17 @@
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef __WIFI_H__ #ifndef __AHOYWIFI_H__
#define __WIFI_H__ #define __AHOYWIFI_H__
#include "dbg.h" #include "dbg.h"
#include <ESP8266WiFi.h> #ifdef ESP8266
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WebServer.h>
#include <WiFi.h>
#endif
// NTP // NTP
#include <WiFiUdp.h> #include <WiFiUdp.h>
@ -21,10 +26,10 @@
class app; class app;
class wifi { class ahoywifi {
public: public:
wifi(app *main, sysConfig_t *sysCfg, config_t *config); ahoywifi(app *main, sysConfig_t *sysCfg, config_t *config);
~wifi() {} ~ahoywifi() {}
void setup(uint32_t timeout, bool settingValid); void setup(uint32_t timeout, bool settingValid);
bool loop(void); bool loop(void);
@ -52,4 +57,4 @@ class wifi {
bool wifiWasEstablished; bool wifiWasEstablished;
}; };
#endif /*__WIFI_H__*/ #endif /*__AHOYWIFI_H__*/

20
tools/esp8266/app.cpp

@ -3,23 +3,24 @@
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ // 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.h"
#include <ArduinoJson.h> #include <ArduinoJson.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
app::app() { app::app() {
Serial.begin(115200);
DPRINTLN(DBG_VERBOSE, F("app::app")); DPRINTLN(DBG_VERBOSE, F("app::app"));
mEep = new eep(); mEep = new eep();
Serial.begin(115200); mWifi = new ahoywifi(this, &mSysConfig, &mConfig);
mWifi = new wifi(this, &mSysConfig, &mConfig);
mWebInst = new web(this, &mSysConfig, &mConfig, mVersion);
mWebInst->setup();
resetSystem(); resetSystem();
loadDefaultConfig(); loadDefaultConfig();
mSys = new HmSystemType(); mSys = new HmSystemType();
} }
@ -39,6 +40,9 @@ void app::setup(uint32_t timeout) {
setupMqtt(); setupMqtt();
#endif #endif
mSys->setup(&mConfig); 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, "/"); const char *token = strtok(topic, "/");
while (token != NULL) while (token != NULL)
{ {
if (std::strcmp(token,"devcontrol")==0){ if (strcmp(token,"devcontrol")==0){
token = strtok(NULL, "/"); token = strtok(NULL, "/");
uint8_t iv_id = std::stoi(token); uint8_t iv_id = std::stoi(token);
if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){ if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){

23
tools/esp8266/app.h

@ -21,7 +21,7 @@
#include "CircularBuffer.h" #include "CircularBuffer.h"
#include "hmSystem.h" #include "hmSystem.h"
#include "mqtt.h" #include "mqtt.h"
#include "wifi.h" #include "ahoywifi.h"
#include "web.h" #include "web.h"
// hier läst sich das Verhalten der app in Bezug auf MQTT // hier läst sich das Verhalten der app in Bezug auf MQTT
@ -56,7 +56,7 @@ typedef struct {
} invPayload_t; } invPayload_t;
class wifi; class ahoywifi;
class web; class web;
class app { class app {
@ -200,10 +200,19 @@ class app {
void stats(void) { void stats(void) {
DPRINTLN(DBG_VERBOSE, F("main.h:stats")); DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free; #ifdef ESP8266
uint16_t max; uint32_t free;
uint8_t frag; uint16_t max;
ESP.getHeapStats(&free, &max, &frag); 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("free: ") + String(free));
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%"); DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag)); DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
@ -224,7 +233,7 @@ class app {
bool mShowRebootRequest; bool mShowRebootRequest;
wifi *mWifi; ahoywifi *mWifi;
web *mWebInst; web *mWebInst;
sysConfig_t mSysConfig; sysConfig_t mSysConfig;
config_t mConfig; config_t mConfig;

14
tools/esp8266/eep.h

@ -8,11 +8,23 @@
#include "Arduino.h" #include "Arduino.h"
#include <EEPROM.h> #include <EEPROM.h>
#ifdef ESP32
#include <nvs_flash.h>
#endif
class eep { class eep {
public: public:
eep() { eep() {
EEPROM.begin(4096);
#ifdef ESP32
if(!EEPROM.begin(4096)) {
nvs_flash_init();
EEPROM.begin(4096);
}
#else
EEPROM.begin(4096);
#endif
} }
~eep() { ~eep() {
EEPROM.end(); EEPROM.end();

5
tools/esp8266/hmInverter.h

@ -6,6 +6,11 @@
#ifndef __HM_INVERTER_H__ #ifndef __HM_INVERTER_H__
#define __HM_INVERTER_H__ #define __HM_INVERTER_H__
#if defined(ESP32) && defined(F)
#undef F
#define F(sl) (sl)
#endif
#include "hmDefines.h" #include "hmDefines.h"
/** /**

4
tools/esp8266/include/dbg.h

@ -5,6 +5,10 @@
#ifndef __DBG_H__ #ifndef __DBG_H__
#define __DBG_H__ #define __DBG_H__
#if defined(ESP32) && defined(F)
#undef F
#define F(sl) (sl)
#endif
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// available levels // available levels

17
tools/esp8266/mqtt.h

@ -6,7 +6,16 @@
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __MQTT_H__ #define __MQTT_H__
#include <ESP8266WiFi.h> #ifdef ESP8266
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#if defined(ESP32) && defined(F)
#undef F
#define F(sl) (sl)
#endif
#include <PubSubClient.h> #include <PubSubClient.h>
#include "defines.h" #include "defines.h"
@ -70,7 +79,11 @@ class mqtt {
void reconnect(void) { void reconnect(void) {
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect")); DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect"));
DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) ); 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; boolean resub = false;
if(!mClient->connected()) { if(!mClient->connected()) {
if(strlen(mDevName) > 0) { if(strlen(mDevName) > 0) {

28
tools/esp8266/platformio.ini

@ -12,10 +12,7 @@
src_dir = . src_dir = .
[env] [env]
platform = espressif8266
framework = arduino framework = arduino
board = esp12e
board_build.f_cpu = 80000000L
; ;;;;; Possible Debug options ;;;;;; ; ;;;;; Possible Debug options ;;;;;;
; https://docs.platformio.org/en/latest/platforms/espressif8266.html#debug-level ; https://docs.platformio.org/en/latest/platforms/espressif8266.html#debug-level
@ -46,17 +43,40 @@ lib_deps =
;esp8266/Ticker@^1.0 ;esp8266/Ticker@^1.0
[env:esp8266-release] [env:esp8266-release]
platform = espressif8266
board = esp12e
board_build.f_cpu = 80000000L
build_flags = -D RELEASE build_flags = -D RELEASE
monitor_filters = monitor_filters =
;default ; Remove typical terminal control codes from input ;default ; Remove typical terminal control codes from input
time ; Add timestamp with milliseconds for each new line 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 ;log2file ; Log data to a file “platformio-device-monitor-*.log” located in the current working directory
[env:esp8266-debug] [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_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 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 = monitor_filters =
;default ; Remove typical terminal control codes from input ;default ; Remove typical terminal control codes from input
time ; Add timestamp with milliseconds for each new line time ; Add timestamp with milliseconds for each new line

20
tools/esp8266/web.cpp

@ -3,6 +3,11 @@
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ // 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 "web.h"
#include "html/h/index_html.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; mSysCfg = sysCfg;
mConfig = config; mConfig = config;
mVersion = version; mVersion = version;
mWeb = new ESP8266WebServer(80); #ifdef ESP8266
mUpdater = new ESP8266HTTPUpdateServer(); mWeb = new ESP8266WebServer(80);
mUpdater = new ESP8266HTTPUpdateServer();
#elif defined(ESP32)
mWeb = new WebServer(80);
mUpdater = new HTTPUpdateServer();
#endif
mUpdater->setup(mWeb); mUpdater->setup(mWeb);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void web::setup(void) { void web::setup(void) {
DPRINTLN(DBG_VERBOSE, F("app::setup-begin"));
mWeb->begin(); mWeb->begin();
DPRINTLN(DBG_VERBOSE, F("app::setup-on"));
mWeb->on("/", std::bind(&web::showIndex, this)); mWeb->on("/", std::bind(&web::showIndex, this));
mWeb->on("/style.css", std::bind(&web::showCss, this)); mWeb->on("/style.css", std::bind(&web::showCss, this));
mWeb->on("/favicon.ico", std::bind(&web::showFavicon, 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 // process payload from web request corresponding to the cmd
if (mMain->mSys->NextInfoCmd == AlarmData) if (mMain->mSys->NextInfoCmd == AlarmData)
iv->alarmMesIndex = response["payload"]; 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 "));
} }

18
tools/esp8266/web.h

@ -7,8 +7,13 @@
#define __WEB_H__ #define __WEB_H__
#include "dbg.h" #include "dbg.h"
#include <ESP8266WebServer.h> #ifdef ESP8266
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#elif defined(ESP32)
#include <WebServer.h>
#include <HTTPUpdateServer.h>
#endif
#include "app.h" #include "app.h"
@ -40,8 +45,13 @@ class web {
void showWebApi(void); void showWebApi(void);
private: private:
ESP8266WebServer *mWeb; #ifdef ESP8266
ESP8266HTTPUpdateServer *mUpdater; ESP8266WebServer *mWeb;
ESP8266HTTPUpdateServer *mUpdater;
#elif defined(ESP32)
WebServer *mWeb;
HTTPUpdateServer *mUpdater;
#endif
config_t *mConfig; config_t *mConfig;
sysConfig_t *mSysCfg; sysConfig_t *mSysCfg;

Loading…
Cancel
Save