-
Notifications
You must be signed in to change notification settings - Fork 0
/
air_quality_sensor_v6.ino
399 lines (322 loc) · 12.2 KB
/
air_quality_sensor_v6.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* v1 First Pass
* v2 Adding the aqi ppm sensor
* v3 button controls
* v4 adding lte support via tinyGMS
* v5 - That modem didnt work without external power
* v6 Calibrated the humidity sensor and used excel to regression fit
*/
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Hardware-specific library for ST7789
#include <Adafruit_ST7789.h>
#include <SPI.h>
// Use dedicated hardware SPI pins
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//Battery Monitor
#include "Adafruit_MAX1704X.h"
Adafruit_MAX17048 maxlipo;
//SCD40 air sensor
#include <Wire.h>
#include "SparkFun_SCD4x_Arduino_Library.h"
SCD4x mySensor;
#include <Arduino.h>
#include <SensirionI2CScd4x.h>
#include <Wire.h>
SensirionI2CScd4x scd4x;
//SGP MOX gas sensor
#include "Adafruit_SGP40.h"
#include "Adafruit_SHT31.h"
Adafruit_SGP40 sgp;
Adafruit_SHT31 sht31;
// Adafruit PM2.5 sensor
#include "Adafruit_PM25AQI.h"
Adafruit_PM25AQI aqi = Adafruit_PM25AQI();
/////////////////
//LTE
#define TINY_GSM_MODEM_SIM7600
#define SerialAT Serial1
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
#define GSM_PIN ""
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include "./config.h"
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);
//TinyGsmClientSecure client(modem);
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVER_PORT 80
#define AIO_SERVER_SSLPORT 443 //ssl
//////
bool displayOn = true;
struct battery_val {
float volt;
float percent;
};
struct air_val {
uint16_t co2;
float temperature;
float relative_humidity;
float calibrated_humidity;
};
struct voc_val {
uint16_t sraw;
int32_t voc_index;
};
float calibrate_humidity(float x) {
float a=0.7410450839;
float b=0.0004917991;
float c=0.0;
float d=20.9176974029;
return x*a+pow(x,2)*b+d;
//return x*a+x*x*b+x*x*x*c+d;
}
struct air_val get_air_values() {
uint16_t error;
char errorMessage[256];
struct air_val air_values;
error = scd4x.readMeasurement(air_values.co2, air_values.temperature, air_values.relative_humidity);
if (error) {
SerialMon.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 256);
SerialMon.println(errorMessage);
} else if (air_values.co2 == 0) {
SerialMon.println("Invalid sample detected, skipping.");
}
air_values.calibrated_humidity=calibrate_humidity(air_values.relative_humidity);
return air_values;
}
struct voc_val get_voc(float temperature, float relative_humidity) {
// Get VOC index
struct voc_val voc_values;
voc_values.sraw = sgp.measureRaw(temperature, relative_humidity);
voc_values.voc_index = sgp.measureVocIndex(temperature, relative_humidity);
return voc_values;
}
void updateTFT(struct air_val air_values,struct voc_val voc_values, struct battery_val battery_values,PM25_AQI_Data pm_values) {
float temp = air_values.temperature;
float humidity = air_values.relative_humidity;
float chumidity = air_values.calibrated_humidity;
uint16_t co_two = air_values.co2;
int32_t voc_index = voc_values.voc_index;
uint16_t voc_raw = voc_values.sraw;
float batt_volt = battery_values.volt;
float batt_perc = battery_values.percent;
int text_size = 2;
tft.setTextSize(text_size);
tft.setCursor(0, 0);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.print(F("Temp: ")); tft.print(temp); tft.print("C ");
tft.print(((temp*9)/5) + 32); tft.println("F");
tft.setTextColor(ST77XX_YELLOW);
tft.print(F("Cal Rel Hmdty: ")); tft.print(chumidity); tft.println("%");
tft.setTextColor(ST77XX_GREEN);
tft.print(F("CO2:")); tft.print(co_two); tft.println(" ppm");
tft.setTextColor(ST77XX_CYAN);
tft.print("VOC Idx: "); tft.println(voc_index);
tft.setTextColor(ST77XX_MAGENTA);
tft.print(F("PM1.0: ")); tft.print(pm_values.pm10_env);tft.println(" ppm");
tft.print(F("PM2.5: ")); tft.print(pm_values.pm25_env);tft.println(" ppm");
tft.print(F("PM10: ")); tft.print(pm_values.pm100_env);tft.println(" ppm");
text_size = 1;
tft.setTextSize(text_size);
tft.setTextColor(ST77XX_ORANGE);
tft.print(F("Chrg: ")); tft.print(batt_perc,1); tft.print("%");
tft.print(F(" Batt Volt: ")); tft.print(batt_volt,1); tft.println("V");
}
void pushMetrics(struct air_val air_values,struct voc_val voc_values, struct battery_val battery_values,PM25_AQI_Data pm_values) {
HttpClient http(client, AIO_SERVER, AIO_SERVER_PORT);
String feedTemp = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.temp") + String("/data.json");
String feedHumidity = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.humidity") + String("/data.json");
String feedcHumidity = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.calibrated-humidity") + String("/data.json");
String feedCO2 = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.co2") + String("/data.json");
String feedVOC = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.voc") + String("/data.json");
String feedPM10 = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.pm10") + String("/data.json");
String feedPM25 = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.pm25") + String("/data.json");
String feedPM100 = String("/api/v2/") + String(AIO_USERNAME) + String("/feeds/") + String("airquality.pm100") + String("/data.json");
pushToAdafruit( feedTemp, String( air_values.temperature ) );
pushToAdafruit( feedHumidity, String( air_values.relative_humidity ) );
pushToAdafruit( feedcHumidity, String( air_values.calibrated_humidity ) );
pushToAdafruit( feedCO2, String( air_values.co2 ) );
pushToAdafruit( feedVOC, String( voc_values.voc_index ) );
pushToAdafruit( feedPM10, String( pm_values.pm10_env ) );
pushToAdafruit( feedPM25, String( pm_values.pm25_env ) );
pushToAdafruit( feedPM100, String( pm_values.pm100_env ) );
}
void pushToAdafruit( String feedUrl, String value ) {
HttpClient http(client, AIO_SERVER, AIO_SERVER_PORT);
String contentType = "content-type: application/json;";
String stringBody = String("\{ \"value\": \"") + String(value) + String( "\" \}" );
SerialMon.print(F("Trying to push Value: "));
SerialMon.print(stringBody);
SerialMon.print(F(" to URL Path: "));
SerialMon.println(feedUrl);
http.beginRequest();
http.post(feedUrl);
http.sendHeader(contentType);
http.sendHeader("Accept: */*");
http.sendHeader("X-AIO-Key",AIO_KEY);
http.sendHeader("Content-Length: " + String(stringBody.length()));
http.beginBody();
http.print(stringBody);
http.endRequest();
int status = http.responseStatusCode();
String respBody = http.responseBody();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
}
void setup(void) {
SerialMon.begin(9600);
SerialMon.println(F("Serial Terminal Initialized"));
SerialMon.println(F("Starting Initialization of TFT Display"));
pinMode(0,INPUT_PULLUP);
pinMode(1,INPUT_PULLDOWN);
pinMode(2,INPUT_PULLDOWN);
// turn on backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
// turn on the TFT / I2C power supply
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
delay(10);
// initialize TFT
tft.init(135, 240); // Init ST7789 240x135
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
//Configure tft
tft.setTextWrap(false);
SerialMon.println(F("Initialized TFT Display"));
////////////////////////////////////////
//check for battery
if (!maxlipo.begin()) {
SerialMon.println(F("Couldnt find Adafruit MAX17048?\nMake sure a battery is plugged in!"));
while (1) delay(10);
}
SerialMon.print(F("Found MAX17048")); Serial.print(F(" with Chip ID: 0x")); Serial.println(maxlipo.getChipID(), HEX);
////////////////////////////////////////
////////////////////////////////////////
// Initialize SCD40
SerialMon.println(F("Starting Initialization of SCD40 Air Sensor"));
Wire.begin();
uint16_t error;
char errorMessage[256];
scd4x.begin(Wire);
// stop potentially previously started measurement
error = scd4x.stopPeriodicMeasurement();
if (error) {
SerialMon.print("Error trying to execute stopPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
SerialMon.println(errorMessage);
}
////////////////////////////////////////
////////////////////////////////////////
// Initialize SCD40
SerialMon.println(F("Starting Initialization of SCD40 Air Sensor"));
uint16_t serial0;
uint16_t serial1;
uint16_t serial2;
error = scd4x.getSerialNumber(serial0, serial1, serial2);
if (error) {
SerialMon.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
SerialMon.println(errorMessage);
}
// Start Measurement
error = scd4x.startPeriodicMeasurement();
if (error) {
SerialMon.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
SerialMon.println(errorMessage);
}
//delay(5000);
SerialMon.println(F("Initialized of SCD40 Air Sensor"));
////////////////////////////////////////
////////////////////////////////////////
// Start the MOX sensoe
SerialMon.println("SGP40 test with SHT31 compensation, it takes time to start and get enough values");
if (! sgp.begin()){
SerialMon.println("SGP40 sensor not found :(");
while (1);
}
SerialMon.println("SGP40 test DONE");
////////////////////////////////////////
////////////////////////////////////////
SerialMon.println("AQI PM begin starting");
delay(2000);
aqi.begin_I2C();
SerialMon.println("AQI PM begin Done");
////////////////////////////////////////
////////////////////////////////////////
// Set GSM module baud rate
SerialMon.println("TinyGSM autobaud starting");
TinyGsmAutoBaud(SerialAT, GSM_AUTOBAUD_MIN, GSM_AUTOBAUD_MAX);
SerialMon.println("TinyGSM autobaud done");
SerialMon.println("TinyGSM modem restart starting");
modem.restart();
SerialMon.println("TinyGSM modem restart done");
////////////////////////////////////////
SerialMon.println("setup done");
delay(5000);
}
void loop() {
SerialMon.println("Loop: top");
struct battery_val battery_values;
struct air_val air_values;
struct voc_val voc_values;
PM25_AQI_Data pm_values;
bool pin1 = digitalRead(1);
bool pin2 = digitalRead(2);
if ( pin1 == 1 ) {
//Send sleep mode command
SerialMon.println("Button 2 pressed");
tft.enableSleep( true );
// turn off backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, LOW);
displayOn = false;
}
if ( pin2 == 1 ){
//send wake mode command
SerialMon.println("Button 3 pressed");
tft.enableSleep( false );
// turn on backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
displayOn = true;
}
// get batter status
battery_values.volt = maxlipo.cellVoltage();
battery_values.percent = maxlipo.cellPercent();
air_values = get_air_values();
voc_values = get_voc(air_values.temperature, air_values.relative_humidity);
aqi.read(&pm_values);
updateTFT(air_values,voc_values,battery_values,pm_values);
SerialMon.print("displayOn:"); SerialMon.println(displayOn);
////
IPAddress local;
SerialMon.println("Checking if modem ready");
if (not modem.isNetworkConnected()) {
//modem.gprsConnect(apn, gprsUser, gprsPass);
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork(600000L)) { // You may need lengthen this in poor service areas
SerialMon.println(F("waitForNetwork [fail]"));
//delay(10000);
}else{
SerialMon.println(F("waitForNetwork [OK]"));
}
}
if (modem.isNetworkConnected()){
if (not modem.isGprsConnected()){
modem.gprsConnect(apn, gprsUser, gprsPass);
}
}
if( modem.isGprsConnected() ){
pushMetrics(air_values,voc_values,battery_values,pm_values);
}
SerialMon.println("Loop: Bottom");
delay(5000);
}