-
Notifications
You must be signed in to change notification settings - Fork 0
/
TBMS-ESP32S3.ino
156 lines (106 loc) · 3.38 KB
/
TBMS-ESP32S3.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "BMSModuleManager.h"
#include <EEPROM.h>
#include <Arduino.h>
#include "driver/gpio.h"
#include "driver/twai.h"
#include "WiFi.h"
#include <WebServer.h>
#include <ArduinoOTA.h>
#include <WiFiUdp.h>
const char *ssid = "KOLOSSUS";
const char *password = "XXX";
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
WebServer server(80);
size_t EEPROM_SIZE = 512;
#define BMS_BAUD 612500
#define RX2 8
#define TX2 18
BMSModuleManager bms;
EEPROMSettings settings;
uint32_t lastUpdate;
void loadSettings()
{
EEPROM.get(EEPROM_PAGE, settings);
if (settings.version != EEPROM_VERSION) //if settings are not the current version then erase them and set defaults
{
Serial.println("Resetting to factory defaults");
settings.version = EEPROM_VERSION;
settings.checksum = 0;
settings.canSpeed = 500000;
settings.batteryID = 0x01; //in the future should be 0xFF to force it to ask for an address
settings.OverVSetpoint = 4.1f;
settings.UnderVSetpoint = 3.4f;
settings.OverTSetpoint = 65.0f;
settings.UnderTSetpoint = -10.0f;
settings.balanceVoltage = 3.5f;
settings.balanceHyst = 0.03f;
settings.logLevel = 2;
//EEPROM.write(EEPROM_PAGE, settings);
EEPROM.put(EEPROM_PAGE, settings);
EEPROM.commit();
}
else {
Serial.println("Using stored values from EEPROM");
}
}
void handle_OnConnect() {
server.send(200, "text/html", bms.htmlPackDetails());
}
void setup()
{
delay(4000); //just for easy debugging. It takes a few seconds for USB to come up properly on most OS's
WiFi.begin(ssid, password);
server.on("/", handle_OnConnect);
server.begin();
Serial.begin(115200);
Serial.println("Starting up!");
SERIAL.begin(BMS_BAUD, SERIAL_8N1, RX2, TX2);
Serial.println("Started serial interface to BMS.");
EEPROM.begin(EEPROM_SIZE);
loadSettings();
bms.renumberBoardIDs();
lastUpdate = 0;
bms.clearFaults();
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS
// using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() { Serial.println("\nEnd"); })
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR)
Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR)
Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR)
Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR)
Serial.println("Receive Failed");
else if (error == OTA_END_ERROR)
Serial.println("End Failed");
});
ArduinoOTA.begin();
}
void loop()
{
server.handleClient();
ArduinoOTA.handle();
if (millis() > (lastUpdate + 1000))
{
lastUpdate = millis();
bms.balanceCells();
bms.getAllVoltTemp();
bms.printPackDetails();
}
}