-
Notifications
You must be signed in to change notification settings - Fork 5
/
NVSettingInterface.cpp
333 lines (296 loc) · 18.8 KB
/
NVSettingInterface.cpp
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "Arduino.h"
#include "NVSettingInterface.h"
extern String wifiSSID ;
extern String wifiPassword;
extern String NameOfThisThrottle;
extern void OLED_5_line_display(String L1,String L2,String L3,String L4,String L5);
extern void OLED_5_line_display_p(String L1,String L2,String L3,String L4,String L5);
extern int BrokerAddr;
extern uint8_t Volts_Calibration;
long analog_sum;
extern long ReadADC(int avg);
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
bool _ProcessedSerialInput;
byte ndx;
char rx_byte; // to try serial input
int SerioLevel; // for the serial interface for updates
int MSG_content_length(){
int Length;
int i;
bool EOM;
Length =0;i=0;
EOM=false;
while (!EOM && i<=numChars) {
if (receivedChars[i] == 0){EOM=true;Length=i;}
i++;
}
return Length;
}
extern uint8_t NodeMCUPinD[12];
extern bool SelectPressed();
extern int ADC_IN;
void CheckForSerialInput(){
String MSGText;
String MSGText1;
String MSGText2;
String MSGText3;
String MSGText4;
String TestData;
long Timestarted;
bool UpdateInProgress;
long FlashTime;
char CtrlE;
CtrlE=5;
// will try to change wifiSSID,wifiPassword
UpdateInProgress=false;
_ProcessedSerialInput=false;ndx=0;newData = false;
if (wifiSSID=="Router Name"){UpdateInProgress=true;Serial.println(" Forcing request for new entries as Default Router name has not been set in Secrets.h");
Serial.println("--Serial port update Started--");
Serial.print(" SSID currently is <");Serial.print(wifiSSID);Serial.print("> Password is <");Serial.print(wifiPassword); Serial.println(">");
Serial.println("Type in New SSID");newData = false;SerioLevel=1;
OLED_5_line_display("EEPROM settings required","Use Serial port @115200","To enter WiFi details","Press Select"," to start");
}else{
Serial.println("");
Serial.println(F("--- To enter new wifi details / Press Select or type 'xxx' BEFORE wifi connects"));
Serial.println(F("-- Use 'Newline' OR 'CR' to end input line --"));
Serial.println(F("Starting~~~~~~~~~~~~~~~~~~~~~~~~waiting~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Timeout "));
delay(10);Serial.print(CtrlE);delay(100); OLED_5_line_display_p("","","Pausing for Serial I/O","type 'xxx' to start","or 'Select'");
}
Timestarted=millis();
FlashTime=millis();
bool LAMP;
long cal_input;
// test to see if we can sense if serial cable connected -- Answer no we cannot!! only the Leonardo can if (Serial){OLED_5_line_display_p("","","","Serial ON","");}else{OLED_5_line_display_p("","","","Serial OFF","");}
// but we can test the "select" button..
while ((millis()<= Timestarted+4000) || UpdateInProgress) {
/// toggle signal lamp
if ((millis()>= FlashTime)&& (!UpdateInProgress)) { LAMP=!LAMP; FlashTime=millis()+50; digitalWrite (NodeMCUPinD[0] , LAMP) ;Serial.print("~");}
delay(1); // Allow esp to process other events .. may not be needed, but here to be safe..
recvWithEndMarker();
if ( (SelectPressed()&& (SerioLevel==0) )||(newData == true) )
{
if (newData == true){TestData=receivedChars;_ProcessedSerialInput=true;ndx=0;newData = false;}
//Serial.print("Data <");Serial.print(TestData);Serial.println(">");
//("> Looking for {");Serial.print(LookFor);Serial.println("}");
switch (SerioLevel){
case 0:
if ((TestData=="xxx\0")||(TestData=="XXX\0")|| SelectPressed()){
UpdateInProgress=true;
//display.clear(); display.drawString(64, 32, "Type in New SSID"); display.display();
OLED_5_line_display("Type in New SSID",""," ","","");
Serial.println("-");
Serial.println("--Update EEPROM Started--");
Serial.print(" SSID currently is <");Serial.print(wifiSSID);Serial.println(">");
Serial.println("Type in New SSID");newData = false;SerioLevel=1;
}
break;
case 1: if(MSG_content_length()>1) {wifiSSID=receivedChars;} newData = false;SerioLevel=2;
MSGText1="SSID <";
MSGText1+=wifiSSID;
MSGText1+=">";
OLED_5_line_display(MSGText1,"Type Password"," "," "," ");
// display.clear(); display.drawString(64, 12, MSGText); display.display();
Serial.print(" SSID<");Serial.print(wifiSSID);Serial.print("> current Password<");Serial.print(wifiPassword); Serial.println(">");
Serial.println("Type in New Password");
break;
case 2:
if(MSG_content_length()>1) {wifiPassword=receivedChars;} newData = false;SerioLevel=3;
MSGText2="Password <";
MSGText2+=wifiPassword;
MSGText2+=">";
OLED_5_line_display(MSGText1,MSGText2,"Broker addr?"," "," "); //display.drawString(64, 24, MSGText); display.display();
Serial.print(" SSID<");Serial.print(wifiSSID);Serial.print("> Password<");Serial.print(wifiPassword); Serial.println(">");
Serial.print("Broker Addr:");Serial.println(BrokerAddr);Serial.println("Type in MQTT Broker address");
break;
case 3:
if(MSG_content_length()>1) {BrokerAddr= TestData.toInt();} newData = false;SerioLevel=4;
MSGText3="Broker Addr<";
MSGText3+=BrokerAddr;
MSGText3+=">";
OLED_5_line_display(MSGText1,MSGText2,MSGText3," This Throttle's Name ?","");
//display.drawString(64, 32, MSGText); display.display();
Serial.print("Broker Addr:");Serial.print(BrokerAddr);Serial.print(" WiFi SSID<");Serial.print(wifiSSID);Serial.print("> Password<");Serial.print(wifiPassword); Serial.println(">");
Serial.print("Current name <");Serial.print(NameOfThisThrottle);Serial.println(">");
Serial.println("Type Throttle Name");
break;
case 4:
if(MSG_content_length()>1) {NameOfThisThrottle=receivedChars;} newData = false;SerioLevel=5;
MSGText4="Name<";
MSGText4+=NameOfThisThrottle;
MSGText4+=">";
OLED_5_line_display(MSGText1,MSGText2,MSGText3,MSGText4," sss to save or rrr to retry");
Serial.print("Summary: Name of Throttle:");Serial.print(NameOfThisThrottle);
Serial.print(" Broker Addr:");Serial.print(BrokerAddr);Serial.print(" WiFi SSID<");Serial.print(wifiSSID);Serial.print("> Password<");Serial.print(wifiPassword); Serial.println(">");
Serial.println("Type sss to save, rrr to return to start ccc to add cal ");
break;
case 5:
Serial.print("Summary: Name of Throttle:");Serial.print(NameOfThisThrottle);
Serial.print(" Broker Addr:");Serial.print(BrokerAddr);Serial.print(" WiFi SSID<");Serial.print(wifiSSID);Serial.print("> Password<");Serial.print(wifiPassword); Serial.println(">");
if (TestData=="sss\0"){
//display.clear(); display.drawString(64, 32, "EEPROM Updated"); display.display();
Serial.println("I will now save this data and continue");
WriteWiFiSettings();
UpdateInProgress=false;
newData = false;SerioLevel=6;
break;
}
if (TestData=="rrr\0"){
OLED_5_line_display("Resuming Serial Input","Type in xxx to restart",""," ","");
Serial.println("-----------------");Serial.println("---Starting again---");Serial.println(" Type xxx again to re-start sequence");
newData = false;SerioLevel=0;
break;}
if (TestData=="ccc\0"){
analog_sum=ReadADC(100); //
MSGText4="Current V batt <";MSGText4+=analog_sum;MSGText4+=">";
OLED_5_line_display("Starting voltage calibration","Type in actual V as xxx",MSGText4,"","");
Serial.print(MSGText4);Serial.println(" Type in actual voltage without decimal (xxx)");
newData = false;SerioLevel=8;
break;
}
break;
case 6:
newData = false;
Serial.println("Wait to try reconnect, or turn off to restart with new values");
Serial.println("Type sss to save, rrr to return to start");
break;
case 8:
if(MSG_content_length()>1) {cal_input= TestData.toInt();}
if ((cal_input>=200)&&(cal_input<=600)) { // valid input
Serial.print("adjusted Vcal from<");Serial.print(Volts_Calibration);
cal_input=(Volts_Calibration*cal_input)/(analog_sum); // calc as long 1% error on wifi starting
Volts_Calibration= cal_input;// convert to uint8_t
newData = false;
Serial.print("> to <");Serial.print(Volts_Calibration); Serial.println("> "); }
else {Serial.println("- unchanged calibration factor -"); }
analog_sum=ReadADC(100); //
MSGText4="V Batt <";MSGText4+=analog_sum;MSGText4+=">";
OLED_5_line_display(MSGText4," In batt measurement cal","","","");
Serial.println(MSGText4);
Serial.println("Type 'sss' to save this calibration or 'ccc' to check and repeat");newData = false;SerioLevel=9;
break;
case 9:
if (TestData=="sss\0"){
//display.clear(); display.drawString(64, 32, "EEPROM Updated"); display.display();
Serial.println("I will now save this data and continue");
WriteWiFiSettings();
UpdateInProgress=false;
newData = false;SerioLevel=6;
break;
}
if (TestData=="rrr\0"){
OLED_5_line_display("Resuming Serial Input","Type in xxx to restart",""," ","");
Serial.println("-----------------");Serial.println("---Starting again---");Serial.println(" Type xxx again to re-start sequence");
newData = false;SerioLevel=0;
break;}
if (TestData=="ccc\0"){
analog_sum=ReadADC(100);
MSGText4=" Measured <";MSGText4+=analog_sum;MSGText4+="";
OLED_5_line_display("Repeating voltage calibration","Type in measured voltage without decimal xxx ",MSGText4," ","");
Serial.print(MSGText4);Serial.println("> Type in actual voltage without decimal (xxx)");
newData = false;SerioLevel=8;
break;
}
Serial.println("Please type 'sss' to save, 'ccc' to recheck Batt cal, or 'rrr' to return to start");newData = false;
newData = false;
Serial.println("Non valid input");
break;
default:
newData = false;
Serial.println("Not Understood");
break;
}
}
showNewData(); // only shows results if newData=true and not understood (can happen if looping after a rrr)
} // end of main while serial input loop
}
void recvWithEndMarker() {
char endMarker = '\n';
char rc;
while (Serial.available() > 0 ) {
rc = Serial.read(); //echo on
if ((rc != 10)&&(rc != 13) ){
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) { ndx = numChars - 1;} //truncate if too bit to avoid filling up
}
else {
newData = true;receivedChars[ndx] = '\0'; // replace NL/CR with /0 terminator. Mark that new data is available, but do not increment ndx
}
delay(1); } // added delay to allow other processes whilst reading serial.
ndx = 0; //once all serial data has been processed, reset the ndx pointer
}
void showNewData() {
if (newData == true) {
Serial.println(" ");
Serial.print("Seen but not understood <");
Serial.print(receivedChars);Serial.println("> ");
// to assist debug serial io //Serial.print("Ascii is <"); for (int i=0; i<=(numChars) ;i++) { Serial.print(int(receivedChars[i]));Serial.print("> <");}
newData = false;}
}
void WriteWiFiSettings(){
Serial.println(" --Writing the SSID, Password, BrokerAddr and Throttle_Name to EEPROM-- ");
writeString(ssidEEPROMLocation,wifiSSID);delay(10);
writeString(passwordEEPROMLocation,wifiPassword);delay(10);
writeString(ThrottleNameEEPROMLocation,NameOfThisThrottle);delay(10);
EEPROM.write(BrokerEEPROMLocation,BrokerAddr);delay(10);
EEPROM.write(CalEEPROMLocation, Volts_Calibration);
EEPROM.commit();delay(100);
}
void TestFillEEPROM(int d){
#ifdef _EEPROMdebug
Serial.print("~~~WARNING--FILLING EEPROM with int<");Serial.print(d);Serial.println(">~~");
#endif
for(int i=0;i<=EEPROM_Size;i++) { EEPROM.write(i,d); }
EEPROM.commit();delay(100);//
}
void writeString(int add,String data)
{
int _size = data.length();
int i;
#ifdef _EEPROMdebug
Serial.print("Writing :");Serial.print(_size);Serial.print(" bytes of data <");Serial.print(data);Serial.print("> at EEprom index starting:");Serial.println(int(add));
#endif
for(i=0;i<_size;i++) { if ((add+i) < (EEPROM_Size)){EEPROM.write(add+i,data[i]);
// Serial.print("Writing<");Serial.print(data[i]);Serial.print("> to offset:");Serial.print(i);Serial.print(" index:");Serial.println(add+i);
} // write the data up to the point it would overflow the eeprom
else{ // Serial.print("Stopping overflow Ignoring <");Serial.print(data[i]);Serial.print("> ");
}
}
//Got all the data, so add the \0
if ((add+i) <= (EEPROM_Size-1)){
EEPROM.write(add+i,'\0');
// Serial.print("Adding end_mark to offset:");Serial.print(i);Serial.print(" index:");Serial.println(add+i);
}
else{
EEPROM.write(EEPROM_Size,'\0');
//Serial.print("Adding end mark at end EEPRom size index:");Serial.println(EEPROM_Size);
}
//Add termination null character for String Data
}
String read_String(int add)
{
int i;
char data[100]; //Max 100 Bytes
int len;
unsigned char k;
// #ifdef _EEPROMdebug
// Serial.print("Starting Read EEprom Data at Addr<");Serial.print(int(add));Serial.println("> ");
// #endif
len=0;
k=EEPROM.read(add);
while((k != '\0') && (len<99) && ((len+add)<=EEPROM_Size)) //Read until null character, or 100 or end of eeprom
{
k=EEPROM.read(add+len);
//#ifdef _EEPROMdebug
// Serial.print(len);Serial.print(" ");Serial.print(add+len);Serial.print(" ");Serial.println(k);
//#endif
data[len]=k;
len++;
}
data[len]='\0';
#ifdef _EEPROMdebug
Serial.print("EEprom data from Addr:");Serial.print(int(add));Serial.print(" is <");Serial.print(data);Serial.println("> ");
#endif
return String(data);
}