Skip to content

Commit

Permalink
Wrapping up the POC
Browse files Browse the repository at this point in the history
Increased the msg buffer to allow cell voltages to be transmitted (at least Leaf). Tested OK
  • Loading branch information
Cabooman committed Feb 1, 2024
1 parent 44e8e0b commit 620db2c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
28 changes: 28 additions & 0 deletions Software/src/battery/NISSAN-LEAF-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

#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
20 changes: 12 additions & 8 deletions Software/src/devboard/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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);

/** 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"
Expand All @@ -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) */
Expand Down Expand Up @@ -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);
}
6 changes: 4 additions & 2 deletions Software/src/devboard/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <Arduino.h>
#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"
Expand All @@ -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
2 changes: 1 addition & 1 deletion Software/src/lib/knolleary-pubsubclient/PubSubClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 620db2c

Please sign in to comment.