-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServerCodes.ino
176 lines (149 loc) · 6.01 KB
/
WebServerCodes.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
#include <ESP8266WebServer.h>
//#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
ESP8266WebServer webServer(80);
ESP8266HTTPUpdateServer httpUpdater;
//const char* host = "meteolaru";
const char* update_path = "/firmware";
const String html_ssid = "#=ssid#";
const String html_clientIP = "#=clientIP#";
const String html_apState = "#=apState#";
const String html_password = "#=password#";
const String html_thinkSpeakAPIKey= "#=thinkSpeakAPIKey#";
const String html_thinkSpeakChNumber= "#=thinkSpeakChNumber#";
const String html_errMsg = "#=errMsg#";
const String html_errMsgVisible = "#=errMsgVisible#";
const String html_fwPassword = "#=fwPassword#";
const String html_fwLogin = "#=fwLogin#";
const String html_temperature1 = "#=temperature1#";
const String html_temperature2 = "#=temperature2#";
const String html_pressure = "#=pressure#";
const String html_humidity = "#=humidity#";
String indexHtml;
String configHtml;
void WebServer_Init(){
FileStorage_Init();
indexHtml = FileStorage_LoadFile("index.html");
configHtml = FileStorage_LoadFile("config.html");
webServer.on("/", handleRoot);
webServer.on("/config", handleConfig);
webServer.on("/save", HTTP_POST, handleSave);
webServer.on("/ap", handleAP);
//MDNS.begin(host);
if(cfg.fwLogin == NULL || cfg.fwPassword == NULL){
strcpy(cfg.fwLogin, "admin");
strcpy(cfg.fwPassword, "admin");
Storage_SaveConfig(cfg);
}
httpUpdater.setup(&webServer, update_path, cfg.fwLogin, cfg.fwPassword);
webServer.begin();
Serial.println("HTTP server started");
//MDNS.addService("http", "tcp", 80);
//String addr = "192.168.4.1";
//if(wifiClientConnected){
// addr = WiFi.localIP().toString();
//}
Serial.println("HTTPUpdateServer ready");
//Serial.println("Url: http://"+addr+update_path+", Login: "+cfg.fwLogin+", Password: " + cfg.fwPassword);
}
void headerDisableCache(){
webServer.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate", true);
webServer.sendHeader("Pragma", "no-cache", true);
webServer.sendHeader("Expires", "0", true);
}
void handleRoot() {
String ssid = "NaN";
String ipAddr = "NaN";
if(wifiClientConnected){
ssid = String(cfg.ssid);
ipAddr = WiFi.localIP().toString();
}
String html = indexHtml;
html.replace(html_ssid, ssid);
html.replace(html_clientIP, ipAddr);
html.replace(html_apState, cfg.apState ? "true" : "false");
html.replace(html_temperature1, BME280_ReadStrTemperature());
html.replace(html_temperature2, DS18B20_ReadStrTemperature());
html.replace(html_pressure, BME280_ReadStrPressure());
html.replace(html_humidity, BME280_ReadStrHumidity());
headerDisableCache();
webServer.send(200, "text/html", html);
}
void handleAP(){
for (int i = 0; i < webServer.args(); i++) {
if(webServer.argName(i) == "switch" && webServer.arg(i) == "off"){
Wifi_EndAP();
cfg.apState = false;
}
if(webServer.argName(i) == "switch" && webServer.arg(i) == "on"){
Wifi_StartAP();
cfg.apState = true;
}
}
Storage_SaveConfig(cfg);
headerDisableCache();
webServer.sendHeader("Location", "/", true);
webServer.send(302, "text/plain", "");
}
void handleConfig() {
String errMsg = "";
for (int i = 0; i < webServer.args(); i++) {
if(webServer.argName(i) == "conn" && webServer.arg(i) == "1"){
if(wifiClientConnected){
headerDisableCache();
webServer.sendHeader("Location", String("http://") + WiFi.localIP().toString(), true);
webServer.send(302, "text/plain", "");
return;
}
else{
errMsg = "Connection failed. Please, check data and try it again.";
}
}
}
String html = configHtml;
html.replace(html_errMsgVisible, errMsg.length()>0 ? "true" : "false");
html.replace(html_errMsg, errMsg);
html.replace(html_ssid, String(cfg.ssid));
html.replace(html_password, String(cfg.password));
html.replace(html_thinkSpeakAPIKey, String(cfg.thinkSpeakAPIKey));
html.replace(html_thinkSpeakChNumber, String(cfg.thinkSpeakChNumber));
html.replace(html_fwPassword, String(cfg.fwLogin));
html.replace(html_fwLogin, String(cfg.fwPassword));
headerDisableCache();
webServer.send(200,"text/html", html);
}
void handleSave() { // If a POST request is made to URI /login
if( ! webServer.hasArg("ssid") || ! webServer.hasArg("password") || ! webServer.hasArg("thinkSpeakAPIKey") || ! webServer.hasArg("thinkSpeakChNumber")
|| webServer.arg("ssid") == NULL || webServer.arg("password") == NULL || webServer.arg("thinkSpeakAPIKey") == NULL || webServer.arg("thinkSpeakChNumber") == NULL) { // If the POST request doesn't have username and password data
headerDisableCache();
webServer.send(400, "text/plain", "400: Invalid Request");
return;
}
String tsnStr = webServer.arg("thinkSpeakChNumber");
//char *tsnChars;
//tsnStr.toCharArray(tsnChars,tsnStr.length()+1);
cfg.thinkSpeakChNumber = tsnStr.toInt(); // strtoul(tsnChars,NULL,10);
String tsStr = webServer.arg("thinkSpeakAPIKey");
tsStr.toCharArray(cfg.thinkSpeakAPIKey,tsStr.length()+1);
String sStr = webServer.arg("ssid");
sStr.toCharArray(cfg.ssid,sStr.length()+1);
String pStr = webServer.arg("password");
pStr.toCharArray(cfg.password ,pStr.length()+1);
String fwlStr = webServer.arg("fwLogin");
fwlStr.toCharArray(cfg.fwLogin,fwlStr.length()+1);
String fwpStr = webServer.arg("fwPassword");
fwpStr.toCharArray(cfg.fwPassword ,fwpStr.length()+1);
headerDisableCache();
webServer.sendHeader("Connection", "close", true);
webServer.sendHeader("Refresh", "30;url=/config?conn=1", true);
webServer.send(200, "text/html", "<p>Connecting to wifi " + String(cfg.ssid) +"...</p>");
Storage_SaveConfig(cfg);
Serial.println("Connecting ssid: " + String(cfg.ssid) + ", pass: " + String(cfg.password) + ", thinkSpeakAPIKey: " + String(cfg.thinkSpeakAPIKey));
wifiClientConnected = Wifi_ClientConnect(cfg.ssid, cfg.password);
if(!wifiClientConnected){
Wifi_StartAP();
}
}
void WebServer_HandleClient(){
webServer.handleClient();
}