Compare commits

...

2 Commits
2.4.2 ... 2.4.3

Author SHA1 Message Date
37d1140eda Updated library version 2023-04-14 12:29:43 +07:00
63e1bcdc30 Updated outdoor C3 example to send channels as sub objects 2023-04-14 11:51:12 +07:00
2 changed files with 152 additions and 117 deletions

View File

@ -11,6 +11,8 @@ The codes needs the following libraries installed:
For built instructions and how to patch the PMS library: https://www.airgradient.com/open-airgradient/instructions/diy-open-air-presoldered-v11/
Note that below code only works with both PM sensor modules connected.
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
@ -30,36 +32,122 @@ HTTPClient client;
PMS pms1(Serial0);
PMS::DATA data1;
float pm1Value01=0;
float pm1Value25=0;
float pm1Value10=0;
float pm1PCount=0;
float pm1temp = 0;
float pm1hum = 0;
PMS pms2(Serial1);
PMS::DATA data2;
float pm1Value=0;
int pm1Position = 0;
float pm2Value01=0;
float pm2Value25=0;
float pm2Value10=0;
float pm2PCount=0;
float pm2temp = 0;
float pm2hum = 0;
float pm2Value=0;
int pm2Position = 0;
int countPosition = 0;
int targetCount = 20;
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/";
bool ledOn = false;
int loopCount = 0;
int averageCount = 10;
void IRAM_ATTR isr() {
debugln("pushed");
}
// select board LOLIN C3 mini to flash
void setup() {
if (DEBUG) {
Serial.begin(115200);
// see https://github.com/espressif/arduino-esp32/issues/6983
Serial.setTxTimeoutMs(0); // <<<====== solves the delay issue
}
debug("starting ...");
debug("Serial Number: "+ getNormalizedMac());
// default hardware serial, PMS connector on the right side of the C3 mini on the Open Air
Serial0.begin(9600);
// second hardware serial, PMS connector on the left side of the C3 mini on the Open Air
Serial1.begin(9600, SERIAL_8N1, 0, 1);
// led
pinMode(10, OUTPUT);
// push button
pinMode(9, INPUT_PULLUP);
attachInterrupt(9, isr, FALLING);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// give the PMSs some time to start
countdown(3);
connectToWifi();
sendPing();
switchLED(false);
}
void loop() {
if(WiFi.status()== WL_CONNECTED) {
if (pms1.readUntil(data1, 2000) && pms2.readUntil(data2, 2000)) {
pm1Value01=pm1Value01+data1.PM_AE_UG_1_0;
pm1Value25=pm1Value25+data1.PM_AE_UG_2_5;
pm1Value10=pm1Value10+data1.PM_AE_UG_10_0;
pm1PCount=pm1PCount+data1.PM_RAW_0_3;
pm1temp=pm1temp+data1.AMB_TMP;
pm1hum=pm1hum+data1.AMB_HUM;
pm2Value01=pm2Value01+data2.PM_AE_UG_1_0;
pm2Value25=pm2Value25+data2.PM_AE_UG_2_5;
pm2Value10=pm2Value10+data2.PM_AE_UG_10_0;
pm2PCount=pm2PCount+data2.PM_RAW_0_3;
pm2temp=pm2temp+data2.AMB_TMP;
pm2hum=pm2hum+data2.AMB_HUM;
countPosition++;
if (countPosition==targetCount) {
pm1Value01 = pm1Value01 / targetCount;
pm1Value25 = pm1Value25 / targetCount;
pm1Value10 = pm1Value10 / targetCount;
pm1PCount = pm1PCount / targetCount;
pm1temp = pm1temp / targetCount;
pm1hum = pm1hum / targetCount;
pm2Value01 = pm2Value01 / targetCount;
pm2Value25 = pm2Value25 / targetCount;
pm2Value10 = pm2Value10 / targetCount;
pm2PCount = pm2PCount / targetCount;
pm2temp = pm2temp / targetCount;
pm2hum = pm2hum / targetCount;
postToServer(pm1Value01, pm1Value25,pm1Value10,pm1PCount, pm1temp,pm1hum,pm2Value01, pm2Value25,pm2Value10,pm2PCount, pm2temp,pm2hum);
countPosition=0;
pm1Value01=0;
pm1Value25=0;
pm1Value10=0;
pm1PCount=0;
pm1temp=0;
pm1hum=0;
pm2Value01=0;
pm2Value25=0;
pm2Value10=0;
pm2PCount=0;
pm2temp=0;
pm2hum=0;
}
}
}
countdown(2);
}
void debug(String msg) {
if (DEBUG)
@ -83,27 +171,54 @@ void debugln(int msg) {
void switchLED(boolean ledON) {
if (ledON) {
digitalWrite(10, HIGH);
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
digitalWrite(10, LOW);
}
}
void IRAM_ATTR isr() {
debugln("pushed");
void sendPing(){
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ ", \"boot\":" + loopCount
+ "}";
sendPayload(payload);
}
void postToServer(String postfix, int pm25,float tem, float hum) {
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ ", \"pm02\":" + String(pm25)
+ ", \"atmp\":" + String(tem/10)
+ ", \"rhum\":" + String(hum/10)
+ ", \"boot\":" + loopCount
void postToServer(int pm1Value01, int pm1Value25, int pm1Value10, int pm1PCount, float pm1temp, float pm1hum,int pm2Value01, int pm2Value25, int pm2Value10, int pm2PCount, float pm2temp, float pm2hum) {
String payload = "{\"wifi\":" + String(WiFi.RSSI())
+ ", \"pm01\":" + String((pm1Value01+pm2Value01)/2)
+ ", \"pm02\":" + String((pm1Value25+pm2Value25)/2)
+ ", \"pm10\":" + String((pm1Value10+pm2Value10)/2)
+ ", \"pm003_count\":" + String((pm1PCount+pm2PCount)/2)
+ ", \"atmp\":" + String((pm1temp+pm2temp)/20)
+ ", \"rhum\":" + String((pm1hum+pm2hum)/20)
+ ", \"boot\":" + loopCount
+ ", \"channels\": {"
+ "\"1\":{"
+ "\"pm01\":" + String(pm1Value01)
+ ", \"pm02\":" + String(pm1Value25)
+ ", \"pm10\":" + String(pm1Value10)
+ ", \"pm003_count\":" + String(pm1PCount)
+ ", \"atmp\":" + String(pm1temp/10)
+ ", \"rhum\":" + String(pm1hum/10)
+ "}"
+ ", \"2\":{"
+ " \"pm01\":" + String(pm1Value01)
+ ", \"pm02\":" + String(pm2Value25)
+ ", \"pm10\":" + String(pm2Value10)
+ ", \"pm003_count\":" + String(pm2PCount)
+ ", \"atmp\":" + String(pm2temp/10)
+ ", \"rhum\":" + String(pm2hum/10)
+ "}"
+ "}"
+ "}";
if(WiFi.status()== WL_CONNECTED){
sendPayload(payload);
}
void sendPayload(String payload) {
if(WiFi.status()== WL_CONNECTED){
switchLED(true);
String url = APIROOT + "sensors/airgradient:" + getNormalizedMac() + postfix + "/measures";
String url = APIROOT + "sensors/airgradient:" + getNormalizedMac() + "/measures";
debugln(url);
debugln(payload);
client.setConnectTimeout(5 * 1000);
@ -117,7 +232,7 @@ void postToServer(String postfix, int pm25,float tem, float hum) {
}
else {
debug("post skipped, not network connection");
}
}
loopCount++;
}
@ -131,92 +246,12 @@ void countdown(int from) {
debug("\n");
}
// select board LOLIN C3 mini to flash
void setup() {
if (DEBUG) {
Serial.begin(115200);
// see https://github.com/espressif/arduino-esp32/issues/6983
Serial.setTxTimeoutMs(0); // <<<====== solves the delay issue
}
debug("starting ...");
// default hardware serial, PMS connector on the right side of the C3 mini on the Open Air
Serial0.begin(9600);
// second hardware serial, PMS connector on the left side of the C3 mini on the Open Air
Serial1.begin(9600, SERIAL_8N1, 0, 1);
// led
pinMode(10, OUTPUT);
// push button
pinMode(9, INPUT_PULLUP);
attachInterrupt(9, isr, FALLING);
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
// give the PMSs some time to start
countdown(3);
connectToWifi();
switchLED(false);
}
void resetWatchdog() {
digitalWrite(2, HIGH);
digitalWrite(2, HIGH);
delay(20);
digitalWrite(2, LOW);
}
void loop() {
if(WiFi.status()== WL_CONNECTED) {
if (pms1.readUntil(data1, 2000)) {
pm1Value=pm1Value+data1.PM_AE_UG_2_5;
temp_pm1=temp_pm1+data1.AMB_TMP;
hum_pm1=hum_pm1+data1.AMB_HUM;
debugln("PMS 1 measurement "+String(pm1Position)+": "+String(data1.PM_AE_UG_2_5)+"ug/m3");
pm1Position++;
if (pm1Position==averageCount) {
pm1Value = pm1Value / averageCount;
temp_pm1 = temp_pm1 / averageCount;
hum_pm1 = hum_pm1 / averageCount;
postToServer("-1", pm1Value, temp_pm1,hum_pm1);
pm1Position=0;
pm1Value=0;
}
} else {
debugln("PMS 1 does't respond");
}
if (pms2.readUntil(data2, 2000)) {
pm2Value=pm2Value+data2.PM_AE_UG_2_5;
temp_pm2=temp_pm2+data2.AMB_TMP;
hum_pm2=hum_pm2+data2.AMB_HUM;
debugln("PMS 2 measurement "+String(pm2Position)+": "+String(data2.PM_AE_UG_2_5)+"ug/m3");
pm2Position++;
if (pm2Position==averageCount) {
pm2Value = pm2Value / averageCount;
temp_pm2 = temp_pm2 / averageCount;
hum_pm2 = hum_pm2 / averageCount;
postToServer("-2", pm2Value, temp_pm2, hum_pm2);
pm2Position=0;
pm2Value=0;
}
} else {
debugln("PMS 2 does't respond");
}
}
countdown(2);
}
// Wifi Manager
void connectToWifi() {
WiFiManager wifiManager;
@ -224,7 +259,7 @@ countdown(2);
//WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(getNormalizedMac());
wifiManager.setTimeout(180);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
switchLED(false);

View File

@ -1,5 +1,5 @@
name=AirGradient Air Quality Sensor
version=2.4.2
version=2.4.3
author=AirGradient <support@airgradient.com>
maintainer=AirGradient <support@airgradient.com>
sentence=ESP8266 library for an air quality sensor featuring PM2.5, CO2, Temperature, TVOC and Humidity with OLED display.