-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipowersw.ino
345 lines (297 loc) · 8.21 KB
/
ipowersw.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Ethernet IP power switch with HTTP web interface and REST API. Includes temperature, humidity and ambient light metering.
* Powered by Arduino + ENC28J60 module, relay, DHT11 sensor, photoresistor.
* Read more at https://github.com/vinklat/ipowersw
* by Vaclav Vinklat, 2015, GPLv2
*/
#include <UIPEthernet.h>
#include <DHT.h>
/*
* Setup your application here:
*/
#define DEBUG
#define PIN_RELAY 4
#define PIN_DHT 5
#define PIN_LIGHT 2
#define USE_DHCP
#define IP_ADDR 192,168,0,2
#define MAC_ADDR 0xDE,0xAD,0xBE,0xEF,0xFE,0xED
#define REQ_BUF_SIZE 5
#define LOC_BUF_SIZE 10
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
/*
*
*/
template<class T> inline Print &operator <<(Print &obj, T arg) {
obj.print(arg); return obj;
}
/*
*
*/
EthernetServer server(80);
int relay_state;
#ifdef PIN_DHT
DHT dht(PIN_DHT, DHTTYPE);
#endif
/*
*
*/
void setup() {
#ifdef DEBUG
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
#endif
const byte mac[] = { MAC_ADDR };
// start the Ethernet connection:
#ifdef USE_DHCP
while (!Ethernet.begin(mac)) {
#ifdef DEBUG
Serial.println("Failed to configure Ethernet using DHCP");
#endif
delay(2);
}
#else
IPAddress ip(IP_ADDR);
Ethernet.begin(mac, ip);
#endif
server.begin();
#ifdef DEBUG
Serial << "server is at " << Ethernet.localIP() << '\n';
#endif
// default relay pin state HIGH (=relay output LOW)
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_RELAY, HIGH);
relay_state=0;
#ifdef PIN_DHT
dht.begin();
#endif
}
/*
*
*/
#define REQ_ERR 0
#define REQ_GET 1
#define REQ_POST 2
void get_req(EthernetClient *client, int *req, char *loc_buf) {
#define STATE_FIND_REQ 0
#define STATE_GET_LOC 1
#define STATE_SKIP_LINE 2
#define STATE_SKIP_ALL 3
#define STATE_GET_BODY 4
int pos=0;
int state=STATE_FIND_REQ;
char req_buf[REQ_BUF_SIZE];
*req=REQ_ERR;
while (client->connected()) {
if (client->available()) {
char c = client->read();
/*
#ifdef DEBUG
Serial << "ch='" << c << "'(" << (byte)c << ") state=" << state << '\n';
#endif
*/
switch (state) {
case STATE_FIND_REQ:
if (pos==REQ_BUF_SIZE-1 || c==' ')
c='\0';
*(req_buf+pos) = c;
if (c=='\0') {
if (!strcmp(req_buf,"GET")) {
*req=REQ_GET;
state=STATE_GET_LOC;
pos=0;
}
else
if (!strcmp(req_buf,"POST")) {
*req=REQ_POST;
state=STATE_GET_LOC;
pos=0;
}
else {
state = STATE_SKIP_LINE;
}
}
else
pos++;
break;
case STATE_SKIP_LINE:
if (c=='\n') {
state=STATE_FIND_REQ;
pos=0;
}
break;
case STATE_GET_LOC:
if (pos==LOC_BUF_SIZE-1 || c==' ' || c=='\n' || c=='\r')
c='\0';
*(loc_buf+pos) = c;
if (c=='\0') {
state = STATE_SKIP_ALL;
}
else
{
pos++;
}
break;
case STATE_SKIP_ALL:
if (c=='\n') {
if (!pos)
state=STATE_GET_BODY;
pos=0;
}
else
if (c != 13)
pos++;
break;
case STATE_GET_BODY:
//not implemented yet
break;
}
}
else
break;
}
}
/*
*
*/
#define HTTP "HTTP/1.0 "
#define REPLY_200 "200 OK\n"
#define REPLY_400 F("400 Bad Request\n")
#define REPLY_404 F("404 Not Found\n")
#define API_CONTYPE F("Content-Type: application/json\n")
#define HTML_CONTYPE F("Content-Type: text/html\n")
#define CLOSE F("Connection: close\n")
#define NO_CACHE F("Cache-Control: no-cache, no-store, must-revalidate\nPragma: no-cache\nExpires: 0\n")
#define REDIRECT F("Refresh: 3; url=/")
#define HTML_HEAD F("<!DOCTYPE HTML><html><body>")
#define HTML_FOOT F("</body></html>")
#define HTML_FORM_ON F("<form action=\"/1\" method=\"POST\"><input type=\"submit\" value=\"Turn ON\"></form>")
#define HTML_FORM_OFF F("<form action=\"/0\" method=\"POST\"><input type=\"submit\" value=\"Turn OFF\"></form>")
#define LOC_ROOT 0
#define LOC_0 1
#define LOC_1 2
#define LOC_API_ROOT 3
#define LOC_API_0 4
#define LOC_API_1 5
void render_response(EthernetClient *client, int *req, char *loc_buf) {
const char* const locations[] = {"/", "/0", "/1", "/api/", "/api/0", "/api/1" };
bool found=false;
int loc;
for (loc=0; loc<6; loc++) {
if (!strcmp(loc_buf, locations[loc])) {
found=true;
break;
}
}
if (!*req) {
*client << HTTP << REPLY_400 << HTML_CONTYPE << CLOSE << '\n';
*client << HTML_HEAD << REPLY_400 << HTML_FOOT;
return;
}
if (found)
{
if (*req==REQ_GET) {
#ifdef PIN_DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
#endif
#ifdef PIN_LIGHT
int light = analogRead(PIN_LIGHT);
#endif
switch (loc) {
case LOC_ROOT:
*client << HTTP << REPLY_200 << HTML_CONTYPE << NO_CACHE << CLOSE << '\n';
*client << HTML_HEAD << "relay: " << relay_state << "<br/>";
#ifdef PIN_DHT
*client << "humidity: " << h << "%<br/>temperature: " << t << "°C<br/>";
#endif
#ifdef PIN_LIGHT
*client << "light: " << light << "<br/>";
#endif
if (relay_state)
*client << HTML_FORM_OFF;
else
*client << HTML_FORM_ON;
*client << HTML_FOOT;
break;
case LOC_API_ROOT:
*client << HTTP << REPLY_200 << API_CONTYPE << CLOSE << '\n';
*client << "{\"relay\": " << relay_state;
#ifdef PIN_DHT
*client << ", \"humidity\": " << h << ", \"temperature\": " << t;
#ifdef PIN_LIGHT
*client << ", \"light\": " << light;
#endif
#endif
*client << "}";
break;
default:
found=false;
}
}
if (*req==REQ_POST) {
switch (loc) {
case LOC_API_0:
digitalWrite(PIN_RELAY, HIGH);
relay_state=0;
*client << HTTP << REPLY_200 << API_CONTYPE << CLOSE << '\n';
*client << "{\"relay\": " << relay_state << "}";
break;
case LOC_API_1:
digitalWrite(PIN_RELAY, LOW);
relay_state=1;
*client << HTTP << REPLY_200 << API_CONTYPE << CLOSE << '\n';
*client << "{\"relay\": " << relay_state << "}";
break;
case LOC_0:
digitalWrite(PIN_RELAY, HIGH);
relay_state=0;
*client << HTTP << REPLY_200 << HTML_CONTYPE << NO_CACHE << REDIRECT << '\n';
// *client << HTML_HEAD << "relay: " << relay_state << HTML_FORM_ON << HTML_FOOT;
break;
case LOC_1:
digitalWrite(PIN_RELAY, LOW);
relay_state=1;
*client << HTTP << REPLY_200 << HTML_CONTYPE << NO_CACHE << REDIRECT << '\n';
// *client << HTML_HEAD << "relay: " << relay_state << HTML_FORM_OFF << HTML_FOOT;
break;
default:
found=false;
}
}
}
if (!found) {
*client << HTTP << REPLY_404 << HTML_CONTYPE << CLOSE << '\n';
*client << HTML_HEAD << REPLY_404 << HTML_FOOT;
return;
}
}
/*
*
*/
void loop() {
char loc_buf[LOC_BUF_SIZE];
int req;
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
#ifdef DEBUG
Serial << "new client...\n";
#endif
get_req(&client, &req, loc_buf);
render_response(&client, &req, loc_buf);
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
#ifdef DEBUG
Serial.println("client disconnected");
#endif
}
}