Compare commits

...

42 Commits

Author SHA1 Message Date
6314e52770 Updated indoor examples 2023-04-13 09:33:26 +07:00
43f599a0a7 Corrected Arduino JSON version 2023-03-22 15:55:16 +07:00
cfc37d2d96 Added example for PCB v4.2 2023-03-11 06:53:37 +07:00
693d1f78aa Adjusted license to CC BY-SA 2023-03-09 07:35:49 +07:00
ef802593a6 Updated default display orientation 2023-02-26 16:57:08 +07:00
8ec543e9c1 Updated Arduino library version tag 2023-02-22 08:44:08 +07:00
9722eda5fa Set bus clock for u8g2 to 100kHz for increased stability.
Changed SenseAir CO2 request code to 0X04 ...
2023-02-22 08:37:56 +07:00
253d8a6810 Updated outdoor example (removed HEX from Chip ID) 2023-01-13 08:27:48 +07:00
6101429d30 Updated outdoor example 2023-01-13 06:56:17 +07:00
47c55ae0dd Updated example sketches 2022-12-20 07:59:52 +07:00
0511f5430f Added Display Rotation on Config Button 2022-12-10 10:36:31 +07:00
5481bd51c8 Added Outdoor Code Example 2022-12-09 14:51:19 +07:00
7d717cc01c Added PM01 and PM10 2022-11-15 14:35:03 +07:00
05b47e6b3d Fixed humidity reading 2022-11-15 11:05:54 +07:00
77ee1de9c1 Added example for DIY V3.7 Board with Push Button 2022-11-14 07:27:27 +07:00
183f62daf7 Adding additional parameters for Plantower PM sensors 2022-11-10 16:25:01 +07:00
5dd8a8be32 Testing Sensirion SGP41 libraries with hum/temp compensation 2022-11-07 07:04:26 +07:00
98ec0c4d10 Improved example code DIY Basic 2022-11-06 19:21:28 +07:00
724d041684 Improved formatting 2022-11-03 13:51:29 +07:00
afd4d16866 Adding CO2 Baseline Calibration Example 2022-11-03 13:49:52 +07:00
dc1a070ee3 Adjusted examples to not send error codes (e.g. -1) to server 2022-11-01 14:04:41 +07:00
84fe9c2575 Test with datapos shifts on CO2 readings 2022-10-31 08:43:13 +07:00
90eee5d17f CO2 error handling in library
Small improvements in examples
2022-10-29 07:39:11 +07:00
d5c8af68a0 Fixed description 2022-10-02 07:59:36 +07:00
3c7180e642 Fixed display rotation and added example for SGP40 2022-10-02 07:38:44 +07:00
ee40b60aad updated documentations 2022-07-29 16:26:06 +02:00
b9a968ef7a added 90 degree rotation code 2022-07-29 11:11:39 +02:00
4bb434562f new examples 2022-06-11 16:18:21 +02:00
ab1f6c1ae7 corrected version number 2022-01-20 14:08:47 +07:00
63c7e9ca00 updated date and version number 2022-01-20 14:06:23 +07:00
0b8408ef13 improved CO2 code (details https://forum.airgradient.com/t/s8-co2-reading-of-1/69/15) 2022-01-20 08:26:27 +07:00
ff34d2c143 changed HTTP Post delay 2021-12-28 07:08:24 +07:00
73498894e4 added traffic light example 2021-12-10 11:12:26 +07:00
ed3b0ff53f corrected library version number 2021-12-08 10:11:45 +07:00
07e1b5e97a improved examples code 2021-12-08 09:59:32 +07:00
348ddba048 Merge pull request #4 from AlanBell/patch-1
Update AirGradient.cpp
2021-12-08 08:32:12 +07:00
a3c8054839 Merge pull request #8 from markasoftware/markasoftware/math-and-wificlient
correct math.h header and specify wificlient as per new http library requirement.
2021-12-08 08:31:26 +07:00
555693cda8 Merge pull request #12 from StrongholdIII/master
Added return to compile with platformIO
2021-12-08 08:30:14 +07:00
0a9f184946 added comment 2021-12-08 08:20:22 +07:00
7c02564ed4 Added return to compile with platformIO 2021-10-15 22:01:57 +02:00
0abc5629fc correct math.h header and specify wificlient as per new http library requirement 2021-05-29 00:04:15 -07:00
a40399d082 Update AirGradient.cpp
math.h should be lower case - otherwise won't compile on case sensitive platforms.
2021-01-02 16:18:36 +00:00
21 changed files with 3255 additions and 230 deletions

View File

@ -105,7 +105,128 @@ int AirGradient::getPM2_Raw(){
pm02 = data.PM_AE_UG_2_5;
return pm02;
} else {
return 0;
return -1;
}
}
int AirGradient::getPM1_Raw(){
int pm02;
DATA data;
requestRead();
if (readUntil(data)) {
pm02 = data.PM_AE_UG_1_0;
return pm02;
} else {
return -1;
}
}
int AirGradient::getPM10_Raw(){
int pm02;
DATA data;
requestRead();
if (readUntil(data)) {
pm02 = data.PM_AE_UG_10_0;
return pm02;
} else {
return -1;
}
}
int AirGradient::getPM0_3Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_0_3;
return count;
} else {
return -1;
}
}
int AirGradient::getPM10_0Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_10_0;
return count;
} else {
return -1;
}
}
int AirGradient::getPM5_0Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_5_0;
return count;
} else {
return -1;
}
}
int AirGradient::getPM2_5Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_2_5;
return count;
} else {
return -1;
}
}
int AirGradient::getPM1_0Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_1_0;
return count;
} else {
return -1;
}
}
int AirGradient::getPM0_5Count(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_RAW_0_5;
return count;
} else {
return -1;
}
}
int AirGradient::getAMB_TMP(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_TMP;
return count;
} else {
return -1;
}
}
int AirGradient::getAMB_HUM(){
int count;
DATA data;
requestRead();
if (readUntil(data)) {
count = data.PM_HUM;
return count;
} else {
return -1;
}
}
@ -248,6 +369,21 @@ void AirGradient::loop()
_data->PM_AE_UG_1_0 = makeWord(_payload[6], _payload[7]);
_data->PM_AE_UG_2_5 = makeWord(_payload[8], _payload[9]);
_data->PM_AE_UG_10_0 = makeWord(_payload[10], _payload[11]);
// Total particles count per 100ml air
_data->PM_RAW_0_3 = makeWord(_payload[12], _payload[13]);
_data->PM_RAW_0_5 = makeWord(_payload[14], _payload[15]);
_data->PM_RAW_1_0 = makeWord(_payload[16], _payload[17]);
_data->PM_RAW_2_5 = makeWord(_payload[18], _payload[19]);
_data->PM_RAW_5_0 = makeWord(_payload[20], _payload[21]);
_data->PM_RAW_10_0 = makeWord(_payload[22], _payload[23]);
// Formaldehyde concentration (PMSxxxxST units only)
_data->AMB_HCHO = makeWord(_payload[24], _payload[25]) / 1000;
// Temperature & humidity (PMSxxxxST units only)
_data->PM_TMP = makeWord(_payload[20], _payload[21]) / 10;
_data->PM_HUM = makeWord(_payload[22], _payload[23]) / 10;
}
_index = 0;
@ -303,7 +439,7 @@ TMP_RH AirGradient::periodicFetchData() //
return result;
}
else
returnError(error);
return returnError(error);
}
TMP_RH_ErrorCode AirGradient::periodicStop() {
@ -589,65 +725,74 @@ void AirGradient::CO2_Init(int rx_pin,int tx_pin,int baudRate){
delay(10000);
}
}
const char* AirGradient::getCO2(int retryLimit) {
int ctr = 0;
int result_CO2 = getCO2_Raw();
while(result_CO2 == -1){
result_CO2 = getCO2_Raw();
if((ctr == retryLimit) || (result_CO2 == -1)){
Char_CO2[0] = 'N';
Char_CO2[1] = 'U';
Char_CO2[2] = 'L';
Char_CO2[3] = 'L';
return Char_CO2;
int AirGradient::getCO2(int numberOfSamplesToTake) {
int successfulSamplesCounter = 0;
int co2AsPpmSum = 0;
for (int sample = 0; sample < numberOfSamplesToTake; sample++) {
int co2AsPpm = getCO2_Raw();
if (co2AsPpm > 300 && co2AsPpm < 10000) {
Serial.println("CO2 read success " + String(co2AsPpm));
successfulSamplesCounter++;
co2AsPpmSum += co2AsPpm;
} else {
Serial.println("CO2 read failed with " + String(co2AsPpm));
}
ctr++;
// without delay we get a few 10ms spacing, add some more
delay(250);
}
sprintf(Char_CO2,"%d", result_CO2);
return Char_CO2;
if (successfulSamplesCounter <= 0) {
// total failure
return -5;
}
Serial.println("# of CO2 reads that worked: " + String(successfulSamplesCounter));
Serial.println("CO2 reads sum " + String(co2AsPpmSum));
return co2AsPpmSum / successfulSamplesCounter;
}
int AirGradient::getCO2_Raw(){
int retry = 0;
CO2_READ_RESULT result;
const byte CO2Command[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25};
byte CO2Response[] = {0,0,0,0,0,0,0};
while(!(_SoftSerial_CO2->available())) {
retry++;
// keep sending request until we start to get a response
_SoftSerial_CO2->write(CO2Command, 7);
delay(50);
if (retry > 10) {
return -1;
}
}
// <<>>
int AirGradient::getCO2_Raw() {
int timeout = 0;
while (_SoftSerial_CO2->available() < 7) {
timeout++;
if (timeout > 10) {
while(_SoftSerial_CO2->available())
_SoftSerial_CO2->read();
break;
}
delay(50);
}
while(_SoftSerial_CO2->available()) // flush whatever we might have
_SoftSerial_CO2->read();
for (int i=0; i < 7; i++) {
int byte = _SoftSerial_CO2->read();
if (byte == -1) {
result.success = false;
return -1;
}
CO2Response[i] = byte;
}
int valMultiplier = 1;
int high = CO2Response[3];
int low = CO2Response[4];
unsigned long val = high*256 + low;
const byte CO2Command[] = {0XFE, 0X04, 0X00, 0X03, 0X00, 0X01, 0XD5, 0XC5};
byte CO2Response[] = {0,0,0,0,0,0,0};
int datapos = -1;
return val;
const int commandSize = 8;
const int responseSize = 7;
int numberOfBytesWritten = _SoftSerial_CO2->write(CO2Command, commandSize);
if (numberOfBytesWritten != commandSize) {
// failed to write request
return -2;
}
// attempt to read response
int timeoutCounter = 0;
while (_SoftSerial_CO2->available() < responseSize) {
timeoutCounter++;
if (timeoutCounter > 10) {
// timeout when reading response
return -3;
}
delay(50);
}
// we have 7 bytes ready to be read
for (int i=0; i < responseSize; i++) {
CO2Response[i] = _SoftSerial_CO2->read();
if ((CO2Response[i] == 0xFE) && (datapos == -1)){
datapos = i;
}
Serial.print (CO2Response[i],HEX);
Serial.print (":");
}
return CO2Response[datapos + 3]*256 + CO2Response[datapos + 4];
}
//END CO2 FUNCTIONS //

View File

@ -186,6 +186,21 @@ class AirGradient
uint16_t PM_AE_UG_1_0;
uint16_t PM_AE_UG_2_5;
uint16_t PM_AE_UG_10_0;
// Raw particles count (number of particles in 0.1l of air
uint16_t PM_RAW_0_3;
uint16_t PM_RAW_0_5;
uint16_t PM_RAW_1_0;
uint16_t PM_RAW_2_5;
uint16_t PM_RAW_5_0;
uint16_t PM_RAW_10_0;
// Formaldehyde (HCHO) concentration in mg/m^3 - PMSxxxxST units only
uint16_t AMB_HCHO;
// Temperature & humidity - PMSxxxxST units only
int16_t PM_TMP;
uint16_t PM_HUM;
};
void PMS(Stream&);
@ -197,8 +212,22 @@ class AirGradient
void requestRead();
bool read_PMS(DATA& data);
bool readUntil(DATA& data, uint16_t timeout = SINGLE_RESPONSE_TIME);
const char* getPM2();
int getPM2_Raw();
int getPM1_Raw();
int getPM10_Raw();
int getPM0_3Count();
int getPM0_5Count();
int getPM1_0Count();
int getPM2_5Count();
int getPM5_0Count();
int getPM10_0Count();
int getAMB_TMP();
int getAMB_HUM();
//PMS VARIABLES PUBLIC_END
@ -223,7 +252,7 @@ class AirGradient
void CO2_Init();
void CO2_Init(int,int);
void CO2_Init(int,int,int);
const char* getCO2(int retryLimit = 5);
int getCO2(int numberOfSamplesToTake = 5);
int getCO2_Raw();
SoftwareSerial *_SoftSerial_CO2;
@ -252,7 +281,7 @@ class AirGradient
enum STATUS { STATUS_WAITING, STATUS_OK };
enum MODE { MODE_ACTIVE, MODE_PASSIVE };
uint8_t _payload[12];
uint8_t _payload[32];
Stream* _stream;
DATA* _data;
STATUS _PMSstatus;
@ -264,7 +293,9 @@ class AirGradient
uint16_t _calculatedChecksum;
SoftwareSerial *_SoftSerial_PMS;
void loop();
char Char_PM2[10];
char Char_PM1[10];
char Char_PM2[10];
char Char_PM10[10];
//PMS VARIABLES PRIVATE END
//TMP_RH VARIABLES PRIVATE START

View File

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2020 AirGradient
Copyright (c) 2022 AirGradient
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -5,4 +5,4 @@ Build your own low cost air quality sensor with optional display measuring PM2.5
This library makes it easy to read the sensor data from the Plantower PMS5003 PM2.5 sensor, the Senseair S8 and the SHT30/31 Temperature and Humidity sensor. Visit our DIY section for detailed build instructions and PCB layout.
https://www.airgradient.com/diy/
https://www.airgradient.com/open-airgradient/instructions/

View File

@ -0,0 +1,127 @@
/*
This is the code for forced calibration of the SenseAir S8 sensor. The sensor also has a one-week automatic baseline calibration that should calibrate the sensor latest after one week.0
However if you need a faster calibration please proceed as following:
1. Flash this code
2. Bring the sensor outside into fresh air and leave it there for at least 10 minutes
3. Power on the sensor
4. Follow the instructions on the display
5. After the calibration has been done, flash back the previous code for AQ measurements
The codes needs the following libraries installed:
“S8_UART” by Josep Comas tested with version 1.0.1
“U8g2” by oliver tested with version 2.32.15
Many thanks to Josep Comas of the S8_UART library from which parts of below code are borrowed.
*/
#include <Arduino.h>
#include "s8_uart.h"
#include <U8g2lib.h>
/* BEGIN CONFIGURATION */
// Display
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);//for DIY PRO
U8G2_SSD1306_64X48_ER_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //for DIY BASIC
#define DEBUG_BAUDRATE 115200
#if (defined USE_SOFTWARE_SERIAL || defined ARDUINO_ARCH_RP2040)
#define S8_RX_PIN 2
#define S8_TX_PIN 0
#else
#define S8_UART_PORT 1
#endif
#define COUNTDOWN (60) //seconds
/* END CONFIGURATION */
#ifdef USE_SOFTWARE_SERIAL
SoftwareSerial S8_serial(S8_RX_PIN, S8_TX_PIN);
#else
#if defined(ARDUINO_ARCH_RP2040)
REDIRECT_STDOUT_TO(Serial)
UART S8_serial(S8_TX_PIN, S8_RX_PIN, NC, NC);
#else
HardwareSerial S8_serial(S8_UART_PORT);
#endif
#endif
S8_UART *sensor_S8;
S8_sensor sensor;
void setup() {
Serial.begin(DEBUG_BAUDRATE);
u8g2.begin();
int i = 0;
while (!Serial && i < 50) {
delay(10);
i++;
}
S8_serial.begin(S8_BAUDRATE);
sensor_S8 = new S8_UART(S8_serial);
sensor_S8->get_firmware_version(sensor.firm_version);
int len = strlen(sensor.firm_version);
if (len == 0) {
Serial.println("SenseAir S8 CO2 sensor not found!");
updateOLED2("SenseAir", "not", "found");
while (1) { delay(1); };
}
Serial.println(">>> SenseAir S8 NDIR CO2 sensor <<<");
printf("Firmware version: %s\n", sensor.firm_version);
sensor.sensor_id = sensor_S8->get_sensor_ID();
Serial.print("Sensor ID: 0x"); printIntToHex(sensor.sensor_id, 4); Serial.println("");
Serial.println("Now, you put the sensor outside and wait.");
Serial.println("Countdown begins...");
unsigned int seconds = COUNTDOWN;
while (seconds > 0) {
printf("Time remaining: %d minutes %d seconds\n", seconds / 60, seconds % 60);
updateOLED2("Wait", "for", String(seconds) + " Sec");
delay(1000);
seconds--;
}
Serial.println("Time reamining: 0 minutes 0 seconds");
// Start manual calibration
Serial.println("Starting manual calibration...");
updateOLED2("Starting", "Manual", "Calibration");
delay(2000);
if (!sensor_S8->manual_calibration()) {
Serial.println("Error setting manual calibration!");
updateOLED2("Error", "Manual", "Calibration");
while (1) { delay(10); }
}
}
void loop() {
static unsigned int elapsed = 0;
delay(2000);
elapsed += 2;
// Check if background calibration is finished
sensor.ack = sensor_S8->get_acknowledgement();
if (sensor.ack & S8_MASK_CO2_BACKGROUND_CALIBRATION) {
printf("Manual calibration is finished. Elapsed: %u seconds\n", elapsed);
updateOLED2("Calibration", "finished", "");
while (1) { delay(10); }
} else {
Serial.println("Doing manual calibration...");
updateOLED2("Doing", "manual", "calibration");
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 28, String(ln2).c_str());
u8g2.drawStr(1, 46, String(ln3).c_str());
} while ( u8g2.nextPage() );
}

View File

@ -1,141 +0,0 @@
/*
This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Microcontroller.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
For build instructions please visit https://www.airgradient.com/diy/
Compatible with the following sensors:
Plantower PMS5003 (Fine Particle Sensor)
SenseAir S8 (CO2 Sensor)
SHT30/31 (Temperature/Humidity Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
The codes needs the following libraries installed:
"WifiManager by tzapu, tablatronix" tested with Version 2.0.3-alpha
"ESP8266 and ESP32 OLED driver for SSD1306 displays by ThingPulse, Fabrice Weinberg" tested with Version 4.1.0
Configuration:
Please set in the code below which sensor you are using and if you want to connect it to WiFi.
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/schools/
MIT License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Wire.h>
#include "SSD1306Wire.h"
AirGradient ag = AirGradient();
SSD1306Wire display(0x3c, SDA, SCL);
// set sensors that you do not use to false
boolean hasPM=true;
boolean hasCO2=true;
boolean hasSHT=true;
// set to true if you want to connect to wifi. The display will show values only when the sensor has wifi connection
boolean connectWIFI=false;
// change if you want to send the data to another server
String APIROOT = "http://hw.airgradient.com/";
void setup(){
Serial.begin(9600);
display.init();
display.flipScreenVertically();
showTextRectangle("Init", String(ESP.getChipId(),HEX),true);
if (hasPM) ag.PMS_Init();
if (hasCO2) ag.CO2_Init();
if (hasSHT) ag.TMP_RH_Init(0x44);
if (connectWIFI) connectToWifi();
delay(2000);
}
void loop(){
// create payload
String payload = "{\"wifi\":" + String(WiFi.RSSI()) + ",";
if (hasPM) {
int PM2 = ag.getPM2_Raw();
payload=payload+"\"pm02\":" + String(PM2);
showTextRectangle("PM2",String(PM2),false);
delay(3000);
}
if (hasCO2) {
if (hasPM) payload=payload+",";
int CO2 = ag.getCO2_Raw();
payload=payload+"\"rco2\":" + String(CO2);
showTextRectangle("CO2",String(CO2),false);
delay(3000);
}
if (hasSHT) {
if (hasCO2 || hasPM) payload=payload+",";
TMP_RH result = ag.periodicFetchData();
payload=payload+"\"atmp\":" + String(result.t) + ",\"rhum\":" + String(result.rh);
showTextRectangle(String(result.t),String(result.rh)+"%",false);
delay(3000);
}
payload=payload+"}";
// send payload
if (connectWIFI){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(),HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
}
// DISPLAY
void showTextRectangle(String ln1, String ln2, boolean small) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
if (small) {
display.setFont(ArialMT_Plain_16);
} else {
display.setFont(ArialMT_Plain_24);
}
display.drawString(32, 16, ln1);
display.drawString(32, 36, ln2);
display.display();
}
// Wifi Manager
void connectToWifi(){
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AIRGRADIENT-"+String(ESP.getChipId(),HEX);
wifiManager.setTimeout(120);
if(!wifiManager.autoConnect((const char*)HOTSPOT.c_str())) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}
}

View File

@ -3,24 +3,29 @@ This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Micr
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
For build instructions please visit https://www.airgradient.com/diy/
For build instructions please visit https://www.airgradient.com/open-airgradient/instructions/
Compatible with the following sensors:
SenseAir S8 (CO2 Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/schools/
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
Kits with all required components are available at https://www.airgradient.com/open-airgradient/shop/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
MIT License
*/
#include <AirGradient.h>
AirGradient ag = AirGradient();
void setup(){
Serial.begin(9600);
Serial.begin(115200);
ag.CO2_Init();
}

View File

@ -0,0 +1,132 @@
/*
This is the code for the AirGradient DIY CO2 Traffic light with an ESP8266 Microcontroller.
For build instructions please visit:
https://www.airgradient.com/open-airgradient/instructions/diy-co2-traffic-light/
Kits (including a pre-soldered version) are available:
https://www.airgradient.com/open-airgradient/kits/
Compatible with the following sensors:
SenseAir S8 (CO2 Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
Please install the following libraries:
"Adafruit NeoMatrix" Library (tested with 1.2.0)
"WifiManager by tzapu, tablatronix" tested with Version 2.0.3-alpha
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif
AirGradient ag = AirGradient();
#define PIN D8
int co2 = 0;
String text = "AirGradient CO2";
// set to true if you want to connect to wifi. The display will show values only when the sensor has wifi connection
boolean connectWIFI=true;
int greenToOrange = 800;
int orangeToRed = 1200;
// change if you want to send the data to another server
String APIROOT = "http://hw.airgradient.com/";
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
ag.CO2_Init();
matrix.begin();
matrix.setRotation(1); // change rotation
matrix.setTextWrap(false);
matrix.setBrightness(40);
matrix.setTextColor(matrix.Color(70,130,180));
Serial.println("Chip ID: "+String(ESP.getChipId(),HEX));
if (connectWIFI) connectToWifi();
delay(2000);
}
void loop() {
showText();
}
int x = matrix.width();
void showText() {
Serial.println("in loop");
matrix.fillScreen(0);
matrix.setCursor(x, 0);
matrix.print(String(text));
if(--x < -100) {
x = matrix.width();
Serial.println("end text");
co2 = ag.getCO2_Raw();
text = String(co2)+"ppm";
if (co2>350) matrix.setTextColor(matrix.Color(0, 255, 0));
if (co2>greenToOrange) matrix.setTextColor(matrix.Color(255, 90, 0));
if (co2>orangeToRed) matrix.setTextColor(matrix.Color(255,0, 0));
// send payload
String payload = "{\"wifi\":" + String(WiFi.RSSI()) + ",";
payload = payload + "\"rco2\":" + String(co2);
payload = payload + "}";
if (connectWIFI) {
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
delay(20000);
}
delay(10000);
}
matrix.show();
delay(100);
}
// Wifi Manager
void connectToWifi(){
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AIRGRADIENT-"+String(ESP.getChipId(),HEX);
wifiManager.setTimeout(120);
if(!wifiManager.autoConnect((const char*)HOTSPOT.c_str())) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}
}

View File

@ -0,0 +1,228 @@
/*
This is the code for the AirGradient DIY BASIC Air Quality Sensor with an ESP8266 Microcontroller.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
MIT License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
U8G2_SSD1306_64X48_ER_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //for DIY BASIC
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// PM2.5 in US AQI (default ug/m3)
boolean inUSAQI = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
long val;
void setup()
{
Serial.begin(115200);
u8g2.setBusClock(100000);
u8g2.begin();
updateOLED();
if (connectWIFI) {
connectToWifi();
}
updateOLED2("Warm Up", "Serial#", String(ESP.getChipId(), HEX));
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop()
{
currentMillis = millis();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln1;
String ln2;
String ln3;
if (inUSAQI){
ln1 = "AQI:" + String(PM_TO_AQI_US(pm25)) ;
} else {
ln1 = "PM: " + String(pm25) +"ug" ;
}
ln2 = "CO2:" + String(Co2);
if (inF) {
ln3 = String((temp* 9 / 5) + 32).substring(0,4) + " " + String(hum)+"%";
} else {
ln3 = String(temp).substring(0,4) + " " + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 28, String(ln2).c_str());
u8g2.drawStr(1, 46, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("Connect", "Wifi AG-", String(ESP.getChipId(), HEX));
delay(2000);
wifiManager.setTimeout(90);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("Booting", "offline", "mode");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,212 @@
/*
Important: This code is only for the AirGradient DIY OUTDOOR.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-outdoor/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
Configuration:
Install required libraries
Patch PMS library to accept temperature and humidity from PMS5003T
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include "PMS.h"
#include "SoftwareSerial.h"
#include <Wire.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
SoftwareSerial softSerial(D6, D5);
SoftwareSerial soft2(D3, D7);
PMS pms(softSerial);
PMS pms2(soft2);
PMS::DATA data;
PMS::DATA data2;
float pm1Value=0;
int pm1Position = 0;
float pm2Value=0;
int pm2Position = 0;
float temp_pm1 = 0;
float hum_pm1 = 0;
float temp_pm2 = 0;
float hum_pm2 = 0;
unsigned long currentMillis = 0;
const int pm1Interval = 5000;
unsigned long previousPm1 = 0;
const int pm2Interval = 5000;
unsigned long previousPm2 = 0;
String APIROOT = "http://hw.airgradient.com/";
void setup()
{
Serial.begin(115200);
Serial.println("Chip ID: "+String(ESP.getChipId()));
softSerial.begin(9600);
soft2.begin(9600);
Wire.begin();
pinMode(D7, OUTPUT);
connectToWifi();
}
void loop()
{
currentMillis = millis();
updatePm1();
updatePm2();
}
void updatePm1()
{
if (currentMillis - previousPm1 >= pm1Interval) {
digitalWrite(D7, HIGH);
delay(400);
digitalWrite(D7, LOW);
Serial.println("updatePm1: "+String(pm1Position));
previousPm1 += pm1Interval;
pms.requestRead();
if (pms.readUntil(data)){
Serial.println("success read");
int pm1 = data.PM_AE_UG_2_5;
temp_pm1 = data.AMB_TMP;
hum_pm1 = data.AMB_HUM;
Serial.print("PMS 1: PM 2.5 (ug/m3): ");
Serial.println(pm1);
Serial.print("PMS 1: Temp: ");
Serial.println(temp_pm1);
Serial.print("PMS 1: Hum: ");
Serial.println(hum_pm1);
Serial.println();
delay(1000);
pm1Value=pm1Value+pm1;
pm1Position++;
if (pm1Position==20) {
sendToServerPM1(pm1Value);
pm1Position=0;
pm1Value=0;
}
}
}
}
void updatePm2()
{
if (currentMillis - previousPm2 >= pm2Interval) {
Serial.println("updatePm2: "+String(pm2Position));
previousPm2 += pm2Interval;
pms2.requestRead();
if (pms2.readUntil(data2)){
int pm2 = data2.PM_AE_UG_2_5;
temp_pm2 = data2.AMB_TMP ;
hum_pm2 = data2.AMB_HUM;
Serial.print("PMS 2: PM 2.5 (ug/m3): ");
Serial.println(pm2);
Serial.print("PMS 2: Temp: ");
Serial.println(temp_pm2);
Serial.print("PMS 2: Hum: ");
Serial.println(hum_pm2);
Serial.println();
delay(1000);
pm2Value=pm2Value+pm2;
pm2Position++;
if (pm2Position==20) {
sendToServerPM2(pm2Value);
pm2Position=0;
pm2Value=0;
}
}
}
}
void sendToServerPM1(float pm1Value) {
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ ", \"pm02\":" + String(pm1Value/20)
+ ", \"atmp\":" + String(temp_pm1/10)
+ ", \"rhum\":" + String(hum_pm1/10)
+ "}";
if(WiFi.status()== WL_CONNECTED){
digitalWrite(D7, HIGH);
delay(300);
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId()) + "-1/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
digitalWrite(D7, LOW);
}
else {
Serial.println("WiFi Disconnected");
}
}
void sendToServerPM2(float pm2Value) {
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ ", \"pm02\":" + String(pm2Value/20)
+ ", \"atmp\":" + String(temp_pm2/10)
+ ", \"rhum\":" + String(hum_pm2/10)
+ "}";
if(WiFi.status()== WL_CONNECTED){
digitalWrite(D7, HIGH);
delay(300);
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId()) + "-2/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
digitalWrite(D7, LOW);
}
else {
Serial.println("WiFi Disconnected");
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AIRGRADIENT-" + String(ESP.getChipId());
wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}

View File

@ -0,0 +1,220 @@
/*
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
void setup()
{
Serial.begin(115200);
u8g2.setBusClock(100000);
u8g2.begin();
updateOLED();
if (connectWIFI) {
connectToWifi();
}
updateOLED2("Warming up the", "sensors.", "");
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop()
{
currentMillis = millis();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1 = "PM:" + String(pm25) + " AQI:" + String(PM_TO_AQI_US(pm25)) ;
String ln2 = "CO2:" + String(Co2);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("60s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,424 @@
/*
Important: This code is only for the DIY PRO PCB Version 3.7 that has a push button mounted.
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller with the SGP40 TVOC module from AirGradient.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro-v37/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
"Sensirion I2C SGP41" by Sensation Version 0.1.0
"Sensirion Gas Index Algorithm" by Sensation Version 3.2.1
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <EEPROM.h>
//#include "SGP30.h"
#include <SensirionI2CSgp41.h>
#include <NOxGasIndexAlgorithm.h>
#include <VOCGasIndexAlgorithm.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
SensirionI2CSgp41 sgp41;
VOCGasIndexAlgorithm voc_algorithm;
NOxGasIndexAlgorithm nox_algorithm;
// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;
// for peristent saving and loading
int addr = 4;
byte value;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// PM2.5 in US AQI (default ug/m3)
boolean inUSAQI = false;
// Display Position
boolean displayTop = true;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int tvocInterval = 1000;
unsigned long previousTVOC = 0;
int TVOC = 0;
int NOX = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
int buttonConfig=4;
int lastState = LOW;
int currentState;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("Hello");
u8g2.begin();
//u8g2.setDisplayRotation(U8G2_R0);
EEPROM.begin(512);
delay(500);
buttonConfig = String(EEPROM.read(addr)).toInt();
setConfig();
updateOLED2("Press Button", "Now for", "Config Menu");
delay(2000);
currentState = digitalRead(D7);
if (currentState == HIGH)
{
updateOLED2("Entering", "Config Menu", "");
delay(3000);
lastState = LOW;
inConf();
}
if (connectWIFI)
{
connectToWifi();
}
updateOLED2("Warming Up", "Serial Number:", String(ESP.getChipId(), HEX));
sgp41.begin(Wire);
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop() {
currentMillis = millis();
updateTVOC();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void inConf(){
setConfig();
currentState = digitalRead(D7);
if(lastState == LOW && currentState == HIGH) {
pressedTime = millis();
}
else if(lastState == HIGH && currentState == LOW) {
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < 1000 ) {
buttonConfig=buttonConfig+1;
if (buttonConfig>7) buttonConfig=0;
}
}
if (lastState == HIGH && currentState == HIGH){
long passedDuration = millis() - pressedTime;
if( passedDuration > 4000 ) {
// to do
// if (buttonConfig==4) {
// updateOLED2("Saved", "Release", "Button Now");
// delay(1000);
// updateOLED2("Starting", "CO2", "Calibration");
// delay(1000);
// Co2Calibration();
// } else {
updateOLED2("Saved", "Release", "Button Now");
delay(1000);
updateOLED2("Rebooting", "in", "5 seconds");
delay(5000);
EEPROM.write(addr, char(buttonConfig));
EEPROM.commit();
delay(1000);
ESP.restart();
// }
}
}
lastState = currentState;
delay(100);
inConf();
}
void setConfig() {
if (buttonConfig == 0) {
updateOLED2("Temp. in C", "PM in ug/m3", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = false;
inUSAQI = false;
}
if (buttonConfig == 1) {
updateOLED2("Temp. in C", "PM in US AQI", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = false;
inUSAQI = true;
}
if (buttonConfig == 2) {
updateOLED2("Temp. in F", "PM in ug/m3", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = true;
inUSAQI = false;
}
if (buttonConfig == 3) {
updateOLED2("Temp. in F", "PM in US AQI", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = true;
inUSAQI = true;
}
if (buttonConfig == 4) {
updateOLED2("Temp. in C", "PM in ug/m3", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = false;
inUSAQI = false;
}
if (buttonConfig == 5) {
updateOLED2("Temp. in C", "PM in US AQI", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = false;
inUSAQI = true;
}
if (buttonConfig == 6) {
updateOLED2("Temp. in F", "PM in ug/m3", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = true;
inUSAQI = false;
}
if (buttonConfig == 7) {
updateOLED2("Temp. in F", "PM in US AQI", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = true;
inUSAQI = true;
}
// to do
// if (buttonConfig == 8) {
// updateOLED2("CO2", "Manual", "Calibration");
// }
}
void updateTVOC()
{
uint16_t error;
char errorMessage[256];
uint16_t defaultRh = 0x8000;
uint16_t defaultT = 0x6666;
uint16_t srawVoc = 0;
uint16_t srawNox = 0;
uint16_t defaultCompenstaionRh = 0x8000; // in ticks as defined by SGP41
uint16_t defaultCompenstaionT = 0x6666; // in ticks as defined by SGP41
uint16_t compensationRh = 0; // in ticks as defined by SGP41
uint16_t compensationT = 0; // in ticks as defined by SGP41
delay(1000);
compensationT = static_cast<uint16_t>((temp + 45) * 65535 / 175);
compensationRh = static_cast<uint16_t>(hum * 65535 / 100);
if (conditioning_s > 0) {
error = sgp41.executeConditioning(compensationRh, compensationT, srawVoc);
conditioning_s--;
} else {
error = sgp41.measureRawSignals(compensationRh, compensationT, srawVoc,
srawNox);
}
if (currentMillis - previousTVOC >= tvocInterval) {
previousTVOC += tvocInterval;
TVOC = voc_algorithm.process(srawVoc);
NOX = nox_algorithm.process(srawNox);
Serial.println(String(TVOC));
}
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1;
if (inUSAQI) {
ln1 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " CO2:" + String(Co2);
} else {
ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
}
String ln2 = "TVOC:" + String(TVOC) + " NOX:" + String(NOX);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ (TVOC < 0 ? "" : ", \"tvoc_index\":" + String(TVOC))
+ (NOX < 0 ? "" : ", \"nox_index\":" + String(NOX))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("90s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(90);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,418 @@
/*
Important: This code is only for the DIY PRO PCB Version 3.7 that has a push button mounted.
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller with the SGP40 TVOC module from AirGradient.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro-v37/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
"Sensirion I2C SGP41" by Sensation Version 0.1.0
"Sensirion Gas Index Algorithm" by Sensation Version 3.2.1
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <EEPROM.h>
//#include "SGP30.h"
#include <SensirionI2CSgp41.h>
#include <NOxGasIndexAlgorithm.h>
#include <VOCGasIndexAlgorithm.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
SensirionI2CSgp41 sgp41;
VOCGasIndexAlgorithm voc_algorithm;
NOxGasIndexAlgorithm nox_algorithm;
// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;
// for peristent saving and loading
int addr = 4;
byte value;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// PM2.5 in US AQI (default ug/m3)
boolean inUSAQI = false;
// Display Position
boolean displayTop = true;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int tvocInterval = 1000;
unsigned long previousTVOC = 0;
int TVOC = 0;
int NOX = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
int buttonConfig=4;
int lastState = LOW;
int currentState;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("Hello");
u8g2.begin();
//u8g2.setDisplayRotation(U8G2_R0);
EEPROM.begin(512);
delay(500);
buttonConfig = String(EEPROM.read(addr)).toInt();
setConfig();
updateOLED2("Press Button", "Now for", "Config Menu");
delay(2000);
currentState = digitalRead(D7);
if (currentState == HIGH)
{
updateOLED2("Entering", "Config Menu", "");
delay(3000);
lastState = LOW;
inConf();
}
if (connectWIFI)
{
connectToWifi();
}
updateOLED2("Warming Up", "Serial Number:", String(ESP.getChipId(), HEX));
sgp41.begin(Wire);
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop() {
currentMillis = millis();
updateTVOC();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void inConf(){
setConfig();
currentState = digitalRead(D7);
if(lastState == LOW && currentState == HIGH) {
pressedTime = millis();
}
else if(lastState == HIGH && currentState == LOW) {
releasedTime = millis();
long pressDuration = releasedTime - pressedTime;
if( pressDuration < 1000 ) {
buttonConfig=buttonConfig+1;
if (buttonConfig>7) buttonConfig=0;
}
}
if (lastState == HIGH && currentState == HIGH){
long passedDuration = millis() - pressedTime;
if( passedDuration > 4000 ) {
// to do
// if (buttonConfig==4) {
// updateOLED2("Saved", "Release", "Button Now");
// delay(1000);
// updateOLED2("Starting", "CO2", "Calibration");
// delay(1000);
// Co2Calibration();
// } else {
updateOLED2("Saved", "Release", "Button Now");
delay(1000);
updateOLED2("Rebooting", "in", "5 seconds");
delay(5000);
EEPROM.write(addr, char(buttonConfig));
EEPROM.commit();
delay(1000);
ESP.restart();
// }
}
}
lastState = currentState;
delay(100);
inConf();
}
void setConfig() {
if (buttonConfig == 0) {
updateOLED2("Temp. in C", "PM in ug/m3", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = false;
inUSAQI = false;
} else if (buttonConfig == 1) {
updateOLED2("Temp. in C", "PM in US AQI", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = false;
inUSAQI = true;
} else if (buttonConfig == 2) {
updateOLED2("Temp. in F", "PM in ug/m3", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = true;
inUSAQI = false;
} else if (buttonConfig == 3) {
updateOLED2("Temp. in F", "PM in US AQI", "Display Top");
u8g2.setDisplayRotation(U8G2_R2);
inF = true;
inUSAQI = true;
} else if (buttonConfig == 4) {
updateOLED2("Temp. in C", "PM in ug/m3", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = false;
inUSAQI = false;
}
if (buttonConfig == 5) {
updateOLED2("Temp. in C", "PM in US AQI", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = false;
inUSAQI = true;
} else if (buttonConfig == 6) {
updateOLED2("Temp. in F", "PM in ug/m3", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = true;
inUSAQI = false;
} else if (buttonConfig == 7) {
updateOLED2("Temp. in F", "PM in US AQI", "Display Bottom");
u8g2.setDisplayRotation(U8G2_R0);
inF = true;
inUSAQI = true;
}
// to do
// if (buttonConfig == 8) {
// updateOLED2("CO2", "Manual", "Calibration");
// }
}
void updateTVOC()
{
uint16_t error;
char errorMessage[256];
uint16_t defaultRh = 0x8000;
uint16_t defaultT = 0x6666;
uint16_t srawVoc = 0;
uint16_t srawNox = 0;
uint16_t defaultCompenstaionRh = 0x8000; // in ticks as defined by SGP41
uint16_t defaultCompenstaionT = 0x6666; // in ticks as defined by SGP41
uint16_t compensationRh = 0; // in ticks as defined by SGP41
uint16_t compensationT = 0; // in ticks as defined by SGP41
delay(1000);
compensationT = static_cast<uint16_t>((temp + 45) * 65535 / 175);
compensationRh = static_cast<uint16_t>(hum * 65535 / 100);
if (conditioning_s > 0) {
error = sgp41.executeConditioning(compensationRh, compensationT, srawVoc);
conditioning_s--;
} else {
error = sgp41.measureRawSignals(compensationRh, compensationT, srawVoc,
srawNox);
}
if (currentMillis - previousTVOC >= tvocInterval) {
previousTVOC += tvocInterval;
TVOC = voc_algorithm.process(srawVoc);
NOX = nox_algorithm.process(srawNox);
Serial.println(String(TVOC));
}
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1;
if (inUSAQI) {
ln1 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " CO2:" + String(Co2);
} else {
ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
}
String ln2 = "TVOC:" + String(TVOC) + " NOX:" + String(NOX);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ (TVOC < 0 ? "" : ", \"tvoc_index\":" + String(TVOC))
+ (NOX < 0 ? "" : ", \"nox_index\":" + String(NOX))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("90s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(90);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,310 @@
/*
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller with the SGP40 TVOC module from AirGradient.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
"Sensirion I2C SGP41" by Sensation Version 0.1.0
"Sensirion Gas Index Algorithm" by Sensation Version 3.2.1
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
MIT License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//#include "SGP30.h"
#include <SensirionI2CSgp41.h>
#include <NOxGasIndexAlgorithm.h>
#include <VOCGasIndexAlgorithm.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
SensirionI2CSgp41 sgp41;
VOCGasIndexAlgorithm voc_algorithm;
NOxGasIndexAlgorithm nox_algorithm;
// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int tvocInterval = 1000;
unsigned long previousTVOC = 0;
int TVOC = 0;
int NOX = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
void setup()
{
Serial.begin(115200);
u8g2.setBusClock(100000);
u8g2.begin();
updateOLED();
if (connectWIFI) {
connectToWifi();
}
updateOLED2("Warming up the", "sensors.", "");
sgp41.begin(Wire);
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop()
{
currentMillis = millis();
updateTVOC();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void updateTVOC()
{
uint16_t error;
char errorMessage[256];
uint16_t defaultRh = 0x8000;
uint16_t defaultT = 0x6666;
uint16_t srawVoc = 0;
uint16_t srawNox = 0;
uint16_t defaultCompenstaionRh = 0x8000; // in ticks as defined by SGP41
uint16_t defaultCompenstaionT = 0x6666; // in ticks as defined by SGP41
uint16_t compensationRh = 0; // in ticks as defined by SGP41
uint16_t compensationT = 0; // in ticks as defined by SGP41
delay(1000);
compensationT = static_cast<uint16_t>((temp + 45) * 65535 / 175);
compensationRh = static_cast<uint16_t>(hum * 65535 / 100);
// 3. Measure SGP4x signals
if (conditioning_s > 0) {
// During NOx conditioning (10s) SRAW NOx will remain 0
error = sgp41.executeConditioning(compensationRh, compensationT, srawVoc);
conditioning_s--;
} else {
// Read Measurement
error = sgp41.measureRawSignals(compensationRh, compensationT, srawVoc,
srawNox);
}
if (error) {
Serial.print("Error trying to execute measureRawSignals(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("SRAW_VOC:");
Serial.print(srawVoc);
Serial.print("\t");
Serial.print("SRAW_NOx:");
Serial.println(srawNox);
}
if (currentMillis - previousTVOC >= tvocInterval) {
previousTVOC += tvocInterval;
TVOC = voc_algorithm.process(srawVoc);
NOX = nox_algorithm.process(srawNox);
// TVOC = sgp40.getVoclndex();
Serial.println(String(TVOC));
}
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
// String ln2 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " TVOC:" + String(TVOC);
String ln2 = "TVOC:" + String(TVOC) + " NOX:" + String(NOX);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ (TVOC < 0 ? "" : ", \"tvoc_index\":" + String(TVOC))
+ (NOX < 0 ? "" : ", \"nox_index\":" + String(NOX))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("60s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(60);
WiFiManagerParameter custom_text("<p>This is just a text paragraph</p>");
wifiManager.addParameter(&custom_text);
WiFiManagerParameter parameter("parameterId", "Parameter Label", "default value", 40);
wifiManager.addParameter(&parameter);
Serial.println("Parameter 1:");
Serial.println(parameter.getValue());
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
Serial.println("Parameter 2:");
Serial.println(parameter.getValue());
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,248 @@
/*
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
IMPORTANT: If you use SGP30 modules, you need to remove the Pull up resistors to make it work.
See https://www.airgradient.com/open-airgradient/instructions/tvoc-on-airgradient-diy-sensor/
If you want to use TVOC we strongly recommend you use the AirGradient SGP4x module available in our online shop.
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
“SGP30” by Rob Tilaart tested with Version 0.1.5
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include "SGP30.h"
#include <U8g2lib.h>
AirGradient ag = AirGradient();
SGP30 SGP;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int tvocInterval = 1000;
unsigned long previousTVOC = 0;
int TVOC = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
void setup()
{
Serial.begin(115200);
u8g2.setBusClock(100000);
u8g2.begin();
updateOLED();
if (connectWIFI) {
connectToWifi();
}
updateOLED2("Warming up the", "sensors.", "");
Serial.println(SGP.begin());
SGP.GenericReset();
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop()
{
currentMillis = millis();
updateTVOC();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void updateTVOC()
{
if (currentMillis - previousTVOC >= tvocInterval) {
previousTVOC += tvocInterval;
SGP.measure(true);
TVOC = SGP.getTVOC();
Serial.println(String(TVOC));
}
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
String ln2 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " TVOC:" + String(TVOC);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ (TVOC < 0 ? "" : ", \"tvoc\":" + String(TVOC))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("60s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,255 @@
/*
This is the code for the AirGradient DIY PRO Air Quality Sensor with an ESP8266 Microcontroller with the SGP40 TVOC module from AirGradient.
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
“U8g2” by oliver tested with version 2.32.15
“DFRobot_SGP40” by DFRobot tested with Version 1.0.3
Configuration:
Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
//#include "SGP30.h"
#include <DFRobot_SGP40.h>
#include <U8g2lib.h>
AirGradient ag = AirGradient();
DFRobot_SGP40 sgp40;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0;
const int oledInterval = 5000;
unsigned long previousOled = 0;
const int sendToServerInterval = 10000;
unsigned long previoussendToServer = 0;
const int tvocInterval = 1000;
unsigned long previousTVOC = 0;
int TVOC = 0;
const int co2Interval = 5000;
unsigned long previousCo2 = 0;
int Co2 = 0;
const int pm25Interval = 5000;
unsigned long previousPm25 = 0;
int pm25 = 0;
const int tempHumInterval = 2500;
unsigned long previousTempHum = 0;
float temp = 0;
int hum = 0;
void setup()
{
Serial.begin(115200);
u8g2.setBusClock(100000);
u8g2.begin();
updateOLED();
if (connectWIFI) {
connectToWifi();
}
updateOLED2("Warming up the", "sensors.", "");
sgp40.begin();
ag.CO2_Init();
ag.PMS_Init();
ag.TMP_RH_Init(0x44);
}
void loop()
{
currentMillis = millis();
updateTVOC();
updateOLED();
updateCo2();
updatePm25();
updateTempHum();
sendToServer();
}
void updateTVOC()
{
if (currentMillis - previousTVOC >= tvocInterval) {
previousTVOC += tvocInterval;
TVOC = sgp40.getVoclndex();
Serial.println(String(TVOC));
}
}
void updateCo2()
{
if (currentMillis - previousCo2 >= co2Interval) {
previousCo2 += co2Interval;
Co2 = ag.getCO2_Raw();
Serial.println(String(Co2));
}
}
void updatePm25()
{
if (currentMillis - previousPm25 >= pm25Interval) {
previousPm25 += pm25Interval;
pm25 = ag.getPM2_Raw();
Serial.println(String(pm25));
}
}
void updateTempHum()
{
if (currentMillis - previousTempHum >= tempHumInterval) {
previousTempHum += tempHumInterval;
TMP_RH result = ag.periodicFetchData();
temp = result.t;
hum = result.rh;
Serial.println(String(temp));
}
}
void updateOLED() {
if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval;
String ln3;
String ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
String ln2 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " TVOC:" + String(TVOC);
if (inF) {
ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else {
ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
}
}
void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9];
u8g2.firstPage();
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_t0_16_tf);
u8g2.drawStr(1, 10, String(ln1).c_str());
u8g2.drawStr(1, 30, String(ln2).c_str());
u8g2.drawStr(1, 50, String(ln3).c_str());
} while ( u8g2.nextPage() );
}
void sendToServer() {
if (currentMillis - previoussendToServer >= sendToServerInterval) {
previoussendToServer += sendToServerInterval;
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ (Co2 < 0 ? "" : ", \"rco2\":" + String(Co2))
+ (pm25 < 0 ? "" : ", \"pm02\":" + String(pm25))
+ (TVOC < 0 ? "" : ", \"tvoc_index\":" + String(TVOC))
+ ", \"atmp\":" + String(temp)
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}";
if(WiFi.status()== WL_CONNECTED){
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, POSTURL);
http.addHeader("content-type", "application/json");
int httpCode = http.POST(payload);
String response = http.getString();
Serial.println(httpCode);
Serial.println(response);
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
}
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("60s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(60);
WiFiManagerParameter custom_text("<p>This is just a text paragraph</p>");
wifiManager.addParameter(&custom_text);
WiFiManagerParameter parameter("parameterId", "Parameter Label", "default value", 40);
wifiManager.addParameter(&parameter);
Serial.println("Parameter 1:");
Serial.println(parameter.getValue());
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout");
delay(6000);
}
Serial.println("Parameter 2:");
Serial.println(parameter.getValue());
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -0,0 +1,380 @@
/*
This is the code for the AirGradient DIY Mini Display with an ESP8266 Microcontroller.
It can be configures to show the outside air quality as well as one indoor location from the AirGradient platform.
For build instructions please visit
https://www.airgradient.com/open-airgradient/blog/airgradient-diy-display/
The codes needs the following libraries installed:
"WifiManager by tzapu, tablatronix" tested with Version 2.0.5-alpha
"Adafruit_ILI9341" tested with Version 1.5.10
"Adafruit GFX library" tested with Version 1.10.12 (often automatically installed with above ILI9341 library)
"ArduinoJSON" by Benoit Blanchon tested with Version 5.13.5
Configuration:
Please set in the code below (line 90-) if you want to display the PM2.5 values in US AQI and temperature in F.
If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSans18pt7b.h>
#define TFT_CS D0
#define TFT_DC D8
#define TFT_RST - 1
#define TS_CS D3
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const char * locNameInside;
const char * locNameOutside;
const char * place_timezone;
const char * location;
bool outdoor_offline;
bool indoor_offline;
const char * outdoor_policy;
const char * outdoor_date;
const char * indoor_date;
boolean prodMode = true;
String deviceID;
const char * timex;
int pm02;
int pi02;
int pi02_outside;
int rco2;
float atmp;
float atmp_outside;
int rhum_outside;
int rhum;
int heat;
const char * pi02_color;
const char * pi02_color_outside;
const char * pi02_category;
const char * pm02_color;
const char * pm02_category;
const char * rco2_color;
const char * rco2_category;
const char * heat_color;
const char * heat_color_outside;
const char * heat_category;
// Configuration
#define API_ROOT "http://hw.airgradient.com/displays/"
boolean inUSaqi = false;
boolean inF = false;
String getDeviceId() {
return String(ESP.getChipId(), HEX);
}
void setup() {
Serial.begin(115200);
Serial.println("Chip ID");
Serial.println(String(ESP.getChipId(), HEX));
tft.begin();
tft.setRotation(2);
while (!Serial && (millis() <= 1000));
welcomeMessage();
connectToWifi();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
tft.fillScreen(ILI9341_BLACK);
delay(2000);
}
void loop() {
WiFiClient client;
HTTPClient http;
http.begin(client, API_ROOT + getDeviceId());
int httpCode = http.GET();
if (httpCode == 200) {
String airData = http.getString();
payloadToDataInside(airData);
Serial.print("airData1 : ");
Serial.println(airData);
} else {
Serial.println("error");
Serial.println(httpCode);
}
http.end();
delay(1000);
updateDisplay();
delay(120000);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setFont( & FreeSans12pt7b);
tft.setCursor(5, 20);
tft.println("requesting data...");
}
void payloadToDataInside(String payload) {
const size_t capacity = JSON_ARRAY_SIZE(1) + 2 * JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(10) + JSON_OBJECT_SIZE(13) + 530;
DynamicJsonBuffer jsonBuffer(capacity);
JsonObject & root = jsonBuffer.parseObject(payload);
location = root["place"]["name"];
place_timezone = root["place"]["timezone"];
JsonObject & outdoor = root["outdoor"];
locNameOutside = outdoor["name"];
outdoor_offline = outdoor["offline"];
outdoor_policy = outdoor["guidelines"][0]["title"];
JsonObject & outdoor_current = outdoor["current"];
atmp_outside = outdoor_current["atmp"];
rhum_outside = outdoor_current["rhum"];
outdoor_date = outdoor_current["date"];
JsonObject & indoor = root["indoor"];
locNameInside = indoor["name"];
indoor_offline = indoor["offline"];
JsonObject & indoor_current = indoor["current"];
atmp = indoor_current["atmp"];
rhum = indoor_current["rhum"];
rco2 = indoor_current["rco2"];
indoor_date = indoor_current["date"];
rco2_color = indoor_current["rco2_clr"];
rco2_category = indoor_current["rco2_lbl"];
if (inUSaqi) {
pi02_outside = outdoor_current["pi02"];
pi02_color_outside = outdoor_current["pi02_clr"];
pi02_category = outdoor_current["pi02_lbl"];
pi02 = indoor_current["pi02"];
pi02_color = indoor_current["pi02_clr"];
pi02_category = indoor_current["pi02_lbl"];
} else {
pi02_outside = outdoor_current["pm02"];
pi02_color_outside = outdoor_current["pm02_clr"];
pi02_category = outdoor_current["pm02_lbl"];
pi02 = indoor_current["pm02"];
pi02_color = indoor_current["pm02_clr"];
pi02_category = indoor_current["pm02_lbl"];
}
}
void updateDisplay() {
int y = 25;
int boxHeight = 75;
int boxWidth = 110;
int radius = 8;
tft.fillScreen(ILI9341_BLACK);
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(5, y);
tft.println(location);
tft.drawLine(0, 35, 250, 35, ILI9341_WHITE);
y = y + 50;
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(5, y);
tft.println(locNameOutside);
tft.setFont( & FreeSans12pt7b);
y = y + 12;
if (String(pi02_color_outside) == "green") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_GREEN);
} else if (String(pi02_color_outside) == "yellow") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_YELLOW);
} else if (String(pi02_color_outside) == "orange") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_ORANGE);
} else if (String(pi02_color_outside) == "red") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_RED);
} else if (String(pi02_color_outside) == "purple") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_PURPLE);
} else if (String(pi02_color_outside) == "brown") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_MAROON);
}
if (String(heat_color_outside) == "green") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_GREEN);
} else if (String(heat_color_outside) == "yellow") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_YELLOW);
} else if (String(heat_color_outside) == "orange") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_ORANGE);
} else if (String(heat_color_outside) == "red") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_RED);
} else if (String(heat_color_outside) == "purple") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_PURPLE);
} else if (String(heat_color_outside) == "brown") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_MAROON);
}
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_BLACK, ILI9341_BLACK);
tft.setCursor(20, y + boxHeight - 10);
if (inUSaqi) {
tft.println("US AQI");
} else {
tft.println("ug/m3");
}
tft.setFont( & FreeSans18pt7b);
tft.setTextColor(ILI9341_BLACK, ILI9341_BLACK);
tft.setCursor(20, y + 40);
tft.println(String(pi02_outside));
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(20 + boxWidth + 10, y + 20);
if (inF) {
tft.println(String((atmp_outside * 9 / 5) + 32) + "F");
} else {
tft.println(String(atmp_outside) + "C");
}
tft.setCursor(20 + boxWidth + 10, y + 40);
tft.println(String(rhum_outside) + "%");
tft.setTextColor(ILI9341_DARKGREY, ILI9341_BLACK);
tft.setCursor(20 + boxWidth + 10, y + 60);
tft.println(String(outdoor_date));
//inside
y = y + 110;
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(5, y);
tft.println(locNameInside);
tft.setFont( & FreeSans12pt7b);
y = y + 12;
if (String(pi02_color) == "green") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_GREEN);
} else if (String(pi02_color) == "yellow") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_YELLOW);
} else if (String(pi02_color) == "orange") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_ORANGE);
} else if (String(pi02_color) == "red") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_RED);
} else if (String(pi02_color) == "purple") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_PURPLE);
} else if (String(pi02_color) == "brown") {
tft.fillRoundRect(5, y, boxWidth, boxHeight, radius, ILI9341_MAROON);
}
if (String(rco2_color) == "green") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_GREEN);
} else if (String(rco2_color) == "yellow") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_YELLOW);
} else if (String(rco2_color) == "orange") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_ORANGE);
} else if (String(rco2_color) == "red") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_RED);
} else if (String(rco2_color) == "purple") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_PURPLE);
} else if (String(rco2_color) == "brown") {
tft.fillRoundRect(5 + boxWidth + 10, y, boxWidth, boxHeight, radius, ILI9341_MAROON);
}
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_BLACK, ILI9341_BLACK);
tft.setCursor(20, y + boxHeight - 10);
if (inUSaqi) {
tft.println("US AQI");
} else {
tft.println("ug/m3");
}
tft.setCursor(20 + boxWidth + 10, y + boxHeight - 10);
tft.println("CO2 ppm");
tft.setFont( & FreeSans18pt7b);
tft.setTextColor(ILI9341_BLACK, ILI9341_BLACK);
tft.setCursor(20, y + 40);
tft.println(String(pi02));
tft.setCursor(20 + boxWidth + 10, y + 40);
tft.println(String(rco2));
y = y + 100;
tft.setFont( & FreeSans9pt7b);
tft.setTextColor(ILI9341_DARKGREY, ILI9341_BLACK);
tft.setCursor(boxWidth - 30, y);
tft.println(String(indoor_date));
}
void welcomeMessage() {
Serial.println("Welcome Message 2");
tft.setFont( & FreeSans9pt7b);
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(40, 24);
tft.setFont( & FreeSans12pt7b);
tft.setCursor(5, 20);
tft.println("AirGradient");
tft.setFont( & FreeSans9pt7b);
tft.setCursor(5, 100);
tft.println("id: " + String(ESP.getChipId(), HEX));
tft.setCursor(5, 140);
tft.println("connecting ...");
delay(2000);
}
void connectToWifi() {
delay(2000);
WiFiManager wifiManager;
//chWiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AIRGRADIENT-DISPLAY-" + String(ESP.getChipId(), HEX);
wifiManager.setTimeout(120);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
Serial.println("failed to connect and hit timeout");
delay(3000);
ESP.restart();
delay(5000);
}
}

View File

@ -3,32 +3,52 @@ This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Micr
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
For build instructions please visit https://www.airgradient.com/diy/
For build instructions please visit https://www.airgradient.com/open-airgradient/instructions/
Compatible with the following sensors:
Plantower PMS5003 (Fine Particle Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/schools/
If you have any questions please visit our forum at https://forum.airgradient.com/
MIT License
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/
Kits with all required components are available at https://www.airgradient.com/open-airgradient/shop/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
AirGradient ag = AirGradient();
void setup(){
Serial.begin(9600);
void setup() {
Serial.begin(115200);
ag.PMS_Init();
}
void loop(){
int PM2 = ag.getPM2_Raw();
Serial.print("PM2: ");
Serial.println(ag.getPM2());
void loop() {
delay(5000);
int PM2 = ag.getPM2_Raw();
Serial.print("PM2.5 in ug/m3: ");
Serial.println(String(PM2));
Serial.print("PM2.5 in US AQI: ");
Serial.println(String(PM_TO_AQI_US(PM2)));
delay(5000);
}
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -3,7 +3,7 @@ This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Micr
It is a high quality sensor showing PM2.5, CO2, Temperature and Humidity on a small display and can send data over Wifi.
For build instructions please visit https://www.airgradient.com/diy/
For build instructions please visit https://www.airgradient.com/open-airgradient/instructions/
Compatible with the following sensors:
SHT30/31 (Temperature/Humidity Sensor)
@ -11,24 +11,35 @@ SHT30/31 (Temperature/Humidity Sensor)
Please install ESP8266 board manager (tested with version 3.0.0)
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/schools/
https://www.airgradient.com/
MIT License
Kits with all required components are available at https://www.airgradient.com/open-airgradient/shop/
If you have any questions please visit our forum at https://forum.airgradient.com/
CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/
#include <AirGradient.h>
AirGradient ag = AirGradient();
void setup(){
Serial.begin(9600);
Serial.begin(115200);
ag.TMP_RH_Init(0x44); //check for SHT sensor with address 0x44
}
void loop(){
TMP_RH result = ag.periodicFetchData();
Serial.print("Humidity: ");
Serial.print(result.rh_char);
Serial.print(" Temperature: ");
Serial.println(result.t_char);
Serial.print("Relative Humidity in %: ");
Serial.println(result.rh);
Serial.print(" Temperature in Celcius: ");
Serial.println(result.t);
Serial.print(" Temperature in Fahrenheit: ");
Serial.println((result.t * 9 / 5) + 32);
delay(5000);
}

View File

@ -1,9 +1,9 @@
name=AirGradient Air Quality Sensor
version=1.3.5
version=2.4.1
author=AirGradient <support@airgradient.com>
maintainer=AirGradient <support@airgradient.com>
sentence=ESP8266 library for an air quality sensor featuring PM2.5, CO2, Temperature and Humidity with OLED display.
paragraph=The library is very robust and works with the Plantower PMS5003 particle sensor, the Senseair S8 CO2 sensor and the SHT30/31 sensor for humidity and temperature. You can also connect an OLED display or send the air quality data to the AirGradient platform or any other backend.
sentence=ESP8266 library for an air quality sensor featuring PM2.5, CO2, Temperature, TVOC and Humidity with OLED display.
paragraph=Air quality monitoring library supporting the Plantower PMS5003 particle sensor, the Senseair S8 CO2 sensor and the SHT30/31 sensor for humidity and temperature. Kits with all components including a nice enclosure are available in our online shop. You can also connect an OLED display or send the air quality data to the AirGradient platform or any other backend. Optionally you can connect the Sensirion SGP4x TVOC module from AirGradient.
category=Sensors
url=https://github.com/airgradienthq/arduino
url=https://www.airgradient.com/open-airgradient/instructions/
architectures=*

View File

@ -5,4 +5,4 @@ Build your own low cost air quality sensor with optional display measuring PM2.5
This library makes it easy to read the sensor data from the Plantower PMS5003 PM2.5 sensor, the Senseair S8 and the SHT30/31 Temperature and Humidity sensor. Visit our DIY section for detailed build instructions and PCB layout.
https://www.airgradient.com/diy/
https://www.airgradient.com/open-airgradient/instructions/