-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_BaseStation.ino
More file actions
517 lines (434 loc) · 13 KB
/
Arduino_BaseStation.ino
File metadata and controls
517 lines (434 loc) · 13 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* Base Station Receiver
The circuit:
IO Allocations
Digital
0 (RX)
1 (TX)
2
3 (PWM) - 1-Wire (DS18x20 Pinout 1 - GND, 2 - DATA, 3 - VDD) - Fitted but not currently used.
4 -
5 (PWM) -
6 (PWM) -
7 -
8 - RFM12 nIRQ --> This means you need to uncomment line 19 and change line 86 to 8 in RF12.cpp
9 (PWM) - RFM12 nSEL
10(PWM) - CS, Ethernet
11(PWM) - SI, Ethernet, RFM12 SDI
12 - SO, Ethernet, RFM12 SDO
13 - SCK, Ethernet, RFM12 SCK
Analogue
1
2
3
4 - Data, DS1307
5 - Clock, DS1307
*/
#define DEBUG 0
//includes
#include <EtherCard.h> //Ethernet Shield Library
#include <JeeLib.h> //Various Librarys need for I2C
//Constants
#define MYNODE 15 // Node ID
#define FREQ RF12_868MHZ // Frequency
#define GROUP 212 // Network group
#define EMONTX 5 // Current Monitor TX node ID
#define WXTX 18 // Weather Sensor node ID
#define BARO 17 // Barometer node ID
#define AQUA 11 // Aquarium node ID
#define MAINSVOLTAGE 240 // Mains voltage
#define POWERUPDATE 200 // Power Update Period (ms)
#define PERSISTENTVARIABLEUPDATE 30000 // Persistent Variable Update PEriod (ms)
#define TWEETPERIOD 1200000 // Tweet Period
#define NTPPERIOD 300000 // NTP Period
#define CO2COST 0.521 // 0.521 kg/kWh from http://www.energysavingtrust.org.uk/Energy-Saving-Trust/Our-calculations
//RTC Setup
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
//Local Network Setup
static byte mymac[] = { 0x54,0x55,0x58,0x10,0x00,0x31 }; // ethernet mac address
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
#define CSPIN 10 // Chip Select Pin Ethernet card
#if STATIC
// ethernet interface ip address
static byte myip[] = { 10,0,1,4 };
// gateway ip address
static byte gwip[] = { 10,0,1,1 };
//dns ip
static byte dnsip[] = { 10,0,1,1 };
#endif
//Timeserver setup
#define NUM_TIMESERVERS 5
prog_char ntp0[] PROGMEM = "ntp2d.mcc.ac.uk";
prog_char ntp1[] PROGMEM = "ntp2c.mcc.ac.uk";
prog_char ntp2[] PROGMEM = "ntp.exnet.com";
prog_char ntp3[] PROGMEM = "ntp.cis.strath.ac.uk";
prog_char ntp4[] PROGMEM = "clock01.mnuk01.burstnet.eu";
// Now define another array in PROGMEM for the above strings
prog_char *ntpList[] PROGMEM = { ntp0, ntp1, ntp2, ntp3, ntp4 };
// Packet buffer, must be big enough to packet and payload
#define BUFFER_SIZE 550
byte Ethernet::buffer[BUFFER_SIZE];
uint8_t clientPort = 123;
static BufferFiller bfill;
// Converting time received from NTP servers to a value that can be displayed.
#define GETTIMEOFDAY_TO_NTP_OFFSET 2208988800UL //seconds between 1-Jan-1900 (NTP Start) and 1-Jan-1970 (unix starts)
#define EPOCH_YR 1970
#define SECS_DAY 86400UL //(24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
//General Variables
unsigned char currentBuf[7];
static uint8_t currentTimeserver = 0;
long lastTweet = 0;
uint32_t lastNTP = -30000;
uint32_t timeLong;
Stash stash;
uint32_t lastPwrMsg;
float maxTemperature;
float minTemperature;
long lastPowerUpdate = 0;
double todaysKwh = -0.00000000012; // Total kWh usage since midnight
long lastPersistentVariableUpdate;
typedef struct { int16_t temperature; int32_t pressure; int battery;} payloadbarotx;
payloadbarotx barotx;
typedef struct { double current; int battery; } PayloadTX;
PayloadTX emontx;
typedef struct { int light; int humidity; int temperature; int dewpoint; int cloudbase; int battery; } payloadwxtx;
payloadwxtx wxtx;
typedef struct { int temperature; bool light; bool moon;} payloadaquarium;
payloadaquarium aquarium;
// End of config
// Setup Routine
void setup(){
#if DEBUG
Serial.begin(9600);
Serial.println("\n[Base Stn]");
#endif
//Start RTC
Wire.begin();
RTC.begin();
//Ethernet setup
if (ether.begin(sizeof Ethernet::buffer, mymac, CSPIN) == 0)
{
#if DEBUG
Serial.println(F("Ethernet init failed"));
#endif
}
#if STATIC
ether.staticSetup(myip, gwip, dnsip);
#else
if (!ether.dhcpSetup())
{
Serial.println(F("DHCP failed"));
}
#endif
#if DEBUG
ether.printIp("IP:", ether.myip);
ether.printIp("GW:", ether.gwip);
ether.printIp("DNS:", ether.dnsip);
#endif
// Reciever setup
rf12_set_cs(9);
rf12_initialize(MYNODE, FREQ, GROUP);
//Used to set min/max temp first time.
wxtx.dewpoint = barotx.pressure = 9999;
maxTemperature = -99;
minTemperature = 99;
//Trigger get stored variable from prior to power cycle.
readFromRam ();
}
// Main Loop
void loop()
{
/* Main loop needs to:
1. Listen for rf readings.
2. Update COSM after every new sensor update.
3. Update weatherunderground every 5 minutes and winew sensor update.
4. Update Twitter every 20 minutes.
5. Update time from NTP x minutes.
6. Process returned ethernet data.
7. Transmit NTP time stamp after successful update.
*/
//1 & 2. Listen for incomming RF Data, Update COSM
if (rf12_recvDone())
{
if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
{
int node_id = (rf12_hdr & 0x1F);
if (node_id == EMONTX)
{
emontx = *(PayloadTX*) rf12_data; // get emontx data
byte sd = stash.create();
stash.print(F("Current,"));
stash.println(emontx.current);
stash.print(F("Power,"));
stash.println(emontx.current * MAINSVOLTAGE);
stash.print(F("Battery,"));
stash.println(emontx.battery);
stash.print(F("Interval,"));
stash.println(millis()-lastPwrMsg);
stash.print(F("kWh,"));
stash.println(todaysKwh);
stash.print(F("CO2,"));
stash.println(todaysKwh * CO2COST);
stash.save();
sendToCosm(sd, PSTR("92072"));
//Reset kWh at Midnight
DateTime now = RTC.now();
if ((now.hour() == 0) & (now.minute() == 0)) todaysKwh = 0;
lastPwrMsg = millis();
}
if (node_id == WXTX)
{
wxtx = *(payloadwxtx*) rf12_data;
float h = wxtx.humidity * 0.1;
float t = wxtx.temperature * 0.1;
byte sd = stash.create();
if (wxtx.light >= 0 & wxtx.light <= 100)
{
stash.print(F("AmbientLightLevel,"));
stash.println(wxtx.light);
}
if (t >= -50 & t <= 60)
{
//Get time
DateTime now = RTC.now();
//Calculate min/max temp
if ((now.hour() == 0) & (now.minute() == 0)) maxTemperature = minTemperature = t;
if (t > maxTemperature) maxTemperature = t;
if (t < minTemperature) minTemperature = t;
stash.print(F("Temperature,"));
stash.println(t);
stash.print(F("MinimumTemperature,"));
stash.println(minTemperature);
stash.print(F("MaximumTemperature,"));
stash.println(maxTemperature);
}
if (h >= 0 & h <= 100)
{
stash.print(F("Humidity,"));
stash.println(h);
stash.print(F("Dewpoint,"));
stash.println(wxtx.dewpoint);
stash.print(F("Cloudbase,"));
stash.println(wxtx.cloudbase);
}
if (wxtx.battery >= 0 & wxtx.battery <= 6000)
{
stash.print(F("Battery,"));
stash.println(wxtx.battery);
}
stash.save();
sendToCosm(sd, PSTR("20161"));
}
if (node_id == BARO)
{
barotx = *(payloadbarotx*) rf12_data;
byte sd = stash.create();
float p = barotx.pressure * 0.01;
stash.print(F("Pressure,"));
stash.println(p);
stash.print(F("BaroBattery,"));
stash.println(barotx.battery);
stash.save();
#if DEBUG
Serial.print(F("Pressure: "));
Serial.print(p);
Serial.print(F(", BaroBattery: "));
Serial.println(barotx.battery);
#endif
sendToCosm(sd, PSTR("20161"));
}
}
}
//4. Update twitter every 20 mins
if ((millis() - lastTweet > TWEETPERIOD))
{
#if DEBUG
Serial.print("Twt: ");
Serial.println(millis() - lastTweet);
#endif
DateTime now = RTC.now();
char time[5];
sprintf(time,"%02d:%02d", now.hour(), now.minute());
byte sd = stash.create();
stash.print(F("status= #Hove "));
stash.print(time);
stash.print(F(", Temp: "));
stash.print(wxtx.temperature/10);
stash.print(F("C, Humidity: "));
stash.print(int(wxtx.humidity/10));
stash.print(F("%25,"));
if (wxtx.dewpoint < 9998)
{
stash.print(F(" Dewpoint: "));
stash.print(wxtx.dewpoint);
stash.print(F("C, Cloudbase: "));
stash.print(int(wxtx.cloudbase));
stash.print(F("Ft,"));
}
if (barotx.pressure/100 < 9998)
{
stash.print(F(" Pres: "));
stash.print(int(barotx.pressure/100));
stash.print(F("hPa,"));
}
stash.print(F(" Today's Min/Max: "));
stash.print(minTemperature);
stash.print(F("/"));
stash.print(maxTemperature);
stash.print(F("C. #Weather"));
stash.save();
sendToTwitter(sd);
lastTweet = millis();
#if DEBUG
Serial.print("Twt: ");
Serial.println(lastTweet);
#endif
}
//5. Update NTP
if ((millis() - lastNTP > NTPPERIOD) | (lastNTP == 0))
{
if (!ether.dnsLookup( (char*)pgm_read_word(&(ntpList[currentTimeserver])) ))
{
//Serial.println( F("DNS failed" ));
}
else
{
ether.ntpRequest(ether.hisip, ++clientPort);
}
if( ++currentTimeserver >= NUM_TIMESERVERS )
currentTimeserver = 0;
lastNTP += 6000;
}
//Faster Update Cycle for kWh Power Calc
if (millis() - lastPowerUpdate > POWERUPDATE)
{
todaysKwh += (emontx.current * MAINSVOLTAGE) * 0.2 / 3600000;
lastPowerUpdate = millis();
}
//Store persistent variables
if (millis() - lastPersistentVariableUpdate > PERSISTENTVARIABLEUPDATE){
storeInRam ();
lastPersistentVariableUpdate = millis();
}
//6. Process Ethernet Data
int len = ether.packetReceive();
int pos=ether.packetLoop(len);
//7. Ethernet Packet
if (len)
{
if (pos)
{
bfill = ether.tcpOffset();
char* data = (char *) Ethernet::buffer + pos;
//Serve Webpage
if (strncmp("GET /", data, 5) == 0)
{
homePage(data, bfill);
}
}
//NTP Responce
timeLong = 0L;
if (ether.ntpProcessAnswer(&timeLong,clientPort))
{
if (timeLong)
{
timeLong -= GETTIMEOFDAY_TO_NTP_OFFSET;
gmtime(timeLong);
sendTime();
lastNTP = millis();
}
}
}
}
void storeInRam (){
DateTime now = RTC.now();
RTC.writeByteInRam (8, now.day());
RTC.writeByteInRam (9, now.month());
RTC.writeByteInRam (10, now.year());
RTC.writeByteInRam (11, now.hour());
RTC.writeByteInRam (12, now.minute());
RTC.writeByteInRam (13, now.second());
RTC.writeByteInRam (14, int (todaysKwh));
int todaysKwhDecimal = (todaysKwh - int(todaysKwh)) * 255;
RTC.writeByteInRam (15, todaysKwhDecimal);
RTC.writeByteInRam (16, int (maxTemperature));
int maxTemperatureDecimal = (maxTemperature - int(maxTemperature)) * 255;
RTC.writeByteInRam (17, maxTemperatureDecimal);
RTC.writeByteInRam (18, int (minTemperature));
int minTemperatureDecimal = (minTemperature - int(minTemperature)) * 255;
RTC.writeByteInRam (19, minTemperatureDecimal);
}
void readFromRam (){
DateTime now = RTC.now();
if (now.day() == RTC.readByteInRam (8))
{
if (now.hour() >= RTC.readByteInRam (11))
{
if (now.minute() >= RTC.readByteInRam (12))
{
todaysKwh = RTC.readByteInRam (14) + (float (RTC.readByteInRam (15)) / 255);
maxTemperature = RTC.readByteInRam (16) + (float (RTC.readByteInRam (17)) / 255);
minTemperature = RTC.readByteInRam (18) + (float (RTC.readByteInRam (19)) / 255);
}
}
}
}
char okHeader[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
;
int getIntArg(const char* data, const char* key, int value =-1) {
char temp[10];
if (ether.findKeyVal(data + 6, temp, sizeof temp, key) > 0)
value = atoi(temp);
return value;
}
void sendLight(bool s)
{
char data[] = {'l', s};
int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
rf12_sendStart(0, data, sizeof data);
rf12_sendWait(0);
}
void homePage(const char* data, BufferFiller buf)
{
if (data[5] == '?')
{
int a = getIntArg(data, "a");
if (a != -1) sendLight(a);
int t = getIntArg(data, "t");
if (t == 1) sendTime();
}
DateTime now = RTC.now();
buf.emit_p(PSTR("$F\r\n"
"<meta http-equiv='refresh' content='10; url=?'/>"
"<title>Base Station</title>"
"<h2>Base Station Interface</h2>"
"<form>"
"<h3>Aquarium</h3>"
"Light: <input type=radio name=a value='1' $S>On<input type=radio name=a value='0' $S>Off<br>"
"Temperature: $D C<br>"
"<input type=submit value=Set>"
"</form>"
"<form>"
"<h3>Time</h3>"
"Time: $D$D:$D$D:$D$D<br>"
"<input type=hidden name=t value=1>"
"<input type=submit value=Send>"
"</form>"), okHeader,
aquarium.light ? "CHECKED" : "", aquarium.light ? "" : "CHECKED",
aquarium.temperature, now.hour()/10, now.hour()%10, now.minute()/10, now.minute()%10, now.second()/10, now.second()%10);
ether.httpServerReply(buf.position()); // send web page data
}
void sendTime()
{
DateTime now = RTC.now();
char data[] = {'t',now.hour(), now.minute(), now.second(), now.year(), now.month(), now.day()};
int i = 0; while (!rf12_canSend() && i<10) {rf12_recvDone(); i++;}
rf12_sendStart(0, data, sizeof data);
rf12_sendWait(0);
}