2021-06-08 08:33:51 +07:00
|
|
|
/*
|
|
|
|
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)
|
|
|
|
|
|
|
|
Please install ESP8266 board manager (tested with version 3.0.0)
|
|
|
|
|
2021-12-08 09:59:32 +07:00
|
|
|
If you have any questions please visit our forum at https://forum.airgradient.com/
|
|
|
|
|
2021-06-08 08:33:51 +07:00
|
|
|
If you are a school or university contact us for a free trial on the AirGradient platform.
|
|
|
|
https://www.airgradient.com/schools/
|
|
|
|
|
2021-12-08 09:59:32 +07:00
|
|
|
Kits with all required components are available at https://www.airgradient.com/diyshop/
|
|
|
|
|
2021-06-08 08:33:51 +07:00
|
|
|
MIT License
|
|
|
|
*/
|
|
|
|
|
2020-07-24 21:52:28 +08:00
|
|
|
#include <AirGradient.h>
|
2021-12-08 09:59:32 +07:00
|
|
|
|
2020-07-24 21:52:28 +08:00
|
|
|
AirGradient ag = AirGradient();
|
|
|
|
|
2021-12-08 09:59:32 +07:00
|
|
|
void setup() {
|
2020-07-24 21:52:28 +08:00
|
|
|
Serial.begin(9600);
|
|
|
|
ag.PMS_Init();
|
|
|
|
}
|
|
|
|
|
2021-12-08 09:59:32 +07:00
|
|
|
void loop() {
|
|
|
|
|
|
|
|
int PM2 = ag.getPM2_Raw();
|
2020-07-24 21:52:28 +08:00
|
|
|
|
2021-12-08 09:59:32 +07:00
|
|
|
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);
|
2020-07-25 14:28:18 +08:00
|
|
|
}
|
2021-12-08 09:59:32 +07:00
|
|
|
|
|
|
|
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;
|
|
|
|
};
|