Skip to content

Commit

Permalink
Added Example for OLED and Wifi. Improved OLED code.
Browse files Browse the repository at this point in the history
  • Loading branch information
achim committed Aug 13, 2020
1 parent 16a29b4 commit f170404
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
16 changes: 10 additions & 6 deletions examples/C02_PM_SHT_OLED/C02_PM_SHT_OLED.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void setup(){
Serial.begin(9600);
display.init();
display.flipScreenVertically();
showTextRectangle("Init", String(ESP.getChipId(),HEX));
showTextRectangle("Init", String(ESP.getChipId(),HEX),true);

ag.PMS_Init();
ag.CO2_Init();
Expand All @@ -23,19 +23,23 @@ void loop(){
int PM2 = ag.getPM2_Raw();
int CO2 = ag.getCO2_Raw();
TMP_RH result = ag.periodicFetchData();
showTextRectangle(String(result.t),String(result.rh)+"%");
showTextRectangle(String(result.t),String(result.rh)+"%",false);
delay(2000);
showTextRectangle("PM2",String(PM2));
showTextRectangle("PM2",String(PM2),false);
delay(2000);
showTextRectangle("CO2",String(CO2));
showTextRectangle("CO2",String(CO2),false);
delay(2000);
}

// DISPLAY
void showTextRectangle(String ln1, String ln2) {
void showTextRectangle(String ln1, String ln2, boolean small) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
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();
Expand Down
16 changes: 10 additions & 6 deletions examples/C02_PM_SHT_OLED_WIFI/C02_PM_SHT_OLED_WIFI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void setup(){

display.init();
display.flipScreenVertically();
showTextRectangle("Init", String(ESP.getChipId(),HEX));
showTextRectangle("Init", String(ESP.getChipId(),HEX),true);

ag.PMS_Init();
ag.CO2_Init();
Expand All @@ -32,11 +32,11 @@ void loop(){
int CO2 = ag.getCO2_Raw();
TMP_RH result = ag.periodicFetchData();

showTextRectangle(String(result.t),String(result.rh)+"%");
showTextRectangle(String(result.t),String(result.rh)+"%",false);
delay(2000);
showTextRectangle("PM2",String(PM2));
showTextRectangle("PM2",String(PM2),false);
delay(2000);
showTextRectangle("CO2",String(CO2));
showTextRectangle("CO2",String(CO2),false);
delay(2000);

// send payload
Expand All @@ -57,10 +57,14 @@ void loop(){
}

// DISPLAY
void showTextRectangle(String ln1, String ln2) {
void showTextRectangle(String ln1, String ln2, boolean small) {
display.clear();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
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();
Expand Down
78 changes: 78 additions & 0 deletions examples/PM_OLED_WIFI/PM_OLED_WIFI.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#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);

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);
}

}

0 comments on commit f170404

Please sign in to comment.