Advertisement
Tywais

Blynk Enviornment Station

Mar 18th, 2025 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.42 KB | Source Code | 0 0
  1. /*
  2. Latest version GOOD 09/11/2024
  3. Updated for Blynk 10/11/2024
  4.  
  5. For VB 6 digits sddddd s=p/h/t d=data
  6. 'A = AQI Not used, calculated internally with VB program
  7. 'p = PM2.5
  8. 'h = HUMIDITY
  9. 't = TEMPERATURE
  10. */
  11. #define BLYNK_TEMPLATE_ID "TMPL6lkSTd5p_"
  12. #define BLYNK_TEMPLATE_NAME "Enviornment"
  13. #define BLYNK_AUTH_TOKEN "***********************"
  14. #define BLYNK_FIRMWARE_VERSION        "1.2.0"
  15.  
  16. #define BLYNK_PRINT Serial
  17. #include <WiFi.h>
  18. #include <WiFiClient.h>
  19. #include <BlynkSimpleEsp32.h>
  20. char auth[] = BLYNK_AUTH_TOKEN;
  21. char ssid[] = "***************";  // type your WiFi SSID
  22. char pass[] = "*********";  // type your WiFi password
  23. BlynkTimer timer;
  24.  
  25. #include <Wire.h>
  26. #include <Adafruit_GFX.h>
  27. #include <Adafruit_SH110X.h>
  28. #include <Adafruit_Sensor.h>
  29.  
  30. #define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
  31.  
  32. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  33. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  34. #define OLED_RESET -1   //   QT-PY / XIAO
  35. Adafruit_SH1106G oled = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  36.  
  37. #include <DHT.h>
  38. #define DHTPIN 23  // pin for Dustation Devkit v1.4
  39. float t;
  40. float h;
  41. float heatindex;
  42.  
  43. #include "PMS.h"
  44. PMS pms(Serial2);
  45. PMS::DATA data;
  46. float pm2;
  47. float aqidata;
  48.  
  49. //#define DHTTYPE    DHT11     // DHT 11
  50. #define DHTTYPE      DHT22     // DHT 22 (AM2302)
  51. //#define DHTTYPE    DHT21     // DHT 21 (AM2301)
  52. #define I2C_ADDRESS 0x3C
  53.  
  54. DHT dht(DHTPIN, DHTTYPE);
  55.  
  56. void setup() {
  57.    Blynk.connectWiFi(ssid, pass);
  58.    Blynk.begin(auth, ssid, pass, "blynk.cloud",80);
  59.    Blynk.config(auth);
  60.    Blynk.connect();
  61.    timer.setInterval(1000L, send2blynk);
  62.  
  63.    Serial2.begin(9600);
  64.    dht.begin();
  65.    Wire.begin();
  66.    Wire.setClock(400000L);
  67.    oled.begin(I2C_ADDRESS,true);
  68.    oled.setCursor(0,0); //(col,row)
  69.    oled.clearDisplay();
  70.    oled.display();
  71.  
  72.    delay(2000);
  73.    oled.clearDisplay();
  74.    oled.setTextSize(1.5);
  75.    oled.setTextColor(SH110X_WHITE);
  76. }
  77.  
  78. void loop() {
  79.   Blynk.run();
  80.  
  81.   int AQI;
  82.   String state;
  83.  
  84.   if (pms.read(data))
  85.   {
  86.     pm2 = data.PM_AE_UG_2_5;
  87.  
  88.     am2302();
  89.    
  90.     oledupdate();
  91.     getAQI(pm2, &AQI, &state);
  92.    
  93.   }
  94. }
  95.  
  96.   void am2302() {
  97.  
  98.   //read temperature and humidity
  99.   t = dht.readTemperature();
  100.   h = dht.readHumidity();
  101.   heatindex = dht.computeHeatIndex(t, h, false);
  102.   if (isnan(h) || isnan(t)) {
  103.   }
  104. }
  105.  
  106.   void oledupdate() {
  107.   delay(1000);
  108.   oled.clearDisplay();
  109.  
  110.   // display temperature
  111.    oled.setCursor(1,0); //(col,row)
  112.    oled.print(t);
  113.    oled.print("C");
  114.  
  115.    oled.setCursor(1,12); //(col,row)
  116.    oled.print(h);
  117.    oled.print("%");
  118.  
  119.    oled.setCursor(1,22); //(col,row)
  120.    oled.print("PM2.5 = ");
  121.    int temppm2 = pm2;
  122.    oled.print(temppm2);
  123. }
  124.  
  125. void getAQI(float pm25, int* aqi, String* state){
  126.   int aqitemp;
  127.   String statetemp;
  128.  
  129.   // aqi = Ilow + (C-Clow)*(Ihigh-Ilow)/(Chigh-Clow) with I=aqi and C=concentration
  130.   if(pm25 < 12){
  131.       *aqi = 0 + (pm25-0)*(50.0-0)/(12-0);
  132.       *state = "GOOD";
  133.       aqitemp = *aqi;
  134.       statetemp = *state;
  135.   }else if(pm25 < 35){
  136.       *aqi = 51 + (pm25-13)*(100.0-51)/(35-13);
  137.       *state = "MODERATE";
  138.       aqitemp = *aqi;
  139.       statetemp = *state;
  140.   }else if(pm25 < 55){
  141.       *aqi = 101 + (pm25-36)*(150.0-101)/(55-36);
  142.       *state = "UNHEALTHY SENS";  
  143.        aqitemp = *aqi;
  144.        statetemp = *state;
  145.  }else if(pm25 < 150){
  146.       *aqi = 151 + (pm25-56)*(200.0-151)/(150-56);
  147.       *state = "UNHEALTHY";
  148.       aqitemp = *aqi;
  149.       statetemp = *state;
  150.   }else if(pm25 < 250){
  151.       *aqi = 201 + (pm25-151)*(300.0-201)/(250-151);
  152.       *state = "VERY UNHEALTHY";
  153.       aqitemp = *aqi;
  154.       statetemp = *state;
  155.   }else{
  156.       *aqi = 300 + (pm25-250)*(500.0-300)/(500-250);
  157.       *state = "HAZARDOUS";
  158.       aqitemp = *aqi;
  159.       statetemp = *state;
  160.   }
  161.    aqidata=aqitemp;
  162.    oled.setCursor(1,32); //(col,row)
  163.    oled.print("AQI = ");
  164.    oled.print(aqitemp);
  165.      
  166.    oled.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' text
  167.    oled.setTextSize(1.7);
  168.    oled.setCursor(1,52);
  169.    oled.print(statetemp);
  170.    oled.display();
  171.    oled.setTextColor(SH110X_WHITE);
  172.    oled.setTextSize(1.5);
  173.    
  174.    send2pc();  
  175.    send2blynk();
  176. }
  177.  
  178. void send2blynk(){
  179.     Blynk.virtualWrite(V0, t);
  180.     Blynk.virtualWrite(V1, h);  
  181.     Blynk.virtualWrite(V2, pm2);  
  182.     Blynk.virtualWrite(V3, aqidata);  
  183.     Blynk.virtualWrite(V4, heatindex);  
  184.      
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement