-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiGarduino.ino
148 lines (131 loc) · 4.14 KB
/
iGarduino.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
#include <NTPtimeESP.h>
#include <ESP8266WiFi.h>
#define DEBUG_ON
#ifdef DEBUG_ON
#define Sprint(a) (Serial.print(a))
#define Sprintln(a) (Serial.println(a)
#else
#define Sprint(a)
#define Sprintln(a)
#endif
#ifdef SECONDS_MODE
#define dateTime.hour dateTime.second
#endif
//NTP-clock
NTPtime NTPch("nl.pool.ntp.org"); // Choose server pool as required
strDateTime dateTime;
int previousHour;
//Wifi & thingspeak
String apiKey = "IKOLI389JPQ71CKZ";
const char *ssid = "Catharijnesingel93b";
const char *pass = "Catharijnesingel93b!";
const char* server = "api.thingspeak.com";
WiFiClient client;
const int MOISTURE_PIN = A0;
const char WATERPUMP_PIN = D5;
const int WATER_THRESHOLD = 650; //higher means dryer
const int WATERING_TIME = 15000;
const int WATERING_HOUR = 20;
//AnalogRead
int reading = -1;
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(MOISTURE_PIN, INPUT);
pinMode(WATERPUMP_PIN, OUTPUT);
//Connect to wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//Connection succesfull
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
//Get NTP time
Serial.println("Getting NTP time");
dateTime = NTPch.getNTPtime(1.0, 1);
while (!(dateTime.valid)) {
Serial.println(".");
dateTime = NTPch.getNTPtime(1.0, 1);
delay(10);
}
//NTP time retrieved
Serial.println("");
previousHour = dateTime.hour;
Serial.print("Fetched current hour is: ");
Serial.println(previousHour);
digitalWrite(WATERPUMP_PIN, LOW);
}
void loop() {
dateTime = NTPch.getNTPtime(1.0, 1);
if (dateTime.valid) { //check if time in in right format
int currentHour = dateTime.hour;
if (currentHour == 0 && previousHour == 23 || currentHour == previousHour + 1) { // the only correct hour transitions
executeCycle(currentHour);
}
else if (currentHour == previousHour) {
Serial.print("not a new hour, currentHour: " + currentHour);
delay(100);
//TODO: deepsleep
//delay(100); //remove this delay after deepsleep has been implemented
}
else {
Serial.println("faulty time detected");
}
}
}
void executeCycle(int pCurrentHour) {
Serial.println("new hour, executing cycle");
Serial.print("previous hour: " + previousHour);
Serial.println(" current hour: " + pCurrentHour);
int reading = analogRead(MOISTURE_PIN);
Serial.println("current reading: " + reading);
updateWeb(reading);
if (pCurrentHour == WATERING_HOUR) {
Serial.println("It's watering time: " + dateTime.hour);
if (reading > WATER_THRESHOLD) {
Serial.print("threshold broken, threshold: "+WATER_THRESHOLD);
Serial.println(" reading: "+reading);
waterPlants();
reading = analogRead(MOISTURE_PIN);
delay(1000); //Small delay for analogRead
Serial.println("reading after watering: "+reading);
updateWeb(reading); //Upload again after watering
}
}
Serial.print("Update 'previousHour' -> previousHour:"+previousHour);
Serial.println(" currentHour:"+pCurrentHour);
previousHour = pCurrentHour;
Serial.println("updated 'previousHour' is now: " +previousHour);
}
void waterPlants() {
//activates waterpump
digitalWrite(WATERPUMP_PIN, HIGH);
delay(WATERING_TIME);
digitalWrite(WATERPUMP_PIN, LOW);
delay(30000); //Let water get into soil before measuring
}
void updateWeb(int value) {
//uploads 'value' to thingspeak
if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr += "&field1=";
postStr += value;
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
}