-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBMS_new_180602_WiFi_tryPROGMEM.ino
257 lines (228 loc) · 10.1 KB
/
BMS_new_180602_WiFi_tryPROGMEM.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <LiquidCrystal.h>
const char string_0[] PROGMEM = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: xxx\r\n\r\n<!DOCTYPE html><html><head><title>Will's PowerWall</title></head>";
const char string_1[] PROGMEM = "<body background = \"https://cdn.wallpapersafari.com/18/9/3sTDBS.jpg\">";
const char string_2[] PROGMEM = "<h1><center><b>Will's PowerWall Status</b></center></h1><table border = \"0\" width = 100%>";
const char string_3[] PROGMEM = "<tr><td width = 33%><h2><center>Commanded </center></h2></td><td width = 34%><h2><center>Inverter Status</center></h2></td><td width = 33%><h2><center>Pack Voltage</center></h2></td></tr><tr><td><h3><center>";
const char string_4[] PROGMEM = "</center></h3></td><td><h3><center>";
const char string_5[] PROGMEM = " V</center></h3></td></tr><tr><td><h3></br></h3></td></tr><tr><td><h2><center>Lowest Cell</center></h2></td><td><h2><center>Why Off?</center></h2></td><td><h2><center>Charge Time</center></h2></td></tr><tr><td><h3><center>";
const char string_6[] PROGMEM = "v </center></h3></td><td><h3><center> ";
const char string_7[] PROGMEM = " </center></h3></td><td><h3><center> ";
const char string_8[] PROGMEM = " Mins</center></h3></td></tr></table></body></html>\r\n";
const char* const string_table[] PROGMEM =
{
string_0,
string_1,
string_2,
string_3,
string_4,
string_5,
string_6,
string_7,
string_8,
};
char longstring[1000];char str1[11];
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int lowloaddisc = 1; //0 means disabled, 1 means enabled, 2, while running, means currently low-load powered-down
int RemoteEnable = 0;
int RemoteInputPin = 9;
int InverterControlPin = 6;
int InverterStatusPin = 8;
int ledpin = 13;
float TurnOnVoltage = 15.3;
float ShutdownVoltage = 11.80;
float ShutdownCellVoltage = 2.70;
unsigned long PowerDownMillis = 0;
unsigned long PowerUpMillis = 0;
unsigned long OnTime = 0;
unsigned long PUDelay = 300000; //300000 for 5 minutes-ish
int i;float myArray[5];
int CurrentInverterStatus = 2;
int UserDesiredInverterStatus;
float PackVoltage; float CellVoltage0; float CellVoltage1; float CellVoltage2; float CellVoltage3;
float LowestCellVoltage; float HighestCellVoltage; float cellArray[4];
int connectionId;
String cipSend;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(InverterControlPin, OUTPUT);
pinMode(ledpin, OUTPUT);
pinMode(InverterStatusPin, INPUT);
pinMode(RemoteInputPin, INPUT);
delay(2000);
sendData("AT+RST\r\n",6000); // reset module
// sendData("AT+CWMODE_DEF=1\r\n", 5000); // I used a command like this to setup esp8266 initially.
// sendData("AT+CWJAP_DEF=\"MySSIDhere\",\"mypassword\"\r\n", 5000); // this too for initial setup...remove the \ if sending directly
// sendData("AT+CWAUTOCONN=1\r\n", 5000); // might need this too for initial setup
// sendData("AT+UART_DEF=115200,8,1,0,0\r\n", 5000); // maybe this too for initial setup. I needed 115200 to get iOS to load the webpage.
sendData("AT+CIPMUX=1\r\n",200); // configure for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",500); // turn on server on port 80
digitalWrite(ledpin,HIGH);
}
void loop() {
OnTime = millis() / 1000; // In Seconds
if(CurrentInverterStatus == 1) PowerDownMillis = millis();
getInvStatus();
int AnalogInput0 = 0;
for (i=0;i<10;i=i+1) {
AnalogInput0 = AnalogInput0 + analogRead(A0);
delay(25);
}
AnalogInput0 = AnalogInput0 / 10;
int AnalogInput1 = analogRead(A1);
int AnalogInput2 = analogRead(A2);
int AnalogInput3 = analogRead(A3);
delay(1000);
PackVoltage = AnalogInput0 * .02087;
CellVoltage0 = PackVoltage - (AnalogInput1 * .0209);
CellVoltage1 = PackVoltage - CellVoltage0 - (AnalogInput2 * .0210);
CellVoltage3 = AnalogInput3 * .0210;
CellVoltage2 = (AnalogInput2 * .02085) - CellVoltage3;
MinMax();
if(bitRead(OnTime,2) == 0)
DisplayStatus();
if(bitRead(OnTime,2) == 1)
DisplayCells();
lcd.setCursor(0,1); lcd.print("Total "); lcd.print(PackVoltage);
lcd.setCursor(15,1);
if(UserDesiredInverterStatus == 1)lcd.print(".");
if(UserDesiredInverterStatus == 0)lcd.print(" ");
for (i=0;i<4;i=i+1) {myArray[i] = myArray[i+1];}
myArray[4] = PackVoltage;
if(PackVoltage > TurnOnVoltage && UserDesiredInverterStatus == 1 && lowloaddisc != 2 && CurrentInverterStatus == 0 && (PowerDownMillis + PUDelay) < millis()) PowerUp();
if((PackVoltage < ShutdownVoltage || LowestCellVoltage < ShutdownCellVoltage) && CurrentInverterStatus == 1) PowerDown();
if(lowloaddisc == 1 && myArray[0] != 0 && myArray[4] - 0.70 > myArray[0]) {lowloaddisc = 2; PowerDown();}
if (digitalRead(RemoteInputPin) == 1 && CurrentInverterStatus == 0 && RemoteEnable == 1) PowerUp();
if (digitalRead(RemoteInputPin) == 0 && CurrentInverterStatus == 1 && RemoteEnable == 1) PowerDown();
// SerStatus();
if(Serial.available())
{
if(Serial.find("+IPD,"))
{
delay(300);
connectionId = Serial.read()-48;
sendWebpage();
cipSend = "AT+CIPCLOSE=";
cipSend+=connectionId;
cipSend+="\r\n";
sendData(cipSend,300);
}
}
}
int DisplayStatus() {
lcd.setCursor(0,0); lcd.print(" ");
lcd.setCursor(0,0); lcd.print("LCM: ");lcd.print((PowerDownMillis - PowerUpMillis)/60000);
lcd.print(" ");
if(lowloaddisc == 2)lcd.print("LLD");
if(lowloaddisc != 2 && CurrentInverterStatus == 0 && UserDesiredInverterStatus == 1) lcd.print("LVD");
}
int DisplayCells() {
lcd.setCursor(0,0); lcd.print(" ");
lcd.setCursor(0,0); lcd.print(CellVoltage3,1);
lcd.print(" "); lcd.print(CellVoltage2,1);
lcd.print(" "); lcd.print(CellVoltage1,1);
lcd.print(" "); lcd.print(CellVoltage0,1);
}
int PowerDown() {
while(digitalRead(InverterStatusPin) == 1) {
// digitalWrite(ledpin,HIGH);
digitalWrite(InverterControlPin,HIGH);
delay(1000);
// digitalWrite(ledpin,LOW);
digitalWrite(InverterControlPin,LOW);
delay(3000);
}
for (i=0;i<5;i=i+1) {myArray[i] = 0;}
lcd.setCursor(12,1); lcd.print("OFF"); CurrentInverterStatus = 0;
PowerDownMillis = millis();
}
int PowerUp() {
while(digitalRead(InverterStatusPin) == 0) {
// digitalWrite(ledpin,HIGH);
digitalWrite(InverterControlPin,HIGH);
delay(1000);
// digitalWrite(ledpin,LOW);
digitalWrite(InverterControlPin,LOW);
delay(3000);
}
for (i=0;i<5;i=i+1) {myArray[i] = 0;}
lcd.setCursor(12,1); lcd.print(" ON"); CurrentInverterStatus = 1;
PowerUpMillis = millis();
}
int getInvStatus() {
if (digitalRead(InverterStatusPin) == 1) {
lcd.setCursor(12,1); lcd.print(" ON");
if (CurrentInverterStatus != 1) {
UserDesiredInverterStatus = 1;
if (lowloaddisc == 2) lowloaddisc = 1;
for (i=0;i<5;i=i+1) {myArray[i] = 0;}
PowerUpMillis = millis();
CurrentInverterStatus = 1;
}
}
else {
lcd.setCursor(12,1); lcd.print("OFF");
if (CurrentInverterStatus != 0) {
UserDesiredInverterStatus = 0;
for (i=0;i<5;i=i+1) {myArray[i] = 0;}
CurrentInverterStatus = 0;
}
}
}
int MinMax() {
float cellArray[4] = {CellVoltage3, CellVoltage2, CellVoltage1, CellVoltage0};
LowestCellVoltage = 10; //set to unreasonable values
HighestCellVoltage = 0; //ditto
for(i=0;i<4;i=i+1) {
LowestCellVoltage = min(LowestCellVoltage,cellArray[i]);
HighestCellVoltage = max(HighestCellVoltage,cellArray[i]);
}
}
void sendData(String command, const int timeout)
{
// String response = "";
Serial.print(command);
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial.available())
{
char c = Serial.read(); // read the next character.
// response+=c;
}
}
}
void sendWebpage()
{
longstring[0] = '\0';
strcat_P(longstring, (char*)pgm_read_word(&(string_table[0])));
strcat_P(longstring, (char*)pgm_read_word(&(string_table[1])));
strcat_P(longstring, (char*)pgm_read_word(&(string_table[2])));
strcat_P(longstring, (char*)pgm_read_word(&(string_table[3])));
if(UserDesiredInverterStatus == 1){strcpy(str1,"On");}else{strcpy(str1,"Off");}strcat(longstring, str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[4])));
if(CurrentInverterStatus == 1){strcpy(str1,"On");}else{strcpy(str1,"Off");}strcat(longstring,str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[4])));
dtostrf(PackVoltage,5,2,str1);strcat(longstring,str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[5])));
dtostrf(LowestCellVoltage,3,1,str1);strcat(longstring,str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[6])));
if(CurrentInverterStatus == 0 && UserDesiredInverterStatus == 1 && lowloaddisc != 2){strcpy(str1,"Batt Dead ");}else{strcpy(str1,"Too Soon ");}
if(lowloaddisc == 2){strcpy(str1,"Bike Full ");}strcat(longstring,str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[7])));
dtostrf((PowerDownMillis - PowerUpMillis)/60000,3,0,str1);strcat(longstring,str1);
strcat_P(longstring, (char*)pgm_read_word(&(string_table[8])));
int len = strlen(longstring);
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend +=len;
cipSend +="\r\n";
sendData(cipSend,200); // was 100
dtostrf(len - 67,3,0,str1);
for(int i = 58;i<61;i++){longstring[i] = str1[i-58];}
for(int i = 0;i<len;i++){
Serial.print(longstring[i]);
}
}