-
Notifications
You must be signed in to change notification settings - Fork 7
/
weathermon.ino
173 lines (136 loc) · 3.86 KB
/
weathermon.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
// This #include statement was automatically added by the Spark IDE.
#include "SFE_TSL2561.h"
// This #include statement was automatically added by the Spark IDE.
#include "dht.h"
// This #include statement was automatically added by the Spark IDE.
#include "bmp085.h"
#define DHTPIN D4
#define SOUNDPIN A0
#define DHTTYPE DHT22
// Initialize sensor classes
Adafruit_BMP085 bmp;
DHT dht(DHTPIN, DHTTYPE);
SFE_TSL2561 tsl = SFE_TSL2561();
// Initialize variables
char eventinfo[64];
unsigned int ms;
int publishdelay = 60 * 1000;
// Initialize BMP085
void InitializeBMP085(){
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
}
// Initialize DHT
void InitializeDHT(){
dht.begin();
}
// Initialize TSL2561
void InitializeTSL2561(){
unsigned char id;
tsl.begin();
tsl.getID(id);
tsl.setTiming(0, 2, ms);
tsl.setPowerUp();
}
// Initialize Sound Sensor
void InitializeSoundSensor(){
pinMode(SOUNDPIN, INPUT);
}
// Publish Pressure, Altitude
void PublishBMP085Info(){
Serial.print("Temperature = ");
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
// Calculate altitude assuming 'standard' barometric
// pressure of 1013.25 millibar = 101325 Pascal
Serial.print("Altitude = ");
Serial.print(bmp.readAltitude());
Serial.println(" meters");
// you can get a more precise measurement of altitude
// if you know the current sea level pressure which will
// vary with weather and such. If it is 1015 millibars
// that is equal to 101500 Pascals.
Serial.print("Real altitude = ");
Serial.print(bmp.readAltitude(101500));
Serial.println(" meters");
sprintf(eventinfo, "Pressure=%.2f|hPa,Altitude=%2.2f|meters", bmp.readPressure()/100.0, bmp.readAltitude());
Serial.println();
Publish(eventinfo);
}
// Publish Temperature, Humidity, Dew Point
void PublishDHTInfo(){
float h = dht.readHumidity();
float t = dht.readTemperature();
float d = dht.dewPoint(t, h);
sprintf(eventinfo, "Temperature=%.2f|°C,Humidity=%.2f|%%,Dew Point=%.2f|°C", t, h, d);
Publish(eventinfo);
}
// Publish luminosity
void PublishTSL2561Info(){
unsigned int data1;
unsigned int data2;
boolean gain;
tsl.getData(data1, data2);
double lux;
boolean good = tsl.getLux(gain,ms,data1,data2,lux);
sprintf(eventinfo, "Lux=%.2f|Lux", lux);
Publish(eventinfo);
}
// Publish sound info
void PublishSoundInfo(){
// map analog reading (0-4095) to (0-100)
int level = map(analogRead(SOUNDPIN), 0, 4095, 0, 100);
// Just some arbitrary values
if(level > 60){
sprintf(eventinfo, "Sound Level=%d (High)|", level);
}
else if(level > 30){
sprintf(eventinfo, "Sound Level=%d (Medium)|", level);
}
else{
sprintf(eventinfo, "Sound Level=%d (Low)|", level);
}
Publish(eventinfo);
}
// Publush event
void Publish(char* szEventInfo){
Spark.publish("weatherinfo", szEventInfo);
}
// Initialize applicaiton
void InitializeApplication(){
Serial.begin(9600);
pinMode(D7, OUTPUT);
}
// Blink LED and wait for some time
void BlinkLED(){
digitalWrite(D7, HIGH);
delay(500);
digitalWrite(D7, LOW);
delay(500);
delay(publishdelay);
}
void setup() {
InitializeApplication();
Serial.println("Initializing sensors...");
InitializeBMP085();
InitializeDHT();
InitializeTSL2561();
InitializeSoundSensor();
}
void loop() {
// Publish events. Wait for 2 second between publishes
PublishBMP085Info();
delay(2000);
PublishDHTInfo();
delay(2000);
PublishTSL2561Info();
delay(2000);
PublishSoundInfo();
delay(2000);
BlinkLED();
}