Skip to content

Commit 03e883f

Browse files
author
Tom Igoe
committed
Added Office environment sensor example
1 parent 73900b4 commit 03e883f

File tree

2 files changed

+52
-59
lines changed

2 files changed

+52
-59
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
WifiOfficeSensorsPachube/passwords.h

WiFiWeatherStation/WiFiWeatherStation.ino renamed to WifiOfficeSensorsPachube/WifiOfficeSensorsPachube.ino

+50-59
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Pachube sensor client with Strings for my office
33
44
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
5-
using an Arduino Ethernet.
5+
using an Arduino WiFi shield.
66
77
This example has been updated to use version 2.0 of the Pachube.com API.
88
@@ -11,32 +11,41 @@
1111
* BMP085 Temp and pressure sensor attached to I2C
1212
* DHT22 Temp and humidity sensor attached to pin 2
1313
* TSL2561 luminance sensor attached to I2C
14-
* Ethernet attached to SPI
14+
* WiFi shield attached to SPI
1515
1616
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.
1920
2021
*/
2122

2223
// 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
2931

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
3243
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+
*/
3645

3746

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
4049

4150
int status = WL_IDLE_STATUS;
4251

@@ -49,11 +58,9 @@ DHT dht(DHTPIN, DHTTYPE);
4958

5059
// set up net client info:
5160
char server[] = "api.pachube.com";
52-
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
5361
boolean lastConnected = false; // state of the connection last time through the main loop
5462
const unsigned long postingInterval = 60000; //delay between updates to Pachube.com
5563
const unsigned long readingInterval = postingInterval / 10; // delay between sensor reads;
56-
unsigned long lastReadTime = 0;
5764
String dataString = "";
5865

5966
// set up sensor averaging variables:
@@ -89,7 +96,7 @@ void setup() {
8996
// wait 10 seconds for connection:
9097
delay(10000);
9198
}
92-
99+
93100
// you're connected now, so print out the status:
94101
printWifiStatus();
95102

@@ -108,7 +115,7 @@ void setup() {
108115
bmp.begin();
109116

110117
// set blink pin:
111-
pinMode(A0, OUTPUT);
118+
pinMode(9, OUTPUT);
112119
dht.begin();
113120

114121
// reserve space for dataString:
@@ -120,7 +127,7 @@ void loop() {
120127
long now = millis();
121128

122129
// if reading interval has passed, read:
123-
if (millis() - lastReadTime > readingInterval) {
130+
if (now % readingInterval < 5) {
124131
// increment sample count:
125132
samples++;
126133

@@ -151,26 +158,24 @@ void loop() {
151158

152159
// convert the readings to a String to send it:
153160
dataString = "temp,";
161+
//dataString += avgTemp;
154162
dataString += floatToString(avgTemp, 2);
155163
// add lux:
156164
dataString += "\nLight,";
165+
// dataString += avgLux;
157166
dataString += floatToString(avgLux, 2);
158167
// add pressure:
159168
dataString += "\nPressure,";
169+
//dataString += avgPressure;
160170
dataString += floatToString(avgPressure, 2);
161171

162172
dataString += "\nHumidity,";
173+
//dataString += avgHumidity;
163174
dataString += floatToString(avgHumidity, 2);
164-
// if serial monitor's open, print it:
165-
if (Serial) {
166-
Serial.println(dataString);
167-
}
175+
168176
// toggle the LED to give a physical indicator of activity:
169177
blinkState = !blinkState;
170-
digitalWrite(A0, blinkState);
171-
172-
// update the last reading time:
173-
lastReadTime = now;
178+
digitalWrite(9, blinkState);
174179
}
175180
// if there's incoming data from the net connection,
176181
// send it out the serial port. This is for debugging
@@ -182,32 +187,22 @@ void loop() {
182187
}
183188
}
184189

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+
192191

193192
// if you're not connected, and the sending interval has passed since
194193
// 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();
197196
// reset all the variables for next gathering of data:
198-
lastConnectionTime = now;
199-
avgTemp= 0.0;
200-
avgLux = 0.0;
201-
avgPressure = 0.0;
202197
samples = 0;
203198
}
204-
// store the state of the connection for next time through
205-
// the loop:
206-
lastConnected = client.connected();
207199
}
208200

209201
// 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+
211206
// if there's a successful connection:
212207
if (client.connect(server, 80)) {
213208
Serial.println("connecting...");
@@ -221,32 +216,29 @@ void sendData(String thisData) {
221216
client.print("User-Agent: ");
222217
client.println(USERAGENT);
223218
client.print("Content-Length: ");
224-
client.println(thisData.length(), DEC);
219+
client.println(dataString.length());
225220

226221
// last pieces of the HTTP PUT request:
227222
client.print("Content-Type: text/csv\n");
228223
client.println("Connection: close\n");
224+
delay(1);
229225

230226
// 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);
236230
}
237231
else {
238232
// if you couldn't make a connection:
239233
Serial.println("connection failed");
240234
Serial.println();
241235
Serial.println("disconnecting.");
242236
client.stop();
243-
lastConnected = client.connected();
244237
}
245238
}
246239

247240

248-
String floatToString(double number, uint8_t digits)
249-
{
241+
String floatToString(double number, uint8_t digits) {
250242
String resultString = "";
251243
// Handle negative numbers
252244
if (number < 0.0)
@@ -282,8 +274,6 @@ String floatToString(double number, uint8_t digits)
282274
return resultString;
283275
}
284276

285-
286-
287277
void printWifiStatus() {
288278
// print the SSID of the network you're attached to:
289279
Serial.print("SSID: ");
@@ -299,4 +289,5 @@ void printWifiStatus() {
299289
Serial.print("signal strength (RSSI):");
300290
Serial.print(rssi);
301291
Serial.println(" dBm");
302-
}
292+
}
293+

0 commit comments

Comments
 (0)