-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdaGarden.ino
309 lines (259 loc) · 7.98 KB
/
AdaGarden.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
#include "AdaGarden.h"
// Board
#include "Esp.h"
#include "SPI.h"
// Adafruit IO
#include "AdafruitIO_WiFi.h"
// Sensors
#include "Adafruit_Si7021.h"
#include "DallasTemperature.h"
#include "OneWire.h"
#include <Wire.h>
#include <Adafruit_INA219.h>
// Time
#include <NTPClient.h>
#include "Time.h"
#include <TimeLib.h>
#include "WiFiUdp.h"
ADC_MODE(ADC_VCC);
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
AdafruitIO_Feed *garden_pump = io.feed("garden-pump");
AdafruitIO_Feed *garden_vcc = io.feed("garden-vcc");
AdafruitIO_Feed *garden_outdoor_temp = io.feed("garden-outdoor-temp");
AdafruitIO_Feed *garden_board_temp = io.feed("garden-board-temp");
AdafruitIO_Feed *garden_board_rh = io.feed("garden-board-rh");
AdafruitIO_Feed *garden_free_heap = io.feed("garden-free-heap");
AdafruitIO_Feed *garden_battery_voltage = io.feed("garden-battery-voltage");
AdafruitIO_Feed *garden_battery_current = io.feed("garden-battery-current");
// Sensors
Adafruit_Si7021 sensor = Adafruit_Si7021();
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_INA219 ina219;
Adafruit_INA219 ina219b;
// Time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Sketch variables
typedef struct {
bool pump_is_open;
time_t last_push;
time_t last_debug_push;
time_t pump_last_open;
} storeDefinition;
struct {
uint32_t crc32;
storeDefinition store;
} rtcData;
uint32_t push_interval = 60;
uint32_t push_debug_interval = 5 * 60;
uint32_t pump_max_interval = 5 * 60;
void setup() {
pinMode(PUMP_SET_PIN, OUTPUT);
pinMode(FAN_SET_PIN, OUTPUT);
pinMode(FAN_UNSET_PIN, OUTPUT);
if(sensor.begin()){
Serial.println("Temp/RH Sensor found");
} else {
Serial.println("Temp/RH Sensor not found");
}
float board_temp = sensor.readTemperature();
// Start fan when its hot (before connecting to wifi)
if(board_temp > FAN_TRIGGER_TEMP) {
openFan();
} else {
closeFan();
}
// Start current sensors
ina219.begin(0x41);
ina219.setCalibration_16V_400mA();
ina219b.begin(0x44);
ina219b.setCalibration_16V_400mA();
// Start the serial connection
Serial.begin(115200);
// Wait for serial monitor to open
while(! Serial);
loadRtc();
Serial.println("Connecting to Adafruit IO");
// connect to io.adafruit.com
io.connect();
// set up a message handler for the count feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
garden_pump->onMessage(handlePump);
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println(io.statusText());
// We are connected
printWifiDetails();
sensors.begin();
// Sync time
timeClient.begin();
setSyncProvider(getNtpTime);
while(timeStatus() == timeNotSet) { delay(50); }
}
void loop() {
bool store_updated = false;
// Max pump run time
if(rtcData.store.pump_is_open && now() > (rtcData.store.pump_last_open + pump_max_interval)) {
closeRelay();
store_updated = true;
garden_pump->save(0);
}
Serial.println("Time now is :");
Serial.println(now());
float board_temp = sensor.readTemperature();
float board_humidity = sensor.readHumidity();
Serial.print("Board Temp: "); Serial.print(board_temp); Serial.println("c");
Serial.println("");
float shuntvoltage = ina219.getShuntVoltage_mV();
float busvoltage = ina219.getBusVoltage_V();
float current_mA = ina219.getCurrent_mA();
float loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
float shuntvoltage_b = ina219b.getShuntVoltage_mV();
float busvoltage_b = ina219b.getBusVoltage_V();
float current_mA_b = ina219b.getCurrent_mA();
float loadvoltage_b = busvoltage_b + (shuntvoltage_b / 1000);
Serial.print("Bus Voltage B: "); Serial.print(busvoltage_b); Serial.println(" V");
Serial.print("Shunt Voltage B: "); Serial.print(shuntvoltage_b); Serial.println(" mV");
Serial.print("Load Voltage B: "); Serial.print(loadvoltage_b); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA_b); Serial.println(" mA");
// Start fan when its hot
if(board_temp > FAN_TRIGGER_TEMP) {
openFan();
Serial.println("opening fan");
} else {
closeFan();
Serial.println("closing fan");
}
if(now() > (rtcData.store.last_push + push_interval)) {
rtcData.store.last_push = now();
store_updated = true;
sensors.requestTemperatures();
garden_board_temp->save(board_temp);
garden_board_rh->save(board_humidity);
garden_outdoor_temp->save(sensors.getTempCByIndex(0));
garden_battery_voltage->save(loadvoltage);
garden_battery_current->save(current_mA);
}
if(now() > (rtcData.store.last_debug_push + push_debug_interval)) {
rtcData.store.last_debug_push = now();
store_updated = true;
garden_vcc->save((float)ESP.getVcc()/1024.0);
garden_free_heap->save((float)ESP.getFreeHeap());
}
if(store_updated) {
Serial.println("Saving store...");
saveRtc();
}
io.run();
}
void handlePump(AdafruitIO_Data *data) {
Serial.print("received <- ");
Serial.println(data->value());
if(data->toBool()) {
openRelay();
} else {
closeRelay();
}
Serial.println("");
}
void openRelay() {
digitalWrite(PUMP_SET_PIN, HIGH);
rtcData.store.pump_is_open = true;
rtcData.store.pump_last_open = now();
}
void closeRelay() {
digitalWrite(PUMP_SET_PIN, LOW);
rtcData.store.pump_is_open = false;
}
void openFan() {
digitalWrite(FAN_SET_PIN, HIGH);
delay(50);
digitalWrite(FAN_SET_PIN, LOW);
}
void closeFan() {
digitalWrite(FAN_UNSET_PIN, HIGH);
delay(50);
digitalWrite(FAN_UNSET_PIN, LOW);
}
void printWifiDetails() {
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Mac Address: ");
Serial.println(WiFi.macAddress());
}
void printStoreState() {
Serial.println("");
Serial.print("pump_is_open : ");
Serial.println(rtcData.store.pump_is_open);
Serial.print("last_push : ");
Serial.println(rtcData.store.last_push);
Serial.print("last_debug_push : ");
Serial.println(rtcData.store.last_debug_push);
Serial.print("pump_last_open : ");
Serial.println(rtcData.store.pump_last_open);
}
void loadRtc(){
if (ESP.rtcUserMemoryRead(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
Serial.println("Read: ");
printStoreState();
Serial.println();
uint32_t crcOfData = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
if (crcOfData != rtcData.crc32) {
Serial.println("CRC32 INVALID");
Serial.print("CRC32 of data: ");
Serial.println(crcOfData, HEX);
Serial.print("CRC32 read from RTC: ");
Serial.println(rtcData.crc32, HEX);
Serial.println("Reseting store...");
rtcData.store = { false, 0, 0, 0 };
saveRtc();
} else {
Serial.println("CRC32 VALID");
}
}
}
void saveRtc() {
// Update CRC32 of data
rtcData.crc32 = calculateCRC32(((uint8_t*) &rtcData) + 4, sizeof(rtcData) - 4);
// Write struct to RTC memory
if (ESP.rtcUserMemoryWrite(0, (uint32_t*) &rtcData, sizeof(rtcData))) {
Serial.println("Write: ");
printStoreState();
Serial.println();
}
}
uint32_t calculateCRC32(const uint8_t *data, size_t length) {
uint32_t crc = 0xffffffff;
while (length--) {
uint8_t c = *data++;
for (uint32_t i = 0x80; i > 0; i >>= 1) {
bool bit = crc & 0x80000000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x04c11db7;
}
}
}
return crc;
}
// Return the time from the Ntp time client
time_t getNtpTime() {
timeClient.update();
return timeClient.getEpochTime();
}