-
Notifications
You must be signed in to change notification settings - Fork 2
/
standaloneweatherstation.ino
383 lines (316 loc) · 10.3 KB
/
standaloneweatherstation.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
373
374
375
376
377
378
379
380
#include <Wire.h>
#include <stdlib.h>
#include <SPI.h>
#include <Ethernet.h>
#include <DHT.h>
#include <SFE_BMP180.h>
#include <HMC5883L.h>
#define ALTITUDE 9.0 //Your Altitude
#define PHANT_UPDATE_TIME 300000 //Update SparkFun data server every 60000 ms (1 minute).
#define TIMEOUT 1000 //1 second timout
HMC5883L compass;
DHT dht; //Object creation
SFE_BMP180 pressure; //object creation
int time_span=0;
int previous_time=0;
int new_time=0;
float velocity;
const int anemo_input = 8; // Anemometer digital Input
const float pi = 3.14159265; // Pi value for wind speed calc
String convsting;
char PhantDataServer[] = "data.sparkfun.com";
#define PUBLIC_KEY "xxxxxxxxxxxxxxxxx" //Your phant public_key
#define PRIVATE_KEY "xxxxxxxxxxxxxxxxx" //Your phant private_key
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xAE, 0xCD };
IPAddress ip(192, 168, 1, 60); //Your local IP if DHCP fails.
IPAddress dnsServerIP(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet client.
EthernetClient client;
unsigned long timer1 = 0;
unsigned long timer2 = 0;
int failedResponse = 0;
float humidity1;
float temperature1;
float windspeed;
String shumidity;
String stemperature;
String swindspeed;
String parameter;
String finaldirection;
char buff1[7]; //buffer for conversion
char buff2[7]; //buffer for conversion
char buff3[7];// buffer for conversion
char buff4[7];// buffer for conversion
char buff6[7];//buffer for conversion
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
void setup()
{
pinMode(anemo_input, INPUT);
//Initiallize the serial port.
Serial.begin(9600);
Serial.println("-= phant data client =-\n");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// DHCP failed, so use a fixed IP address:
Ethernet.begin(mac, ip, dnsServerIP, gateway, subnet);
}
Serial.print("LocalIP:\t\t");
Serial.println(Ethernet.localIP());
Serial.print("SubnetMask:\t\t");
Serial.println(Ethernet.subnetMask());
Serial.print("GatewayIP:\t\t");
Serial.println(Ethernet.gatewayIP());
Serial.print("dnsServerIP:\t\t");
Serial.println(Ethernet.dnsServerIP());
dht.setup(3);
pressure.begin();
compass.begin();
// Initialize Initialize HMC5883L
/* Serial.println("Initialize HMC5883L");
while (!compass.begin())
{
compass.begin();
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);
// Set number of samples averaged
compass.setSamples(HMC5883L_SAMPLES_8);
// Set calibration offset. See HMC5883L_calibration.ino
compass.setOffset(0, 0);
*/
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
void loop()
{
float pressure=getpressure();
Serial.println("mmmmmm****PRsssure88888");
Serial.println(pressure);
Serial.println("mg");
delay(dht.getMinimumSamplingPeriod());
humidity1 = dht.getHumidity();
temperature1 = dht.getTemperature();
String finaldirection=wind_vane();
float windspeed=anemometer(anemo_input);
String shumidity=dtostrf(humidity1,3,2,buff1); //converting float to string
String stemperature=dtostrf(temperature1,3,2,buff2);
String swindspeed=dtostrf(windspeed,3,2,buff3); //converting float to string
String spressure=dtostrf(pressure,3,2,buff4);
Serial.println(shumidity); //Just for testing purpose
Serial.println(stemperature);
Serial.println(swindspeed);
Serial.println(spressure);
Serial.println(finaldirection);
Serial.println("---------------------");
//Serial.println("temp and humi got");
//Update sparkfun data server every 60 seconds.
if(millis() > timer1 + PHANT_UPDATE_TIME)
{
timer1 = millis(); //Update timer1 with the current time in miliseconds.
sendToPhantDataServer(shumidity, stemperature,swindspeed,spressure,finaldirection); //Send data to sparkfun data server.
Serial.println("main ends"); //added only for testing.
}
}
void sendToPhantDataServer(String humidity,String temperature,String windspeed,String pressure,String finaldir)
{
Serial.println("send to started");
//Establish a TCP connection to sparkfun server.
if (client.connect(PhantDataServer, 80))
{
//if the client is connected to the server...
if(client.connected())
{
Serial.println("Sending data to SparkFun...\n");
// send the HTTP PUT request:
client.print("GET /input/");
client.print(PUBLIC_KEY);
client.print("?private_key=");
client.print(PRIVATE_KEY);
client.print("&humidity=");
client.print(humidity); //send the number stored in 'humidity' string. Select only one.
client.print("&temperature=");
client.print(temperature);
client.print("&windspeed=");
client.print(windspeed);
client.print("&pressure=");
client.print(pressure);
client.print("&direction=");
client.println(finaldir);
delay(1000); //Give some time to Sparkfun server to send the response to ENC28J60 ethernet module.
timer2 = millis();
while((client.available() == 0)&&(millis() < timer2 + TIMEOUT)); //Wait here until server respond or timer2 expires.
// if there are incoming bytes available
// from the server, read them and print them:
while(client.available() > 0)
{
char inData = client.read();
Serial.print(inData); //only for debugging.
} //If error shows up use the indata to resolve it
Serial.println("\n");
client.stop(); //Disconnect the client from server.
}
}
Serial.println("000n000");
}
float anemometer(int an_in)
{
long int b=millis();
Serial.println("Counting starts");
int a=0;
int d=0;
int count =0;
float speedwind=0;
int i=0;
float totalspeed=0;
float velocity[100];
while(1)
{
d=a;
//Serial.println("value of d");
//Serial.println(d);
long int c=millis();
if((c-b)>=10000)
{
Serial.println("*******");
Serial.println("Total count");
Serial.println(count);
for (int j=2;j<=count;j++)
{totalspeed=totalspeed+velocity[j];
}
speedwind=(totalspeed/(count-1));
Serial.println(speedwind);
delay(10000);
break;
}
else
{
a = digitalRead(anemo_input);
//Serial.println("value of a");
//Serial.println(a);
if(a==1)
{
// Serial.println("first value of d");
// Serial.println(d);
if(d==0)
{ new_time=millis();
count=count+1;
Serial.println(count);
time_span=new_time-previous_time;
previous_time=new_time;
//Serial.println(time_span);
velocity[count]=(16049.96/time_span);
Serial.println(velocity[count]);
}
else if (d==1)
{
count=count;
}
}
}
}
return speedwind;
Serial.println("End");
}
float getpressure()
{
double T,P,p0,b;
char status;
status = pressure.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed pressure measurement:
// Note that the measurement is stored in the variable P.
// Note also that the function requires the previous temperature measurement (T).
// (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// Function returns 1 if successful, 0 if failure.
status = pressure.getPressure(P,T);
if (status != 0)
{
// Print out the measurement:
Serial.print("absolute pressure: ");
Serial.print(P,2);
Serial.print(" mb, ");
p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
Serial.print("relative (sea-level) pressure: ");
Serial.print(p0,2);
Serial.print(" mb, ");
}
else Serial.println("error retrieving pressure measurement\n");
}
else Serial.println("error starting pressure measurement\n");
}
return p0;
}
String wind_vane()
{
Vector norm = compass.readNormalize();
// Calculate heading
float heading = atan2(norm.YAxis, norm.XAxis);
// Set declination angle on your location and fix heading
// You can find your declination on: http://magnetic-declination.com/
// (+) Positive or (-) for negative
// For Bytom / Poland declination angle is 4'26E (positive)
// Formula: (deg + (min / 60.0)) / (180 / M_PI);
float declinationAngle = (4.0 + (26.0 / 60.0)) / (180 / M_PI);
heading += declinationAngle;
// Correct for heading < 0deg and heading > 360deg
if (heading < 0)
{
heading += 2 * PI;
}
if (heading > 2 * PI)
{
heading -= 2 * PI;
}
// Convert to degrees
float headingDegrees = heading * 180/M_PI;
// Output
//Serial.print(" Heading = ");
//Serial.print(heading);
//Serial.print(" Degress = ");
//Serial.print(headingDegrees);
//Serial.println();
//delay(100);
if((headingDegrees>0)&&(headingDegrees<90))
{
parameter="NE";
}
if((headingDegrees>90)&&(headingDegrees<180))
{
parameter="ES";
}
if((headingDegrees>180)&&(headingDegrees<270))
{
parameter="SW";
}
if((headingDegrees>270)&&(headingDegrees<360))
{
parameter="WN";
}
String convstring=dtostrf(headingDegrees,3,2,buff6);
finaldirection=convstring + parameter;
//Serial.println(finaldirection);
return finaldirection;
}