Integrate JBD BMS telemetry into ESP32 status
This commit is contained in:
@@ -6,30 +6,59 @@
|
||||
#include "protocol.h"
|
||||
#include "relays.h"
|
||||
#include "sensors.h"
|
||||
#include "bms/bms.h"
|
||||
|
||||
WebServer server(80);
|
||||
HardwareSerial DashboardSerial(2);
|
||||
|
||||
String uartLineBuffer;
|
||||
|
||||
float batterySOC = 82.0;
|
||||
float batteryVoltage = 13.2;
|
||||
float batteryCurrent = -6.4;
|
||||
float batteryRemainingAh = 82.0;
|
||||
float batteryRuntimeHours = 12.0;
|
||||
float batteryTemp = 76.0;
|
||||
float calculateRuntimeHours() {
|
||||
if (!bmsData.valid || bmsData.current >= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float dischargeAmps = -bmsData.current;
|
||||
if (dischargeAmps <= 0.1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bmsData.remainingAh / dischargeAmps;
|
||||
}
|
||||
|
||||
void buildStatusDocument(JsonDocument& doc) {
|
||||
doc["type"] = MSG_STATUS_RESPONSE;
|
||||
doc["timestamp"] = millis();
|
||||
|
||||
JsonObject battery = doc.createNestedObject("battery");
|
||||
battery["soc"] = batterySOC;
|
||||
battery["voltage"] = batteryVoltage;
|
||||
battery["current"] = batteryCurrent;
|
||||
battery["remaining_ah"] = batteryRemainingAh;
|
||||
battery["runtime_hours"] = batteryRuntimeHours;
|
||||
battery["temperature_f"] = batteryTemp;
|
||||
|
||||
if (bmsData.valid) {
|
||||
battery["source"] = "jbd_bms";
|
||||
battery["connected"] = bmsData.connected;
|
||||
battery["soc"] = bmsData.soc;
|
||||
battery["voltage"] = bmsData.voltage;
|
||||
battery["current"] = bmsData.current;
|
||||
battery["remaining_ah"] = bmsData.remainingAh;
|
||||
battery["capacity_ah"] = bmsData.capacityAh;
|
||||
battery["runtime_hours"] = calculateRuntimeHours();
|
||||
battery["temperature_f"] = bmsData.temperatureF;
|
||||
battery["cycle_count"] = bmsData.cycleCount;
|
||||
battery["cell_count"] = bmsData.cellCount;
|
||||
battery["ntc_count"] = bmsData.ntcCount;
|
||||
} else {
|
||||
battery["source"] = "placeholder";
|
||||
battery["connected"] = false;
|
||||
battery["soc"] = 0;
|
||||
battery["voltage"] = 0;
|
||||
battery["current"] = 0;
|
||||
battery["remaining_ah"] = 0;
|
||||
battery["capacity_ah"] = 150;
|
||||
battery["runtime_hours"] = 0;
|
||||
battery["temperature_f"] = 0;
|
||||
battery["cycle_count"] = 0;
|
||||
battery["cell_count"] = 0;
|
||||
battery["ntc_count"] = 0;
|
||||
}
|
||||
|
||||
JsonObject temps = doc.createNestedObject("temps");
|
||||
|
||||
@@ -237,6 +266,7 @@ void setup() {
|
||||
|
||||
initRelays();
|
||||
initSensors();
|
||||
initBms();
|
||||
|
||||
WiFi.mode(WIFI_AP);
|
||||
|
||||
@@ -264,6 +294,7 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
server.handleClient();
|
||||
updateBms();
|
||||
pollDashboardUart();
|
||||
|
||||
static unsigned long lastSensorUpdate = 0;
|
||||
|
||||
Reference in New Issue
Block a user