#include "bms.h"
#define NUM_CELLS 3
OverkillSolarBms bms = OverkillSolarBms();
uint32_t last_update;
void setup() {
Serial.begin(115200);
Serial1.begin(9600);
while (!Serial) { // Wait for the debug serial port to initialize
}
while (!Serial1) { // Wait for the BMS serial port to initialize
}
last_update = millis();
bms.begin(&Serial1);
}
void loop() {
bms.main_task();
// print status every 2.5 seconds
if (millis() - last_update >= 2500) {
// Get voltage
float voltage = bms.get_voltage();
Serial.print("Voltage: ");
Serial.print(voltage, 3);
Serial.println(" volts");
// Get current
float current = bms.get_current();
Serial.print("Current:" );
Serial.print(current, 1);
Serial.println(" amps");
// Get voltage diff
float lowest = 99;
float highest = 0;
float v = 0;
for (int i = 0; i < NUM_CELLS; i++) {
v = bms.get_cell_voltage(i);
if (v < lowest) {
lowest = v;
}
if (v > highest) {
highest = v;
}
}
float voltageDiff = highest - lowest;
// Get state of charge
uint8_t soc = bms.get_state_of_charge();
// Get charge fet status
bool cFet = bms.get_charge_mosfet_status() ? "on" : "off";
// Get discharge fet status
bool dFet = bms.get_discharge_mosfet_status() ? "on" : "off";
// // Get flags
ProtectionStatus flags = bms.get_protection_status();
String flagString = "{";
flagString += (String)"single_cell_overvoltage_protection" + ":" + flags.single_cell_overvoltage_protection;
flagString += (String)",single_cell_undervoltage_protection" + ":" + flags.single_cell_undervoltage_protection;
flagString += (String)",whole_pack_overvoltage_protection" + ":" + flags.whole_pack_overvoltage_protection;
flagString += (String)",whole_pack_undervoltage_protection" + ":" + flags.whole_pack_undervoltage_protection;
flagString += (String)",charging_over_temperature_protection" + ":" + flags.charging_over_temperature_protection;
flagString += (String)",charging_low_temperature_protection" + ":" + flags.charging_low_temperature_protection;
flagString += (String)",discharge_over_temperature_protection" + ":" + flags.discharge_over_temperature_protection;
flagString += (String)",discharge_low_temperature_protection" + ":" + flags.discharge_low_temperature_protection;
flagString += (String)",charging_overcurrent_protection" + ":" + flags.charging_overcurrent_protection;
flagString += (String)",discharge_overcurrent_protection" + ":" + flags.discharge_overcurrent_protection;
flagString += (String)",short_circuit_protection" + ":" + flags.short_circuit_protection;
flagString += (String)",front_end_detection_ic_error" + ":" + flags.front_end_detection_ic_error;
flagString += (String)",software_lock_mos" + ":" + flags.software_lock_mos;
flagString += "}";
// Get bms name
String name = bms.get_bms_name();
// Build the json we'll send
String jsonStr = "{";
jsonStr += "name:" + (String)name;
jsonStr += ",voltage:" + (String)voltage;
jsonStr += ",current:" + (String)current;
jsonStr += ",voltageDiff:" + (String)voltageDiff;
jsonStr += ",soc:" + (String)soc;
jsonStr += ",cFet:" + (String)cFet;
jsonStr += ",dFet:" + (String)dFet;
jsonStr += ",lowestCell:" + (String)lowest;
jsonStr += ",highestCell:" + (String)highest;
// Serial.print(flagString);
Serial.print(jsonStr);
Serial.println();
last_update = millis();
}
}