-
Notifications
You must be signed in to change notification settings - Fork 9
/
BlynkSprinklerRelay.ino
371 lines (298 loc) · 8 KB
/
BlynkSprinklerRelay.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**************************************************************
* Blynk is a platform with iOS and Android apps to control
* Arduino, Raspberry Pi and the likes over the Internet.
* You can easily build graphic interfaces for all your
* projects by simply dragging and dropping widgets.
*
* Downloads, docs, tutorials: http://www.blynk.cc
* Blynk community: http://community.blynk.cc
* Social groups: http://www.fb.com/blynkapp
* http://twitter.com/blynk_app
*
* Blynk library is licensed under MIT license
* This example code is in public domain.
*
**************************************************************
* This example runs directly on ESP8266 chip.
*
* You need to install this for ESP8266 development:
* https://github.com/esp8266/Arduino
*
* Change WiFi ssid, pass, and Blynk auth token to run :)
*
**************************************************************/
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#define BLYNK_EXPERIMENTAL
#define TIME_SECONDS 1000
#define TIME_MINUTES 60000
#define TIME_UNIT TIME_MINUTES
//#define TIME_UNIT TIME_SECONDS
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h> //https://github.com/blynkkk/blynk-library
#include <EEPROM.h>
#include <ESP8266mDNS.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h> //http://playground.arduino.cc/Code/SimpleTimer || https://github.com/jfturcot/SimpleTimer
#include <DHT.h>
//either create a config.h and define auth token there
//or comment and include here like:
//char auth[] = "BLYNK_AUTH_TOKEN";
#include "Config.h"
SimpleTimer timer;
int timerId;
unsigned long counter = 0;
float h = 0;
float t = 0;
//DHT22 config
#define DHTPIN 4 // what pin DHT is connected to
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE, 20);
boolean started = false;
int selected = 0;
const int channelPins[4] = {5, 4, 2, 0};
const int channelRemainingPins[4] = {1, 2, 3, 4};
const int channelStatusPins[4] = {5, 6, 7, 8};
unsigned long wateringStart[4] = {0, 0, 0, 0};
unsigned long wateringLength[4] = {0, 0, 0, 0};
String statuses[4] = {"", "", "", ""};
//String status = "boot";
const int CMD_WAIT = 0;
const int CMD_START = 1;
const int CMD_STOP = 2;
const int CMD_STOP_SELECTED = 3;
const int CMD_STOP_EXPIRED = 4;
int state = CMD_WAIT;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
void sendData() {
Serial.print("connecting to ");
Serial.println(host);
WiFiClient emoClient;
const int httpPort = 80;
if (!emoClient.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/input/post.json?node=";
url += nodeId;
url += "&apikey=";
url += privateKey;
url += "&json={temperature:";
url += t;
url += ",humidity:";
url += h;
url += "}";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
emoClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while (emoClient.available()) {
String line = emoClient.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
void heartBeat() {
counter++;
int i = counter % 4;
unsigned long remaining = 0;
if (wateringStart[i] > 0) {
remaining = (wateringStart[i] + wateringLength[i] - millis()) / TIME_UNIT ;
}
//Blynk.virtualWrite(19, remaining);
Blynk.virtualWrite(channelRemainingPins[i], remaining);
if (wateringStart[i] > 0 && wateringStart[i] + wateringLength[i] < millis()) {
//done watering
state = CMD_STOP_EXPIRED;
}
if (counter % 60 == 0) {
h = dht.readHumidity();
t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
sendData();
}
}
}
void setStatus(int channel, String s) {
statuses[channel] = s;
Blynk.virtualWrite(channelStatusPins[channel], s);
}
void startWatering(int channel, int wl) {
wateringLength[channel] = wl * TIME_UNIT;
state = CMD_START;
}
void stopWatering(int channel) {
digitalWrite(channelPins[channel], LOW);
wateringStart[channel] = 0;
wateringLength[channel] = 0;
setStatus(channel, " off");
Blynk.virtualWrite(channelRemainingPins[channel], 0);
}
void setup()
{
Serial.begin(115200);
WiFiManager wifi;
//taking care of wifi connection
wifi.autoConnect("Blynk");
//String ssid = wifi.getSSID();
//String pass = wifi.getPassword();
//config blynk
Blynk.config(auth);
//setup pins
for (int i = 0; i < 4; i++) {
pinMode(channelPins[i], OUTPUT);
digitalWrite(channelPins[i], LOW);
delay(10);
}
//setup timers
timer.setInterval(1000, heartBeat);
//setup hardware
dht.begin();
Serial.println("done setup");
}
void loop()
{
//blynk connect and run loop
Blynk.run();
//timer run loop
timer.run();
//run once after blynk is connected
if (!started && Blynk.connected()) {
started = true;
//setStatus(0, status);
setStatus(0, "Node");
yield();
setStatus(1, "has");
yield();
setStatus(2, "boot");
yield();
setStatus(3, "ed");
yield();
Blynk.virtualRead(0);
yield();
}
// if (Blynk.connected()) {
if (state != CMD_WAIT) {
Serial.println("new command");
switch (state) {
case CMD_START:
Serial.println("start water");
digitalWrite(channelPins[selected], HIGH);
if (wateringStart[selected] == 0) {
wateringStart[selected] = millis();
setStatus(selected, " ON");
}
break;
case CMD_STOP:
Serial.println("stop all");
for (int i = 0; i < 4; i++) {
//done watering
stopWatering(i);
}
break;
case CMD_STOP_SELECTED:
Serial.println("stop selected");
//done watering
stopWatering(selected);
break;
case CMD_STOP_EXPIRED:
Serial.println("stop expired");
for (int i = 0; i < 4; i++) {
if (wateringStart[i] > 0 && wateringStart[i] + wateringLength[i] < millis()) {
//done watering
stopWatering(i);
}
}
break;
}
//reset state
state = CMD_WAIT;
}
// }
//TODO:
//timer with auto timeout
//check to see when we lost connectivity and reboot after a set time
}
BLYNK_WRITE(0) {
Serial.println("selected");
int a = param.asInt();
selected = a - 1;
}
BLYNK_WRITE(11) {
int a = param.asInt();
if (a != 0) {
startWatering(selected, 30);
}
}
BLYNK_WRITE(12) {
int a = param.asInt();
if (a != 0) {
startWatering(selected, 60);
}
}
BLYNK_WRITE(13) {
int a = param.asInt();
if (a != 0) {
startWatering(selected, 90);
}
}
BLYNK_WRITE(14) {
int a = param.asInt();
if (a != 0) {
startWatering(selected, 120);
}
}
//status indicator - value display widget
BLYNK_READ(5) {
Blynk.virtualWrite(5, statuses[0]);
}
BLYNK_READ(6) {
Blynk.virtualWrite(6, statuses[1]);
}
BLYNK_READ(7) {
Blynk.virtualWrite(7, statuses[2]);
}
BLYNK_READ(8) {
Blynk.virtualWrite(8, statuses[3]);
}
//stop all - button
BLYNK_WRITE(9) {
int a = param.asInt();
if (a != 0) {
state = CMD_STOP;
}
}
//stop - button
BLYNK_WRITE(18) {
int a = param.asInt();
if (a != 0) {
state = CMD_STOP_SELECTED;
}
}
//time left indicator - value display widget
/*BLYNK_READ(19) {
unsigned long remaining = 0;
if (wateringStart[0] > 0) {
remaining = (wateringStart[0] + wateringLength[0] - millis()) / TIME_UNIT ;
}
Blynk.virtualWrite(19, remaining);
}*/
BLYNK_READ(30) {
long uptime = millis() / TIME_UNIT;
Blynk.virtualWrite(30, uptime);
}
//RESET ESP V31 - Button
BLYNK_WRITE(31) {
int a = param.asInt();
if (a != 0) {
Serial.println("reset");
ESP.reset();
delay(1000);
}
}