forked from nejohnson2/air_quality
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathair_quality.ino
157 lines (133 loc) · 4.2 KB
/
air_quality.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
// Include the GSM library
#include <GSM.h>
#include "credentials.h"
// APN data - this is unique to the carrier
//#define GPRS_APN "wap.cingular" // AT&T
#define GPRS_APN "epc.tmobile.com" // T-Mobile
#define GPRS_LOGIN "admin" // this is not required by the carrier but by the library
#define GPRS_PASSWORD "password" // ditto
#define PINNUMBER ""
// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess(false); // true for debugging
char server[] = "dweet.io";
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 30*1000; // milliseconds
// Setting up the PM Sensor
int pin = 8; // Yellow wire
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 30000; //sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
pinMode(8, INPUT);
starttime = millis(); // get the current time
Serial.println("Starting Arduino Shield");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
// as long as you're not sending anything, start counting
if(!client.connected()){
duration = pulseIn(pin, LOW);
lowpulseoccupancy = lowpulseoccupancy + duration;
}
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected)
{
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
{
Serial.println(lowpulseoccupancy);
sendData(lowpulseoccupancy);
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
void sendData(unsigned long thisData)
{
// if there's a successful connection:
if (client.connect(server, 80))
{
Serial.println("connecting...");
// Get character length of incoming data
String dataLength = String(thisData);
int dataLengthReal = dataLength.length();
// Calculate the dust particle concentration and ratio
ratio = thisData / (sampletime_ms*10.0); // Interger percentage 0=>100
concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; //using spec sheet curve
// For viewing in serial monitor
Serial.print(thisData);
Serial.print(",");
Serial.print(ratio);
Serial.print(",");
Serial.println(concentration);
// Get the length of the concentration value
int sendConcentration = int(concentration);
String sendReal = String(sendConcentration);
int sendRealReal = sendReal.length();
// Total length of data being sent
// 14 characters for the labels plus data lengths
int sendLength = 14 + sendRealReal + dataLengthReal + 8;
// send the HTTP PUT request:
client.print("GET /dweet/for/");
client.print(THINGNAME);
client.print("?lpo=");
client.print(lowpulseoccupancy);
client.print("&ratio=");
client.print(ratio);
client.print("&con=");
client.print(concentration);
client.println(" HTTP/1.1");
client.println("Host: dweet.io");
client.println("Connection: close");
client.println();
}
else
{
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// reset the count
lowpulseoccupancy = 0;
// note the time that the connection was made or attempted
lastConnectionTime = millis();
}