Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* OK Standalone April 16, 2023 */
- #include "Free_Fonts.h"
- #include "Adafruit_PM25AQI.h"
- int pm25value = 0;
- int pm25envvalue = 0;
- uint16_t BreakPointColor = 0;
- int offset = 5; //used to shift the Legend right
- const float AQI_BREAKPOINTS[8] = {0, 12.1, 35.5, 55.5, 150.5, 250.5, 350.5, 500.5};
- const int AQI_INDEX[8] = {0, 50, 100, 150, 200, 300, 400, 500};
- float pm25 = 0.0; // store PM2.5 value here
- float pm25std = 0.0; //test only
- int aqinew = 0;
- char tempstring[8]; // Sh000 "S"=start, h=h/t/p
- #include "DHT.h"
- #define DHTPIN D0 // what digital pin we're connected to
- #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- DHT dht(DHTPIN, DHTTYPE);
- #include <SoftwareSerial.h>
- SoftwareSerial pmSerial(D1, D2); //D1 RECEIVE DATA FROM PMS7003
- Adafruit_PM25AQI aqi = Adafruit_PM25AQI();
- #include"TFT_eSPI.h"
- TFT_eSPI tft;
- float t = 0; //temperature C
- float h = 0; //humidity %
- float f = 0; //temperature F
- void setup() {
- BreakPointColor = TFT_GREEN;
- pinMode(D0,INPUT);
- dht.begin();
- Serial.begin(38400);
- // while (!Serial) delay(10);
- pmSerial.begin(9600);
- if (! aqi.begin_UART(&pmSerial)) { // connect to the sensor over software serial
- //Serial.println("Could not find PM 2.5 sensor!");
- while (1) delay(10);
- }
- tft.begin();
- tft.setRotation(3);
- drawborders();
- drawlegend();
- delay(1000);
- //Serial.println("PM25 found!");
- }
- void loop() {
- tft.fillRect(2,10,70,45,TFT_BLACK);
- tft.setTextColor(BreakPointColor); //sets the text colour to yellow
- tft.setFreeFont(FSS18); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString(String(pm25envvalue) , 10, 18,GFXFF);
- delay(100);
- tft.fillRect(83,10,70,50,TFT_BLACK);
- tft.setTextColor(BreakPointColor); //sets the text colour to yellow
- tft.setFreeFont(FSS18);
- tft.drawString(String(aqinew) , 90, 20,GFXFF);
- delay(100);
- readdht22(); //t = temperature h = humidity
- readpms7003();
- // TEMP/HUM BOX
- tft.fillRect(2,68,74,28,TFT_BLACK);
- tft.setTextColor(TFT_YELLOW); //sets the text colour to green
- tft.setFreeFont(FSS9);
- tft.drawString(String(t) +"c" , 8, 72,GFXFF);
- delay(100);
- tft.fillRect(85,68,74,28,TFT_BLACK);
- tft.setTextColor(TFT_YELLOW); //sets the text colour to green
- tft.setFreeFont(FSS9);
- tft.drawString(String(h)+"%" , 85, 72,GFXFF);
- delay(1000);
- }
- void drawborders() {
- tft.fillScreen(TFT_BLACK);
- tft.drawRect(0,0,160,64,TFT_GREEN);
- tft.drawLine(80, 0, 80, 63, TFT_GREEN);
- //TEMP/HUM BOX
- tft.drawRect(0,66,160,32,TFT_BLUE);
- tft.drawLine(80, 66, 80, 64+32, TFT_BLUE);
- tft.setTextColor(TFT_WHITE); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("PM2.5 ug/m^3" , 3, 3,GFXFF);
- tft.drawString("AQI(PM2.5)" , 82, 3,GFXFF);
- //TEMP/HUM LABELS
- /*tft.setTextColor(TFT_WHITE); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("TEMP" , 3, 64,GFXFF);
- tft.drawString("HUM %)" , 82, 64,GFXFF);
- */
- //delay(5000);
- }
- void drawlegend() {
- // Draw legend
- // GOOD
- tft.fillRect(0+offset,100,30,20,TFT_GREEN);
- tft.setTextColor(TFT_BLACK); //sets the text colour to black
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("GOOD" , 2+offset, 105,GFXFF);
- //MODERATE
- tft.fillRect(30+offset,100,22,20,TFT_YELLOW);
- tft.setTextColor(TFT_BLACK); //sets the text colour to black
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("MOD" , 32+offset, 105,GFXFF);
- //SENSITIVE
- tft.fillRect(52+offset,100,24,20,TFT_ORANGE);
- tft.setTextColor(TFT_BLACK); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("SEN" , 56+offset, 105,GFXFF);
- //UNHEALTHY
- tft.fillRect(76+offset,100,24,20,TFT_RED);
- tft.setTextColor(TFT_BLACK); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("UNH" , 80+offset, 105,GFXFF);
- //VERY UNHEALTHY
- tft.fillRect(100+offset,100,24,20,TFT_PURPLE);
- tft.setTextColor(TFT_WHITE); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("VER" , 104+offset, 105,GFXFF);
- //HAZARDOUS
- tft.fillRect(124+offset,100,24,20,TFT_BROWN);
- tft.setTextColor(TFT_WHITE); //sets the text colour to yellow
- tft.setFreeFont(NULL); //FF18=FreeSans12pt7b FMB12=FreeMonoBold12pt7b FSS18=FreeSans18pt7b
- tft.drawString("HAZ" , 128+offset, 105,GFXFF);
- }
- void readdht22(){
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- h = dht.readHumidity();
- // Read temperature as Celsius (the default)
- t = dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- f = dht.readTemperature(true);
- // Serial.print("Sh");
- // Serial.printf("%00.2f",h);
- //Serial.println();
- // Serial.print("St");
- // Serial.printf("%00.2f",t);
- //Serial.println();
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t) || isnan(f)) {
- //Serial.println("Failed to read from DHT sensor!");
- return;
- }
- }
- void readpms7003() {
- PM25_AQI_Data data;
- if (! aqi.read(&data)) {
- //Serial.println("Could not read from AQI");
- delay(500); // try again in a bit!
- return;
- }
- //Serial.println("AQI reading success");
- pm25value = data.pm25_standard;
- pm25envvalue = data.pm25_env;
- convertpm25toaqi();
- Breakpoints();
- }
- void convertpm25toaqi() {
- // read PM2.5 value from sensor
- pm25 = pm25envvalue; // read PM2.5 value from sensor (replace with your code to read sensor data)
- //pm25std = pm25value;
- // convert PM2.5 value to AQI
- //int aqi = 0;
- for (int i = 0; i < 8; i++) {
- if (pm25 >= AQI_BREAKPOINTS[i] && pm25 <= AQI_BREAKPOINTS[i+1]) {
- aqinew = (AQI_INDEX[i+1] - AQI_INDEX[i]) / (AQI_BREAKPOINTS[i+1] - AQI_BREAKPOINTS[i]) * (pm25 - AQI_BREAKPOINTS[i]) + AQI_INDEX[i];
- break;
- }
- }
- // print all values
- Serial.print("p");
- sprintf(tempstring,"%05d",pm25envvalue);
- Serial.print(tempstring);
- Serial.print("h");
- Serial.printf("%00.2f",h);
- Serial.print("t");
- Serial.printf("%00.2f",t);
- /* Serial.print("STD: "); //test only
- sprintf(tempstring,"%05d",pm25value);
- Serial.println(tempstring);
- Serial.print("Env: "); //test only
- sprintf(tempstring,"%05d",pm25envvalue);
- Serial.print(tempstring);
- //Serial.println();
- */
- }
- void Breakpoints(){
- if(pm25 <= 12){
- BreakPointColor = TFT_GREEN;
- }else if(pm25 <= 35){
- BreakPointColor = TFT_YELLOW;
- }else if(pm25 <= 55){
- BreakPointColor = TFT_ORANGE;
- }else if(pm25 <= 150){
- BreakPointColor = TFT_RED;
- }else if(pm25 <= 250){
- BreakPointColor = TFT_PURPLE;
- }else{
- BreakPointColor = TFT_BROWN;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement