added 90 degree rotation code

This commit is contained in:
achim
2022-07-29 11:11:39 +02:00
parent 4bb434562f
commit b9a968ef7a
4 changed files with 62 additions and 241 deletions

View File

@ -1,182 +0,0 @@
/*
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)
Please install ESP8266 board manager (tested with version 3.0.0)
The codes needs the following libraries installed:
"WifiManager by tzapu, 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
If you have any questions please visit our forum at https://forum.airgradient.com/
Configuration:
Please set in the code below which sensor you are using and if you want to connect it to WiFi.
You can also switch PM2.5 from ug/m3 to US AQI and Celcius to Fahrenheit
If you are a school or university contact us for a free trial on the AirGradient platform.
https://www.airgradient.com/schools/
Kits with all required components are available at https://www.airgradient.com/diyshop/
MIT License
*/
#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);
// set sensors that you do not use to false
boolean hasPM = true;
boolean hasCO2 = true;
boolean hasSHT = true;
// set to true to switch PM2.5 from ug/m3 to US AQI
boolean inUSaqi = false;
// set to true to switch from Celcius to Fahrenheit
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
boolean connectWIFI = false;
// change if you want to send the data to another server
String APIROOT = "http://hw.airgradient.com/";
void setup() {
Serial.begin(9600);
display.init();
display.flipScreenVertically();
showTextRectangle("Init", String(ESP.getChipId(), HEX), true);
if (hasPM) ag.PMS_Init();
if (hasCO2) ag.CO2_Init();
if (hasSHT) ag.TMP_RH_Init(0x44);
if (connectWIFI) connectToWifi();
delay(2000);
}
void loop() {
// create payload
String payload = "{\"wifi\":" + String(WiFi.RSSI()) + ",";
if (hasPM) {
int PM2 = ag.getPM2_Raw();
payload = payload + "\"pm02\":" + String(PM2);
if (inUSaqi) {
showTextRectangle("AQI", String(PM_TO_AQI_US(PM2)), false);
} else {
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);
if (inF) {
showTextRectangle(String((result.t * 9 / 5) + 32), String(result.rh) + "%", false);
} else {
showTextRectangle(String(result.t), String(result.rh) + "%", false);
}
delay(3000);
}
payload = payload + "}";
// send payload
if (connectWIFI) {
Serial.println(payload);
String POSTURL = APIROOT + "sensors/airgradient:" + String(ESP.getChipId(), HEX) + "/measures";
Serial.println(POSTURL);
WiFiClient client;
HTTPClient http;
http.begin(client, 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(21000);
}
}
// 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, 16, 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);
}
}
// Calculate PM2.5 US AQI
int PM_TO_AQI_US(int pm02) {
if (pm02 <= 12.0) return ((50 - 0) / (12.0 - .0) * (pm02 - .0) + 0);
else if (pm02 <= 35.4) return ((100 - 50) / (35.4 - 12.0) * (pm02 - 12.0) + 50);
else if (pm02 <= 55.4) return ((150 - 100) / (55.4 - 35.4) * (pm02 - 35.4) + 100);
else if (pm02 <= 150.4) return ((200 - 150) / (150.4 - 55.4) * (pm02 - 55.4) + 150);
else if (pm02 <= 250.4) return ((300 - 200) / (250.4 - 150.4) * (pm02 - 150.4) + 200);
else if (pm02 <= 350.4) return ((400 - 300) / (350.4 - 250.4) * (pm02 - 250.4) + 300);
else if (pm02 <= 500.4) return ((500 - 400) / (500.4 - 350.4) * (pm02 - 350.4) + 400);
else return 500;
};

View File

@ -1,7 +1,11 @@
/* /*
This is the code for the AirGradient DIY CO2 Traffic light with an ESP8266 Microcontroller. This is the code for the AirGradient DIY CO2 Traffic light with an ESP8266 Microcontroller.
For build instructions please visit https://www.airgradient.com/diy-co2-traffic-light/ For build instructions please visit:
https://www.airgradient.com/open-airgradient/instructions/diy-co2-traffic-light/
Kits (including a pre-soldered version) are available:
https://www.airgradient.com/open-airgradient/kits/
Compatible with the following sensors: Compatible with the following sensors:
SenseAir S8 (CO2 Sensor) SenseAir S8 (CO2 Sensor)
@ -12,8 +16,6 @@ Please install the following libraries:
"Adafruit NeoMatrix" Library (tested with 1.2.0) "Adafruit NeoMatrix" Library (tested with 1.2.0)
"WifiManager by tzapu, tablatronix" tested with Version 2.0.3-alpha "WifiManager by tzapu, tablatronix" tested with Version 2.0.3-alpha
Kits with all required components are available at https://www.airgradient.com/diyshop/
If you have any questions please visit our forum at https://forum.airgradient.com/ If you have any questions please visit our forum at https://forum.airgradient.com/
If you are a school or university contact us for a free trial on the AirGradient platform. If you are a school or university contact us for a free trial on the AirGradient platform.
@ -22,11 +24,6 @@ https://www.airgradient.com/schools/
MIT License MIT License
*/ */
// Adafruit_NeoMatrix example for single NeoPixel Shield.
// Scrolls 'Howdy' across the matrix in a portrait (vertical) orientation.
// Adafruit_NeoMatrix example for single NeoPixel Shield.
// Scrolls 'Howdy' across the matrix in a portrait (vertical) orientation.
#include <AirGradient.h> #include <AirGradient.h>
#include <WiFiManager.h> #include <WiFiManager.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>

View File

@ -1,16 +1,18 @@
/* /*
This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Microcontroller. This is the code for the AirGradient DIY BASIC 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. 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/ Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy/
Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
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 which sensor you are using and if you want to connect it to WiFi. Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/ If you have any questions please visit our forum at https://forum.airgradient.com/
@ -34,6 +36,19 @@ AirGradient ag = AirGradient();
SSD1306Wire display(0x3c, SDA, SCL); SSD1306Wire display(0x3c, SDA, SCL);
// CONFIGURATION START
// set to true to switch PM2.5 from ug/m3 to US AQI
boolean inUSaqi = true;
// set to true to switch from Celcius to Fahrenheit
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
boolean connectWIFI=true;
// CONFIGURATION END
unsigned long currentMillis = 0; unsigned long currentMillis = 0;
const int oledInterval = 5000; const int oledInterval = 5000;
@ -58,14 +73,6 @@ int displaypage = 0;
String APIROOT = "http://hw.airgradient.com/"; String APIROOT = "http://hw.airgradient.com/";
// set to true to switch PM2.5 from ug/m3 to US AQI
boolean inUSaqi = true;
// set to true to switch from Celcius to Fahrenheit
boolean inF = 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=true;
void setup() void setup()
@ -207,12 +214,11 @@ void sendToServer() {
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 = "AIRGRADIENT-" + String(ESP.getChipId(), HEX);
wifiManager.setTimeout(120); wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) { if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
showTextRectangle("offline", "mode", true);
Serial.println("failed to connect and hit timeout"); Serial.println("failed to connect and hit timeout");
delay(3000); delay(6000);
ESP.restart();
delay(5000);
} }
} }

View File

@ -1,13 +1,11 @@
/* /*
This is the code for the AirGradient DIY Air Quality Sensor with an ESP8266 Microcontroller. This is the code for the AirGradient DIY PRO 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. 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/ Build Instructions: https://www.airgradient.com/open-airgradient/instructions/diy-pro/
Instructions on using the TVOC sensor (SGP30) instead of the Temperature / Humidity sensor (SHT3x). Kits (including a pre-soldered version) are available: https://www.airgradient.com/open-airgradient/kits/
https://www.airgradient.com/resources/tvoc-on-airgradient-diy-sensor/
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
@ -15,7 +13,7 @@ The codes needs the following libraries installed:
“SGP30” by Rob Tilaart tested with Version 0.1.5 “SGP30” by Rob Tilaart tested with Version 0.1.5
Configuration: Configuration:
Please set in the code below which sensor you are using and if you want to connect it to WiFi. Please set in the code below the configuration parameters.
If you have any questions please visit our forum at https://forum.airgradient.com/ If you have any questions please visit our forum at https://forum.airgradient.com/
@ -38,8 +36,28 @@ MIT License
AirGradient ag = AirGradient(); AirGradient ag = AirGradient();
SGP30 SGP; SGP30 SGP;
// Display bottom right
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
// Replace above if you have display on top left
//U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R3, /* reset=*/ U8X8_PIN_NONE);
// CONFIGURATION START
//set to the endpoint you would like to use
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch from Celcius to Fahrenheit
boolean inF = 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;
// CONFIGURATION END
unsigned long currentMillis = 0; unsigned long currentMillis = 0;
const int oledInterval = 5000; const int oledInterval = 5000;
@ -65,18 +83,6 @@ unsigned long previousTempHum = 0;
float temp = 0; float temp = 0;
int hum = 0; int hum = 0;
String APIROOT = "http://hw.airgradient.com/";
// set to true to switch PM2.5 from ug/m3 to US AQI
boolean inUSaqi = true;
// set to true to switch from Celcius to Fahrenheit
boolean inF = 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=true;
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
@ -152,28 +158,23 @@ void updateTempHum()
void updateOLED() { void updateOLED() {
if (currentMillis - previousOled >= oledInterval) { if (currentMillis - previousOled >= oledInterval) {
previousOled += oledInterval; previousOled += oledInterval;
String ln1;
String ln2; String ln3;
if (inUSaqi) { String ln1 = "PM:" + String(pm25) + " CO2:" + String(Co2);
ln1 = "CO2:" + String(Co2) + " AQI:" + String(PM_TO_AQI_US(pm25)); String ln2 = "AQI:" + String(PM_TO_AQI_US(pm25)) + " TVOC:" + String(TVOC);
} else {
ln1 = "CO2:" + String(Co2) + " PM:" + String(pm25);
}
if (inF) { if (inF) {
ln2 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%"; ln3 = "F:" + String((temp* 9 / 5) + 32) + " H:" + String(hum)+"%";
} else { } else {
ln2 = "C:" + String(temp) + " H:" + String(hum)+"%"; ln3 = "C:" + String(temp) + " H:" + String(hum)+"%";
} }
String ln3 = "TVOC:" + String(TVOC) ;
updateOLED2(ln1, ln2, ln3); updateOLED2(ln1, ln2, ln3);
} }
} }
void updateOLED2(String ln1, String ln2, String ln3) { void updateOLED2(String ln1, String ln2, String ln3) {
char buf[9]; char buf[9];
u8g2.firstPage(); u8g2.firstPage();
u8g2.firstPage(); u8g2.firstPage();
do { do {
u8g2.setFont(u8g2_font_t0_16_tf); u8g2.setFont(u8g2_font_t0_16_tf);
@ -219,13 +220,12 @@ void sendToServer() {
WiFiManager wifiManager; WiFiManager wifiManager;
//WiFi.disconnect(); //to delete previous saved hotspot //WiFi.disconnect(); //to delete previous saved hotspot
String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX); String HOTSPOT = "AG-" + String(ESP.getChipId(), HEX);
updateOLED2("To setup connect", "to Wifi Hotspot", HOTSPOT); updateOLED2("60s to connect", "to Wifi Hotspot", HOTSPOT);
wifiManager.setTimeout(120); wifiManager.setTimeout(60);
if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) { if (!wifiManager.autoConnect((const char * ) HOTSPOT.c_str())) {
updateOLED2("booting into", "offline mode", "");
Serial.println("failed to connect and hit timeout"); Serial.println("failed to connect and hit timeout");
delay(3000); delay(6000);
ESP.restart();
delay(5000);
} }
} }