Skip to content

Commit

Permalink
precommit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Christensen committed Feb 9, 2024
1 parent dff9788 commit 711aede
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 115 deletions.
6 changes: 3 additions & 3 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include "src/devboard/webserver/webserver.h"
#endif

Preferences settings; // Store user settings
const char* version_number = "5.2.0"; // The current software version, shown on webserver
Preferences settings; // Store user settings
const char* version_number = "5.2.0"; // The current software version, shown on webserver
// Interval settings
int intervalUpdateValues = 4800; // Interval at which to update inverter values / Modbus registers
const int interval10 = 10; // Interval for 10ms tasks
Expand Down Expand Up @@ -188,7 +188,7 @@ void loop() {
{
previousMillisUpdateVal = millis();
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
if(DUMMY_EVENT_ENABLED) {
if (DUMMY_EVENT_ENABLED) {
set_event(EVENT_DUMMY, (uint8_t)millis());
}
}
Expand Down
2 changes: 1 addition & 1 deletion Software/USER_SETTINGS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Maximum of 63 character
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; // Minimum of 8 characters;
const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters;
const char* passwordAP = "123456789"; // Minimum of 8 characters; set to NULL if you want the access point to be open
const uint8_t wifi_channel = 0; // set to 0 for automatic channel selection
const uint8_t wifi_channel = 0; // set to 0 for automatic channel selection
#endif
2 changes: 1 addition & 1 deletion Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
#define MQTT_PORT 1883

/* Event options*/
#define EVENTLOGGING //Enable this line to log events to the event log
#define EVENTLOGGING //Enable this line to log events to the event log
#define DUMMY_EVENT_ENABLED false //Enable this line to have a dummy event that gets logged to test the interface

/* Select charger used (Optional) */
Expand Down
5 changes: 3 additions & 2 deletions Software/src/battery/TESLA-MODEL-3-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ void update_values_tesla_model_3_battery() { //This function maps all the value
}

//Check if BMS is in need of recalibration
if (nominal_full_pack_energy > 1 &&nominal_full_pack_energy < REASONABLE_ENERGYAMOUNT) {
Serial.println("Warning: kWh remaining " + String(nominal_full_pack_energy) + " reported by battery not plausible. Battery needs cycling.");
if (nominal_full_pack_energy > 1 && nominal_full_pack_energy < REASONABLE_ENERGYAMOUNT) {
Serial.println("Warning: kWh remaining " + String(nominal_full_pack_energy) +
" reported by battery not plausible. Battery needs cycling.");
set_event(EVENT_KWH_PLAUSIBILITY_ERROR, nominal_full_pack_energy);
LEDcolor = YELLOW;
} else if (nominal_full_pack_energy <= 1) {
Expand Down
114 changes: 59 additions & 55 deletions Software/src/devboard/mqtt/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,24 @@ struct SensorConfig {
};

SensorConfig sensorConfigs[] = {
{"SOC", "homeassistant/sensor/battery-emulator/SOC/config", "Battery Emulator SOC", "{{ value_json.SOC }}", "%", "battery"},
{"state_of_health", "homeassistant/sensor/battery-emulator/state_of_health/config", "Battery Emulator State Of Health", "{{ value_json.state_of_health }}", "%", "battery"},
{"temperature_min", "homeassistant/sensor/battery-emulator/temperature_min/config", "Battery Emulator Temperature Min", "{{ value_json.temperature_min }}", "°C", "temperature"},
{"temperature_max", "homeassistant/sensor/battery-emulator/temperature_max/config", "Battery Emulator Temperature Max", "{{ value_json.temperature_max }}", "°C", "temperature"},
{"stat_batt_power", "homeassistant/sensor/battery-emulator/stat_batt_power/config", "Battery Emulator Stat Batt Power", "{{ value_json.stat_batt_power }}", "W", "power"},
{"battery_current", "homeassistant/sensor/battery-emulator/battery_current/config", "Battery Emulator Battery Current", "{{ value_json.battery_current }}", "A", "current"},
{"cell_max_voltage", "homeassistant/sensor/battery-emulator/cell_max_voltage/config", "Battery Emulator Cell Max Voltage", "{{ value_json.cell_max_voltage }}", "V", "voltage"},
{"cell_min_voltage", "homeassistant/sensor/battery-emulator/cell_min_voltage/config", "Battery Emulator Cell Min Voltage", "{{ value_json.cell_min_voltage }}", "V", "voltage"},
{"battery_voltage", "homeassistant/sensor/battery-emulator/battery_voltage/config", "Battery Emulator Battery Voltage", "{{ value_json.battery_voltage }}", "V", "voltage"},
{"SOC", "homeassistant/sensor/battery-emulator/SOC/config", "Battery Emulator SOC", "{{ value_json.SOC }}", "%",
"battery"},
{"state_of_health", "homeassistant/sensor/battery-emulator/state_of_health/config",
"Battery Emulator State Of Health", "{{ value_json.state_of_health }}", "%", "battery"},
{"temperature_min", "homeassistant/sensor/battery-emulator/temperature_min/config",
"Battery Emulator Temperature Min", "{{ value_json.temperature_min }}", "°C", "temperature"},
{"temperature_max", "homeassistant/sensor/battery-emulator/temperature_max/config",
"Battery Emulator Temperature Max", "{{ value_json.temperature_max }}", "°C", "temperature"},
{"stat_batt_power", "homeassistant/sensor/battery-emulator/stat_batt_power/config",
"Battery Emulator Stat Batt Power", "{{ value_json.stat_batt_power }}", "W", "power"},
{"battery_current", "homeassistant/sensor/battery-emulator/battery_current/config",
"Battery Emulator Battery Current", "{{ value_json.battery_current }}", "A", "current"},
{"cell_max_voltage", "homeassistant/sensor/battery-emulator/cell_max_voltage/config",
"Battery Emulator Cell Max Voltage", "{{ value_json.cell_max_voltage }}", "V", "voltage"},
{"cell_min_voltage", "homeassistant/sensor/battery-emulator/cell_min_voltage/config",
"Battery Emulator Cell Min Voltage", "{{ value_json.cell_min_voltage }}", "V", "voltage"},
{"battery_voltage", "homeassistant/sensor/battery-emulator/battery_voltage/config",
"Battery Emulator Battery Voltage", "{{ value_json.battery_voltage }}", "V", "voltage"},
};

static void publish_common_info(void) {
Expand All @@ -130,58 +139,53 @@ static void publish_common_info(void) {
for (int i = 0; i < sizeof(sensorConfigs) / sizeof(sensorConfigs[0]); i++) {
SensorConfig& config = sensorConfigs[i];
snprintf(mqtt_msg, sizeof(mqtt_msg),
"{"
"\"name\": \"%s\","
"\"state_topic\": \"%s\","
"\"unique_id\": \"battery-emulator_%s\","
"\"object_id\": \"sensor_battery_%s\","
"\"device\": {"
"\"identifiers\": ["
"\"battery-emulator\""
"],"
"\"manufacturer\": \"DalaTech\","
"\"model\": \"BatteryEmulator\","
"\"name\": \"BatteryEmulator\""
"},"
"\"origin\": {"
"\"name\": \"BatteryEmulator\","
"\"sw\": \"%s-mqtt\","
"\"url\": \"https://github.com/dalathegreat/Battery-Emulator\""
"},"
"\"value_template\": \"%s\","
"\"unit_of_measurement\": \"%s\","
"\"device_class\": \"%s\","
"\"enabled_by_default\": true,"
"\"state_class\": \"measurement\""
"}",
config.name, state_topic, config.object_id, config.object_id, version_number, config.value_template, config.unit, config.device_class);
"{"
"\"name\": \"%s\","
"\"state_topic\": \"%s\","
"\"unique_id\": \"battery-emulator_%s\","
"\"object_id\": \"sensor_battery_%s\","
"\"device\": {"
"\"identifiers\": ["
"\"battery-emulator\""
"],"
"\"manufacturer\": \"DalaTech\","
"\"model\": \"BatteryEmulator\","
"\"name\": \"BatteryEmulator\""
"},"
"\"origin\": {"
"\"name\": \"BatteryEmulator\","
"\"sw\": \"%s-mqtt\","
"\"url\": \"https://github.com/dalathegreat/Battery-Emulator\""
"},"
"\"value_template\": \"%s\","
"\"unit_of_measurement\": \"%s\","
"\"device_class\": \"%s\","
"\"enabled_by_default\": true,"
"\"state_class\": \"measurement\""
"}",
config.name, state_topic, config.object_id, config.object_id, version_number, config.value_template,
config.unit, config.device_class);
mqtt_publish_retain(config.topic);
}
} else {
snprintf(mqtt_msg, sizeof(mqtt_msg),
"{\n"
" \"SOC\": %.3f,\n"
" \"state_of_health\": %.3f,\n"
" \"temperature_min\": %.3f,\n"
" \"temperature_max\": %.3f,\n"
" \"stat_batt_power\": %.3f,\n"
" \"battery_current\": %.3f,\n"
" \"cell_max_voltage\": %d,\n"
" \"cell_min_voltage\": %d,\n"
" \"battery_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,
((float)((int16_t)stat_batt_power)) / 10.0,
((float)((int16_t)battery_current)) / 10.0,
cell_max_voltage,
cell_min_voltage,
battery_voltage / 10.0);
"{\n"
" \"SOC\": %.3f,\n"
" \"state_of_health\": %.3f,\n"
" \"temperature_min\": %.3f,\n"
" \"temperature_max\": %.3f,\n"
" \"stat_batt_power\": %.3f,\n"
" \"battery_current\": %.3f,\n"
" \"cell_max_voltage\": %d,\n"
" \"cell_min_voltage\": %d,\n"
" \"battery_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, ((float)((int16_t)stat_batt_power)) / 10.0,
((float)((int16_t)battery_current)) / 10.0, cell_max_voltage, cell_min_voltage, battery_voltage / 10.0);
bool result = client.publish(state_topic, mqtt_msg, true);
}

//Serial.println(mqtt_msg); // Uncomment to print the payload on serial
}

Expand Down
2 changes: 1 addition & 1 deletion Software/src/devboard/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#define MQTT_MSG_BUFFER_SIZE (1024)

extern const char* version_number; // The current software version, used for mqtt
extern const char* version_number; // The current software version, used for mqtt

extern uint16_t SOC;
extern uint16_t StateOfHealth;
Expand Down
19 changes: 11 additions & 8 deletions Software/src/devboard/utils/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void set_event(EVENTS_ENUM_TYPE event, uint8_t data) {
++entries[event].occurences;
set_event_message(event);
#ifdef DEBUG_VIA_USB
Serial.println("Set event: " + String(get_event_enum_string(event)) + ". Has occured " + String(entries[event].occurences) + " times");
Serial.println("Set event: " + String(get_event_enum_string(event)) + ". Has occured " +
String(entries[event].occurences) + " times");
#endif
#endif
}
Expand Down Expand Up @@ -93,9 +94,11 @@ const char* get_event_message(EVENTS_ENUM_TYPE event) {
case EVENT_BATTERY_CHG_DISCHG_STOP_REQ:
return "ERROR: Battery raised caution indicator AND requested charge/discharge stop. Inspect battery status!";
case EVENT_LOW_SOH:
return "ERROR: State of health critically low. Battery internal resistance too high to continue. Recycle battery.";
return "ERROR: State of health critically low. Battery internal resistance too high to continue. Recycle "
"battery.";
case EVENT_HVIL_FAILURE:
return "ERROR: Battery interlock loop broken. Check that high voltage connectors are seated. Battery will be disabled!";
return "ERROR: Battery interlock loop broken. Check that high voltage connectors are seated. Battery will be "
"disabled!";
case EVENT_INTERNAL_OPEN_FAULT:
return "ERROR: High voltage cable removed while battery running. Opening contactors!";
case EVENT_CELL_UNDER_VOLTAGE:
Expand All @@ -114,11 +117,11 @@ const char* get_event_message(EVENTS_ENUM_TYPE event) {
}

const char* get_event_enum_string(EVENTS_ENUM_TYPE event) {
const char* fullString = EVENTS_ENUM_TYPE_STRING[event];
if (strncmp(fullString, "EVENT_", 6) == 0) {
return fullString + 6; // Skip the first 6 characters
}
return fullString;
const char* fullString = EVENTS_ENUM_TYPE_STRING[event];
if (strncmp(fullString, "EVENT_", 6) == 0) {
return fullString + 6; // Skip the first 6 characters
}
return fullString;
}

static void set_event_message(EVENTS_ENUM_TYPE event) {
Expand Down
46 changes: 21 additions & 25 deletions Software/src/devboard/utils/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,32 @@ extern uint32_t time_seconds;

#include <stdint.h>

#define EVENTS_ENUM_TYPE(XX) \
XX(EVENT_CAN_FAILURE) \
XX(EVENT_CAN_WARNING) \
XX(EVENT_WATER_INGRESS) \
XX(EVENT_12V_LOW) \
XX(EVENT_SOC_PLAUSIBILITY_ERROR) \
XX(EVENT_KWH_PLAUSIBILITY_ERROR) \
XX(EVENT_BATTERY_CHG_STOP_REQ) \
XX(EVENT_BATTERY_DISCHG_STOP_REQ) \
XX(EVENT_BATTERY_CHG_DISCHG_STOP_REQ) \
XX(EVENT_LOW_SOH) \
XX(EVENT_HVIL_FAILURE) \
XX(EVENT_INTERNAL_OPEN_FAULT) \
XX(EVENT_CELL_UNDER_VOLTAGE) \
XX(EVENT_CELL_OVER_VOLTAGE) \
XX(EVENT_CELL_DEVIATION_HIGH) \
XX(EVENT_UNKNOWN_EVENT_SET) \
XX(EVENT_DUMMY) \
XX(EVENT_NOF_EVENTS)
#define EVENTS_ENUM_TYPE(XX) \
XX(EVENT_CAN_FAILURE) \
XX(EVENT_CAN_WARNING) \
XX(EVENT_WATER_INGRESS) \
XX(EVENT_12V_LOW) \
XX(EVENT_SOC_PLAUSIBILITY_ERROR) \
XX(EVENT_KWH_PLAUSIBILITY_ERROR) \
XX(EVENT_BATTERY_CHG_STOP_REQ) \
XX(EVENT_BATTERY_DISCHG_STOP_REQ) \
XX(EVENT_BATTERY_CHG_DISCHG_STOP_REQ) \
XX(EVENT_LOW_SOH) \
XX(EVENT_HVIL_FAILURE) \
XX(EVENT_INTERNAL_OPEN_FAULT) \
XX(EVENT_CELL_UNDER_VOLTAGE) \
XX(EVENT_CELL_OVER_VOLTAGE) \
XX(EVENT_CELL_DEVIATION_HIGH) \
XX(EVENT_UNKNOWN_EVENT_SET) \
XX(EVENT_DUMMY) \
XX(EVENT_NOF_EVENTS)

#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,

typedef enum {
EVENTS_ENUM_TYPE(GENERATE_ENUM)
} EVENTS_ENUM_TYPE;
typedef enum { EVENTS_ENUM_TYPE(GENERATE_ENUM) } EVENTS_ENUM_TYPE;

static const char *EVENTS_ENUM_TYPE_STRING[] = {
EVENTS_ENUM_TYPE(GENERATE_STRING)
};
static const char* EVENTS_ENUM_TYPE_STRING[] = {EVENTS_ENUM_TYPE(GENERATE_STRING)};

const char* get_event_enum_string(EVENTS_ENUM_TYPE event);

Expand Down
35 changes: 18 additions & 17 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ const char index_html[] PROGMEM = R"rawliteral(
)rawliteral";

enum WifiState {
INIT, //before connecting first time
RECONNECTING, //we've connected before, but lost connection
CONNECTED //we are connected
INIT, //before connecting first time
RECONNECTING, //we've connected before, but lost connection
CONNECTED //we are connected
};

WifiState wifi_state = INIT;

unsigned const long WIFI_MONITOR_INTERVAL_TIME = 15000;
unsigned const long INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds
unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms
unsigned const long MAX_WIFI_RETRY_INTERVAL = 30000; // Maximum wifi retry interval in ms
unsigned long last_wifi_monitor_time = millis(); //init millis so wifi monitor doesn't run immediately
unsigned const long INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds
unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms
unsigned const long MAX_WIFI_RETRY_INTERVAL = 30000; // Maximum wifi retry interval in ms
unsigned long last_wifi_monitor_time = millis(); //init millis so wifi monitor doesn't run immediately
unsigned long wifi_reconnect_interval = DEFAULT_WIFI_RECONNECT_INTERVAL;
unsigned long last_wifi_attempt_time = millis(); //init millis so wifi monitor doesn't run immediately
unsigned long last_wifi_attempt_time = millis(); //init millis so wifi monitor doesn't run immediately

void init_webserver() {
// Configure WiFi
Expand All @@ -76,9 +76,8 @@ void init_webserver() {
});

#ifdef EVENTLOGGING
server.on("/events", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send_P(200, "text/html", index_html, events_processor);
});
server.on("/events", HTTP_GET,
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, events_processor); });
#endif

// Route for editing Wh
Expand Down Expand Up @@ -295,14 +294,14 @@ String getConnectResultString(wl_status_t status) {

void wifi_monitor() {
unsigned long currentMillis = millis();
if(currentMillis - last_wifi_monitor_time > WIFI_MONITOR_INTERVAL_TIME) {
if (currentMillis - last_wifi_monitor_time > WIFI_MONITOR_INTERVAL_TIME) {
last_wifi_monitor_time = currentMillis;
wl_status_t status = WiFi.status();
if (status != WL_CONNECTED && status != WL_IDLE_STATUS) {
Serial.println(getConnectResultString(status));
if(wifi_state == INIT) { //we haven't been connected yet, try the init logic
if (wifi_state == INIT) { //we haven't been connected yet, try the init logic
init_WiFi_STA(ssid, password, wifi_channel);
} else { //we were connected before, try the reconnect logic
} else { //we were connected before, try the reconnect logic
if (currentMillis - last_wifi_attempt_time > wifi_reconnect_interval) {
last_wifi_attempt_time = currentMillis;
Serial.println("WiFi not connected, trying to reconnect...");
Expand Down Expand Up @@ -975,8 +974,10 @@ String events_processor(const String& var) {
content.reserve(5000);
// Page format
content.concat(FPSTR(EVENTS_HTML_START));
for(int i = 0; i < EVENT_NOF_EVENTS; i++) {
Serial.println("Event: " + String(get_event_enum_string(static_cast<EVENTS_ENUM_TYPE>(i))) + " count: " + String(entries[i].occurences) + " seconds: " + String(entries[i].timestamp) + " data: " + String(entries[i].data));
for (int i = 0; i < EVENT_NOF_EVENTS; i++) {
Serial.println("Event: " + String(get_event_enum_string(static_cast<EVENTS_ENUM_TYPE>(i))) +
" count: " + String(entries[i].occurences) + " seconds: " + String(entries[i].timestamp) +
" data: " + String(entries[i].data));
if (entries[i].occurences > 0) {
content.concat("<div class='event'>");
content.concat("<div>" + String(get_event_enum_string(static_cast<EVENTS_ENUM_TYPE>(i))) + "</div>");
Expand All @@ -985,7 +986,7 @@ String events_processor(const String& var) {
content.concat("<div>" + String(entries[i].occurences) + "</div>");
content.concat("<div>" + String(entries[i].data) + "</div>");
content.concat("<div>" + String(get_event_message(static_cast<EVENTS_ENUM_TYPE>(i))) + "</div>");
content.concat("</div>"); // End of event row
content.concat("</div>"); // End of event row
}
}
content.concat(FPSTR(EVENTS_HTML_END));
Expand Down
2 changes: 1 addition & 1 deletion Software/src/devboard/webserver/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "../mqtt/mqtt.h"
#endif

extern const char* version_number; // The current software version, shown on webserver
extern const char* version_number; // The current software version, shown on webserver
extern uint16_t SOC; //SOC%, 0-100.00 (0-10000)
extern uint16_t StateOfHealth; //SOH%, 0-100.00 (0-10000)
extern uint16_t battery_voltage; //V+1, 0-500.0 (0-5000)
Expand Down
2 changes: 1 addition & 1 deletion test/utils/events_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#define DUMMY_EVENT_ENABLED true

/* Helper functions */
static char event_message[256]; // Declare the event_message variable
static char event_message[256]; // Declare the event_message variable

static void reset_event_msg(void) {
snprintf(event_message, sizeof(event_message), "");
Expand Down

0 comments on commit 711aede

Please sign in to comment.