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: Environmental Monitor
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-10-19 18:17:19
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The project shall incorporate the MQ2 gas sensor */
- /* for detecting air quality, integrating data with */
- /* the DFPlayer Mini for audio feedback, enhancing */
- /* user interaction in a smart home environment. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h> // https://github.com/plerup/espsoftwareserial
- #include <Ultrasonic.h> // https://github.com/ErickSimoes/Ultrasonic
- #include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
- #include <MQ2.h> // Include the MQ2 library for gas sensing
- #include <DFPlayer_Mini_Mp3.h> // Include the DFPlayer Mini library for audio playback
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t Ultra_HC_SR04_Echo_PIN_D13 = 13;
- const uint8_t Temp_DHT11_DOUT_PIN_D14 = 14;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Smog_MQ2_AOUT_PIN_D25 = D25; // MQ2 sensor output pin
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t Ultra_HC_SR04_Trigger_PIN_D4 = 4;
- /***** DEFINITION OF Software Serial *****/
- const uint8_t Df_DFPlayerMini_Serial_PIN_SERIAL_TX_D18 = 18;
- const uint8_t Df_DFPlayerMini_Serial_PIN_SERIAL_RX_D19 = 19;
- EspSoftwareSerial::UART Df_DFPlayerMini_Serial;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool Ultra_HC_SR04_Trigger_PIN_D4_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float Ultra_HC_SR04_Trigger_PIN_D4_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create the ultrasonic object using the correct constructor
- Ultrasonic ultrasonic(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Echo_PIN_D13); // Create the ultrasonic object
- // Create the DHT object using the correct constructor
- #define DHTTYPE DHT11 // Define the type of DHT sensor
- DHT dht(Temp_DHT11_DOUT_PIN_D14, DHTTYPE); // Create the DHT object
- // Create the MQ2 object
- MQ2 mq2(Smog_MQ2_AOUT_PIN_D25); // Create the MQ2 gas sensor object
- // Create the DFPlayer Mini object
- DFPlayer_Mini_Mp3 dfPlayer; // Create the DFPlayer object
- // Define a threshold for gas concentration
- const float threshold = 300.0; // Example threshold value for gas concentration
- void setup(void)
- {
- Serial.begin(9600); // Initialize serial communication for debugging
- pinMode(Ultra_HC_SR04_Echo_PIN_D13, INPUT);
- pinMode(Temp_DHT11_DOUT_PIN_D14, INPUT_PULLUP);
- pinMode(Smog_MQ2_AOUT_PIN_D25, INPUT);
- pinMode(Ultra_HC_SR04_Trigger_PIN_D4, OUTPUT);
- Df_DFPlayerMini_Serial.begin(9600, SWSERIAL_8N1, Df_DFPlayerMini_Serial_PIN_SERIAL_RX_D19, Df_DFPlayerMini_Serial_PIN_SERIAL_TX_D18, false);
- // Initialize the ultrasonic sensor
- ultrasonic.attach(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Echo_PIN_D13);
- // Initialize the DHT sensor
- dht.begin(); // Start the DHT sensor
- // Initialize the MQ2 sensor
- mq2.begin(); // Start the MQ2 sensor
- // Initialize DFPlayer Mini
- dfPlayer.begin(Df_DFPlayerMini_Serial); // Start the DFPlayer Mini
- }
- void loop(void)
- {
- // Refresh output data
- updateOutputs();
- // Read temperature and humidity from DHT sensor
- float temperature = dht.readTemperature(); // Read temperature as Celsius
- float humidity = dht.readHumidity(); // Read humidity
- // Check if any reads failed and exit early (to try again).
- if (isnan(temperature) || isnan(humidity)) {
- Serial.println(F("Failed to read from DHT sensor!"));
- return;
- }
- // Print the results to the Serial Monitor
- Serial.print(F("Temperature: "));
- Serial.print(temperature);
- Serial.println(F("°C"));
- Serial.print(F("Humidity: "));
- Serial.print(humidity);
- Serial.println(F("%"));
- // Read gas concentration from MQ2 sensor
- float gasConcentration = mq2.read(); // Read gas concentration
- Serial.print(F("Gas Concentration: "));
- Serial.println(gasConcentration);
- // Check gas concentration and play audio feedback if necessary
- if (gasConcentration > threshold) { // Check against defined threshold
- dfPlayer.play(1); // Play the first audio file
- }
- }
- void updateOutputs()
- {
- digitalWrite(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Trigger_PIN_D4_rawData);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement