-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleScript.ino
301 lines (241 loc) · 7.95 KB
/
ParticleScript.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#define UNITNAME 000
// ---- External Libraries ----
#include <Adafruit_DHT.h>
#include <HttpClient.h>
#include <SdFat.h>
#include <SparkFunMAX17043.h>
// ---- HTTP ----
HttpClient http;
#define SENSOR1_TEMP "xxxxx"
#define SENSOR1_HUM "xxxxx"
#define SENSOR2_TEMP "xxxxx"
#define SENSOR2_HUM "xxxxx"
#define SENSOR3_TEMP "xxxxx"
#define SENSOR3_HUM "xxxxx"
#define SENSOR4_TEMP "xxxxx"
#define SENSOR4_HUM "xxxxx"
#define BATTERY_PCT "xxxxx"
#define BATTERY_VOLT "xxxxx"
#define TOKEN "xxxxx"
http_header_t headers[] = {
{ "Content-Type", "application/json" },
{ "X-Auth-Token" , TOKEN },
{ NULL, NULL } // NOTE: Always terminate headers with NULL
};
http_request_t request;
http_response_t response;
String postPackage;
// ---- SENSORS ----
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
#define DHTPIN1 5 // what pin we're connected to
#define DHTPIN2 4
#define DHTPIN3 6
#define DHTPIN4 3
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
DHT dht4(DHTPIN4, DHTTYPE);
float h1 = 0;
double f1 = 0;
float h2 = 0;
double f2 = 0;
float h3 = 0;
double f3 = 0;
float h4 = 0;
double f4 = 0;
float volt = 0;
float pct = 0;
// ---- TIME ----
const long oneSecond = 1000; // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long oneHour = oneMinute * 60;
const long oneDay = oneHour * 24;
// ---- SDCARD ----
// Primary SPI with DMA
// SCK => A3, MISO => A4, MOSI => A5, SS => A2 (default)
SdFat sd;
const uint8_t chipSelect = SS;
File dataFile;
File myFile;
// ---- DEBUG ----
bool debug = false;
// ---- FIRMWARE ----
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
if (debug){
Serial.println("SETUP");
}
request.hostname = "things.ubidots.com";
request.port = 80;
Serial.begin(9600);
dht1.begin();
dht2.begin();
dht3.begin();
dht4.begin();
lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge
lipo.quickStart();
}
void loop() {
//
// PART 1: DATA LOGGING
//
delay(oneSecond * 5);
if (debug){
Serial.println("SETUP");
}
h1 = dht1.getHumidity();
f1 = dht1.getTempFarenheit();
h2 = dht2.getHumidity();
f2 = dht2.getTempFarenheit();
h3 = dht3.getHumidity();
f3 = dht3.getTempFarenheit();
h4 = dht4.getHumidity();
f4 = dht4.getTempFarenheit();
pct = lipo.getSOC();
volt = lipo.getVoltage();
if (debug){
if (isnan(h1) || isnan(f1)) {
Serial.println("Failed to read from DHT SENSOR 1!");
} else {
Serial.println("SENSOR_1");
Serial.println(f1);
Serial.println(h1);
}
if (isnan(h2) || isnan(f2)) {
Serial.println("Failed to read from DHT SENSOR 2!");
} else {
Serial.println("SENSOR_2");
Serial.println(f2);
Serial.println(h2);
}
if (isnan(h3) || isnan(f3)) {
Serial.println("Failed to read from DHT SENSOR 3!");
} else {
Serial.println("SENSOR_3");
Serial.println(f3);
Serial.println(h3);
}
if (isnan(h4) || isnan(f4)) {
Serial.println("Failed to read from DHT SENSOR 4!");
} else {
Serial.println("SENSOR_4");
Serial.println(f4);
Serial.println(h4);
}
if (isnan(volt) || isnan(pct)) {
Serial.println("Failed to read from BATTERY FUEL GAUGE!");
} else {
Serial.println("BATTERY VOLT");
Serial.println(volt);
Serial.println("BATTERY PCT");
Serial.println(pct);
}
}
String fileTime = Time.format("%d-%m-%y");
String readTime = String(Time.now()) + "000";
sd.begin(chipSelect, SPI_HALF_SPEED);
dataFile.open(fileTime+".txt", O_RDWR | O_CREAT | O_AT_END);
// UNIT_NUMBER, READ_TIME, SENSOR_1_TEMP, SENSOR_1_HUM, SENSOR_2_TEMP, SENSOR_2_HUM, SENSOR_3_TEMP, SENSOR_3_HUM, BATTERY_PCT, BATTERY_VOLT
// {"variable": "568405d376254261ef114f35", "value":41.2}, {"variable": "56843ac776254258956b9c05", "value":88.3}
postPackage =
"[{\"variable\": \"" + String(SENSOR1_TEMP) + "\", \"timestamp\": " + readTime + ", \"value\": " + String(f1) + "}"
+ "," +
"{\"variable\": \"" + SENSOR2_TEMP + "\", \"timestamp\": " + readTime + ", \"value\": " + String(f2) + "}"
+ "," +
"{\"variable\": \"" + SENSOR3_TEMP + "\", \"timestamp\": " + readTime + ", \"value\": " + String(f3) + "}"
+ "," +
"{\"variable\": \"" + SENSOR4_TEMP + "\", \"timestamp\": " + readTime + ", \"value\": " + String(f4) + "}"
+ "," +
"{\"variable\": \"" + SENSOR1_HUM + "\", \"timestamp\": " + readTime + ", \"value\": " + String(h1) + "}"
+ "," +
"{\"variable\": \"" + SENSOR2_HUM + "\", \"timestamp\": " + readTime + ", \"value\": " + String(h2) + "}"
+ "," +
"{\"variable\": \"" + SENSOR3_HUM + "\", \"timestamp\": " + readTime + ", \"value\": " + String(h3) + "}"
+ "," +
"{\"variable\": \"" + SENSOR4_HUM + "\", \"timestamp\": " + readTime + ", \"value\": " + String(h4) + "}"
+ "," +
"{\"variable\": \"" + BATTERY_PCT + "\", \"timestamp\": " + readTime + ", \"value\": " + String(pct) + "}"
+ "," +
"{\"variable\": \"" + BATTERY_VOLT + "\", \"timestamp\": " + readTime + ", \"value\": " + String(volt) + "}]";
dataFile.println(postPackage);
dataFile.close();
if (debug){
Serial.println("POST_PACKAGE");
Serial.println(postPackage);
}
//
// PART 2: SENDING DATA
//
if (debug){
WiFi.on();
if (Particle.connected() == false) {
Particle.connect();
}
delay(oneSecond * 15);
Serial.println("WIFISTATUS:");
Serial.println(WiFi.ready());
if (debug){
Serial.println("FILE");
Serial.println(fileTime+".txt");
}
if (!dataFile.open(fileTime+".txt", O_READ)) {
if (debug){
sd.errorHalt("opening "+fileTime+".txt for read failed");
}
}
while (dataFile.available()) {
String line = dataFile.readStringUntil('\n');
Serial.println("LINE");
Serial.println(line);
request.path = "/api/v1.6/collections/values";
request.body = line;
http.post(request, response, headers);
Serial.println("RESPONSE");
Serial.println( response.status );
delay(oneSecond);
}
dataFile.close();
} else {
if (Time.hour() == 23){
WiFi.on();
if (Particle.connected() == false) {
Particle.connect();
}
delay(oneSecond * 15);
if (!dataFile.open(fileTime+".txt", O_READ)) {
if (debug){
sd.errorHalt("opening "+fileTime+".txt for read failed");
}
}
while (dataFile.available()) {
String line = dataFile.readStringUntil('\n');
request.path = "/api/v1.6/collections/values";
request.body = line;
http.post(request, response, headers);
delay(oneSecond);
}
dataFile.close();
WiFi.off();
}
}
if (!debug){
delay(oneMinute);
Particle.sleep(SLEEP_MODE_DEEP, 3600);
} else {
Serial.println("WIFISTATUS:");
Serial.println(WiFi.ready());
Serial.println("END_LOOP");
delay(oneSecond * 30);
}
// Compute heat index
// Must send in temp in Fahrenheit!
/*
hi = dht.getHeatIndex();
dp = dht.getDewPoint();
k = dht.getTempKelvin();
*/
}