-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.cpp
197 lines (167 loc) · 6.16 KB
/
WebServer.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
#include <src/Arduino_FreeRTOS.h>
#include "WebServer.h"
namespace web
{
WebServer::WebServer(EthernetClient *ethClient, EepromConfiguration* configuration)
: _ethClient(ethClient),
_server(80),
_configuration(configuration)
{
}
void WebServer::update()
{
if(!_enabled) return;
// Create a client connection
EthernetClient client = _server.available();
if (client)
{
int index = 0;
char message[200];
while (client.connected())
{
if (client.available())
{
char c = client.read();
//read char by char HTTP request
if (index < sizeof(message) - 1)
{
message[index] = c;
index++;
}
message[index] = 0;
//if HTTP request has ended
if (c == '\n')
{
serveHtml(client);
}
}
}
char *token = strtok(message, "?=&");
char *lastToken = nullptr;
bool configChanged = false;
while (token != nullptr)
{
if(lastToken != nullptr)
{
TokenType lastTokenType = getParameterType(lastToken);
TokenType tokenType = getParameterType(token);
if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->mqttServerAddress, token);
}
else if(lastTokenType == TokenType::DNS_SERVER && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->dnsServerAddress, token);
}
else if(lastTokenType == TokenType::GATEWAY && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->gatewayAddress, token);
}
else if(lastTokenType == TokenType::IP_ADDRESS && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->ipAddress, token);
}
else if(lastTokenType == TokenType::SUBNET_MASK && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->subnetMask, token);
}
else if(lastTokenType == TokenType::MQTT_PUBLISH_INTERVAL && tokenType == TokenType::NONE)
{
configChanged = true;
strcpy(_configuration->mqttPublishInterval, token);
}
}
lastToken = token;
token = strtok(nullptr, "?=&");
}
if(configChanged)
{
_configuration->writeEeprom();
_enabled = false;
}
}
}
void WebServer::serveHtml(EthernetClient& client)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>PZEM004t Energy Monitor</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<FORM ACTION=method=get >");
client.print("MQTT Server: <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->mqttServerAddress);
client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>");
client.print("DNS Server: <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->dnsServerAddress);
client.println("\" NAME=\"DNSSERVER\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
client.print("Gateway: <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->gatewayAddress);
client.println("\" NAME=\"GATEWAY\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
client.print("IP Address: <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->ipAddress);
client.println("\" NAME=\"IPADDRESS\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
client.print("Subnet mask: <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->subnetMask);
client.println("\" NAME=\"SUBNET\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");
client.print("MQTT publish interval (ms): <INPUT TYPE=TEXT VALUE=\"");
client.print(_configuration->mqttPublishInterval);
client.println("\" NAME=\"INTERVAL\" SIZE=\"25\" MAXLENGTH=\"6\"><BR>");
client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");
client.println("</FORM>");
client.println("<BR>");
client.println("</BODY>");
client.println("</HTML>");
vTaskDelay( 5 / portTICK_PERIOD_MS);
//stopping client
client.stop();
}
TokenType WebServer::getParameterType(char *&token)
{
if (strcmp(token, "MQTTSERVER") == 0)
{
return TokenType::MQTT_SERVER;
}
if (strcmp(token, "DNSSERVER") == 0)
{
return TokenType::DNS_SERVER;
}
if (strcmp(token, "GATEWAY") == 0)
{
return TokenType::GATEWAY;
}
if (strcmp(token, "IPADDRESS") == 0)
{
return TokenType::IP_ADDRESS;
}
if (strcmp(token, "SUBNET") == 0)
{
return TokenType::SUBNET_MASK;
}
if (strcmp(token, "INTERVAL") == 0)
{
return TokenType::MQTT_PUBLISH_INTERVAL;
}
return TokenType::NONE;
}
void WebServer::enable()
{
_enabled = true;
}
void WebServer::disable()
{
_enabled = false;
}
bool WebServer::enabled()
{
return _enabled;
}
}