-
Notifications
You must be signed in to change notification settings - Fork 4
/
NVSettingInterface.cpp
250 lines (221 loc) · 13 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
#include "NVSettingInterface.h"
extern String wifiSSID ;
extern String wifiPassword;
//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;
// #include "Globals.h"
extern uint8_t NodeMCUPinD[12];//
extern bool Data_Updated;
extern uint32_t EPROM_Write_Delay;
// 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;
}
void CheckForSerialInput(){
String MSGText;
String MSGText1;
String MSGText2;
String MSGText3;
String MSGText4;
String TestData;
long Timestarted;
long FlashTime;
bool UpdateInProgress;
//will try to change wifiSSID,wifiPassword
UpdateInProgress=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 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("Starting~~~~~~~~~~~~~~~~~~~~~~~~waiting~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Timeout "));
delay(100);
//delayD_5_line_display_p("","","","Pausing for Serial I/O","type 'xxx' to start");
}
Timestarted=millis();
FlashTime=millis();
bool LAMP;
while ((millis()<= Timestarted+4000) || UpdateInProgress) {
if ((millis()>= FlashTime) && !UpdateInProgress) { LAMP=!LAMP; FlashTime=millis()+50; Serial.print(">");digitalWrite (NodeMCUPinD[SignalLed] , LAMP) ;}
delay(1); //Allow esp to process other events .. may not be needed, but here to be safe..
recvWithEndMarker();
if (newData == true) {TestData=receivedChars;
//Serial.print("<");Serial.print(TestData);Serial.print("> Looking for {");Serial.print(LookFor);Serial.println("}");
switch (SerioLevel){
case 0:
if (TestData=="xxx\0"){
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.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(">");
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=5;
}
else {
if (TestData=="rrr\0"){
//OLED_5_line_display("Resuming Serial Input","Type in xxx to restart",""," ","");
//display.clear(); display.drawString(64, 46, "Type xxx to resume input"); display.display();
Serial.println("-----------------");Serial.println("---Starting again---");Serial.println(" Type xxx again to re-start sequence");
newData = false;SerioLevel=0;
}else{Serial.println("Please type 'sss' to save, or 'rrr' to return to start");newData = false;}
}
break;
case 5:
newData = false;
Serial.println("Wait to try reconnect, or turn off to restart with new values");
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() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 ) {
rc = Serial.read();
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
}
}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);
EEPROM.write(BrokerEEPROMLocation,BrokerAddr);delay(10);
EPROM_Write_Delay = millis() + 1000;
EEPROM.commit();Data_Updated = false;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);
}