From 9d517924026052e742cd081ded00b2731a98a856 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Wed, 13 Mar 2024 00:00:20 +0100 Subject: [PATCH] Debug printing Work on MQTT topic switching --- EPaperAnalog.code-workspace | 66 ++++++++++++++++++++++++++++++++++++- src/global.h | 14 ++++++++ src/helpers/mqtt.cpp | 57 +++++++++++++++++++++++++------- src/helpers/mqtt.h | 10 ++++-- src/main.cpp | 32 +++++------------- 5 files changed, 140 insertions(+), 39 deletions(-) diff --git a/EPaperAnalog.code-workspace b/EPaperAnalog.code-workspace index 36a7352..4cfda44 100644 --- a/EPaperAnalog.code-workspace +++ b/EPaperAnalog.code-workspace @@ -27,7 +27,71 @@ "string": "cpp", "unordered_map": "cpp", "unordered_set": "cpp", - "string_view": "cpp" + "string_view": "cpp", + "algorithm": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "clocale": "cpp", + "compare": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "queue": "cpp", + "random": "cpp", + "ratio": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "*.tcc": "cpp", + "memory_resource": "cpp", + "cinttypes": "cpp" } } } \ No newline at end of file diff --git a/src/global.h b/src/global.h index 9f8ce4f..55e7454 100644 --- a/src/global.h +++ b/src/global.h @@ -2,6 +2,20 @@ #define GLOBAL_H #include +#include + +// Debug +#define DEBUG 1 + +#if DEBUG +#define D_SerialBegin(...) Serial.begin(__VA_ARGS__); +#define D_print(...) Serial.print(__VA_ARGS__) +#define D_println(...) Serial.println(__VA_ARGS__) +#else +#define D_SerialBegin(...) +#define D_print(...) +#define D_println(...) +#endif // Wifi #include diff --git a/src/helpers/mqtt.cpp b/src/helpers/mqtt.cpp index 2d6bf9c..0406961 100644 --- a/src/helpers/mqtt.cpp +++ b/src/helpers/mqtt.cpp @@ -1,12 +1,12 @@ #include "mqtt.h" -MqttTopic::MqttTopic(WiFiClient* espClient): wifiClient(espClient), client(PubSubClient(*espClient)) +MqttTopic::MqttTopic(WiFiClient *espClient) : wifiClient(espClient), client(PubSubClient(*espClient)) { IPAddress server; server.fromString(MQTT_ADDR); this->client.setServer(server, MQTT_PORT); - this->client.setCallback(this->callback); + this->client.setCallback([&](char *a, byte *b, unsigned int c){ this->callback(a, b, c); }); } MqttTopic::~MqttTopic() @@ -16,14 +16,49 @@ MqttTopic::~MqttTopic() void MqttTopic::callback(char *topic, byte *payload, unsigned int length) { - Serial.print("Message arrived ["); - Serial.print(topic); - Serial.println("] "); + // Topic should be homeassistant/ead/unique_id + char *token = strtok(topic, "/"); // homeassistant + uint8_t i = 0; + while (token != NULL && i < 2) + { + // Move to next token + token = strtok(NULL, "/"); // ead + i++; + } + + D_print("Topic: "); + D_println(token); + + // Check if subtopic has been found + if (i == 2) + { + // Check if token exists in topics + if (std::find(std::begin(topics), std::end(topics), token) != std::end(topics)) + { + // Token exists + D_println("Token exists"); + } + else + { + // Add token to topics + D_print((char*)topics); + strcpy(topics[available], token); + available++; + } + } + else + { + // In case the strtok fails entirely + D_println("Topic invalid"); + return; + } + + // char array[length + 1] = {}; // memcpy(array, payload, length); // array[length] = '\0'; - // Serial.println(String(array).toFloat() / 1000.0); + // D_println(String(array).toFloat() / 1000.0); // ead.setArm(String(array).toFloat() / 1000.0); } @@ -31,18 +66,18 @@ void MqttTopic::loop() { while (!this->client.connected()) { - Serial.print("Attempting MQTT connection..."); + D_print("Attempting MQTT connection..."); // Attempt to connect if (this->client.connect("espead", "arne", "ThinkCentreM58")) { - Serial.println("connected"); + D_println("connected"); this->client.subscribe("homeassistant/ead/#"); } else { - Serial.print("failed, rc="); - Serial.print(this->client.state()); - Serial.println(" try again in 5 seconds"); + D_print("failed, rc="); + D_print(this->client.state()); + D_println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } diff --git a/src/helpers/mqtt.h b/src/helpers/mqtt.h index b3834a7..091d35d 100644 --- a/src/helpers/mqtt.h +++ b/src/helpers/mqtt.h @@ -10,14 +10,18 @@ class MqttTopic { private: - WiFiClient* wifiClient; // Pointer to WiFiClient, for use with PubSubClient + WiFiClient* wifiClient; PubSubClient client; - uint8_t topic = 0; + + // Home assistant topic should follow /homeassistant/ead/unique_id + char topics[10][8]; // Array of topics available, we'll go with 10 for now + uint8_t available = 0; // Topics present + uint8_t current = 0; // Current topic public: MqttTopic(WiFiClient* espClient); ~MqttTopic(); - static void callback(char *topic, byte *payload, unsigned int length); + void callback(char *topic, byte *payload, unsigned int length); void loop(); }; diff --git a/src/main.cpp b/src/main.cpp index 5d950d9..c7849b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,21 +21,21 @@ void btnHandler(Button2 &btn) case single_click: break; case double_click: - Serial.print("double "); + D_print("double "); break; } - Serial.print("click "); - Serial.print("on button #"); - Serial.print((btn == btn_L) ? "L" : "R"); - Serial.println(); + D_print("click "); + D_print("on button #"); + D_print((btn == btn_L) ? "L" : "R"); + D_println(); } void setup() { - Serial.begin(115200); + D_SerialBegin(115200); delay(10); - Serial.println("T5 Dashboard " __DATE__ " " __TIME__); + D_println("T5 Dashboard " __DATE__ " " __TIME__); // Buttons btn_L.begin(BTN_L, INPUT_PULLUP, true); @@ -61,34 +61,18 @@ void setup() delay(20); } - // display.setCursor(0, 6); - // // display.println("TTGO T5 V2.3 ePaper PlatformIO"); - // display.println("T5 Analog/Digital Display, " __DATE__ " " __TIME__); - // display.nextPage(); - WiFiMulti.addAP("Langeboomgaard", "ACvI4152EK"); WiFiMulti.addAP("ARNE-LAPTOPV3 6821", "P3900:q5"); if (int stat = WiFiMulti.run() != WL_CONNECTED) { - Serial.println(WiFi.status()); + D_println(WiFi.status()); display.print("WiFi connection failed"); display.println(stat); display.nextPage(); esp_deep_sleep_start(); } - // display.printf("WiFi connected to "); - // display.println(WiFi.SSID()); - // display.print("IP address: "); - // display.println(WiFi.localIP()); - // display.nextPage(); - - // delay(1000); - - // display.fillScreen(GxEPD_WHITE); - // display.nextPage(); - display.fillScreen(GxEPD_WHITE); ead.drawFace(0, 9, "Solax Prod.", "kW", 10); ead.drawMeta(WiFi.localIP().toString().c_str());