Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Sensor Monitoring**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-05-23 17:51:15
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Monitors air quality using MQ135 sensor and */
- /* temperature/humidity with DHT22, providing real- */
- /* time data for environmental analysis. Alerts when */
- /* thresholds are exceeded. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <MQUnifiedsensor.h> //https://github.com/miguel5612/MQSensorsLib
- #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t mq135_MQ135_DOUT_PIN_D13 = 13;
- const uint8_t dht22_DHT22_DOUT_PIN_D14 = 14;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t mq135_MQ135_AOUT_PIN_D4 = D4;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // MQ135 Sensor instance
- MQUnifiedsensor mq135("ESP32", 5, 10, mq135_MQ135_AOUT_PIN_D4, "MQ135");
- // DHT22 Sensor instance
- DHT dht(dht22_DHT22_DOUT_PIN_D14, DHT22);
- // Thresholds for air quality and temperature/humidity
- const float airQualityThreshold = 300.0; // Example threshold for air quality
- const float temperatureThreshold = 30.0; // Example threshold for temperature
- const float humidityThreshold = 70.0; // Example threshold for humidity
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- pinMode(mq135_MQ135_DOUT_PIN_D13, INPUT_PULLUP);
- pinMode(dht22_DHT22_DOUT_PIN_D14, INPUT_PULLUP);
- pinMode(mq135_MQ135_AOUT_PIN_D4, INPUT);
- // Initialize the sensors
- mq135.init();
- dht.begin();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- mq135.update();
- float airQuality = mq135.getPPM(); // Get air quality in PPM
- float temperature = dht.readTemperature(); // Read temperature
- float humidity = dht.readHumidity(); // Read humidity
- // Check for errors in reading DHT sensor
- if (isnan(temperature) || isnan(humidity)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // Print the sensor values
- Serial.print("Air Quality (PPM): ");
- Serial.println(airQuality);
- Serial.print("Temperature (°C): ");
- Serial.println(temperature);
- Serial.print("Humidity (%): ");
- Serial.println(humidity);
- // Alert if thresholds are exceeded
- if (airQuality > airQualityThreshold) {
- Serial.println("Alert: Air quality exceeds threshold!");
- }
- if (temperature > temperatureThreshold) {
- Serial.println("Alert: Temperature exceeds threshold!");
- }
- if (humidity > humidityThreshold) {
- Serial.println("Alert: Humidity exceeds threshold!");
- }
- delay(2000); // Delay for 2 seconds before the next loop
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement