-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathedl.ino
372 lines (340 loc) · 8.44 KB
/
edl.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
369
370
371
372
//Ecological Data Logger
//======================
//
//Ed Baker (ebaker.me.uk)
//Libraries
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include "lta_Struct.h"
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <Sleep_n0m1.h>
//DHT (Digital Humidity and Temperature Sensor)
#define DHTPIN 2
#define DHTTYPE DHT22
//SD Card
#define SDPIN 4
//Global variables
int mode; //The mode that the device is running in
EthernetClient client;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "infocology.co.uk"; // Google
char auth_name[] = "token";
char auth_key[] = "$1$jdgjNCEX$.4kM0/YL4a97jn579QDl60";
DHT dht(DHTPIN, DHTTYPE);
Sleep sleep;
//Function Prototypes
void(* softReset) (void) = 0; //soft reset
void setup() {
//Serial connection
Serial.begin(9600);
while(!Serial){
;
}
serial_boilerplate();
//Choose run mode
mode = mode_select();
mode = 4; //TODO remove this when hardware switch enabled
//Check for SD card
if (mode == 2 || mode == 3 || mode == 4) {
Serial.println("Initalising SD card...");
//Pin 53 on mega must be output (10 on other baords)
pinMode(53, OUTPUT);
int i=0;
boolean sd_check = false;
while (sd_check == false && i < 10) {
i++;
sd_check = init_sd();
delay(2000);
if (sd_check == false) {
String error_msg = ("SD card initialisation failed on attempt ");
error_msg.concat(i);
error error = {
error_msg,
3 //WARNING
};
error_condition(error);
}
}
if (sd_check == false) {
int warning;
if (mode == 2 || mode == 3) {
warning = 1; //FATAL IN THESE CASES
} else {
warning = 2; //ERROR - USE OF ETHERNET IS EXPECTED
}
error error = {
"SD card initialisation has failed.",
3 //WARNING
};
error_condition(error);
Serial.println("SD card initalisation has failed.");
}
}
// start the Ethernet connection:
if (mode == 1 || mode == 3 || mode == 4) {
if (Ethernet.begin(mac) == 0) {
String error_msg = "Failed to configure Ethernet using DHCP";
int warning;
if (mode == 1 || mode == 4) {
warning = 1; //FATAL
} else {
warning = 2; //ERROR
}
error error = {
error_msg,
warning
};
error_condition(error);
}
// give the Ethernet shield a second to initialize:
//delay(1000);
Serial.print("MY IP is ");
Serial.println(Ethernet.localIP());
}
//Start DHT
Serial.println("Connecting to humidity and temperature sensor");
dht.begin();
int i =0;
boolean connected = true;
if (isnan(dht.readTemperature())) { connected = false; }
if (isnan(dht.readHumidity())) { connected = false; }
if (dht.readTemperature() == 0 && dht.readHumidity() == 0) { connected = false; }
while (!connected) {
i++;
int warning;
if (i < 10) {
warning = 3;
} else {
warning = 1;
}
String error_msg = "Cannot connect to sensor. Try ";
error_msg.concat(i);
error error = {
error_msg,
warning
};
error_condition(error);
connected = true;
if (isnan(dht.readTemperature())) { connected = false; }
if (isnan(dht.readHumidity())) { connected = false; }
if (dht.readTemperature() == 0 && dht.readHumidity() == 0) { connected = false; }
}
Serial.println("Connected.");
Serial.println();
Serial.println("Starting program loop.");
}
void loop() {
lta data = lta_get_data();
data_post(data);
error_blink(1); //Should move to data_post function
Serial.println("Starting sleep.");
delay(100);
sleep.pwrDownMode();
sleep.sleepDelay(600000);
Serial.println("Ending sleep.");
}
int mode_select() {
//There should be a hardware switch for this
int mode = 1;
String text;
switch (mode) {
case 1:
text = "Ethernet only.";
break;
case 2:
text = "SD card only.";
break;
case 3:
text = "Ethernet if available, otherwise to SD card.";
break;
case 4:
text = "Ethernet and SD card.";
break;
default:
mode = 1;
Serial.println("Unknown mode specified. Defaulting to mode 1.");
}
Serial.print("Mode ");
Serial.print(mode);
Serial.print(" selected: ");
Serial.println(text);
return mode;
}
boolean sd_post(lta data) {
//Generate new line of data
char humidity[6];
char temperature[6];
dtostrf(data.humidity, 1, 2, humidity);
dtostrf(data.temperature, 1, 2, temperature);
String request;
request.concat(data.time);
request += ",";
request += humidity;
request += ",";
request += temperature;
request += ",";
request += data.ethernet;
request += ",";
request += data.fail;
//Append to file
File dataFile = SD.open("data.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(request);
dataFile.close();
Serial.println("Data saved to data.txt");
return true;
}
else {
error error = {
"Failed to open data.txt.",
2 };
error_condition(error);
return false;
}
}
boolean data_post(lta data) {
//First - attempt to post to internet
boolean ethernet = false;
if (mode == 1 || mode == 3 || mode == 4) {
//data.ethernet = true;
int j = 0;
while (ethernet == false && j < 5) {
ethernet = ethernet_post(data);
j++;
}
if (!ethernet) {
//data.ethernet = false;
}
}
//Then SD card
if (mode == 2 || (mode == 3 && !ethernet) || mode == 4) {
//data.ethernet = false;
boolean sd = false;
int i = 0;
while (sd == false && i < 5) {
sd = sd_post(data);
i++;
}
}
}
boolean ethernet_post(lta data) {
char humidity[6];
char temperature[6];
dtostrf(data.humidity, 1, 2, humidity);
dtostrf(data.temperature, 1, 2, temperature);
if (client.connect(server, 80)) {
Serial.println("Connected to remote computer.");
String request = "";
request += "GET ";
request += "/other/drupal7/arduino/post?";
request += auth_name;
request += "=";
request += auth_key;
request += "&field_temp=";
request += temperature;
request += "&field_humidity=";
request += humidity;
request += "&field_time=";
request += data.time;
request += " HTTP/1.0";
//Send request to remote computer
client.println(request);
client.println ("Host: infocology.co.uk");
client.println();
}
else {
error error = {
"Failed to make connection to remote computer.",
2
};
error_condition(error);
return false;
}
client.stop();
return true;
}
boolean init_sd() {
// make sure that the default chip select pin is set to output, even if you don't use it:
pinMode(SDPIN, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(SDPIN)) {
return false;
}
return true;
}
lta lta_get_data(){
uint32_t time = RTC.get();
float h = dht.readHumidity();
float t = dht.readTemperature();
boolean ethernet = false;
boolean fail = false;
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h) || (t == 0 && h == 0)) {
String error_msg = "Failed to read from DHT";
error error = {
error_msg,
2
};
error_condition(error);
fail = true;
}
lta data = {
time,
h,
t,
ethernet,
fail
};
return data;
}
void error_condition(error error) {
Serial.println("Error condition encountered:");
Serial.println(error.condition);
Serial.print("Error level ");
Serial.println(error.level);
//Log error to SD card
String request = "";
request += error.condition;
request += ",";
request += error.level;
File errorFile = SD.open("error.log", FILE_WRITE);
if (errorFile) {
errorFile.println(request);
errorFile.close();
}
//Respond to error condition as appropriate
switch (error.level) {
case 1:
error_blink(5);
softReset();
break;
case 2:
error_blink(4);
break;
case 3:
error_blink(3);
break;
case 4:
error_blink(2);
break;
}
}
void error_blink(int repeat) {
pinMode(9, OUTPUT);
for (int i =0 ; i < repeat; i++) {
digitalWrite(9, HIGH);
delay(50);
digitalWrite(9, LOW);
delay(50);
}
}
void serial_boilerplate() {
Serial.println("Ecological Data Logger v0.1");
Serial.println("===========================");
Serial.println();
Serial.println("Developed by Ed Baker: http://ebaker.me.uk");
}