Skip to content

Commit

Permalink
Merge pull request #166 from kharnt0x/event-log-mqtt-auto
Browse files Browse the repository at this point in the history
Fix Wifi loop more, Event log, more mqtt autoconfig for homeassistant
  • Loading branch information
dalathegreat authored Feb 10, 2024
2 parents 0bd947d + d3300f3 commit 2306af7
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
*.vscode/

# Ignore any files in the build folder
Software/build/
Software/build/
15 changes: 12 additions & 3 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "src/battery/BATTERIES.h"
#include "src/charger/CHARGERS.h"
#include "src/devboard/config.h"
#include "src/devboard/utils/events.h"
#include "src/inverter/INVERTERS.h"
#include "src/lib/adafruit-Adafruit_NeoPixel/Adafruit_NeoPixel.h"
#include "src/lib/eModbus-eModbus/Logging.h"
Expand All @@ -20,8 +21,8 @@
#include "src/devboard/webserver/webserver.h"
#endif

Preferences settings; // Store user settings

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 @@ -129,6 +130,10 @@ void setup() {
init_webserver();
#endif

#ifdef EVENTLOGGING
init_events();
#endif

init_CAN();

init_LED();
Expand All @@ -153,8 +158,8 @@ void loop() {

#ifdef WEBSERVER
// Over-the-air updates by ElegantOTA
wifi_monitor();
ElegantOTA.loop();
WiFi_monitor_loop();
#ifdef MQTT
mqtt_loop();
#endif
Expand Down Expand Up @@ -183,13 +188,17 @@ 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) {
set_event(EVENT_DUMMY, (uint8_t)millis());
}
}

// Output
send_can(); // Send CAN messages
#ifdef DUAL_CAN
send_can2();
#endif
update_event_timestamps();
}

// Initialization functions
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 char* versionNumber = "5.0.1"; // The current software version, shown on webserver
const uint8_t wifi_channel = 0; // set to 0 for automatic channel selection
#endif
6 changes: 6 additions & 0 deletions Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#define MQTT_SERVER "192.168.xxx.yyy"
#define MQTT_PORT 1883

/* Event options*/
#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) */
//#define CHEVYVOLT_CHARGER //Enable this line to control a Chevrolet Volt charger connected to battery - for example, when generator charging or using an inverter without a charging function.
//#define NISSANLEAF_CHARGER //Enable this line to control a Nissan LEAF PDM connected to battery - for example, when generator charging
Expand All @@ -71,4 +75,6 @@ extern volatile float CHARGER_END_A;
extern bool charger_HV_enabled;
extern bool charger_aux12V_enabled;

extern const uint8_t wifi_channel;

#endif
2 changes: 2 additions & 0 deletions Software/src/battery/BMW-I3-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "BMW-I3-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -103,6 +104,7 @@ void update_values_i3_battery() { //This function maps all the values fetched v
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}
Expand Down
2 changes: 2 additions & 0 deletions Software/src/battery/CHADEMO-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CHADEMO-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -107,6 +108,7 @@ void update_values_chademo_battery() { //This function maps all the values fetc
bms_status = FAULT;
errorCode = 7;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}
Expand Down
2 changes: 2 additions & 0 deletions Software/src/battery/IMIEV-CZERO-ION-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "IMIEV-CZERO-ION-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -109,6 +110,7 @@ void update_values_imiev_battery() { //This function maps all the values fetche
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}
Expand Down
7 changes: 7 additions & 0 deletions Software/src/battery/KIA-HYUNDAI-64-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "KIA-HYUNDAI-64-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -200,18 +201,21 @@ void update_values_kiaHyundai_64_battery() { //This function maps all the value
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}

if (waterleakageSensor == 0) {
Serial.println("Water leakage inside battery detected. Operation halted. Inspect battery!");
bms_status = FAULT;
set_event(EVENT_WATER_INGRESS, 0);
}

if (leadAcidBatteryVoltage < 110) {
Serial.println("12V battery source below required voltage to safely close contactors. Inspect the supply/battery!");
LEDcolor = YELLOW;
set_event(EVENT_12V_LOW, leadAcidBatteryVoltage);
}

// Check if cell voltages are within allowed range
Expand All @@ -220,14 +224,17 @@ void update_values_kiaHyundai_64_battery() { //This function maps all the value
if (cell_max_voltage >= MAX_CELL_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_OVER_VOLTAGE, 0);
}
if (cell_min_voltage <= MIN_CELL_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_UNDER_VOLTAGE, 0);
}
if (cell_deviation_mV > MAX_CELL_DEVIATION) {
LEDcolor = YELLOW;
Serial.println("ERROR: HIGH CELL DEVIATION!!! Inspect battery!");
set_event(EVENT_CELL_DEVIATION_HIGH, 0);
}

if (bms_status == FAULT) { //Incase we enter a critical fault state, zero out the allowed limits
Expand Down
12 changes: 12 additions & 0 deletions Software/src/battery/NISSAN-LEAF-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifdef MQTT
#include "../devboard/mqtt/mqtt.h"
#endif
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -262,6 +263,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#ifdef DEBUG_VIA_USB
Serial.println("ERROR: SOC% reported by battery not plausible. Restart battery!");
#endif
set_event(EVENT_SOC_PLAUSIBILITY_ERROR, LB_SOC / 10);
}
}

Expand Down Expand Up @@ -311,6 +313,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#ifdef DEBUG_VIA_USB
Serial.println("ERROR: Battery raised caution indicator AND requested discharge stop. Inspect battery status!");
#endif
set_event(EVENT_BATTERY_DISCHG_STOP_REQ, 0);
break;
case (6):
//Caution Lamp Request & Charging Mode Stop Request
Expand All @@ -319,6 +322,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#ifdef DEBUG_VIA_USB
Serial.println("ERROR: Battery raised caution indicator AND requested charge stop. Inspect battery status!");
#endif
set_event(EVENT_BATTERY_CHG_STOP_REQ, 0);
break;
case (7):
//Caution Lamp Request & Charging Mode Stop Request & Normal Stop Request
Expand All @@ -328,6 +332,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
Serial.println(
"ERROR: Battery raised caution indicator AND requested charge/discharge stop. Inspect battery status!");
#endif
set_event(EVENT_BATTERY_CHG_DISCHG_STOP_REQ, 0);
break;
default:
break;
Expand All @@ -342,6 +347,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#endif
bms_status = FAULT;
errorCode = 5;
set_event(EVENT_LOW_SOH, LB_StateOfHealth);
max_target_discharge_power = 0;
max_target_charge_power = 0;
}
Expand All @@ -355,6 +361,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
"disabled!");
#endif
bms_status = FAULT;
set_event(EVENT_HVIL_FAILURE, 0);
errorCode = 6;
SOC = 0;
max_target_discharge_power = 0;
Expand All @@ -369,6 +376,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#ifdef DEBUG_VIA_USB
Serial.println("ERROR: No CAN communication detected for 60s. Shutting down battery control.");
#endif
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}
Expand All @@ -380,6 +388,7 @@ void update_values_leaf_battery() { /* This function maps all the values fetched
#ifdef DEBUG_VIA_USB
Serial.println("ERROR: High amount of corrupted CAN messages detected. Check CAN wire shielding!");
#endif
set_event(EVENT_CAN_WARNING, 0);
}

/*Finally print out values to serial if configured to do so*/
Expand Down Expand Up @@ -615,6 +624,7 @@ void receive_can_leaf_battery(CAN_frame_t rx_frame) {
#ifdef DEBUG_VIA_USB
Serial.println("HIGH CELL DEVIATION!!! Inspect battery!");
#endif
set_event(EVENT_CELL_DEVIATION_HIGH, 0);
}

if (min_max_voltage[1] >= MAX_CELL_VOLTAGE) {
Expand All @@ -623,13 +633,15 @@ void receive_can_leaf_battery(CAN_frame_t rx_frame) {
#ifdef DEBUG_VIA_USB
Serial.println("CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
#endif
set_event(EVENT_CELL_OVER_VOLTAGE, 0);
}
if (min_max_voltage[0] <= MIN_CELL_VOLTAGE) {
bms_status = FAULT;
errorCode = 9;
#ifdef DEBUG_VIA_USB
Serial.println("CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
#endif
set_event(EVENT_CELL_UNDER_VOLTAGE, 0);
}
break;
}
Expand Down
5 changes: 5 additions & 0 deletions Software/src/battery/RENAULT-KANGOO-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "RENAULT-KANGOO-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -126,21 +127,25 @@ void update_values_kangoo_battery() { //This function maps all the values fetch
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}

if (LB_Cell_Max_Voltage >= ABSOLUTE_CELL_MAX_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_OVER_VOLTAGE, 0);
}
if (LB_Cell_Min_Voltage <= ABSOLUTE_CELL_MIN_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_UNDER_VOLTAGE, 0);
}
if (cell_deviation_mV > MAX_CELL_DEVIATION_MV) {
LEDcolor = YELLOW;
Serial.println("ERROR: HIGH CELL mV DEVIATION!!! Inspect battery!");
set_event(EVENT_CELL_DEVIATION_HIGH, 0);
}

#ifdef DEBUG_VIA_USB
Expand Down
5 changes: 5 additions & 0 deletions Software/src/battery/RENAULT-ZOE-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "RENAULT-ZOE-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -87,21 +88,25 @@ void update_values_zoe_battery() { //This function maps all the values fetched
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}

if (LB_Cell_Max_Voltage >= ABSOLUTE_CELL_MAX_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL OVERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_OVER_VOLTAGE, 0);
}
if (LB_Cell_Min_Voltage <= ABSOLUTE_CELL_MIN_VOLTAGE) {
bms_status = FAULT;
Serial.println("ERROR: CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!");
set_event(EVENT_CELL_UNDER_VOLTAGE, 0);
}
if (cell_deviation_mV > MAX_CELL_DEVIATION_MV) {
LEDcolor = YELLOW;
Serial.println("ERROR: HIGH CELL mV DEVIATION!!! Inspect battery!");
set_event(EVENT_CELL_DEVIATION_HIGH, 0);
}

#ifdef DEBUG_VIA_USB
Expand Down
2 changes: 2 additions & 0 deletions Software/src/battery/SANTA-FE-PHEV-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "SANTA-FE-PHEV-BATTERY.h"
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

Expand Down Expand Up @@ -84,6 +85,7 @@ void update_values_santafe_phev_battery() { //This function maps all the values
if (!CANstillAlive) {
bms_status = FAULT;
Serial.println("No CAN communication detected for 60s. Shutting down battery control.");
set_event(EVENT_CAN_FAILURE, 0);
} else {
CANstillAlive--;
}
Expand Down
Loading

0 comments on commit 2306af7

Please sign in to comment.