-
Notifications
You must be signed in to change notification settings - Fork 8
/
Webserver.ino
187 lines (156 loc) · 4.99 KB
/
Webserver.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
177
178
179
180
181
182
183
184
185
186
/*
Control of Humidifier Connected to a RC Socket
https://github.com/krzychb/OnlineHumidifier
Part A3 - http
Copyright (c) 2015 Krzysztof Budzynski. All rights reserved.
This file is part of OnlineHumidifier project - https://github.com/krzychb/OnlineHumidifier
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
void setupWiFi(void)
{
WiFi.begin(ssid, password);
Serial.println();
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void handleRoot(void)
{
String message = "hello from esp8266!";
message += "\nHumidity = ";
message += (String) humidity;
message += "%";
server.send(200, "text/plain", message);
}
void handleNotFound(void)
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
//
// web server set up and events
//
void setupWebserver(void)
{
server.on("/", handleRoot);
server.on("/hygrostat", []() {
Serial.print("HTTP REQUEST > ");
for (uint8_t i = 0; i < server.args(); i++)
{
if (server.argName(i) == "Humidifier")
{
humidifier = (server.arg(i) == "ON") ? HIGH : LOW;
actionTransmitter.sendSignal(1, rcDevice, humidifier);
}
else if (server.argName(i) == "AutoMode")
{
autoMode = (server.arg(i) == "ON") ? true : false;
}
else if (server.argName(i) == "HumiditySetPoint")
{
humiditySetPoint = server.arg(i).toFloat();
}
else
{
Serial.println("unknown argument! ");
}
Serial.print(server.argName(i));
Serial.print(": ");
Serial.print(server.arg(i));
Serial.print(" > ");
}
Serial.println("done");
showControlScreen();
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void showControlScreen(void)
{
String message;
message += "<html>";
message += "<head><meta http-equiv=\"refresh\" content=\"5; url='/hygrostat\"'><title>Online Humidifier</title></head>";
message += "<body>";
message += "<h3>Hygrostat</h3>";
message += "Humidity: " + (String) humidity + "%<br/>";
message += "Setpoint: " + (String) humiditySetPoint + "%";
message += "<form action=\"/hygrostat\" method=\"get\">";
message += "Change <input type=\"text\" name=\"HumiditySetPoint\" size=\"3\" value=\"" + (String) humiditySetPoint + "\"><input type=\"submit\" value=\"Submit\">";
message += "</form>";
message += "Humidifier: ";
if (humidifier == true)
{
message += "ON, switch it <a href=\"/hygrostat?Humidifier=OFF\">OFF</a><br/>";
}
else
{
message += "OFF, switch it <a href=\"/hygrostat?Humidifier=ON\">ON</a><br/>";
}
message += "Auto Mode: ";
if (autoMode == true)
{
message += "ON, turn it <a href=\"/hygrostat?AutoMode=OFF\">OFF</a><br/>";
}
else
{
message += "OFF, turn it <a href=\"/hygrostat?AutoMode=ON\">ON</a><br/>";
}
message += "</body>";
message += "</html>";
server.send(200, "text/html", message);
}
//
// OTA set up and events
//
void setupOTA(void)
{
ArduinoOTA.setHostname(host);
ArduinoOTA.onStart([]() {
Serial.println("OTA upload start");
// switch humidifier off in case OTA fails
actionTransmitter.sendSignal(1, rcDevice, LOW);
Serial.println("Humidifier switched off");
});
ArduinoOTA.onEnd([]() {
Serial.println("OTA upload end");
Serial.println("Restarting...");
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("OTA initialized");
}