From bb3c57297ea4ffc2aa527da9df8bc4a6145d66e2 Mon Sep 17 00:00:00 2001 From: achim Date: Thu, 13 Aug 2020 08:55:04 +0700 Subject: [PATCH] Refactored OLED code --- examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino | 43 +++++++------- .../C02_PM_SHT_OLED_WIFI.ino | 57 ++++++++++--------- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino b/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino index 1aa47f9..8a8e74f 100644 --- a/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino +++ b/examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino @@ -1,18 +1,21 @@ #include -#include -#include +#include +#include "SSD1306Wire.h" AirGradient ag = AirGradient(); -#define OLED_RESET 0 -Adafruit_SSD1306 display(OLED_RESET); + +SSD1306Wire display(0x3c, SDA, SCL); void setup(){ Serial.begin(9600); - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); + display.init(); + display.flipScreenVertically(); + showTextRectangle("Init", String(ESP.getChipId(),HEX)); + ag.PMS_Init(); ag.CO2_Init(); - ag.TMP_RH_Init(0x44); //check for SHT sensor with address 0x44 - showTextRectangle("Init", String(ESP.getChipId(),HEX),"AirGradient"); + ag.TMP_RH_Init(0x44); + delay(2000); } @@ -20,22 +23,20 @@ void loop(){ int PM2 = ag.getPM2_Raw(); int CO2 = ag.getCO2_Raw(); TMP_RH result = ag.periodicFetchData(); - showTextRectangle(String(result.t)+"c "+String(result.rh)+"%", "PM2: "+ String(ag.getPM2()), "CO2: "+String(ag.getCO2())+""); - delay(5000); + showTextRectangle(String(result.t),String(result.rh)+"%"); + delay(2000); + showTextRectangle("PM2",String(PM2)); + delay(2000); + showTextRectangle("CO2",String(CO2)); + delay(2000); } // DISPLAY -void showTextRectangle(String ln1, String ln2, String ln3) { - display.clearDisplay(); - display.setTextColor(WHITE); - display.setTextSize(1); - display.setCursor(32,8); - display.println(ln1); - display.setTextSize(1); - display.setCursor(32,16); - display.println(ln2); - display.setTextSize(1); - display.setCursor(32,24); - display.println(ln3); +void showTextRectangle(String ln1, String ln2) { + display.clear(); + display.setTextAlignment(TEXT_ALIGN_LEFT); + 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 a30bd58..20d9dd5 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,23 +1,28 @@ #include -#include -#include #include #include #include +#include +#include "SSD1306Wire.h" + AirGradient ag = AirGradient(); -#define OLED_RESET 0 -Adafruit_SSD1306 display(OLED_RESET); + +SSD1306Wire display(0x3c, SDA, SCL); String APIROOT = "http://hw.airgradient.com/"; void setup(){ Serial.begin(9600); - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); + + display.init(); + display.flipScreenVertically(); + showTextRectangle("Init", String(ESP.getChipId(),HEX)); + ag.PMS_Init(); ag.CO2_Init(); - ag.TMP_RH_Init(0x44); //check for SHT sensor with address 0x44 - showTextRectangle("Init", String(ESP.getChipId(),HEX),"AirGradient"); + ag.TMP_RH_Init(0x44); + connectToWifi(); delay(2000); } @@ -26,8 +31,14 @@ void loop(){ int PM2 = ag.getPM2_Raw(); int CO2 = ag.getCO2_Raw(); TMP_RH result = ag.periodicFetchData(); - showTextRectangle(String(result.t)+"c "+String(result.rh)+"%", "PM2: "+ String(PM2), "CO2: "+String(CO2)+""); - + + showTextRectangle(String(result.t),String(result.rh)+"%"); + delay(2000); + showTextRectangle("PM2",String(PM2)); + delay(2000); + showTextRectangle("CO2",String(CO2)); + delay(2000); + // send payload String payload = "{\"pm02\":" + String(ag.getPM2()) + ",\"wifi\":" + String(WiFi.RSSI()) + ",\"rco2\":" + String(ag.getCO2()) + ",\"atmp\":" + String(result.t) + ",\"rhum\":" + String(result.rh) + "}"; Serial.println(payload); @@ -41,30 +52,24 @@ void loop(){ Serial.println(httpCode); Serial.println(response); http.end(); - - delay(15000); + + delay(2000); } // DISPLAY -void showTextRectangle(String ln1, String ln2, String ln3) { - display.clearDisplay(); - display.setTextColor(WHITE); - display.setTextSize(1); - display.setCursor(32,8); - display.println(ln1); - display.setTextSize(1); - display.setCursor(32,16); - display.println(ln2); - display.setTextSize(1); - display.setCursor(32,24); - display.println(ln3); +void showTextRectangle(String ln1, String ln2) { + display.clear(); + display.setTextAlignment(TEXT_ALIGN_LEFT); + display.setFont(ArialMT_Plain_24); + display.drawString(32, 12, ln1); + display.drawString(32, 36, ln2); display.display(); } // Wifi Manager void connectToWifi(){ WiFiManager wifiManager; - //chWiFi.disconnect(); //to delete previous saved hotspot + //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())) { @@ -72,6 +77,6 @@ void connectToWifi(){ delay(3000); ESP.restart(); delay(5000); - } - + } + }