-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootWebPage.ino
183 lines (164 loc) · 6.28 KB
/
RootWebPage.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
/*
Copyright (c) 2023 David Carson (dacarson)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if defined (ESP8266)
#include <ESP8266WebServer.h>
extern ESP8266WebServer server;
#else if defined (ESP32_DEV)
#include <WebServer.h>
extern WebServer server;
#endif
// Default Settings defined in WeatherFlow-RainSensor.ino
// Keep track of when the Rain started
extern long rainStartEpoch;
// Need a stack of the last 24 hrs
// that has the rainfall total.
extern float rainInHour[];
extern int currentHour; // so we can see if we have moved into a new hour
// Rain Settings
extern RainSettings settings;
void handleRoot() {
char temp[100];
// Handle any arguements passed in
if (server.args() > 1) {
String rainFallAmountStr = server.arg("rainFallAmount");
settings.setRainFallAmount(rainFallAmountStr.toInt());
String dryingTimeStr = server.arg("dryingTime");
settings.setDryingTime(dryingTimeStr.toInt());
}
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send ( 200, "text/html", "" );
// Send HTML header
server.sendContent(F("<html>\
<head>\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
<title>WeatherFlow Rain Sensor</title>\
<link rel=\"stylesheet\" href=\"main.css\">\
</head>\
"));
// Send current configuation
server.sendContent(F("<body>\
<h1>IoT Rain Sensor</h1>\
<div class='data-box'>\
<div id='configuration' class='center-data' style='display: block;'>\
<h2>Configuration</h2>\
"));
server.sendContent(F("<p><p><form><B><label for=\"rainFallAmount\">24hr Rainfall Threshold (mm):</label>"));
server.sendContent(F("<select name=rainFallAmount>"));
for (int i = 5; i < 21; i++) {
if (i == settings.rainFallAmount())
server.sendContent(F("<option selected>"));
else
server.sendContent(F("<option>"));
sprintf(temp, "%d", i);
server.sendContent(temp);
server.sendContent(F("</option>"));
}
server.sendContent(F("</select>"));
server.sendContent(F("<p> <label for=\"dryingTime\">Delay Before Turning Back On (hours):</label>"));
server.sendContent(F("<select name=dryingTime>"));
for (int i = 1; i < 24; i++) {
if (i == settings.dryingTime())
server.sendContent(F("<option selected>"));
else
server.sendContent(F("<option>"));
sprintf(temp, "%d", i);
server.sendContent(temp);
server.sendContent(F("</option>"));
}
server.sendContent(F("</select>"));
server.sendContent(F("</b>"));
server.sendContent(F("<button type='submit' value=\"Update\" class=\"bottom-right-button\">Save Settings</button></form>"));
server.sendContent(F("</div><div id=\"status\" class=\"center-data\" style=\"display: none;\">"));
// Send current status
server.sendContent(F("<h2>Status</h2>Rain Sensor: "));
if (relayOffTime == -1) {
server.sendContent(F("Not "));
}
server.sendContent(F("Triggered<br>"));
if (relayOffTime > 0) {
server.sendContent(F("Rain sensor will reset at: <span id='relayOffTime'></span><br>"));
server.sendContent(F("<script> var date = new Date("));
sprintf(temp, "%ld", relayOffTime);
server.sendContent(temp);
server.sendContent(F(" * 1000); var time = date.toLocaleTimeString();"));
server.sendContent(F("document.getElementById('relayOffTime').innerHTML = time;</script>"));
}
server.sendContent(F("Rain detected: <span id='rainStartTime'></span><br>"));
server.sendContent(F("<script> var date = new Date("));
sprintf(temp, "%ld", rainStartEpoch);
server.sendContent(temp);
server.sendContent(F(" * 1000); var time = date.toLocaleTimeString(); var d = date.toDateString();"));
server.sendContent(F("document.getElementById('rainStartTime').innerHTML = d + ' ' + time;</script>"));
server.sendContent(F("Rainfall: Last hour: "));
sprintf(temp, "%.2f", rainInHour[currentHour]);
server.sendContent(temp);
server.sendContent(F("mm, Last 24hrs: "));
float total = 0;
for (int hour = 0; hour < 24; hour++)
total += rainInHour[hour];
sprintf(temp, "%.2f", total);
server.sendContent(temp);
server.sendContent(F("mm<p><hr>"));
// Testing
// Send current status
server.sendContent(F("<h2>Testing</h2>Send Rain start event <button type=\"button\" onclick=\"rainstart()\">Now</button><br>"));
server.sendContent(F("<script>\
function rainstart()\
{\
var xhr = new XMLHttpRequest();\
xhr.open(\"GET\", \"/rainstart\", true); \
xhr.send(null);\
}\
</script>"));
server.sendContent(F("Send observation event with rain total 3.8 <button type=\"button\" onclick=\"obstest()\">Now</button><br>"));
server.sendContent(F("<script>\
function obstest()\
{\
var xhr = new XMLHttpRequest();\
xhr.open(\"GET\", \"/obstest\", true); \
xhr.send(null);\
}\
</script>"));
server.sendContent(F("Reset all stored rain data <button type=\"button\" onclick=\"reset()\">Reset</button><br>"));
server.sendContent(F("<script>\
function reset()\
{\
var xhr = new XMLHttpRequest();\
xhr.open(\"GET\", \"/reset\", true); \
xhr.send(null);\
}\
</script>"));
// Send build info
server.sendContent(F("<p><pre>Compiled: "));
server.sendContent(F(__DATE__));
server.sendContent( F(", "));
server.sendContent(F(__TIME__));
server.sendContent( F(", GCC: "));
server.sendContent(F(__VERSION__));
server.sendContent(F("</pre></p>\
</div></div>\
<div class=\"data-box\">\
<div class=\"link-box\">\
<a href=\"#\" id=\"swapLink\">Status</a>\
</div>\
</div>\
<script src=\"main.js\"></script>\
</body></html>"));
server.client().stop();
}