-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthernetWeatherStation.ino
344 lines (277 loc) · 7.56 KB
/
EthernetWeatherStation.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
/*
Ethernet Weather Station
by Daniel Hjort
Receives weather data via radio and publishes as a web server over Ethernet
interface if operating SENSOR mode or sends weather data to its node via radio if
operating in BASE mode.
Circuit:
* Adafruit BMP085 Pressure/temperature Sensor
Arduino - Pressure Sensor Board
5V - VIN (use 3.3V if old version of this board)
GND - GND
A5 - SCL
A4 - SDA
* DHT22 Temperature/humidity sensor
Arduino - DHT22
5V - Pin 1 (left)
DHTPIN - Pin 2
GND - Pin 4 (right)
Add a 10K resistor between sensor pin 2 and pin 1.
* Analog sensor attached to analog in 0 (light)
* Arduino Ethernet board
* alt. Ethernet (Wiznet) shield attached to pins 10, 11, 12, 13
* nRF24L01+ module
Arduino - nRF24L01+ - Arduino
GND - Pin 1 [*]* Pin 2 - 3.3V
D6 - Pin 3 * * Pin 4 - D7
D11 - Pin 5 * * Pin 6 - D13
D12 - Pin 7 * * Pin 8 - NC (interrupt)
*/
/*
* Mode
*/
#if 1
const bool isBase = true;
#else
const bool isBase = false;
#undef MAC_ADDRESS
#undef IP_ADDRESS
#define MAC_ADDRESS 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
#define IP_ADDRESS 10,0,1,20
#endif
/*
* Physical constants and calculations
*/
#include <math.h>
#include <stdint.h>
#define GRAVITY 9.81
#define GAS_CONSTANT 8.3144621
#define KELVIN 273.15
#define STATION_ELEVATION 36
/*
* Networking & Webserver
*/
#include <SPI.h>
#include <Ethernet.h>
#include <MsTimer2.h>
byte mac[] = {MAC_ADDRESS};
IPAddress ip(IP_ADDRESS);
EthernetServer server(80);
/*
* Outside unit saved values
*/
bool mGotUpdate = false;
float mOutsideTemp = 0;
float mOutsideHumidity = 0;
float mOutsidePressure = 0;
float mOutsideCpm = 0;
/*
* nRF24L01+ Radio
*/
#include "nRF24L01.h"
#include "RF24.h"
// Set up nRF24L01 radio on SPI bus (pins 11, 12 and 13) plus pins 6 & 7
RF24 radio(6,7);
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
/*
* Adafruit BMP085 sensor
*/
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;
/*
* DHT sensor
*/
#include "DHT.h"
#define DHTPIN 2 // DHT data pin, D2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
/*
* Counter
*/
#define COUNTER_IRQ 1 // Uno/Ethernet pin 3
#define CONVERSION_FACTOR 0.0057 // SBM-20
volatile float mCounter = 0;
int mNbrCounterTicks = 0;
float mCpm = 0;
void count();
/*
* Method declarations
*/
float getPressure_MSLP_hPa(uint32_t altitude, float temperature);
float getPressure_hPa();
/*
* Periodic send
*/
const int sendPeriod = 500;
int mNbrSendTicks = 0;
void sendDataPacket() {
Serial.println("sendDataPacket");
float message[4];
message[0] = dht.readTemperature();
message[1] = dht.readHumidity();
message[2] = getPressure_hPa();
message[4] = mCpm;
bool ok = radio.write(&message, sizeof(float)*4);
if (ok)
Serial.println("Sent!");
else
Serial.println("Failed!");
}
void tick() {
mNbrSendTicks++;
mNbrCounterTicks++;
if (!isBase
&& mNbrSendTicks > sendPeriod) {
sendDataPacket();
mNbrSendTicks = 0;
}
if (mNbrCounterTicks >= 300) {
mCpm = mCounter / 5.0;
mNbrCounterTicks = 0;
mCounter = 0;
}
}
/*
* Arduino methods
*/
void setup() {
// start serial port:
Serial.begin(9600);
Serial.print("\nBegin setup.\n");
// Setup DHT sensor
dht.begin();
// Setup BMP085
bmp.begin();
// Setup counter
attachInterrupt(COUNTER_IRQ, count, FALLING);
// Setup radio
radio.begin();
radio.setRetries(15,15); // 15 retries, 15ms between
radio.printDetails();
if (isBase) { // Will receive
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
} else { // Will send
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
}
if (isBase) { // Setup server
Ethernet.begin(mac, ip);
server.begin();
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
// Start timer
MsTimer2::set(1000, tick);
MsTimer2::start();
Serial.print("\nEnd setup.\n");
}
void loop() {
if (isBase) { // Need to check for connecting clients
EthernetClient client = server.available();
if (client) {
Serial.println("New client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.print("Inside temperature: ");
client.print(dht.readTemperature());
client.println("<br />");
client.print("Inside humidity: ");
client.print(dht.readHumidity());
client.println("<br />");
client.print("Inside pressure: ");
client.print(getPressure_hPa());
client.println("<br />");
client.print("Inside CPM: ");
client.print(mCpm);
client.println("<br />");
client.print("Inside microSv/h: ");
client.print(mCpm * CONVERSION_FACTOR);
client.println("<br />");
if (mGotUpdate) {
client.print("Outside temperature: ");
client.print(mOutsideTemp);
client.println("<br />");
client.print("Outside humidity: ");
client.print(mOutsideHumidity);
client.println("<br />");
client.print("Outside pressure: ");
client.print(mOutsidePressure);
client.println("<br />");
client.print("Outside CPM: ");
client.print(mOutsideCpm);
client.println("<br />");
client.print("Outside microSv/h: ");
client.print(mOutsideCpm * CONVERSION_FACTOR);
client.println("<br />");
}
client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("Client disconnected");
}
}
if (isBase) { // Need to check for packets
// If there is data ready
if (radio.available()) {
Serial.println("Radio received packet");
// Dump the payloads until we've gotten everything
float message[3];
mGotUpdate = radio.read(&message, sizeof(float)*3);
if (mGotUpdate) {
Serial.println("Whole packet received");
mOutsideTemp = message[0];
mOutsideHumidity = message[1];
mOutsidePressure = message[2];
mOutsideCpm = message[4];
}
Serial.println("Radio done");
}
} // else we send sensor data packets on a timer
}
/*
* Sensor methods
*/
float getPressure_MSLP_hPa(uint32_t altitude, float temperature)
{
// temperature should be the mean from sea level, can approximate?
int32_t pressurePa = bmp.readPressure();
int32_t MSLP_Pa = pressurePa * exp((GRAVITY*altitude)/(GAS_CONSTANT*(temperature+KELVIN)));
return MSLP_Pa / 100.0;
}
float getPressure_hPa()
{
int32_t pressurePa = bmp.readPressure();
return pressurePa / 100.0;
}
/*
* Interupts
*/
void count()
{
mCounter++;
}