Advertisement
Tywais

PMS7003 Dustation Dev Kit V1_4 SH1106 VB

May 16th, 2025 (edited)
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 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 "template"
  12. #define BLYNK_TEMPLATE_NAME "Enviornment"
  13. #define BLYNK_AUTH_TOKEN "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[] = "ssid";  // type your wifi name
  22. char pass[] = "password";  // 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. //   Serial.begin(38400);
  65.    dht.begin();
  66.    Wire.begin();
  67.    Wire.setClock(400000L);
  68.    oled.begin(I2C_ADDRESS,true);
  69.    oled.setCursor(0,0); //(col,row)
  70.    oled.clearDisplay();
  71.    oled.display();
  72.  
  73.    delay(2000);
  74.    oled.clearDisplay();
  75.    oled.setTextSize(1.5);
  76.    oled.setTextColor(SH110X_WHITE);
  77. }
  78.  
  79. void loop() {
  80.   Blynk.run();
  81.  
  82.   int AQI;
  83.   String state;
  84.  
  85.   if (pms.read(data))
  86.   {
  87.     pm2 = data.PM_AE_UG_2_5;
  88.  
  89.     am2302();
  90.    
  91.     oledupdate();
  92.     getAQI(pm2, &AQI, &state);
  93.    
  94.   }
  95.   //send2pc();  
  96. }
  97.  
  98.   void am2302() {
  99.  
  100.   //read temperature and humidity
  101.   t = dht.readTemperature();
  102.   h = dht.readHumidity();
  103.   heatindex = dht.computeHeatIndex(t, h, false);
  104.   if (isnan(h) || isnan(t)) {
  105.     //Serial.println("AM2302 Failed");
  106.     // Compute heat index in Celsius (isFahreheit = false)
  107.  
  108.   }
  109. }
  110.  
  111.   void oledupdate() {
  112.   delay(1000);
  113.   oled.clearDisplay();
  114.  
  115.   // display temperature
  116.    oled.setCursor(1,0); //(col,row)
  117.    oled.print(t);
  118.    oled.print("C");
  119.    //oled.display();
  120.  
  121.  
  122.    oled.setCursor(1,12); //(col,row)
  123.    oled.print(h);
  124.    oled.print("%");
  125.    //oled.display();
  126.  
  127.    oled.setCursor(1,22); //(col,row)
  128.    oled.print("PM2.5 = ");
  129.    int temppm2 = pm2;
  130.    oled.print(temppm2);
  131.    //oled.display();
  132. }
  133.  
  134. void getAQI(float pm25, int* aqi, String* state){
  135.   int aqitemp;
  136.   String statetemp;
  137.  
  138.   // aqi = Ilow + (C-Clow)*(Ihigh-Ilow)/(Chigh-Clow) with I=aqi and C=concentration
  139.   if(pm25 < 12){
  140.       *aqi = 0 + (pm25-0)*(50.0-0)/(12-0);
  141.       *state = "GOOD";
  142.       aqitemp = *aqi;
  143.       statetemp = *state;
  144.   }else if(pm25 < 35){
  145.       *aqi = 51 + (pm25-13)*(100.0-51)/(35-13);
  146.       *state = "MODERATE";
  147.       aqitemp = *aqi;
  148.       statetemp = *state;
  149.   }else if(pm25 < 55){
  150.       *aqi = 101 + (pm25-36)*(150.0-101)/(55-36);
  151.       *state = "UNHEALTHY SENS";  
  152.        aqitemp = *aqi;
  153.        statetemp = *state;
  154.  }else if(pm25 < 150){
  155.       *aqi = 151 + (pm25-56)*(200.0-151)/(150-56);
  156.       *state = "UNHEALTHY";
  157.       aqitemp = *aqi;
  158.       statetemp = *state;
  159.   }else if(pm25 < 250){
  160.       *aqi = 201 + (pm25-151)*(300.0-201)/(250-151);
  161.       *state = "VERY UNHEALTHY";
  162.       aqitemp = *aqi;
  163.       statetemp = *state;
  164.   }else{
  165.       *aqi = 300 + (pm25-250)*(500.0-300)/(500-250);
  166.       *state = "HAZARDOUS";
  167.       aqitemp = *aqi;
  168.       statetemp = *state;
  169.   }
  170.    aqidata=aqitemp;
  171.    oled.setCursor(1,32); //(col,row)
  172.    oled.print("AQI = ");
  173.    oled.print(aqitemp);
  174.    
  175.    //Serial.print("AQI = ");
  176.    //Serial.println(pm2);  //testing
  177.    
  178.    oled.setTextColor(SH110X_BLACK, SH110X_WHITE); // 'inverted' text
  179.    oled.setTextSize(1.7);
  180.    oled.setCursor(1,52);
  181.    oled.print(statetemp);
  182.    oled.display();
  183.    oled.setTextColor(SH110X_WHITE);
  184.    oled.setTextSize(1.5);
  185.    
  186.    send2pc();  
  187.    send2blynk();
  188. }
  189.  
  190. void send2pc() {
  191. /* Send to PC all values  pm2.5, temperature, humidity
  192.    Serial.print("Sp");
  193.    Serial.printf("%00.2f",pm2);
  194.    
  195.    Serial.print("St");
  196.    Serial.printf("%00.2f",t);
  197.  
  198.    Serial.print("Sh");
  199.    Serial.printf("%00.2f",h);
  200.    */
  201. }  
  202.  
  203. void send2blynk(){
  204.     Blynk.virtualWrite(V0, t);
  205.     Blynk.virtualWrite(V1, h);  
  206.     Blynk.virtualWrite(V2, pm2);  
  207.     Blynk.virtualWrite(V3, aqidata);  
  208.     Blynk.virtualWrite(V4, heatindex);  
  209.      
  210. }
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement