From 620db2cc334da95efa12eaffc7f5ee818dc1cfa1 Mon Sep 17 00:00:00 2001 From: Cabooman <81711263+Cabooman@users.noreply.github.com> Date: Thu, 1 Feb 2024 22:25:21 +0100 Subject: [PATCH] Wrapping up the POC Increased the msg buffer to allow cell voltages to be transmitted (at least Leaf). Tested OK --- Software/src/battery/NISSAN-LEAF-BATTERY.cpp | 28 +++++++++++++++++++ Software/src/devboard/mqtt/mqtt.cpp | 20 +++++++------ Software/src/devboard/mqtt/mqtt.h | 6 ++-- .../lib/knolleary-pubsubclient/PubSubClient.h | 2 +- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/Software/src/battery/NISSAN-LEAF-BATTERY.cpp b/Software/src/battery/NISSAN-LEAF-BATTERY.cpp index cae083ec..81090885 100644 --- a/Software/src/battery/NISSAN-LEAF-BATTERY.cpp +++ b/Software/src/battery/NISSAN-LEAF-BATTERY.cpp @@ -1,4 +1,7 @@ #include "NISSAN-LEAF-BATTERY.h" +#ifdef MQTT +#include "../devboard/mqtt/mqtt.h" +#endif #include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" @@ -155,6 +158,10 @@ static uint16_t temp_raw_min = 0; static int16_t temp_polled_max = 0; static int16_t temp_polled_min = 0; +#ifdef MQTT +void publish_data(void); +#endif + void print_with_units(char* header, int value, char* units) { Serial.print(header); Serial.print(value); @@ -402,6 +409,9 @@ void update_values_leaf_battery() { /* This function maps all the values fetched } #endif +#ifdef MQTT + publish_data(); +#endif } void receive_can_leaf_battery(CAN_frame_t rx_frame) { @@ -917,3 +927,21 @@ uint16_t Temp_fromRAW_to_F(uint16_t temperature) { //This function feels horrib } return static_cast(1094 + (309 - temperature) * 2.5714285714285715); } + +#ifdef MQTT +void publish_data(void) { + Serial.println("Publishing..."); + size_t msg_length = snprintf(mqtt_msg, sizeof(mqtt_msg), "{\n\"cell_voltages\":["); + + for (size_t i = 0; i < 97; ++i) { + msg_length += snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "%s%d", (i == 0) ? "" : ",", 1234); + // msg_length += snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "%s%d", (i == 0) ? "" : ", ", cell_voltages[i]); + } + + snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "]\n}\n"); + + if (mqtt_publish_retain("battery_testing/spec_data") == false) { + Serial.println("Nissan MQTT msg could not be sent"); + } +} +#endif diff --git a/Software/src/devboard/mqtt/mqtt.cpp b/Software/src/devboard/mqtt/mqtt.cpp index 6d2ff5ca..d8dd9fe8 100644 --- a/Software/src/devboard/mqtt/mqtt.cpp +++ b/Software/src/devboard/mqtt/mqtt.cpp @@ -11,7 +11,7 @@ const size_t mqtt_nof_subscriptions = sizeof(mqtt_subscriptions) / sizeof(mqtt_s WiFiClient espClient; PubSubClient client(espClient); -char msg[MSG_BUFFER_SIZE]; +char mqtt_msg[MSG_BUFFER_SIZE]; int value = 0; static unsigned long previousMillisUpdateVal; MyTimer publish_global_timer(5000); @@ -19,7 +19,7 @@ MyTimer publish_global_timer(5000); /** Publish global values and call callbacks for specific modules */ static void publish_values(void) { - snprintf(msg, sizeof(msg), + snprintf(mqtt_msg, sizeof(mqtt_msg), "{\n" " \"SOC\": %.3f,\n" " \"StateOfHealth\": %.3f,\n" @@ -28,10 +28,14 @@ static void publish_values(void) { " \"cell_max_voltage\": %d,\n" " \"cell_min_voltage\": %d\n" "}\n", - ((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0, - ((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage); - bool result = client.publish("battery_testing/info", msg, true); - Serial.println(msg); // Uncomment to print the payload on serial + ((float)SOC) / 100.0, + ((float)StateOfHealth) / 100.0, + ((float)((int16_t)temperature_min)) / 10.0, + ((float)((int16_t)temperature_max)) / 10.0, + cell_max_voltage, + cell_min_voltage); + bool result = client.publish("battery_testing/info", mqtt_msg, true); + Serial.println(mqtt_msg); // Uncomment to print the payload on serial } /* This is called whenever a subscribed topic changes (hopefully) */ @@ -94,6 +98,6 @@ void mqtt_loop(void) { } } -bool mqtt_publish(const String& topic, const String& payload) { - return client.publish(topic.c_str(), payload.c_str()); +bool mqtt_publish_retain(const char *topic) { + return client.publish(topic, mqtt_msg, true); } diff --git a/Software/src/devboard/mqtt/mqtt.h b/Software/src/devboard/mqtt/mqtt.h index 3c362749..35fdd053 100644 --- a/Software/src/devboard/mqtt/mqtt.h +++ b/Software/src/devboard/mqtt/mqtt.h @@ -37,7 +37,7 @@ #include #include "../../../USER_SETTINGS.h" -#define MSG_BUFFER_SIZE (256) +#define MSG_BUFFER_SIZE (1024) #define MQTT_SUBSCRIPTIONS \ { "my/topic/abc", "my/other/topic" } #define MQTT_SERVER "192.168.xxx.yyy" @@ -53,8 +53,10 @@ extern uint16_t cell_min_voltage; //mV, 0-4350 extern const char* mqtt_user; extern const char* mqtt_password; +extern char mqtt_msg[MSG_BUFFER_SIZE]; + void init_mqtt(void); void mqtt_loop(void); -bool mqtt_publish(const String& topic, const String& payload); +bool mqtt_publish_retain(const char *topic); #endif diff --git a/Software/src/lib/knolleary-pubsubclient/PubSubClient.h b/Software/src/lib/knolleary-pubsubclient/PubSubClient.h index 7f195261..0a950acf 100644 --- a/Software/src/lib/knolleary-pubsubclient/PubSubClient.h +++ b/Software/src/lib/knolleary-pubsubclient/PubSubClient.h @@ -23,7 +23,7 @@ // MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize(). #ifndef MQTT_MAX_PACKET_SIZE -#define MQTT_MAX_PACKET_SIZE 512 +#define MQTT_MAX_PACKET_SIZE 1024 #endif // MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()