Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Latest version GOOD 09/11/2024
- Updated for Blynk 10/11/2024
- For VB 6 digits sddddd s=p/h/t d=data
- 'A = AQI Not used, calculated internally with VB program
- 'p = PM2.5
- 'h = HUMIDITY
- 't = TEMPERATURE
- */
- #define BLYNK_TEMPLATE_ID "TMPL6lkSTd5p_"
- #define BLYNK_TEMPLATE_NAME "Enviornment"
- #define BLYNK_AUTH_TOKEN "***********************"
- #define BLYNK_FIRMWARE_VERSION "1.2.0"
- #define BLYNK_PRINT Serial
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.h>
- char auth[] = BLYNK_AUTH_TOKEN;
- char ssid[] = "***************"; // type your WiFi SSID
- char pass[] = "*********"; // type your WiFi password
- BlynkTimer timer;
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SH110X.h>
- #include <Adafruit_Sensor.h>
- #define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // QT-PY / XIAO
- Adafruit_SH1106G oled = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #include <DHT.h>
- #define DHTPIN 23 // pin for Dustation Devkit v1.4
- float t;
- float h;
- float heatindex;
- #include "PMS.h"
- PMS pms(Serial2);
- PMS::DATA data;
- float pm2;
- float aqidata;
- //#define DHTTYPE DHT11 // DHT 11
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- #define I2C_ADDRESS 0x3C
- DHT dht(DHTPIN, DHTTYPE);
- void setup() {
- Blynk.connectWiFi(ssid, pass);
- Blynk.begin(auth, ssid, pass, "blynk.cloud",80);
- Blynk.config(auth);
- Blynk.connect();
- timer.setInterval(1000L, send2blynk);
- Serial2.begin(9600);
- dht.begin();
- Wire.begin();
- Wire.setClock(400000L);
- oled.begin(I2C_ADDRESS,true);
- oled.setCursor(0,0); //(col,row)
- oled.clearDisplay();
- oled.display();
- delay(2000);
- oled.clearDisplay();
- oled.setTextSize(1.5);
- oled.setTextColor(SH110X_WHITE);
- }
- void loop() {
- Blynk.run();
- int AQI;
- String state;
- if (pms.read(data))
- {
- pm2 = data.PM_AE_UG_2_5;
- am2302();
- oledupdate();
- getAQI(pm2, &AQI, &state);
- }
- }
- void am2302() {
- //read temperature and humidity
- t = dht.readTemperature();
- h = dht.readHumidity();
- heatindex = dht.computeHeatIndex(t, h, false);
- if (isnan(h) || isnan(t)) {
- }
- }
- void oledupdate() {
- delay(1000);
- oled.clearDisplay();
- // display temperature
- oled.setCursor(1,0); //(col,row)
- oled.print(t);
- oled.print("C");
- oled.setCursor(1,12); //(col,row)
- oled.print(h);
- oled.print("%");
- oled.setCursor(1,22); //(col,row)
- oled.print("PM2.5 = ");
- int temppm2 = pm2;
- oled.print(temppm2);
- }
- void getAQI(float pm25, int* aqi, String* state){
- int aqitemp;
- String statetemp;
- // aqi = Ilow + (C-Clow)*(Ihigh-Ilow)/(Chigh-Clow) with I=aqi and C=concentration
- if(pm25 < 12){
- *aqi = 0 + (pm25-0)*(50.0-0)/(12-0);
- *state = "GOOD";
- aqitemp = *aqi;
- statetemp = *state;
- }else if(pm25 < 35){
- *aqi = 51 + (pm25-13)*(100.0-51)/(35-13);
- *state = "MODERATE";
- aqitemp = *aqi;
- statetemp = *state;
- }else if(pm25 < 55){
- *aqi = 101 + (pm25-36)*(150.0-101)/(55-36);
- *state = "UNHEALTHY SENS";
- aqitemp = *aqi;
- statetemp = *state;
- }else if(pm25 < 150){
- *aqi = 151 + (pm25-56)*(200.0-151)/(150-56);
- *state = "UNHEALTHY";
- aqitemp = *aqi;
- statetemp = *state;
- }else if(pm25 < 250){
- *aqi = 201 + (pm25-151)*(300.0-201)/(250-151);
- *state = "VERY UNHEALTHY";
- aqitemp = *aqi;
- statetemp = *state;
- }else{
- *aqi = 300 + (pm25-250)*(500.0-300)/(500-250);
- *state = "HAZARDOUS";
- aqitemp = *aqi;
- statetemp = *state;
- }
- aqidata=aqitemp;
- oled.setCursor(1,32); //(col,row)
- oled.print("AQI = ");
- oled.print(aqitemp);
- oled.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' text
- oled.setTextSize(1.7);
- oled.setCursor(1,52);
- oled.print(statetemp);
- oled.display();
- oled.setTextColor(SH110X_WHITE);
- oled.setTextSize(1.5);
- send2pc();
- send2blynk();
- }
- void send2blynk(){
- Blynk.virtualWrite(V0, t);
- Blynk.virtualWrite(V1, h);
- Blynk.virtualWrite(V2, pm2);
- Blynk.virtualWrite(V3, aqidata);
- Blynk.virtualWrite(V4, heatindex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement