-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetUpConf.cpp
More file actions
293 lines (257 loc) · 7.15 KB
/
Copy pathSetUpConf.cpp
File metadata and controls
293 lines (257 loc) · 7.15 KB
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
#include <Arduino.h>
#include "SetUpConf.h"
#include "SPIFFS.h"
static IPAddress apIP(192, 168, 1, 100);
static DNSServer dnsServer;
static WebServer webServer(80);
static String ssid;
static String passwd;
static String tDarkOn;
static String tDarkOff;
static String utcOfst;
static bool bExit, bConnected;
static String ssid_rssi_str[SSIDLIMIT];
static String ssid_str[SSIDLIMIT];
static void writeConfigFile()
{
Serial.print("writeConfigFile");
File fw = SPIFFS.open(CONFIG_FILE_NAME, "w");
fw.println(ssid);
fw.println(passwd);
fw.println(tDarkOn);
fw.println(tDarkOff);
fw.println(utcOfst);
fw.close();
}
static void initConfigFile()
{
Serial.print("initConfigFile");
ssid = DEFAULT_SSID;
passwd = DEFAULT_PASSWD;
tDarkOn = DEFAULT_T_DARK_ON;
tDarkOff = DEFAULT_T_DARK_OFF;
utcOfst = DEFAULT_UTC_OFST;
writeConfigFile();
}
static void readConfigFile()
{
String numstr;
File fr = SPIFFS.open(CONFIG_FILE_NAME, "r");
if (fr) {
ssid = fr.readStringUntil('\n');
ssid.trim();
if (ssid =="") ssid = DEFAULT_SSID;
passwd = fr.readStringUntil('\n');
passwd.trim();
if(passwd == "") passwd = DEFAULT_PASSWD;
tDarkOn = fr.readStringUntil('\n');
tDarkOn.trim();
if(tDarkOn == "") tDarkOn = DEFAULT_T_DARK_ON;
tDarkOff = fr.readStringUntil('\n');
tDarkOff.trim();
if(tDarkOff == "") tDarkOff = DEFAULT_T_DARK_OFF;
utcOfst = fr.readStringUntil('\n');
utcOfst.trim();
if(utcOfst == "") utcOfst = DEFAULT_UTC_OFST;
fr.close();
Serial.println(ssid);
Serial.println(passwd);
Serial.println(tDarkOn);
Serial.println(tDarkOff);
Serial.println(utcOfst);
Serial.println("readConfigFile done!!");
}
else {
Serial.println("read open error");
Serial.print("SPIFFS data seems clash. Default load...");
initConfigFile();
}
}
static String WIFI_Form_str()
{
uint8_t ssid_num = WiFi.scanNetworks();
if(0 == ssid_num) {
Serial.println("no networks found");
}
else {
Serial.printf("%d networks found\r\n", ssid_num);
if (ssid_num > SSIDLIMIT) ssid_num = SSIDLIMIT;
for (int i = 0; i < ssid_num; ++i) {
ssid_str[i] = WiFi.SSID(i);
String wifi_auth_open =
((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
ssid_rssi_str[i] = ssid_str[i];
Serial.printf("%d: %s\r\n", i, ssid_rssi_str[i].c_str());
delay(10);
}
}
String str = "";
str += "<form action='/wifiset' method='get'>";
str += "SSID<br>";
str += "<select name='ssid' id ='ssid'>";
for(int i=0; i<ssid_num; i++){
str += "<option value=" + ssid_str[i] + ">" +
ssid_rssi_str[i] + "</option>";
}
str += "</select><br>\r\n";
str += "<br>Password<br><input type='password' name='passwd' value='" +
passwd + "' size='15'>";
str += "</select><br>\r\n";
str += "<br>Time dark mode ON<br><input type='time' name='tDarkOn' value='" +
tDarkOn + "' size='6'><br>";
str += "<br>Time dark mode OFF<br><input type='time' name='tDarkOff' value='" +
tDarkOff + "' size='6'><br>";
str += "<br>UTC Offset<br><input type='number' step='0.05' name='utcOfst' \
min='-10.0' max='14.0' value='" + utcOfst + "' size='4'><br>";
str += "<br><input type='submit' value='set'>";
str += "</form><br>";
str += "<script>document.getElementById('ssid').value = '"+
ssid +"';</script>";
return str;
}
static String Headder_str() {
String html = "";
html += "<!DOCTYPE html><html><head>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1.3'>";
html += "<meta http-equiv='Pragma' content='no-cache'>";
html += "<meta http-equiv='Cache-Control' content='no-cache'></head>";
html += "<meta http-equiv='Expires' content='0'>";
html += "<style>";
html += "a:link, a:visited { background-color: #009900; color: white; padding: 5px 15px;";
html += "text-align: center; text-decoration: none; display: inline-block;}";
html += "a:hover, a:active { background-color: green;}";
html += "bo32{ background-color: #EEEEEE;}";
html += "input[type=button], input[type=submit], input[type=reset] {";
html += "background-color: #000099; border: none; color: white; padding: 5px 20px;";
html += "text-decoration: none; margin: 4px 2px;";
html += "</style>";
html += "<body>";
html += "<h2>RING CLOCK SETUP</h2>";
return html;
}
static void appStart()
{
String html = Headder_str();
html += "<hr><p>";
html += "<h3>Please wait for a while.</h3><p>";
html += "<hr>";
html += "</body></html>";
webServer.send(200, "text/html", html);
delay(2000); // hold 2 sec
bExit = true;
}
static void appStartConf()
{
String html = Headder_str();
html += "<hr><p>";
html += "<h3>Setup Exit?</h3><p>";
html += "<center><a href='/appStart'>YES</a> <a href='/'>no</a></center>";
html += "<p><hr>";
html += "</body></html>";
webServer.send(200, "text/html", html);
}
static void wifiinput()
{
bConnected = true;
String html = Headder_str();
html += "<hr><p>";
html += WIFI_Form_str();
html += "</body></html>";
webServer.send(200, "text/html", html);
}
static void wifiset()
{
ssid = webServer.arg("ssid");
ssid.trim();
passwd = webServer.arg("passwd");
passwd.trim();
tDarkOn = webServer.arg("tDarkOn");
tDarkOn.trim();
tDarkOff = webServer.arg("tDarkOff");
tDarkOff.trim();
utcOfst = webServer.arg("utcOfst");
utcOfst.trim();
writeConfigFile();
appStartConf();
}
SetUpConf::SetUpConf()
{
Serial.println("SetUpConf()");
if(!SPIFFS.begin(true)) {
Serial.println("SPIFFS mount failed!!");
}
readConfigFile();
Serial.println("SetUpConf exit.");
}
void SetUpConf::webConfig(int mode)
{
bConnected = false;
if(CONF_WITH_TIMEOUT == mode) {
Serial.println("webConf: WITH_TIMEOUT");
}
else {
Serial.println("webConf: WITHOUT_TIMEOUT");
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
delay(100);
WiFi.mode(WIFI_AP);
WiFi.softAP(WIFIMGR_SSID);
delay(200);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", apIP);
webServer.on("/", HTTP_GET, wifiinput);
webServer.on("/wifiset", HTTP_GET, wifiset);
webServer.on("/appStart", appStart);
webServer.onNotFound(wifiinput);
webServer.begin();
bExit = false;
unsigned long tmAct, tmPrv;
int cnt = TIMEOUT_SETUP;
tmAct = tmPrv = millis();
while(!bExit) {
dnsServer.processNextRequest();
webServer.handleClient();
if(CONF_WITH_TIMEOUT == mode) {
if(bConnected) {
mode = CONF_WITHOUT_TIMEOUT;
}
else {
tmAct = millis();
if(1000 <= tmAct - tmPrv) {
tmPrv = tmAct;
Serial.print(".");
this->waitCallback();
if(0 >= --cnt) break;
}
}
}
}
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
const char* SetUpConf::getSsid(void)
{
return ssid.c_str();
}
const char* SetUpConf::getPasswd(void)
{
return passwd.c_str();
}
const char *SetUpConf::getTimeDarkOn(void)
{
return tDarkOn.c_str();
}
const char *SetUpConf::getTimeDarkOff(void)
{
return tDarkOff.c_str();
}
float SetUpConf::getUtcOfst(void)
{
return utcOfst.toFloat();
}
void SetUpConf::setWaitCallback(void(*pt)())
{
this->waitCallback = pt;
}