Advertisement
Tywais

Enviornment Monitor

Nov 25th, 2023
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.58 KB | None | 0 0
  1. /* OK Standalone April 16, 2023 */
  2.  
  3. #include "Free_Fonts.h"
  4. #include "Adafruit_PM25AQI.h"
  5. int pm25value = 0;
  6. int pm25envvalue = 0;
  7. uint16_t BreakPointColor = 0;
  8. int offset = 5;  //used to shift the Legend right
  9.  
  10. const float AQI_BREAKPOINTS[8] = {0, 12.1, 35.5, 55.5, 150.5, 250.5, 350.5, 500.5};
  11. const int AQI_INDEX[8] = {0, 50, 100, 150, 200, 300, 400, 500};
  12. float pm25 = 0.0; // store PM2.5 value here
  13. float pm25std = 0.0;  //test only
  14. int aqinew = 0;
  15. char tempstring[8];  // Sh000 "S"=start, h=h/t/p  
  16.  
  17. #include "DHT.h"
  18. #define DHTPIN D0     // what digital pin we're connected to
  19. #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
  20. DHT dht(DHTPIN, DHTTYPE);
  21.  
  22. #include <SoftwareSerial.h>
  23. SoftwareSerial pmSerial(D1, D2);  //D1 RECEIVE DATA FROM PMS7003
  24. Adafruit_PM25AQI aqi = Adafruit_PM25AQI();
  25. #include"TFT_eSPI.h"
  26. TFT_eSPI tft;
  27.  
  28. float t = 0;  //temperature C
  29. float h = 0;  //humidity %
  30. float f = 0;  //temperature F
  31.  
  32. void setup() {
  33.     BreakPointColor = TFT_GREEN;
  34.     pinMode(D0,INPUT);
  35.     dht.begin();
  36.     Serial.begin(38400);
  37. //    while (!Serial) delay(10);
  38.  
  39.     pmSerial.begin(9600);
  40.     if (! aqi.begin_UART(&pmSerial)) { // connect to the sensor over software serial
  41.     //Serial.println("Could not find PM 2.5 sensor!");
  42.     while (1) delay(10);
  43.   }
  44.     tft.begin();
  45.     tft.setRotation(3);
  46.  
  47.     drawborders();
  48.     drawlegend();
  49.  
  50.    delay(1000);
  51.    //Serial.println("PM25 found!");
  52.  
  53. }
  54.  
  55. void loop() {    
  56.     tft.fillRect(2,10,70,45,TFT_BLACK);
  57.  
  58.     tft.setTextColor(BreakPointColor);           //sets the text colour to yellow
  59.     tft.setFreeFont(FSS18);  //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  60.     tft.drawString(String(pm25envvalue) , 10, 18,GFXFF);
  61. delay(100);
  62.     tft.fillRect(83,10,70,50,TFT_BLACK);
  63.     tft.setTextColor(BreakPointColor);           //sets the text colour to yellow
  64.     tft.setFreeFont(FSS18);
  65.     tft.drawString(String(aqinew) , 90, 20,GFXFF);
  66. delay(100);    
  67.     readdht22();  //t = temperature h = humidity
  68.     readpms7003();
  69. // TEMP/HUM BOX
  70.     tft.fillRect(2,68,74,28,TFT_BLACK);
  71.     tft.setTextColor(TFT_YELLOW);            //sets the text colour to green
  72.     tft.setFreeFont(FSS9);
  73.     tft.drawString(String(t) +"c" , 8, 72,GFXFF);
  74. delay(100);
  75.     tft.fillRect(85,68,74,28,TFT_BLACK);
  76.     tft.setTextColor(TFT_YELLOW);            //sets the text colour to green
  77.     tft.setFreeFont(FSS9);
  78.     tft.drawString(String(h)+"%" , 85, 72,GFXFF);
  79.  
  80.     delay(1000);
  81. }
  82.  
  83. void drawborders() {
  84.     tft.fillScreen(TFT_BLACK);
  85.     tft.drawRect(0,0,160,64,TFT_GREEN);
  86.     tft.drawLine(80, 0, 80, 63, TFT_GREEN);
  87.     //TEMP/HUM BOX
  88.     tft.drawRect(0,66,160,32,TFT_BLUE);
  89.     tft.drawLine(80, 66, 80, 64+32, TFT_BLUE);
  90.  
  91.     tft.setTextColor(TFT_WHITE);           //sets the text colour to yellow
  92.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  93.     tft.drawString("PM2.5 ug/m^3" , 3, 3,GFXFF);
  94.     tft.drawString("AQI(PM2.5)" , 82, 3,GFXFF);
  95.     //TEMP/HUM LABELS
  96.     /*tft.setTextColor(TFT_WHITE);           //sets the text colour to yellow
  97.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  98.     tft.drawString("TEMP" , 3, 64,GFXFF);
  99.     tft.drawString("HUM %)" , 82, 64,GFXFF);
  100.     */
  101.     //delay(5000);
  102.      
  103. }
  104.  
  105. void drawlegend() {
  106.  // Draw legend  
  107.     // GOOD
  108.     tft.fillRect(0+offset,100,30,20,TFT_GREEN);
  109.     tft.setTextColor(TFT_BLACK);           //sets the text colour to black
  110.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  111.     tft.drawString("GOOD" , 2+offset, 105,GFXFF);
  112.     //MODERATE
  113.     tft.fillRect(30+offset,100,22,20,TFT_YELLOW);
  114.     tft.setTextColor(TFT_BLACK);           //sets the text colour to black
  115.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  116.     tft.drawString("MOD" , 32+offset, 105,GFXFF);
  117.     //SENSITIVE
  118.     tft.fillRect(52+offset,100,24,20,TFT_ORANGE);
  119.     tft.setTextColor(TFT_BLACK);           //sets the text colour to yellow
  120.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  121.     tft.drawString("SEN" , 56+offset, 105,GFXFF);  
  122.     //UNHEALTHY
  123.     tft.fillRect(76+offset,100,24,20,TFT_RED);
  124.     tft.setTextColor(TFT_BLACK);           //sets the text colour to yellow
  125.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  126.     tft.drawString("UNH" , 80+offset, 105,GFXFF);  
  127.     //VERY UNHEALTHY
  128.     tft.fillRect(100+offset,100,24,20,TFT_PURPLE);
  129.     tft.setTextColor(TFT_WHITE);           //sets the text colour to yellow
  130.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  131.     tft.drawString("VER" , 104+offset, 105,GFXFF);  
  132.     //HAZARDOUS
  133.     tft.fillRect(124+offset,100,24,20,TFT_BROWN);
  134.     tft.setTextColor(TFT_WHITE);           //sets the text colour to yellow
  135.     tft.setFreeFont(NULL);                 //FF18=FreeSans12pt7b  FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
  136.     tft.drawString("HAZ" , 128+offset, 105,GFXFF);  
  137.  
  138. }
  139. void readdht22(){
  140.   // Reading temperature or humidity takes about 250 milliseconds!
  141.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  142.   h = dht.readHumidity();
  143.   // Read temperature as Celsius (the default)
  144.   t = dht.readTemperature();
  145.   // Read temperature as Fahrenheit (isFahrenheit = true)
  146.   f = dht.readTemperature(true);
  147.  
  148. //  Serial.print("Sh");
  149. //  Serial.printf("%00.2f",h);
  150.   //Serial.println();
  151.  
  152. //  Serial.print("St");
  153. //  Serial.printf("%00.2f",t);
  154.   //Serial.println();
  155.  
  156.   // Check if any reads failed and exit early (to try again).
  157.   if (isnan(h) || isnan(t) || isnan(f)) {
  158.     //Serial.println("Failed to read from DHT sensor!");
  159.     return;
  160.   }
  161.  
  162. }
  163.  
  164. void readpms7003() {
  165.   PM25_AQI_Data data;
  166.  
  167.   if (! aqi.read(&data)) {
  168.     //Serial.println("Could not read from AQI");
  169.     delay(500);  // try again in a bit!
  170.     return;
  171.   }
  172.   //Serial.println("AQI reading success");
  173.  
  174.   pm25value = data.pm25_standard;
  175.   pm25envvalue = data.pm25_env;
  176.   convertpm25toaqi();
  177.   Breakpoints();
  178. }
  179.  
  180. void convertpm25toaqi() {
  181.     // read PM2.5 value from sensor
  182.   pm25 = pm25envvalue; // read PM2.5 value from sensor (replace with your code to read sensor data)
  183.   //pm25std = pm25value;
  184.   // convert PM2.5 value to AQI
  185.   //int aqi = 0;
  186.   for (int i = 0; i < 8; i++) {
  187.     if (pm25 >= AQI_BREAKPOINTS[i] && pm25 <= AQI_BREAKPOINTS[i+1]) {
  188.       aqinew = (AQI_INDEX[i+1] - AQI_INDEX[i]) / (AQI_BREAKPOINTS[i+1] - AQI_BREAKPOINTS[i]) * (pm25 - AQI_BREAKPOINTS[i]) + AQI_INDEX[i];
  189.       break;
  190.     }
  191.   }
  192.  
  193.   // print all values
  194.   Serial.print("p");
  195.   sprintf(tempstring,"%05d",pm25envvalue);
  196.   Serial.print(tempstring);
  197.  
  198.   Serial.print("h");
  199.   Serial.printf("%00.2f",h);
  200.  
  201.   Serial.print("t");
  202.   Serial.printf("%00.2f",t);
  203.  
  204. /*  Serial.print("STD: ");  //test only
  205.   sprintf(tempstring,"%05d",pm25value);
  206.   Serial.println(tempstring);
  207.   Serial.print("Env: ");  //test only
  208.   sprintf(tempstring,"%05d",pm25envvalue);
  209.   Serial.print(tempstring);
  210.   //Serial.println();
  211. */
  212. }
  213.  
  214. void Breakpoints(){
  215.   if(pm25 <= 12){
  216.      BreakPointColor = TFT_GREEN;
  217.   }else if(pm25 <= 35){
  218.      BreakPointColor = TFT_YELLOW;
  219.   }else if(pm25 <= 55){
  220.      BreakPointColor = TFT_ORANGE;
  221.   }else if(pm25 <= 150){
  222.      BreakPointColor = TFT_RED;
  223.   }else if(pm25 <= 250){
  224.       BreakPointColor = TFT_PURPLE;
  225.   }else{
  226.      BreakPointColor = TFT_BROWN;
  227.   }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement