Debug printing

Work on MQTT topic switching
This commit is contained in:
Arne van Iterson 2024-03-13 00:00:20 +01:00
parent 79968b727a
commit 9d51792402
5 changed files with 140 additions and 39 deletions

View File

@ -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"
}
}
}

View File

@ -2,6 +2,20 @@
#define GLOBAL_H
#include <Arduino.h>
#include <string.h>
// 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 <WiFi.h>

View File

@ -6,7 +6,7 @@ MqttTopic::MqttTopic(WiFiClient* espClient): wifiClient(espClient), client(PubSu
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);
}

View File

@ -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();
};

View File

@ -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());