-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_connect.ino
241 lines (201 loc) · 6.71 KB
/
web_connect.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
void routesSetup(){
// TODO: faire refleter ça dans l'index
server.on ( "/", handleRoot );
server.on ( "/on", lightOn );
server.on ( "/off", lightOff );
server.on ( "/light", HTTP_GET, getLight );
server.on ( "/light", HTTP_POST, setLight );
server.on ( "/sunrise", sunrise );
server.on ( "/dht", getDHT);
server.on ( "/alarms", showAlarms);
server.on ( "/chipId", getChipId);
server.on ( "/update", handleUpdate);
server.on ( "/upgrade", HTTP_POST, handleUpgrade, handleUpload);
server.onNotFound( handleNotFound );
}
void handleNotFound() {
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 );
}
void badRequest(String msg){
server.send ( 400, "text/plain", msg);
}
void getLight() {
String msg = "{\"light\":" + String(lastValue) + "}";
server.send ( 200, "application/json", msg);
}
void setLight() {
if(!server.hasArg("light")){
badRequest("argument light is missing");
return;
}
lastValue = server.arg("light").toInt();
getLight(); // send current value back to client
}
void sunrise(){
int slopeDuration = 10; // progressive slope time
int maxBrightness = 255; // max/final brightness
int postDelay = -1; // delay to keep light on after slope
if(server.hasArg("slopeDuration")){
slopeDuration = server.arg("slopeDuration").toInt();
}
if(server.hasArg("maxBrightness")){
maxBrightness = server.arg("maxBrightness").toInt();
}
if(server.hasArg("postDelay")){
postDelay = server.arg("postDelay").toInt();
}
int step = 20;
int speed = 1000;
if(server.hasArg("step")){
step = server.arg("step").toInt();
}
if(server.hasArg("speed")){
speed = server.arg("speed").toInt();
}
progressiveLight(speed, step);
}
void getDHT() {
// float t = dht.readTemperature(), h = dht.readHumidity();
float t = getTemperature();
float h = getHumidity();
String s = String("{\"temperature\" : " + (String)t + ",\n \"humidity\" : " + (String)h + "}");
server.send(200, "text/json", s);
}
void getChipId() {
String id = String(ESP.getChipId());
String s = String("{\"chipID\" : " + id + "}");
server.send(200, "text/json", s);
}
void handleUpload() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.setDebugOutput(true);
// WiFiUDP::stopAll();
Serial.printf("Update: %s\n", upload.filename.c_str());
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.printError(Serial);
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
Serial.setDebugOutput(false);
}
yield();
}
void handleRoot() {
const int buffSize = 1024;
char temp[buffSize];
int sec = millis() / 1000;
int minut = sec / 60;
int hr = minut / 60;
int h = dht.readHumidity();
// Read temperature as Celsius (the default)
int t = dht.readTemperature();
snprintf ( temp, buffSize,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>Sunrize</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Sunrize</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<p><a href=\"/update\">Update internal sketch</a></p>\
<p><a href=\"/dht\">Get DHT values</a></p>\
<p><a href=\"/alarms\">Set alarms</a></p>\
<p>Last light strength sent : %02d</p>\
<p><a href=\"/on\"><button>ON</button></a> <a href=\"/off\"><button>OFF</button></a></p>\
<p><a href=\"/sunrise\"><button>Start sunrise (default values)</button></a></p>\
<br/>\
<p>Temperature : %d, humidity : %d</p>\
</body>\
</html>",
hr, minut % 60, sec % 60,
//hour(), minute(), second(), ((weekday()+5)%7) + 1,
//status,
lastValue,
t, h
);
server.send ( 200, "text/html", temp );
}
void handleUpdate(){
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
const char* updateIndex = "<form method='POST' action='/upgrade' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
server.send(200, "text/html", updateIndex);
}
void handleUpgrade(){
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
if (Update.hasError()) {
server.send(200, "text/plain", "Update failed");
} else {
server.sendHeader("Location", String("/"), true);
server.send ( 302, "text/plain", "");
}
ESP.restart();
}
void showAlarms(){
const int buffSize = 1024;
char temp[buffSize];
char listTemp[buffSize/2];
String HTMLAlarms = listHTMLAlarms();
HTMLAlarms.toCharArray(listTemp, buffSize/2);
Serial.println("Alarms : ");
Serial.println(HTMLAlarms);
snprintf ( temp, buffSize,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<meta charset=\"UTF-8\">\
<title>Sunrize - Alarms</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>Alarms</h1>\
<form action=\"set_alarms\" method=\"get\">\
Switch heater on under <input type=\"text\" size=\"2\" name=\"minTemp\" value=\"19\"> \°C <br/>\
Heater IP address : \
<input type=\"text\" size=\"3\" name=\"heater_ip_1\" value=\"192\"> .\
<input type=\"text\" size=\"3\" name=\"heater_ip_2\" value=\"168\"> .\
<input type=\"text\" size=\"3\" name=\"heater_ip_3\" value=\"1\"> .\
<input type=\"text\" size=\"3\" name=\"heater_ip_4\" value=\"16\"> <br/> \
<br/>\
<input type=\"submit\" value=\"Save\">\
</form>\
<br/>\
<h2>Existing alarms</h2>\
%s\
</body>\
</html>",
//hour(), minute(), second(), ((weekday()+5)%7) + 1,
//status,
listTemp
);
server.send ( 200, "text/html", temp );
}
/**/