Improved example code DIY Basic

This commit is contained in:
Achim
2022-11-06 19:21:28 +07:00
parent 724d041684
commit 98ec0c4d10

View File

@ -9,7 +9,7 @@ Kits (including a pre-soldered version) are available: https://www.airgradient.c
The codes needs the following libraries installed: The codes needs the following libraries installed:
“WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta “WifiManager by tzapu, tablatronix” tested with version 2.0.11-beta
"ESP8266 and ESP32 OLED driver for SSD1306 displays by ThingPulse, Fabrice Weinberg" tested with Version 4.1.0 “U8g2” by oliver tested with version 2.32.15
Configuration: Configuration:
Please set in the code below the configuration parameters. Please set in the code below the configuration parameters.
@ -29,26 +29,30 @@ MIT License
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <Wire.h> #include <U8g2lib.h>
#include "SSD1306Wire.h"
AirGradient ag = AirGradient(); AirGradient ag = AirGradient();
SSD1306Wire display(0x3c, SDA, SCL); U8G2_SSD1306_64X48_ER_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); //for DIY BASIC
// CONFIGURATION START // CONFIGURATION START
// set to true to switch PM2.5 from ug/m3 to US AQI //set to the endpoint you would like to use
boolean inUSaqi = true; String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit // set to true to switch from Celcius to Fahrenheit
boolean inF = false; boolean inF = false;
// set to true if you want to connect to wifi. The display will show values only when the sensor has wifi connection // PM2.5 in US AQI (default ug/m3)
boolean inUSAQI = false;
// set to true if you want to connect to wifi. You have 60 seconds to connect. Then it will go into an offline mode.
boolean connectWIFI=true; boolean connectWIFI=true;
// CONFIGURATION END // CONFIGURATION END
unsigned long currentMillis = 0; unsigned long currentMillis = 0;
const int oledInterval = 5000; const int oledInterval = 5000;
@ -69,24 +73,20 @@ const int tempHumInterval = 2500;
unsigned long previousTempHum = 0; unsigned long previousTempHum = 0;
float temp = 0; float temp = 0;
int hum = 0; int hum = 0;
int displaypage = 0; long val;
String APIROOT = "http://hw.airgradient.com/";
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
display.init(); u8g2.begin();
display.flipScreenVertically(); updateOLED();
if (connectWIFI) { if (connectWIFI) {
connectToWifi(); connectToWifi();
} }
showTextRectangle("Init", String(ESP.getChipId(), HEX), true); updateOLED2("Warming", "up the", "sensors");
ag.CO2_Init(); ag.CO2_Init();
ag.PMS_Init(); ag.PMS_Init();
@ -137,46 +137,38 @@ void updateOLED() {
if (currentMillis - previousOled >= oledInterval) { if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval; previousOled += oledInterval;
switch (displaypage) { String ln1;
case 0: String ln2;
if (inUSaqi) { String ln3;
showTextRectangle("AQI", String(PM_TO_AQI_US(pm25)), false);
} else {
showTextRectangle("PM2", String(pm25), false); if (inUSAQI){
} ln1 = "AQI:" + String(PM_TO_AQI_US(pm25)) ;
displaypage = 1; } else {
break; ln1 = "PM: " + String(pm25) +"ug" ;
case 1:
showTextRectangle("CO2", String(Co2), false);
displaypage = 2;
break;
case 2:
if (inF) {
showTextRectangle("F", String((temp * 9 / 5) + 32), false);
} else {
showTextRectangle("C", String(temp), false);
}
displaypage = 3;
break;
case 3:
showTextRectangle("Hum", String(hum)+"%", false);
displaypage = 0;
break;
} }
ln2 = "CO2:" + String(Co2);
if (inF) {
ln3 = String((temp* 9 / 5) + 32).substring(0,4) + " " + String(hum)+"%";
} else {
ln3 = String(temp).substring(0,4) + " " + String(hum)+"%";
}
updateOLED2(ln1, ln2, ln3);
} }
} }
void showTextRectangle(String ln1, String ln2, boolean small) { void updateOLED2(String ln1, String ln2, String ln3) {
display.clear(); char buf[9];
display.setTextAlignment(TEXT_ALIGN_LEFT); u8g2.firstPage();
if (small) { u8g2.firstPage();
display.setFont(ArialMT_Plain_16); do {
} else { u8g2.setFont(u8g2_font_t0_16_tf);
display.setFont(ArialMT_Plain_24); u8g2.drawStr(1, 10, String(ln1).c_str());
} u8g2.drawStr(1, 28, String(ln2).c_str());
display.drawString(32, 16, ln1); u8g2.drawStr(1, 46, String(ln3).c_str());
display.drawString(32, 38, ln2); } while ( u8g2.nextPage() );
display.display();
} }
void sendToServer() { void sendToServer() {
@ -190,7 +182,6 @@ void sendToServer() {
+ (hum < 0 ? "" : ", \"rhum\":" + String(hum)) + (hum < 0 ? "" : ", \"rhum\":" + String(hum))
+ "}"; + "}";
if(WiFi.status()== WL_CONNECTED){ if(WiFi.status()== WL_CONNECTED){
Serial.println(payload); Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures"; String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
@ -215,10 +206,12 @@ void sendToServer() {
void connectToWifi() { void connectToWifi() {
WiFiManager wifiManager; WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot //WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AIRGRADIENT-" + String(ESP.getChipId(), HEX); String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("Connect", "Wifi", HOTSPOT);
delay(2000);
wifiManager.setTimeout(60); wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) { if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
showTextRectangle("offline", "mode", true); updateOLED2("Booting", "offline", "mode");
Serial.println("failed to connect and hit timeout"); Serial.println("failed to connect and hit timeout");
delay(6000); delay(6000);
} }