From 0753a4c9dd57d2d23f978b34b1cf7f787ea52571 Mon Sep 17 00:00:00 2001 From: achim Date: Fri, 14 Aug 2020 16:25:09 +0700 Subject: [PATCH] Updated example code --- examples/C02_PM_SHT/C02_PM_SHT.ino | 26 ------ examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino | 46 ---------- .../C02_PM_SHT_OLED_WIFI.ino | 90 +++++++++++++++---- examples/PM_OLED_WIFI/PM_OLED_WIFI.ino | 78 ---------------- library.properties | 2 +- 5 files changed, 73 insertions(+), 169 deletions(-) delete mode 100644 examples/C02_PM_SHT/C02_PM_SHT.ino delete mode 100644 examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino delete mode 100644 examples/PM_OLED_WIFI/PM_OLED_WIFI.ino diff --git a/examples/C02_PM_SHT/C02_PM_SHT.ino b/examples/C02_PM_SHT/C02_PM_SHT.ino deleted file mode 100644 index c3c7f143..00000000 --- a/examples/C02_PM_SHT/C02_PM_SHT.ino +++ /dev/null @@ -1,26 +0,0 @@ -#include -AirGradient ag = AirGradient(); - -void setup(){ - Serial.begin(9600); - ag.PMS_Init(); - ag.CO2_Init(); - ag.TMP_RH_Init(0x44); //check for SHT sensor with address 0x44 -} - -void loop(){ - -Serial.print("PM2: "); -Serial.println(ag.getPM2()); - - -Serial.print("CO2: "); -Serial.println(ag.getCO2()); - -TMP_RH result = ag.periodicFetchData(); -Serial.print("Humidity: "); -Serial.print(result.rh); -Serial.print(" Temperature: "); -Serial.println(result.t); -delay(5000); -} diff --git a/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino b/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino deleted file mode 100644 index 79ec2839..00000000 --- a/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino +++ /dev/null @@ -1,46 +0,0 @@ -#include -#include -#include "SSD1306Wire.h" - -AirGradient ag = AirGradient(); - -SSD1306Wire display(0x3c, SDA, SCL); - -void setup(){ - Serial.begin(9600); - display.init(); - display.flipScreenVertically(); - showTextRectangle("Init", String(ESP.getChipId(),HEX),true); - - ag.PMS_Init(); - ag.CO2_Init(); - ag.TMP_RH_Init(0x44); - - delay(2000); -} - -void loop(){ - int PM2 = ag.getPM2_Raw(); - int CO2 = ag.getCO2_Raw(); - TMP_RH result = ag.periodicFetchData(); - showTextRectangle(String(result.t),String(result.rh)+"%",false); - delay(2000); - showTextRectangle("PM2",String(PM2),false); - delay(2000); - showTextRectangle("CO2",String(CO2),false); - delay(2000); -} - -// 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, 12, ln1); - display.drawString(32, 36, ln2); - display.display(); -} diff --git a/examples/C02_PM_SHT_OLED_WIFI/C02_PM_SHT_OLED_WIFI.ino b/examples/C02_PM_SHT_OLED_WIFI/C02_PM_SHT_OLED_WIFI.ino index 4b33bbf5..71cfdf21 100644 --- a/examples/C02_PM_SHT_OLED_WIFI/C02_PM_SHT_OLED_WIFI.ino +++ b/examples/C02_PM_SHT_OLED_WIFI/C02_PM_SHT_OLED_WIFI.ino @@ -1,3 +1,30 @@ +/* +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) + +Dependent Libraries: +The codes needs the following libraries installed: +ESP8266 board with standard libraries +WifiManager by tzar, 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 #include #include @@ -10,6 +37,15 @@ 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(){ @@ -19,28 +55,47 @@ void setup(){ display.flipScreenVertically(); showTextRectangle("Init", String(ESP.getChipId(),HEX),true); - ag.PMS_Init(); - ag.CO2_Init(); - ag.TMP_RH_Init(0x44); + if (hasPM) ag.PMS_Init(); + if (hasCO2) ag.CO2_Init(); + if (hasSHT) ag.TMP_RH_Init(0x44); - connectToWifi(); + if (connectWIFI) connectToWifi(); delay(2000); } void loop(){ - int PM2 = ag.getPM2_Raw(); - int CO2 = ag.getCO2_Raw(); - TMP_RH result = ag.periodicFetchData(); - showTextRectangle(String(result.t),String(result.rh)+"%",false); - delay(2000); - showTextRectangle("PM2",String(PM2),false); - delay(2000); - showTextRectangle("CO2",String(CO2),false); - delay(2000); + // 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 - String payload = "{\"pm02\":" + String(ag.getPM2()) + ",\"wifi\":" + String(WiFi.RSSI()) + ",\"rco2\":" + String(ag.getCO2()) + ",\"atmp\":" + String(result.t) + ",\"rhum\":" + String(result.rh) + "}"; + if (connectWIFI){ Serial.println(payload); String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(),HEX) + "/measures"; Serial.println(POSTURL); @@ -52,8 +107,7 @@ void loop(){ Serial.println(httpCode); Serial.println(response); http.end(); - - delay(2000); + } } // DISPLAY @@ -65,7 +119,7 @@ void showTextRectangle(String ln1, String ln2, boolean small) { } else { display.setFont(ArialMT_Plain_24); } - display.drawString(32, 12, ln1); + display.drawString(32, 16, ln1); display.drawString(32, 36, ln2); display.display(); } @@ -77,7 +131,7 @@ void connectToWifi(){ 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"); + Serial.println("failed to connect and hit timeout"); delay(3000); ESP.restart(); delay(5000); diff --git a/examples/PM_OLED_WIFI/PM_OLED_WIFI.ino b/examples/PM_OLED_WIFI/PM_OLED_WIFI.ino deleted file mode 100644 index 671d88f0..00000000 --- a/examples/PM_OLED_WIFI/PM_OLED_WIFI.ino +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include - -#include -#include "SSD1306Wire.h" - -AirGradient ag = AirGradient(); - -SSD1306Wire display(0x3c, SDA, SCL); - -String APIROOT = "http://hw.airgradient.com/"; - -void setup(){ - Serial.begin(9600); - - display.init(); - display.flipScreenVertically(); - showTextRectangle("Init", String(ESP.getChipId(),HEX),true); - - ag.PMS_Init(); - - connectToWifi(); - delay(2000); -} - -void loop(){ - int PM = ag.getPM2_Raw(); - showTextRectangle("PM2",String(PM),false); - - // send payload - String payload = "{\"pm02\":" + String(PM) + ",\"wifi\":" + String(WiFi.RSSI()) + "}"; - Serial.println(payload); - String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(),HEX) + "/measures"; - Serial.println(POSTURL); - HTTPClient http; - http.begin(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(30000); -} - -// 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, 12, 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); - } - -} diff --git a/library.properties b/library.properties index ede9c510..bac7c832 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=AirGradient Air Quality Sensor -version=1.3.3 +version=1.3.4 author=AirGradient maintainer=AirGradient sentence=ESP8266 library for an air quality sensor featuring PM2.5, CO2, Temperature and Humidity with OLED display.