Advertisement
pleasedontcode

**Sensor Monitoring** rev_01

May 23rd, 2025
90
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: **Sensor Monitoring**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-05-23 17:51:15
  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.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <MQUnifiedsensor.h>    //https://github.com/miguel5612/MQSensorsLib
  30. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t mq135_MQ135_DOUT_PIN_D13      = 13;
  38. const uint8_t dht22_DHT22_DOUT_PIN_D14      = 14;
  39.  
  40. /***** DEFINITION OF ANALOG INPUT PINS *****/
  41. const uint8_t mq135_MQ135_AOUT_PIN_D4       = D4;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. // MQ135 Sensor instance
  45. MQUnifiedsensor mq135("ESP32", 5, 10, mq135_MQ135_AOUT_PIN_D4, "MQ135");
  46. // DHT22 Sensor instance
  47. DHT dht(dht22_DHT22_DOUT_PIN_D14, DHT22);
  48.  
  49. // Thresholds for air quality and temperature/humidity
  50. const float airQualityThreshold = 300.0; // Example threshold for air quality
  51. const float temperatureThreshold = 30.0; // Example threshold for temperature
  52. const float humidityThreshold = 70.0; // Example threshold for humidity
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     Serial.begin(115200);
  58.    
  59.     pinMode(mq135_MQ135_DOUT_PIN_D13, INPUT_PULLUP);
  60.     pinMode(dht22_DHT22_DOUT_PIN_D14, INPUT_PULLUP);
  61.     pinMode(mq135_MQ135_AOUT_PIN_D4, INPUT);
  62.  
  63.     // Initialize the sensors
  64.     mq135.init();
  65.     dht.begin();
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // put your main code here, to run repeatedly:
  71.     mq135.update();
  72.     float airQuality = mq135.getPPM(); // Get air quality in PPM
  73.     float temperature = dht.readTemperature(); // Read temperature
  74.     float humidity = dht.readHumidity(); // Read humidity
  75.  
  76.     // Check for errors in reading DHT sensor
  77.     if (isnan(temperature) || isnan(humidity)) {
  78.         Serial.println("Failed to read from DHT sensor!");
  79.         return;
  80.     }
  81.  
  82.     // Print the sensor values
  83.     Serial.print("Air Quality (PPM): ");
  84.     Serial.println(airQuality);
  85.     Serial.print("Temperature (°C): ");
  86.     Serial.println(temperature);
  87.     Serial.print("Humidity (%): ");
  88.     Serial.println(humidity);
  89.  
  90.     // Alert if thresholds are exceeded
  91.     if (airQuality > airQualityThreshold) {
  92.         Serial.println("Alert: Air quality exceeds threshold!");
  93.     }
  94.     if (temperature > temperatureThreshold) {
  95.         Serial.println("Alert: Temperature exceeds threshold!");
  96.     }
  97.     if (humidity > humidityThreshold) {
  98.         Serial.println("Alert: Humidity exceeds threshold!");
  99.     }
  100.  
  101.     delay(2000); // Delay for 2 seconds before the next loop
  102. }
  103.  
  104. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement