-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalParticulateTempHumidityLCD.ino
219 lines (184 loc) · 6.01 KB
/
FinalParticulateTempHumidityLCD.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
// BEGIN CONFIGURATION
#define USERAGENT "Outdoor Air Particle Sensor" // paste your project name
#define FEEDID FEED-ID // paste your Xively Feed ID
#define APIKEY "API-KEY" // paste your Xivley API Key, including quotes
#define MACADDRESS 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // type your Arduino Ethernet MAC address
// END CONFIGURATION
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
#include <Wire.h>
#include "DHT.h"
#include "rgb_lcd.h"
// sets digital pin for air particle sensor
int particleSensorPin = 8;
// sets analog pin for temperature and humidity sensor, then sets type of sensor
#define DHTPIN A0
#define DHTTYPE DHT11
// instantiates temperature and humidity sensor and lcd screen
DHT dht(DHTPIN, DHTTYPE);
rgb_lcd lcd;
// makes a custom degree character for LCD screen
byte degree[8] =
{
0b00011,
0b00011,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
// sets color of LCD
const int colorR = 255;
const int colorG = 0;
const int colorB = 0;
// defines strings for sensors
char particleSensor[] = "concentration";
char temperatureSensor[] = "temperature";
char humiditySensor[] = "humidity";
// package individual sensors into three datastreams
XivelyDatastream datastreams[] =
{
XivelyDatastream(particleSensor, strlen(particleSensor), DATASTREAM_FLOAT),
XivelyDatastream(temperatureSensor, strlen(temperatureSensor), DATASTREAM_FLOAT),
XivelyDatastream(humiditySensor, strlen(humiditySensor), DATASTREAM_FLOAT)
};
// MAC address
byte mac[] =
{
MACADDRESS
};
// Xively API Key
char xivelyKey[] = APIKEY;
// Xively Feed ID and number of datastreams
XivelyFeed feed(FEEDID, datastreams, 3);
// initialize ethernet and Xively clients
EthernetClient client;
XivelyClient xivelyclient(client);
// numeric IP for api.xively.com for particle average
IPAddress server(216,52,233,122);
// starting values for particle count calculation
unsigned long duration;
unsigned long lowPulseOccupancy = 0;
float ratio = 0;
float concentration = 0;
// starting value for average particle count
int averageParticles = 0;
// waiting periods and start times for multitasking
unsigned long wait30s = 30000;
unsigned long wait60s = 60000;
unsigned long wait10m = 600000;
unsigned long start30s;
unsigned long start60s;
unsigned long start10m;
void setup()
{
// begin temperature sensing
dht.begin();
// set up the LCD's number of columns and rows and color
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
// create the degree character defined in sketch setup
#if 1
lcd.createChar(0, degree);
#endif
// initialize port for Serial Monitor
Serial.begin(9600);
// initialize pin for particle sensor
pinMode(8, INPUT);
// begin Ethernet connection, pause for any problems and try again
while (Ethernet.begin(mac) != 1)
{
Serial.println(F("Error getting IP address via DHCP, trying again..."));
delay(15000);
}
// set start times for calculations and multitasking
start30s = millis();
start60s = millis();
start10m = millis();
}
void loop()
{
// create variables to carry temperature and humidity data from sensor
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
// estimate air particulate
duration = pulseIn(particleSensorPin, LOW);
lowPulseOccupancy = lowPulseOccupancy + duration;
// measure air particlate and post all sensor data to Xively every 30 seconds
if ((millis() - start30s) > wait30s)
{
ratio = lowPulseOccupancy / (wait30s * 10.0); // integer percentage 0 => 100
concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
// once particle sensing data has been gathered, send particle, temperature, and humidity data to Xively
datastreams[0].setFloat(concentration);
datastreams[1].setFloat(temperature);
datastreams[2].setFloat(humidity);
int ret = xivelyclient.put(feed, xivelyKey); // send put request to Xively
// Serial.print(F("xivelyclient.put returned ")); // uncomment this line and next line to see return code from Xively
// Serial.println(ret); // print return from Xively to Serial Monitor
client.stop();
client.flush();
// reset start time and lowPulseOccupancy
lowPulseOccupancy = 0;
start30s = millis();
}
// update LCD screen every 60 seconds
if ((millis() - start60s) > wait60s)
{
// print values to LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Particles: ");
lcd.print(averageParticles);
lcd.setCursor(0, 1);
lcd.print("T: ");
lcd.print(temperature);
lcd.write((unsigned char)0);
lcd.print("C ");
lcd.print("H: ");
lcd.print(humidity);
lcd.print(" %");
start60s = millis();
}
// pull average particle sensing data from Xively every 10 minutes
if ((millis() - start10m) > wait10m)
{
sendGetRequest();
start10m = millis();
}
}
// makes an HTTP connection to the Xively server and pulls average from last hour
void sendGetRequest() {
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("Connecting to Xively for average particle value...");
// send the HTTP GET request:
client.print("GET /v2/feeds/");
client.print(F("INSERT FEED ID"));
client.println("/datastreams/concentration.csv?interval=3600&function=average&limit=1&duration=1hour HTTP/1.1");
client.println("Host: api.xively.com");
client.print("X-ApiKey: ");
client.println(F("INSERT API KEY"));
client.println("Content-Type: text/csv");
client.println("Connection: close");
client.println();
if (client.connected())
{
client.find("Z,"); // skip the timestamp
averageParticles = client.parseInt(); // get parsed numeric value
Serial.println();
Serial.print(averageParticles);
Serial.println(" particles per 0.01 cubic feet");
Serial.println();
}
client.stop();
}
else {
Serial.println("Connection failed, disconnecting...");
client.stop();
}
}