Skip to content

Commit 98eb7a8

Browse files
committed
Merge feat/multicore-task-architecture into claude/fix-ds18b20-plausibility
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012JY8RZscs5W8tLQMuZB95K
2 parents 4472dee + b51ea07 commit 98eb7a8

15 files changed

Lines changed: 123 additions & 125 deletions

‎.github/CODING_GUIDELINES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ Dieses Dokument beschreibt die Coding-Richtlinien für das Pool-Controller Proje
66
Super-Linter Fehler zu vermeiden und Best Practices für ESP32 Entwicklung mit PlatformIO
77
zu befolgen.
88

9+
## 0. Sprachstandard: C++17
10+
11+
Firmware-Code in `src/` ist C++17 (`-std=gnu++17`) auf allen drei Boards.
12+
`platformio.ini` nimmt dafür die Standard-Flags der Plattformen
13+
(`-std=gnu++11` bei espressif32 7.x, `-std=gnu++2b` bei pioarduino) per
14+
`build_unflags` heraus. Ohne diese Zeilen gewinnt der Default der Toolchain.
15+
16+
C++17 ist der gemeinsame Nenner: `esp32dev` und `norvi_ae01_r` laufen auf
17+
espressif32 7.0.1 (Arduino 2.0.17, GCC 8.4), das C++20/23 nicht vollständig
18+
kann. C++23 kommt erst mit dem Umstieg dieser Boards auf pioarduino
19+
(Arduino Core 3.x), der eine eigene Migration ist.
20+
21+
Die nativen Tests bauen ebenfalls mit C++17. Funktionen aus C++20 und neuer
22+
(`std::span`, Concepts, `std::expected`) nicht verwenden.
23+
924
## 1. C++ Code-Formatierung (clang-format)
1025

1126
### 1.1 Grundlegende Regeln

‎.github/copilot-instructions.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Project Overview
44

55
This repository contains PlatformIO-based IoT firmware for the ESP32 platform.
6-
It uses C++17, follows strict standards for architecture, memory management,
6+
It uses C++17 (`-std=gnu++17` on all boards; see `.github/CODING_GUIDELINES.md`), follows strict standards for architecture, memory management,
77
maintainability, security, and 24/7 reliability.
88

99
## General Coding Standards

‎docs/software-guide.de.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ menu:
1919

2020
## Benötigte Bibliotheken
2121

22-
- [AsyncMqttClient](https://github.com/marvinroger/async-mqtt-client) @ 0.9.0
22+
- [espMqttClient](https://github.com/bertmelis/espMqttClient) @ 1.7 (Async-Variante auf [AsyncTCP](https://github.com/ESP32Async/AsyncTCP) 3.x)
2323
- [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library)
2424
- [OneWire](https://github.com/PaulStoffregen/OneWire)
25-
- [Adafruit Unified Sensor](https://github.com/adafruit/Adafruit_Sensor)
26-
- [DHT sensor library](https://github.com/adafruit/DHT-sensor-library)
2725
- [NTPClient](https://github.com/arduino-libraries/NTPClient) @ 3.2.1
2826
- [Timezone](https://github.com/JChristensen/Timezone) @ 1.2.6
29-
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) @ 7.3.2
30-
- [Bounce2](https://github.com/thomasfredericks/Bounce2)
27+
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) @ 7.4.3
3128
- [Wire](https://github.com/espressif/arduino-esp32/tree/master/libraries/Wire)
3229

3330
Vielen Dank an die Entwickler dieser Bibliotheken!

‎docs/software-guide.md‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ menu:
1919

2020
## Required Libraries
2121

22-
- [AsyncMqttClient](https://github.com/marvinroger/async-mqtt-client) @ 0.9.0
22+
- [espMqttClient](https://github.com/bertmelis/espMqttClient) @ 1.7 (async variant on [AsyncTCP](https://github.com/ESP32Async/AsyncTCP) 3.x)
2323
- [DallasTemperature](https://github.com/milesburton/Arduino-Temperature-Control-Library)
2424
- [OneWire](https://github.com/PaulStoffregen/OneWire)
25-
- [Adafruit Unified Sensor](https://github.com/adafruit/Adafruit_Sensor)
26-
- [DHT sensor library](https://github.com/adafruit/DHT-sensor-library)
2725
- [NTPClient](https://github.com/arduino-libraries/NTPClient) @ 3.2.1
2826
- [Timezone](https://github.com/JChristensen/Timezone) @ 1.2.6
29-
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) @ 7.3.2
30-
- [Bounce2](https://github.com/thomasfredericks/Bounce2)
27+
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson) @ 7.4.3
3128
- [Wire](https://github.com/espressif/arduino-esp32/tree/master/libraries/Wire)
3229

3330
Many thanks to the maintainers of these libraries!

‎platformio.ini‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ serial_speed = 115200
1616

1717
[common_env_data]
1818
lib_deps =
19-
DallasTemperature
19+
milesburton/DallasTemperature @ 4.0.6
2020
Wire
2121
https://github.com/PaulStoffregen/OneWire.git#800f26f3ee6eb446a72e013785cac3700e54cc13
22-
Adafruit Unified Sensor
2322
NTPClient @ 3.2.1
2423
TimeZone @ 1.2.6
25-
bblanchon/ArduinoJson @ 7.3.2
26-
thomasfredericks/Bounce2
27-
marvinroger/AsyncMqttClient
24+
bblanchon/ArduinoJson @ 7.4.3
25+
bertmelis/espMqttClient @ ^1.7.3
26+
ESP32Async/AsyncTCP @ ^3.4.0
2827

2928
[env:norvi_ae01_r]
3029
platform = espressif32 @ 7.0.1
@@ -37,9 +36,12 @@ build_flags =
3736
-D SERIAL_SPEED=${common.serial_speed}
3837
-D NORVI_AE01_R
3938
-D LOG_BUFFER_SIZE=8192
40-
-std=c++17
39+
-std=gnu++17
4140
-Wno-deprecated-declarations
42-
build_unflags = -Werror=reorder
41+
build_unflags =
42+
-Werror=reorder
43+
-std=gnu++11
44+
-std=gnu++2b
4345
lib_deps =
4446
${common_env_data.lib_deps}
4547
adafruit/Adafruit SSD1306 @ ^2.5.13
@@ -62,9 +64,12 @@ build_flags =
6264
-D OLIMEX_ESP32_C6_EVB
6365
-D HAS_LOCAL_TFT_UI
6466
-D LOG_BUFFER_SIZE=8192
65-
-std=c++17
67+
-std=gnu++17
6668
-Wno-deprecated-declarations
67-
build_unflags = -Werror=reorder
69+
build_unflags =
70+
-Werror=reorder
71+
-std=gnu++11
72+
-std=gnu++2b
6873
lib_deps =
6974
${common_env_data.lib_deps}
7075
adafruit/Adafruit GFX Library @ ^1.12.0
@@ -88,9 +93,12 @@ build_flags =
8893
'-D GITHUB_REPO="smart-swimmingpool/pool-controller"'
8994
-D SERIAL_SPEED=${common.serial_speed}
9095
-D LOG_BUFFER_SIZE=8192
91-
-std=c++17
96+
-std=gnu++17
9297
-Wno-deprecated-declarations ; Suppress deprecation warnings from libraries
93-
build_unflags = -Werror=reorder
98+
build_unflags =
99+
-Werror=reorder
100+
-std=gnu++11
101+
-std=gnu++2b
94102
lib_deps =
95103
${common_env_data.lib_deps}
96104
monitor_speed = ${common.serial_speed}

‎src/MqttCommandQueue.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @brief Fixed-size, thread-safe hand-over of MQTT commands from the
88
* AsyncTCP task to the loop task.
99
*
10-
* AsyncMqttClient delivers messages on the AsyncTCP task. Handling them there
10+
* The MQTT client (espMqttClientAsync) delivers messages on the AsyncTCP task. Handling them there
1111
* changed controller state (operation mode String, settings, relays, NVS)
1212
* concurrently with the loop task. The MQTT callback therefore only copies
1313
* topic and payload into this queue; the loop task processes them.

‎src/MqttPublisher.cpp‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ void MqttPublisher::publishStates() {
822822
// ═══════════════════════════════════════════════════════════════════════
823823

824824
void MqttPublisher::exportLogEvents() {
825-
// Only one export may run at a time: AsyncMqttClient callbacks (handleMqttMessage
825+
// Only one export may run at a time: MQTT client callbacks (handleMqttMessage
826826
// → publishStates) run on the AsyncTCP task and can preempt the loop task's
827827
// periodic export. Both share the static snapshot buffer and the watermark, so a
828828
// concurrent entry would clobber the snapshot or regress s_lastExportedSeq and
@@ -917,7 +917,7 @@ void MqttPublisher::exportLogEvents() {
917917
ok = NetworkManager::publish("pool-controller/log", rawBuf, false) && ok;
918918
}
919919

920-
// A required publish failed (e.g. AsyncMqttClient refused to enqueue
920+
// A required publish failed (e.g. the MQTT client refused to enqueue
921921
// during the discovery/state burst right after reconnect): keep the
922922
// watermark before this entry so the whole tail is retried next pass.
923923
// Do not drop the event by marking it exported without a successful queue.
@@ -1051,9 +1051,7 @@ static bool shouldEnforceMqttAuth() {
10511051
return false; // Made optional as per user request
10521052
}
10531053

1054-
void MqttPublisher::onMqttMessage(
1055-
char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
1056-
(void)properties;
1054+
void MqttPublisher::onMqttMessage(const char *topic, const char *payload, size_t len, size_t index, size_t total) {
10571055
// Only complete single-chunk messages are supported (HA commands are tiny)
10581056
if (index != 0 || len != total) {
10591057
LOG_WARN(
@@ -1078,13 +1076,11 @@ void MqttPublisher::processPendingCommands() {
10781076
// a burst is spread over several iterations instead of stalling the loop.
10791077
MqttCommandQueue::Message msg;
10801078
for (size_t handled = 0; handled < kMaxCommandsPerLoop && s_commandQueue.pop(msg); handled++) {
1081-
AsyncMqttClientMessageProperties properties{};
1082-
handleMqttMessage(msg.topic, msg.payload, properties, msg.payloadLen, 0, msg.payloadLen);
1079+
handleMqttMessage(msg.topic, msg.payload, msg.payloadLen, 0, msg.payloadLen);
10831080
}
10841081
}
10851082

1086-
void MqttPublisher::handleMqttMessage(
1087-
char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
1083+
void MqttPublisher::handleMqttMessage(const char *topic, const char *payload, size_t len, size_t index, size_t total) {
10881084
// Only process complete messages (single-chunk delivery for typical HA commands)
10891085
if (index != 0) {
10901086
return;

‎src/MqttPublisher.hpp‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include <Arduino.h>
1313
#include <ArduinoJson.h>
14-
#include <AsyncMqttClient.h>
1514
#include <cstdint>
1615

1716
namespace PoolController {
@@ -39,22 +38,19 @@ class MqttPublisher {
3938
* Changes controller state — call only on the loop task (via
4039
* processPendingCommands()), never from the MQTT client callback.
4140
* @param topic The MQTT topic the message was received on.
42-
* @param payload Null-terminated payload string.
43-
* @param properties Message properties (QoS, retain, dup).
41+
* @param payload Payload bytes (not NUL-terminated).
4442
* @param len Payload length.
4543
* @param index Chunk index (0 for single-chunk messages).
4644
* @param total Total payload size across all chunks.
4745
*/
48-
static void handleMqttMessage(
49-
char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
46+
static void handleMqttMessage(const char *topic, const char *payload, size_t len, size_t index, size_t total);
5047

5148
/**
5249
* MQTT client callback — runs on the AsyncTCP task. Only copies the message
5350
* into the command queue; it is handled by processPendingCommands() on the
5451
* loop task. Incomplete (chunked) or oversized messages are dropped.
5552
*/
56-
static void onMqttMessage(
57-
char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
53+
static void onMqttMessage(const char *topic, const char *payload, size_t len, size_t index, size_t total);
5854

5955
/// Maximum number of queued commands handled per loop iteration.
6056
static constexpr size_t kMaxCommandsPerLoop = 2;

‎src/NetworkManager.cpp‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
#include <esp_wifi.h>
1313
#include <esp_wps.h>
1414
#include <ESPmDNS.h>
15+
#include <espMqttClientAsync.h>
1516

1617
#include "ConfigManager.hpp"
1718
#include "WpsProvisioner.hpp"
1819
#include "LogCapture.hpp"
1920

2021
namespace PoolController {
2122

22-
AsyncMqttClient NetworkManager::mqttClient_;
23+
namespace {
24+
// espMqttClient serialises its internal state with a mutex, so publish() is
25+
// safe from the loop task while callbacks run on the AsyncTCP task.
26+
espMqttClientAsync mqttClient_;
27+
} // namespace
2328

2429
NetworkManager::MqttMessageCallback NetworkManager::mqttCallback_ = nullptr;
2530

@@ -47,7 +52,7 @@ bool NetworkManager::begin() {
4752
// Publish online to LWT topic immediately (async, non-blocking)
4853
mqttClient_.publish("homeassistant/sensor/pool-controller/availability", 1, true, "online");
4954
});
50-
mqttClient_.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
55+
mqttClient_.onDisconnect([](espMqttClientTypes::DisconnectReason reason) {
5156
LOG_ERROR("✖ MQTT disconnected, reason=%d\n", static_cast<int>(reason));
5257
LogCapture::logEvent("MQTT_DISCONNECTED", "MQTT disconnected, reason %d", static_cast<int>(reason));
5358
});
@@ -183,7 +188,7 @@ void NetworkManager::connectMqtt() {
183188
return;
184189
}
185190

186-
// AsyncMqttClient remembers previous config; re-apply for safety
191+
// The client keeps the host pointer; ConfigManager owns the string. Re-apply for safety.
187192
mqttClient_.setServer(config.host.c_str(), config.port);
188193

189194
// Generate standard unique client ID (static to survive async CONNECT packet)
@@ -201,8 +206,7 @@ void NetworkManager::connectMqtt() {
201206
// LWT — broker publishes "offline" if we disconnect unexpectedly
202207
mqttClient_.setWill("homeassistant/sensor/pool-controller/availability", 1, true, "offline");
203208

204-
// onMessage callback is registered once in setMqttCallback() — do NOT re-add here
205-
// to avoid accumulating duplicates in AsyncMqttClient's internal vector.
209+
// onMessage callback is registered once in setMqttCallback() — do NOT re-add here.
206210

207211
// Initiate async connection
208212
mqttClient_.connect();
@@ -224,8 +228,14 @@ bool NetworkManager::subscribe(const char *topic) {
224228

225229
void NetworkManager::setMqttCallback(MqttMessageCallback callback) {
226230
mqttCallback_ = callback;
227-
// AsyncMqttClient onMessage is persistent (not connection-bound)
228-
mqttClient_.onMessage(callback);
231+
// onMessage is persistent (not connection-bound)
232+
mqttClient_.onMessage([](const espMqttClientTypes::MessageProperties &properties, const char *topic, const uint8_t *payload,
233+
size_t len, size_t index, size_t total) {
234+
(void)properties;
235+
if (mqttCallback_) {
236+
mqttCallback_(topic, reinterpret_cast<const char *>(payload), len, index, total);
237+
}
238+
});
229239
}
230240

231241
void NetworkManager::publishOffline() {

‎src/NetworkManager.hpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <Arduino.h>
1313
#include <WiFi.h>
1414
#include <functional>
15-
#include <AsyncMqttClient.h>
1615

1716
namespace PoolController {
1817

@@ -26,8 +25,11 @@ namespace PoolController {
2625
*/
2726
class NetworkManager {
2827
public:
29-
using MqttMessageCallback = std::function<void(
30-
char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)>;
28+
/**
29+
* Incoming MQTT message chunk. @p payload is not NUL-terminated; @p len bytes
30+
* at offset @p index of a @p total byte message. Runs on the AsyncTCP task.
31+
*/
32+
using MqttMessageCallback = std::function<void(const char *topic, const char *payload, size_t len, size_t index, size_t total)>;
3133

3234
NetworkManager() = default;
3335

@@ -68,8 +70,6 @@ class NetworkManager {
6870
static void connectWiFi();
6971
static void connectMqtt();
7072

71-
static AsyncMqttClient mqttClient_;
72-
7373
static MqttMessageCallback mqttCallback_;
7474

7575
static bool apModeActive_;

0 commit comments

Comments
 (0)