Wrapping up the POC

Increased the msg buffer to allow cell voltages to be transmitted (at least Leaf). Tested OK
This commit is contained in:
Cabooman 2024-02-01 22:25:21 +01:00
parent 44e8e0bb06
commit 620db2cc33
4 changed files with 45 additions and 11 deletions

View file

@ -1,4 +1,7 @@
#include "NISSAN-LEAF-BATTERY.h" #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/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.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_max = 0;
static int16_t temp_polled_min = 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) { void print_with_units(char* header, int value, char* units) {
Serial.print(header); Serial.print(header);
Serial.print(value); Serial.print(value);
@ -402,6 +409,9 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
} }
#endif #endif
#ifdef MQTT
publish_data();
#endif
} }
void receive_can_leaf_battery(CAN_frame_t rx_frame) { 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<uint16_t>(1094 + (309 - temperature) * 2.5714285714285715); return static_cast<uint16_t>(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

View file

@ -11,7 +11,7 @@ const size_t mqtt_nof_subscriptions = sizeof(mqtt_subscriptions) / sizeof(mqtt_s
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
char msg[MSG_BUFFER_SIZE]; char mqtt_msg[MSG_BUFFER_SIZE];
int value = 0; int value = 0;
static unsigned long previousMillisUpdateVal; static unsigned long previousMillisUpdateVal;
MyTimer publish_global_timer(5000); MyTimer publish_global_timer(5000);
@ -19,7 +19,7 @@ MyTimer publish_global_timer(5000);
/** Publish global values and call callbacks for specific modules */ /** Publish global values and call callbacks for specific modules */
static void publish_values(void) { static void publish_values(void) {
snprintf(msg, sizeof(msg), snprintf(mqtt_msg, sizeof(mqtt_msg),
"{\n" "{\n"
" \"SOC\": %.3f,\n" " \"SOC\": %.3f,\n"
" \"StateOfHealth\": %.3f,\n" " \"StateOfHealth\": %.3f,\n"
@ -28,10 +28,14 @@ static void publish_values(void) {
" \"cell_max_voltage\": %d,\n" " \"cell_max_voltage\": %d,\n"
" \"cell_min_voltage\": %d\n" " \"cell_min_voltage\": %d\n"
"}\n", "}\n",
((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0, ((float)SOC) / 100.0,
((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage); ((float)StateOfHealth) / 100.0,
bool result = client.publish("battery_testing/info", msg, true); ((float)((int16_t)temperature_min)) / 10.0,
Serial.println(msg); // Uncomment to print the payload on serial ((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) */ /* 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) { bool mqtt_publish_retain(const char *topic) {
return client.publish(topic.c_str(), payload.c_str()); return client.publish(topic, mqtt_msg, true);
} }

View file

@ -37,7 +37,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "../../../USER_SETTINGS.h" #include "../../../USER_SETTINGS.h"
#define MSG_BUFFER_SIZE (256) #define MSG_BUFFER_SIZE (1024)
#define MQTT_SUBSCRIPTIONS \ #define MQTT_SUBSCRIPTIONS \
{ "my/topic/abc", "my/other/topic" } { "my/topic/abc", "my/other/topic" }
#define MQTT_SERVER "192.168.xxx.yyy" #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_user;
extern const char* mqtt_password; extern const char* mqtt_password;
extern char mqtt_msg[MSG_BUFFER_SIZE];
void init_mqtt(void); void init_mqtt(void);
void mqtt_loop(void); void mqtt_loop(void);
bool mqtt_publish(const String& topic, const String& payload); bool mqtt_publish_retain(const char *topic);
#endif #endif

View file

@ -23,7 +23,7 @@
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize(). // MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
#ifndef MQTT_MAX_PACKET_SIZE #ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 512 #define MQTT_MAX_PACKET_SIZE 1024
#endif #endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive() // MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()