-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebConfig.cpp
196 lines (157 loc) · 5.28 KB
/
WebConfig.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
//
//
//
#include "WebConfig.h"
#include "Config.h"
ESP8266WebServer WebConfigClass::gWebServer = ESP8266WebServer(80);
Config* WebConfigClass::config;
void WebConfigClass::Setup(Config *pConfig)
{
config = pConfig;
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
WiFi.mode(WIFI_OFF);
delay(2000);
WiFi.softAP(AP_SSID);
WiFi.mode(WIFI_AP);
Serial.print("Configuring access point as ");
Serial.println(AP_SSID);
IPAddress vIP = WiFi.softAPIP();
Serial.print("AP IP Address: ");
Serial.println(vIP);
SetupWebServer();
while (true)
gWebServer.handleClient();
}
void WebConfigClass::SetupWebServer()
{
Serial.println("Setting up web server on port 80");
gWebServer.on("/", WebServer_GetConfig);
gWebServer.on("/save", HTTP_POST, WebServer_SaveConfig);
gWebServer.begin();
}
void WebConfigClass::WebServer_GetConfig()
{
Serial.println("Responding to HTTP request at '/'");
String html = getConfigPage();
gWebServer.send(200, "text/html", html);
}
void WebConfigClass::WebServer_SaveConfig()
{
/*
char* ssid;
char* ssidPassword;
char* mqtt;
int mqttPort;
char* mqttClientID;
char* mqttPublishTopic;
char* mqttSubscribeTopic;
bool secure;
char* mqttUser;
char* mqttPass;
*/
Config *config = new Config();
String temp;
temp = gWebServer.arg("ssid");
config->ssid = new char[temp.length() + 1];
temp.toCharArray(config->ssid, temp.length() + 1, 0);
temp = gWebServer.arg("ssidPassword");
config->ssidPassword = new char[temp.length() + 1];
temp.toCharArray(config->ssidPassword, temp.length() + 1, 0);
temp = gWebServer.arg("mqtt");
config->mqtt = new char[temp.length() + 1];
temp.toCharArray(config->mqtt, temp.length() + 1, 0);
config->mqttPort = (int)gWebServer.arg("mqttPort").toInt();
temp = gWebServer.arg("mqttClientID");
config->mqttClientID = new char[temp.length() + 1];
temp.toCharArray(config->mqttClientID, temp.length() + 1, 0);
temp = gWebServer.arg("mqttPublishTopic");
config->mqttPublishTopic = new char[temp.length() + 1];
temp.toCharArray(config->mqttPublishTopic, temp.length() + 1, 0);
temp = gWebServer.arg("mqttSubscribeTopic");
config->mqttSubscribeTopic = new char[temp.length() + 1];
temp.toCharArray(config->mqttSubscribeTopic, temp.length() + 1, 0);
temp = gWebServer.arg("mqttUser");
config->mqttUser = new char[temp.length() + 1];
temp.toCharArray(config->mqttUser, temp.length() + 1, 0);
temp = gWebServer.arg("mqttPass");
config->mqttPass = new char[temp.length() + 1];
temp.toCharArray(config->mqttPass, temp.length() + 1, 0);
Serial.println("Saving configuration now:");
config->Print(Serial);
config->Save(0);
String html = saveConfigResponsePage();
gWebServer.send(200, "text/html", html);
ESP.reset();
}
const char* WebConfigClass::AP_SSID = "XNS_DEVICE";
/*
const char* WebConfig::AP_CONFIG_PAGE = "<html><body><form method='POST' action='/save'>SSID: <input name='ssid'/><br/>Password: <input name='pwd' type='password'/><br/><h3>MQTT</h3>Server: <input name='msrv'/><br/>Topic: <input name='mtop'/><br/>Sensor Name: <input name='mname'/><br/><input type='submit' value='submit'/></body></form>";
const char* WebConfig::AP_SUCCESS_PAGE = "<html><body><h1>Successflly Saved!</h1><h3>Please restart the device now</h3></body></form>";
*/
void WebConfigClass::Handle()
{
gWebServer.handleClient();
}
String WebConfigClass::getConfigPage()
{
/*
char* ssid;
char* ssidPassword;
char* mqtt;
int mqttPort;
char* mqttClientID;
char* mqttPublishTopic;
char* mqttSubscribeTopic;
bool secure;
char* mqttUser;
char* mqttPass;
*/
String html = String(F("<html>"));
html += F("<head>");
html += F("<link rel = 'stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>");
html += F("</head>");
html += F("<body>");
html += F("<form method = 'POST' action = '/save'>");
html += F("<h1>[");
html += WiFi.macAddress();
html += F("]</h1>");
html += F("<h2>WiFi Connection</h2>");
html += F("SSID: <input name='ssid' value='");
html += config->ssid;
html += F("'/><br/>");
html += F("Password: <input name='ssidPassword' type='password' value='");
html += config->ssidPassword;
html += F("'/><br/>");
html += "<h2>MQTT</h2>";
html += F("Server: <input name='mqtt' value='");
html += config->mqtt;
html += F("'/>, port: <input name='mqttPort' value='");
html += config->mqttPort;
html += F("'/><br/>");
html += F("Client ID: <input name='mqttClientID' value='");
html += config->mqttClientID;
html += F("'/><br/>");
html += F("Publish Topic: <input name='mqttPublishTopic' value='");
html += config->mqttPublishTopic;
html += F("'/><br/>");
html += F("Subscribe Topic: <input name='mqttSubscribeTopic' value='");
html += config->mqttSubscribeTopic;
html += F("'/><br/>");
html += F("User: <input name='mqttUser' value='");
html += config->mqttUser;
html += F("'/><br/>");
html += F("Password: <input name='mqttPass' type='password' value='");
html += config->mqttPass;
html += F("'/><br/>");
html += F("<input type='submit' value='submit'/>");
html += F("</form></body></html>");
return html;
}
String WebConfigClass::saveConfigResponsePage()
{
String html = String("<html><body>");
html += "Configuration saved, device will now reboot...";
html += "</body></html>";
return html;
}