-
Notifications
You must be signed in to change notification settings - Fork 4
/
NVSettingInterface.cpp
311 lines (282 loc) · 16.4 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
#include "NVSettingInterface.h"
extern String wifiSSID ;
extern String wifiPassword;
extern void OLED_4_RN_displays(int OLed_x,String L1,String L2,String L3,String L4);
extern void SetFont(uint8_t Disp,uint8_t Font);
//extern void OLED_5_line_display_p(String L1,String L2,String L3,String L4,String L5);
extern int BrokerAddr;
// #include "Globals.h"
extern uint8_t NodeMCUPinD[18]; // hard fixed //Number of ports +2//
extern bool Data_Updated;
extern uint32_t EPROM_Write_Delay;
extern void SignOfLifeFlash(bool state);
// globals
char receivedChars[numChars]; //an array to store the received data
boolean newData = false;
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 void OLEDS_Display(String L1,String L2,String L3,String L4);
extern void OLED_Status(void);
extern void DoFTP();
extern void Status();
extern void SetupFTP();
extern void ImmediateStop();
bool _ProcessedSerialInput;
byte ndx;
void CheckForSerialInput(){
String MSGText;
String MSGText1;
String MSGText2;
String MSGText3;
String MSGText4;
String TestData;
String Count;
long Timestarted;
long FlashTime;
int countdown;
bool UpdateInProgress; bool LAMP;
char CtrlE;
CtrlE=5;
//Gives options 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;
countdown=100;OLEDS_Display("EEPROM settings required","Use Serial port @115200","To enter new data"," ");
}else{
Serial.println("");
Serial.println(F(" --- To enter new wifi SSID / Password type 'xxx' BEFORE wifi connects--- "));
Serial.println(F(" -- Use 'Newline' OR 'CR' to end input line --"));
Serial.println(F("~~~waiting~~~"));
delay(10);Serial.print(CtrlE);delay(100);
countdown=10;OLEDS_Display("Node Start-Up","","","");delay(1000);
}
Timestarted=millis();
FlashTime=millis();
#ifdef _ROCDISP_EEPROM_DEBUG
countdown=1;
#endif
OLED_Status();// set up fonts etc?
while ((countdown>= 0) || UpdateInProgress) {
if (SerioLevel==10){
newData = false;
OLEDS_Display("FTP ONLY","Switch off to reset","","");
DoFTP();
if ((millis()>= FlashTime)){FlashTime=millis()+1000; Serial.println("Connected and waiting for ftp HARD reset to escape");}
#ifdef _LOCO_SERVO_Driven_Port // need this to stop loco from running at full speed when just trying to do FTP
pinMode(NodeMCUPinD[_LOCO_SERVO_Driven_Port], OUTPUT);
#ifdef _LocoPWMDirPort
pinMode(NodeMCUPinD[_LocoPWMDirPort], OUTPUT);
#endif
ImmediateStop(); //stop motors as soon as setup set up
#endif
}
if ((millis()>= FlashTime) && !UpdateInProgress) {Count=" Countdown:";Count+=countdown;
FlashTime=millis()+1000; Serial.print(countdown);SignOfLifeFlash(LAMP) ;
OLEDS_Display("Pausing for Serial I/O",Count,"","");delay(100);
LAMP=!LAMP; countdown=countdown-1;}
delay(1); //Allow esp to process other events .. may not be needed, but here to be safe..
recvWithEndMarker(); // SETS UP receivedChars and ndx and newData
if ( ( (SerioLevel==0) )||(newData == true) )
{
if (newData == true) {TestData=receivedChars;_ProcessedSerialInput=true;ndx=0;newData = false;
//Serial.print("Working with <");Serial.print(TestData);Serial.println(">");
}
switch (SerioLevel){
case 0:
if ((TestData=="xxx\0")){
UpdateInProgress=true;
//display.clear(); display.drawString(64, 32, "Type in New SSID"); display.display();
OLEDS_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+=">";
OLEDS_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+=">";
OLEDS_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+=">";
OLEDS_Display(MSGText1,MSGText2,MSGText3," 'sss' to save");
//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.println("Please type 'sss' to save, or 'rrr' to return to start");
break;
case 4:
Serial.print("Settings are: Broker Addr:");Serial.print(BrokerAddr);Serial.print(" WiFi SSID<");Serial.print(wifiSSID);Serial.print("> Password<");Serial.print(wifiPassword); Serial.println(">");
Serial.print("input was:");Serial.println(TestData);
if (TestData=="sss\0"){
OLEDS_Display(MSGText1,MSGText2,MSGText3," Saving to EEPROM");
Serial.println("I will now save this data and continue");
WriteWiFiSettings();
UpdateInProgress=false;
newData = false;SerioLevel=5;
}
if ((TestData=="rrr\0")||(TestData=="xxx\0")){
OLEDS_Display("Resuming Serial Input","Type in xxx to restart","the input sequence"," ");
Serial.println("-----------------");Serial.println("---Starting again---");Serial.println(" Type xxx again to re-start WiFi Setup sequence");
newData = false;SerioLevel=0;
}
if ((TestData=="lll\0")||(TestData=="LLL\0")){
OLEDS_Display("Leaving Serial Input","","","");
Serial.println("Leaving Serial input function");
UpdateInProgress=false;
newData = false;SerioLevel=6;
}
if (TestData=="ftp\0"){
OLEDS_Display("Attempting WiFi connect for FTP"," ","","");
Status();SetupFTP();
newData = false;SerioLevel=10;
}
break;
case 5:
newData = false;
Serial.println("Wait to try reconnect, or turn off to restart with new values");
break;
case 6:
//null
break;
case 10:
//special case that just waits for FTP.. to see if this improves capability to send large files without MQTT getting in the way
newData = false;SerioLevel=10;
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
Serial.println("");Serial.println("");
}
void recvWithEndMarker() {
char endMarker = '\n';
char rc;
while (Serial.available() > 0 ) {
rc = Serial.read();
// Serial.print("<");Serial.print("ndx:");Serial.print(ndx);Serial.print(">");delay(1);
Serial.print(char(rc)); // echo on
if ((rc != 10)&&(rc != 13) ){
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) { ndx = numChars - 1;} //truncate if too big 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
}//once all serial data has been processed, reset ndx
delay(1); //v26 addded to try to make serial interface more reliable..
}
}
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;}
}
extern uint16_t RocNodeID;
extern void WriteEEPROM(void);
extern void Commit_EEprom(String reason);
void WriteWiFiSettings(){
Serial.println(" --WriteWiFisettings-- Saving the SSID, Password, BrokerAddr to EEPROM ");
Data_Updated = true;
WriteEEPROM();
EPROM_Write_Delay = millis() + 500;
delay(20);
Commit_EEprom("Writing WiFi settings ");
delay(100);
}
void TestFillEEPROM(int d){
#ifdef _EEPROMdebug
Serial.print("~~~WARNING--FILLING EEPROM with int<");Serial.print(d);Serial.println(">~~");
#endif
for(int i=Serial_EEPROM_Starts;i<=(Serial_EEPROM_Starts+EEPROM_Serial_store_Size);i++) { EEPROM.write(i,d); }
EEPROM.commit();delay(100);//
}
void writeString(int add,String data){ //from add, write 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) < (Serial_EEPROM_Starts+EEPROM_Serial_store_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) <= (Serial_EEPROM_Starts+EEPROM_Serial_store_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(Serial_EEPROM_Starts+EEPROM_Serial_store_Size,'\0');
//Serial.print("Adding end mark at end EEPRom size index:");Serial.println(Serial_EEPROM_Starts+EEPROM_Serial_store_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)<=(Serial_EEPROM_Starts+EEPROM_Serial_store_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);
}