Skip to content

Commit

Permalink
- change blocking mqtt and WiFi reconnect handling to non-blocking im…
Browse files Browse the repository at this point in the history
…plementation #33

- fix some other small things
  • Loading branch information
dewenni committed May 15, 2023
1 parent 21068cf commit 2ba571b
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 113 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ But there is also a build in WebUI to view and control your Logamatic without an
- [Platform-IO](#platform-io)
- [ESP-Flash-Tool](#esp-flash-tool)
- [OTA-Updates](#ota-updates)
- [Setup-Mode](#setup-mode)
- [Configuration](#configuration)
- [MQTT](#mqtt)
- [Config and Status values](#config-and-status-values)
Expand Down Expand Up @@ -144,7 +145,7 @@ There are 3 predefined Options:
- OPTION 2: standard wireless OTA Update ArduinoOTA (use this to update from 2.x to 3.x)
- OPTION 3: wireless OTA Update AsyncElegantOTA (use this from 3.x)

## ⚠️ Setup Mode ⚠️
## Setup Mode

There is a new "Setup Mode" available. The "Setup Mode" is activated, when you press the "reset-button" of the ESP. Press reset and after 5 seconds, press reset again.
The "Setup Mode" will also activated if there is no wifi connection configured.
Expand Down
20 changes: 19 additions & 1 deletion changelog
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,22 @@ see all details of the first V3 release here: [Release v3.0.0](https://github.co

- mqtt option to set config messages as retain #28
- change format specifier for printf #27
- fix some typos in [param.txt](Doc/param.txt)
- fix some typos in [param.txt](Doc/param.txt)

---

# v3.2.1

## what's new

Handling for WiFi and mqtt redesigned. Blocking while-loop with delay was removed. In the past this could lead to an unwanted activation of the setup mode.

see all details of the first V3 release here: [Release v3.0.0](https://github.com/dewenni/ESP_Buderus_KM271/releases/tag/v3.0.0)

## changelog

- change blocking mqtt reconnect handling to non-blocking implementation #33
- mqtt client Library changed to implementation based on AsyncTCP
- change blocking WiFi reconnect handling to non-blocking implementation
- Prevent division by ZERO if parameter Oil density is not set.
- Prevent writing the time on the Logamatic, 1 hour after the boot-up
4 changes: 2 additions & 2 deletions include/basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern s_wifi wifi;


/* P R O T O T Y P E S ********************************************************/
void check_wifi();
void basic_setup();
void checkWiFi();
void basicSetup();
void sendWiFiInfo();
void storeData();
const char * getDateTimeString();
Expand Down
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/*-------------------------------------------------------------------------------
General Configuration
--------------------------------------------------------------------------------*/
#define VERSION "v3.2.0" // internal program version
#define VERSION "v3.2.1" // internal program version
#define DEBUG_ON // enable debug messages

#define WIFI_RECONNECT 10000 // Delay between wifi reconnection tries
Expand Down
4 changes: 2 additions & 2 deletions include/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <config.h>
#include <Arduino.h>
#include <language.h>
#include <PubSubClient.h>
#include <AsyncMqttClient.h>


/* P R O T O T Y P E S ********************************************************/
const char * addTopic(const char *suffix);
void mqttSetup();
void mqttCyclic();
void checkMqtt();
void mqttPublish(const char* sendtopic, const char* payload, boolean retained);
11 changes: 6 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ framework = arduino
monitor_speed = 115200

monitor_filters = esp32_exception_decoder, colorize
build_type = debug
;build_type = debug
build_type = release

; ----------------------------------------------------------------
; OPTION 1: direct cable upload
; ----------------------------------------------------------------
;upload_port = /dev/cu.usbserial-0258166D

;upload_port = /dev/cu.usbserial-0001

; ----------------------------------------------------------------
; OPTION 2: standard wireless OTA Update (ArduinoOTA)
Expand All @@ -37,7 +38,7 @@ build_type = debug
; ----------------------------------------------------------------
extra_scripts = platformio_upload.py
upload_protocol = custom
upload_url = http://192.168.178.40:8080/update ; Conneted to Router
upload_url = http://192.168.178.197:8080/update ; Conneted to Router
;upload_url = http://192.168.4.1:8080/update ; Setup Mode (ESP create Accesspoint)


Expand All @@ -49,7 +50,7 @@ lib_deps =
https://github.com/me-no-dev/ESPAsyncWebServer.git ;need the latest version because of a undefined reference to `SHA1Init'
bblanchon/ArduinoJson @ ^6.19.4
michael-uray/muTimer @ ^0.1.1
knolleary/pubsubclient @ ^2.8.0
s00500/ESPUI @ ^2.2.1
khoih-prog/ESP_DoubleResetDetector @ ^1.3.2
ayushsharma82/AsyncElegantOTA @ ^2.2.7
ayushsharma82/AsyncElegantOTA @ ^2.2.7
marvinroger/AsyncMqttClient @ ^0.9.0
105 changes: 66 additions & 39 deletions src/basics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void setup_wifi();
/* D E C L A R A T I O N S ****************************************************/
s_wifi wifi; // global WiFi Informations
AsyncWebServer webOTAserver(8080);
muTimer wifiReconnectTimer = muTimer(); // timer for reconnect delay
int wifi_retry = 0;

/**
* *******************************************************************
Expand Down Expand Up @@ -86,7 +88,6 @@ const char * getTimeString() {
return timeInfo;
}


/**
* *******************************************************************
* @brief Setup for OTA Function
Expand All @@ -103,13 +104,68 @@ void setupOTA(){
webOTAserver.begin();
}

/**
* *******************************************************************
* @brief callback function if WiFi is connected to configured AP
* @param none
* @return none
* *******************************************************************/
void onWiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
wifi_retry=0;
Serial.println("Connected to AP successfully!");
}

/**
* *******************************************************************
* @brief callback function if WiFi is connected and client got IP
* @param none
* @return none
* *******************************************************************/
void onWiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

/**
* *******************************************************************
* @brief check WiFi connection and automatic reconnect
* @param none
* @return none
* *******************************************************************/
void checkWiFi(){
if (wifiReconnectTimer.delayOnTrigger(!WiFi.isConnected(), WIFI_RECONNECT)) {
wifiReconnectTimer.delayReset();
if (wifi_retry < 5) {
wifi_retry++;
WiFi.mode(WIFI_STA);
WiFi.begin(config.wifi.ssid, config.wifi.password);
WiFi.hostname(config.wifi.hostname);
MDNS.begin(config.wifi.hostname);
Serial.print("WiFi Mode STA - Trying connect to: ");
Serial.print(config.wifi.ssid);
Serial.print(" - attempt: ");
Serial.print(wifi_retry);
Serial.print("/5");
}
else {
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
Serial.println("Wifi connection not possible, esp rebooting...");
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
storeData(); // store Data before reboot
delay(500);
ESP.restart();
}
}
}

/**
* *******************************************************************
* @brief Setup for general WiFi Function
* @param none
* @return none
* *******************************************************************/
void setup_wifi() {
void setupWiFi() {

if (setupMode) {
// start Accesspoint for initial setup
Expand All @@ -118,13 +174,17 @@ void setup_wifi() {
IPAddress subnet(255,255,255,0);
WiFi.softAPConfig(ip, gateway, subnet);
WiFi.softAP("ESP-Buderus-km271");
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
Serial.println("> WiFi Mode: AccessPoint <");
Serial.println("1. connect your device to SSID: ESP-Buderus-km271");
Serial.println("2. open Browser and go to Address: http://192.168.4.1");
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
Serial.println("\n! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !\n");
}
else {
// setup callback function
WiFi.onEvent(onWiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(onWiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);

// connect to configured wifi AP
WiFi.mode(WIFI_STA);
WiFi.begin(config.wifi.ssid, config.wifi.password);
Expand All @@ -136,49 +196,16 @@ void setup_wifi() {

}

/**
* *******************************************************************
* @brief Check WiFi status and automatic reconnect
* @param none
* @return none
* *******************************************************************/
void check_wifi(){

// Check WiFi connectivity
int wifi_retry = 0;
while(WiFi.status() != WL_CONNECTED && wifi_retry < 5 ) {
wifi_retry++;
Serial.print("WiFi not connected. Trying connect to: ");
Serial.println(config.wifi.ssid);
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(config.wifi.ssid, config.wifi.password);
delay(WIFI_RECONNECT);
}
if(wifi_retry >= 5) {
Serial.println("\nWifi connection not possible, rebooting...");
storeData(); // store Data before reboot
delay(500);
ESP.restart();
} else if (wifi_retry > 0){
Serial.println("WiFi reconnected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
}


/**
* *******************************************************************
* @brief Call all basic Setup functions at once
* @param none
* @return none
* *******************************************************************/
void basic_setup() {
void basicSetup() {
// WiFi
setup_wifi();
setupWiFi();

//NTP
ntpSetup();
Expand Down
4 changes: 4 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ void configSaveToFile() {
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}
else {
Serial.print("config successfully saved to file: ");
Serial.println(filename);
}

// Close the file
file.close();
Expand Down
2 changes: 1 addition & 1 deletion src/km271.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ void parseInfo(uint8_t *data, int len) {
mqttPublish(addStatTopic(statTopic.BOILER_LIFETIME_3[config.lang]), uint8ToString(kmStatus.BurnerOperatingDuration_2), false);
mqttPublish(addStatTopic(statTopic.BOILER_LIFETIME_4[config.lang]), uint64ToString(kmStatus.BurnerOperatingDuration_Sum), false);

if (config.oilmeter.use_virtual_meter) {
if (config.oilmeter.use_virtual_meter && config.oilmeter.oil_density_kg_l != 0) {
kmStatus.BurnerCalcOilConsumption = (double)kmStatus.BurnerOperatingDuration_Sum / 60 * config.oilmeter.consumption_kg_h / config.oilmeter.oil_density_kg_l;
mqttPublish(addStatTopic(statTopic.BOILER_CONSUMPTION[config.lang]), doubleToString(kmStatus.BurnerCalcOilConsumption), false);
}
Expand Down
25 changes: 16 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <oilmeter.h>

// Double-Reset-Detector
#define ESP_DRD_USE_LITTLEFS true
#define ESP_DRD_USE_LITTLEFS true
#define DOUBLERESETDETECTOR_DEBUG true
#define DRD_TIMEOUT 10
#define DRD_ADDRESS 0
#define DRD_ADDRESS 0 // not used > LittleFS
#include <ESP_DoubleResetDetector.h>
DoubleResetDetector* drd;

Expand All @@ -21,6 +22,7 @@ muTimer dstTimer = muTimer(); // timer to check daylight saving time cha

bool main_reboot = true; // reboot flag
int dst_old; // reminder for change of daylight saving time
bool dst_ref; // init flag fpr dst reference

/**
* *******************************************************************
Expand Down Expand Up @@ -57,7 +59,7 @@ void setup()
configSetup();

// basic setup function (WiFi, OTA)
basic_setup();
basicSetup();

// MQTT
if (config.mqtt.enable && !setupMode) {
Expand Down Expand Up @@ -98,17 +100,16 @@ void loop()
// double reset detector
drd->loop();

// chck WiFi
// check WiFi - automatic reconnect
if (!setupMode) {
check_wifi();
checkWiFi();
}

// chck MQTT
// check MQTT - automatic reconnect
if (config.mqtt.enable && !setupMode) {
mqttCyclic();
checkMqtt();
}


if (setupMode){
// LED to Signal Setup-Mode
digitalWrite(LED_BUILTIN, setupModeTimer.cycleOnOff(100,500));
Expand Down Expand Up @@ -156,6 +157,12 @@ void loop()
tm dti;
time(&now); // read the current time
localtime_r(&now, &dti); // update the structure tm with the current time

if (!dst_ref){ // save actual DST as reference after bootup
dst_ref = true;
dst_old = dti.tm_isdst;
}

if (dst_old!=dti.tm_isdst && !main_reboot){ // check if DST has changed
km271SetDateTimeNTP(); // change date and time on buderus
}
Expand Down
Loading

0 comments on commit 2ba571b

Please sign in to comment.