2
2
Pachube sensor client with Strings for my office
3
3
4
4
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
5
- using an Arduino Ethernet .
5
+ using an Arduino WiFi shield .
6
6
7
7
This example has been updated to use version 2.0 of the Pachube.com API.
8
8
11
11
* BMP085 Temp and pressure sensor attached to I2C
12
12
* DHT22 Temp and humidity sensor attached to pin 2
13
13
* TSL2561 luminance sensor attached to I2C
14
- * Ethernet attached to SPI
14
+ * WiFi shield attached to SPI
15
15
16
16
created 15 March 2010
17
- updated 1 Nov 2012
18
- by Tom Igoe with input from Usman Haque and Joe Saavedra
17
+ updated 5 Feb 2013
18
+ by Tom Igoe with input from Usman Haque and Joe Saavedra. TSL2561 code and
19
+ DHT code borrowed from Adafruit's libraries for those sensors.
19
20
20
21
*/
21
22
22
23
// include all Libraries needed:
23
- #include < SPI.h>
24
- #include < WiFi.h>
25
- #include < Wire.h>
26
- #include " TSL2561.h"
27
- #include < BMP085.h>
28
- #include " DHT.h"
24
+ #include < SPI.h> // For the WiFi shield
25
+ #include < WiFi.h> // for the WiFi shield
26
+ #include < Wire.h> // for the TSL2561 and BMP085 sensors
27
+ #include " TSL2561.h" // for the TSL2561 sensor
28
+ #include < BMP085.h> // for the BMP085 sensor
29
+ #include " DHT.h" // for the DHT22 sensor
30
+ #include " passwords.h" // contains my passwords, see below
29
31
30
- #define DHTPIN 2 // DHT22's output pin
31
- #define DHTTYPE DHT22 // DHT 22 (AM2302) as opposed to DHT11
32
+ /*
33
+ NOTE: passwords.h is not included with this repo because it contains my passwords.
34
+ You need to create it for your own version of this application. To do so, make
35
+ a new tab in Arduino, call it passwords.h, and include the following variables and constants:
36
+
37
+ #define APIKEY "foo" // replace your pachube api key here
38
+ #define FEEDID 0000 // replace your feed ID
39
+ #define USERAGENT "my-project" // user agent is the project name
40
+
41
+ char ssid[] = "networkname"; // your network SSID (name)
42
+ char pass[] = "password"; // your network password
32
43
33
- #define APIKEY " YOUR_KEY" // replace your pachube api key here
34
- #define FEEDID 00000 // replace your feed ID
35
- #define USERAGENT " projectName" // user agent is the project name
44
+ */
36
45
37
46
38
- char ssid[] = " networkName " ; // your network SSID (name)
39
- char pass[] = " password " ; // your network password
47
+ # define DHTPIN 2 // DHT22's output pin
48
+ # define DHTTYPE DHT22 // DHT 22 (AM2302) as opposed to DHT11
40
49
41
50
int status = WL_IDLE_STATUS;
42
51
@@ -49,11 +58,9 @@ DHT dht(DHTPIN, DHTTYPE);
49
58
50
59
// set up net client info:
51
60
char server[] = " api.pachube.com" ;
52
- unsigned long lastConnectionTime = 0 ; // last time you connected to the server, in milliseconds
53
61
boolean lastConnected = false ; // state of the connection last time through the main loop
54
62
const unsigned long postingInterval = 60000 ; // delay between updates to Pachube.com
55
63
const unsigned long readingInterval = postingInterval / 10 ; // delay between sensor reads;
56
- unsigned long lastReadTime = 0 ;
57
64
String dataString = " " ;
58
65
59
66
// set up sensor averaging variables:
@@ -89,7 +96,7 @@ void setup() {
89
96
// wait 10 seconds for connection:
90
97
delay (10000 );
91
98
}
92
-
99
+
93
100
// you're connected now, so print out the status:
94
101
printWifiStatus ();
95
102
@@ -108,7 +115,7 @@ void setup() {
108
115
bmp.begin ();
109
116
110
117
// set blink pin:
111
- pinMode (A0 , OUTPUT);
118
+ pinMode (9 , OUTPUT);
112
119
dht.begin ();
113
120
114
121
// reserve space for dataString:
@@ -120,7 +127,7 @@ void loop() {
120
127
long now = millis ();
121
128
122
129
// if reading interval has passed, read:
123
- if (millis () - lastReadTime > readingInterval ) {
130
+ if (now % readingInterval < 5 ) {
124
131
// increment sample count:
125
132
samples++;
126
133
@@ -151,26 +158,24 @@ void loop() {
151
158
152
159
// convert the readings to a String to send it:
153
160
dataString = " temp," ;
161
+ // dataString += avgTemp;
154
162
dataString += floatToString (avgTemp, 2 );
155
163
// add lux:
156
164
dataString += " \n Light," ;
165
+ // dataString += avgLux;
157
166
dataString += floatToString (avgLux, 2 );
158
167
// add pressure:
159
168
dataString += " \n Pressure," ;
169
+ // dataString += avgPressure;
160
170
dataString += floatToString (avgPressure, 2 );
161
171
162
172
dataString += " \n Humidity," ;
173
+ // dataString += avgHumidity;
163
174
dataString += floatToString (avgHumidity, 2 );
164
- // if serial monitor's open, print it:
165
- if (Serial) {
166
- Serial.println (dataString);
167
- }
175
+
168
176
// toggle the LED to give a physical indicator of activity:
169
177
blinkState = !blinkState;
170
- digitalWrite (A0, blinkState);
171
-
172
- // update the last reading time:
173
- lastReadTime = now;
178
+ digitalWrite (9 , blinkState);
174
179
}
175
180
// if there's incoming data from the net connection,
176
181
// send it out the serial port. This is for debugging
@@ -182,32 +187,22 @@ void loop() {
182
187
}
183
188
}
184
189
185
- // if there's no net connection, but there was one last time
186
- // through the loop, then stop the client:
187
- if (!client.connected () && lastConnected) {
188
- Serial.println ();
189
- Serial.println (" disconnecting." );
190
- client.stop ();
191
- }
190
+
192
191
193
192
// if you're not connected, and the sending interval has passed since
194
193
// your last connection, then connect again and send data:
195
- if (!client. connected () && ( now - lastConnectionTime > postingInterval) ) {
196
- sendData (dataString );
194
+ if (now % postingInterval < 5 ) {
195
+ sendData ();
197
196
// reset all the variables for next gathering of data:
198
- lastConnectionTime = now;
199
- avgTemp= 0.0 ;
200
- avgLux = 0.0 ;
201
- avgPressure = 0.0 ;
202
197
samples = 0 ;
203
198
}
204
- // store the state of the connection for next time through
205
- // the loop:
206
- lastConnected = client.connected ();
207
199
}
208
200
209
201
// this method makes a HTTP connection to the server:
210
- void sendData (String thisData) {
202
+ void sendData () {
203
+ // stop the client if it's connected:
204
+ if (client.connected ()) client.stop ();
205
+
211
206
// if there's a successful connection:
212
207
if (client.connect (server, 80 )) {
213
208
Serial.println (" connecting..." );
@@ -221,32 +216,29 @@ void sendData(String thisData) {
221
216
client.print (" User-Agent: " );
222
217
client.println (USERAGENT);
223
218
client.print (" Content-Length: " );
224
- client.println (thisData .length (), DEC );
219
+ client.println (dataString .length ());
225
220
226
221
// last pieces of the HTTP PUT request:
227
222
client.print (" Content-Type: text/csv\n " );
228
223
client.println (" Connection: close\n " );
224
+ delay (1 );
229
225
230
226
// here's the actual content of the PUT request:
231
- client.println (thisData);
232
- Serial.println (thisData.length ());
233
- Serial.println (thisData);
234
- // note the time that the connection was made:
235
- lastConnectionTime = millis ();
227
+ client.println (dataString);
228
+ Serial.println (dataString.length ());
229
+ Serial.println (dataString);
236
230
}
237
231
else {
238
232
// if you couldn't make a connection:
239
233
Serial.println (" connection failed" );
240
234
Serial.println ();
241
235
Serial.println (" disconnecting." );
242
236
client.stop ();
243
- lastConnected = client.connected ();
244
237
}
245
238
}
246
239
247
240
248
- String floatToString (double number, uint8_t digits)
249
- {
241
+ String floatToString (double number, uint8_t digits) {
250
242
String resultString = " " ;
251
243
// Handle negative numbers
252
244
if (number < 0.0 )
@@ -282,8 +274,6 @@ String floatToString(double number, uint8_t digits)
282
274
return resultString;
283
275
}
284
276
285
-
286
-
287
277
void printWifiStatus () {
288
278
// print the SSID of the network you're attached to:
289
279
Serial.print (" SSID: " );
@@ -299,4 +289,5 @@ void printWifiStatus() {
299
289
Serial.print (" signal strength (RSSI):" );
300
290
Serial.print (rssi);
301
291
Serial.println (" dBm" );
302
- }
292
+ }
293
+
0 commit comments