Advertisement
pleasedontcode

Air Quality rev_02

May 23rd, 2025
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Air Quality
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-05-23 17:52:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Monitors air quality using MQ135 sensor and */
  21.     /* temperature/humidity with DHT22, providing real- */
  22.     /* time data for environmental analysis. Alerts when */
  23.     /* thresholds are exceeded. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <MQUnifiedsensor.h>    //https://github.com/miguel5612/MQSensorsLib
  29. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t mq135_MQ135_DOUT_PIN_D13      = 13;
  37. const uint8_t dht22_DHT22_DOUT_PIN_D14      = 14;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t mq135_MQ135_AOUT_PIN_D4       = 34; // Changed D4 to 34
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. // MQ135 Sensor instance
  44. MQUnifiedsensor mq135("ESP32", 5, 10, mq135_MQ135_AOUT_PIN_D4, "MQ135");
  45. // DHT22 Sensor instance
  46. DHT dht(dht22_DHT22_DOUT_PIN_D14, DHT22);
  47.  
  48. // Thresholds for air quality and temperature/humidity
  49. const float airQualityThreshold = 300.0; // Example threshold for air quality
  50. const float temperatureThreshold = 30.0; // Example threshold for temperature
  51. const float humidityThreshold = 70.0; // Example threshold for humidity
  52.  
  53. void setup(void)
  54. {
  55.     // put your setup code here, to run once:
  56.     Serial.begin(115200);
  57.    
  58.     pinMode(mq135_MQ135_DOUT_PIN_D13, INPUT_PULLUP);
  59.     pinMode(dht22_DHT22_DOUT_PIN_D14, INPUT_PULLUP);
  60.     pinMode(mq135_MQ135_AOUT_PIN_D4, INPUT);
  61.  
  62.     // Initialize the sensors
  63.     mq135.init();
  64.     dht.begin();
  65. }
  66.  
  67. void loop(void)
  68. {
  69.     // put your main code here, to run repeatedly:
  70.     mq135.update();
  71.     float airQuality = mq135.getA(); // Changed getPPM to getA
  72.     float temperature = dht.readTemperature(); // Read temperature
  73.     float humidity = dht.readHumidity(); // Read humidity
  74.  
  75.     // Check for errors in reading DHT sensor
  76.     if (isnan(temperature) || isnan(humidity)) {
  77.         Serial.println("Failed to read from DHT sensor!");
  78.         return;
  79.     }
  80.  
  81.     // Print the sensor values
  82.     Serial.print("Air Quality (PPM): ");
  83.     Serial.println(airQuality);
  84.     Serial.print("Temperature (°C): ");
  85.     Serial.println(temperature);
  86.     Serial.print("Humidity (%): ");
  87.     Serial.println(humidity);
  88.  
  89.     // Alert if thresholds are exceeded
  90.     if (airQuality > airQualityThreshold) {
  91.         Serial.println("Alert: Air quality exceeds threshold!");
  92.     }
  93.     if (temperature > temperatureThreshold) {
  94.         Serial.println("Alert: Temperature exceeds threshold!");
  95.     }
  96.     if (humidity > humidityThreshold) {
  97.         Serial.println("Alert: Humidity exceeds threshold!");
  98.     }
  99.  
  100.     delay(2000); // Delay for 2 seconds before the next loop
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement