Advertisement
pleasedontcode

Environmental Monitor rev_01

Oct 19th, 2024
74
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: Environmental Monitor
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-19 18:17:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project shall incorporate the MQ2 gas sensor */
  21.     /* for detecting air quality, integrating data with */
  22.     /* the DFPlayer Mini for audio feedback, enhancing */
  23.     /* user interaction in a smart home environment. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SoftwareSerial.h> // https://github.com/plerup/espsoftwareserial
  28. #include <Ultrasonic.h>     // https://github.com/ErickSimoes/Ultrasonic
  29. #include <DHT.h>           // https://github.com/adafruit/DHT-sensor-library
  30. #include <MQ2.h>           // Include the MQ2 library for gas sensing
  31. #include <DFPlayer_Mini_Mp3.h> // Include the DFPlayer Mini library for audio playback
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t Ultra_HC_SR04_Echo_PIN_D13        = 13;
  40. const uint8_t Temp_DHT11_DOUT_PIN_D14            = 14;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t Smog_MQ2_AOUT_PIN_D25              = D25; // MQ2 sensor output pin
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t Ultra_HC_SR04_Trigger_PIN_D4       = 4;
  47.  
  48. /***** DEFINITION OF Software Serial *****/
  49. const uint8_t Df_DFPlayerMini_Serial_PIN_SERIAL_TX_D18 = 18;
  50. const uint8_t Df_DFPlayerMini_Serial_PIN_SERIAL_RX_D19 = 19;
  51. EspSoftwareSerial::UART Df_DFPlayerMini_Serial;
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. bool Ultra_HC_SR04_Trigger_PIN_D4_rawData        = 0;
  55.  
  56. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  57. float Ultra_HC_SR04_Trigger_PIN_D4_phyData       = 0.0;
  58.  
  59. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  60. // Create the ultrasonic object using the correct constructor
  61. Ultrasonic ultrasonic(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Echo_PIN_D13); // Create the ultrasonic object
  62.  
  63. // Create the DHT object using the correct constructor
  64. #define DHTTYPE DHT11 // Define the type of DHT sensor
  65. DHT dht(Temp_DHT11_DOUT_PIN_D14, DHTTYPE); // Create the DHT object
  66.  
  67. // Create the MQ2 object
  68. MQ2 mq2(Smog_MQ2_AOUT_PIN_D25); // Create the MQ2 gas sensor object
  69.  
  70. // Create the DFPlayer Mini object
  71. DFPlayer_Mini_Mp3 dfPlayer; // Create the DFPlayer object
  72.  
  73. // Define a threshold for gas concentration
  74. const float threshold = 300.0; // Example threshold value for gas concentration
  75.  
  76. void setup(void)
  77. {
  78.     Serial.begin(9600); // Initialize serial communication for debugging
  79.  
  80.     pinMode(Ultra_HC_SR04_Echo_PIN_D13, INPUT);
  81.     pinMode(Temp_DHT11_DOUT_PIN_D14, INPUT_PULLUP);
  82.     pinMode(Smog_MQ2_AOUT_PIN_D25, INPUT);
  83.     pinMode(Ultra_HC_SR04_Trigger_PIN_D4, OUTPUT);
  84.  
  85.     Df_DFPlayerMini_Serial.begin(9600, SWSERIAL_8N1, Df_DFPlayerMini_Serial_PIN_SERIAL_RX_D19, Df_DFPlayerMini_Serial_PIN_SERIAL_TX_D18, false);
  86.  
  87.     // Initialize the ultrasonic sensor
  88.     ultrasonic.attach(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Echo_PIN_D13);
  89.  
  90.     // Initialize the DHT sensor
  91.     dht.begin(); // Start the DHT sensor
  92.  
  93.     // Initialize the MQ2 sensor
  94.     mq2.begin(); // Start the MQ2 sensor
  95.  
  96.     // Initialize DFPlayer Mini
  97.     dfPlayer.begin(Df_DFPlayerMini_Serial); // Start the DFPlayer Mini
  98. }
  99.  
  100. void loop(void)
  101. {
  102.     // Refresh output data
  103.     updateOutputs();
  104.  
  105.     // Read temperature and humidity from DHT sensor
  106.     float temperature = dht.readTemperature(); // Read temperature as Celsius
  107.     float humidity = dht.readHumidity(); // Read humidity
  108.  
  109.     // Check if any reads failed and exit early (to try again).
  110.     if (isnan(temperature) || isnan(humidity)) {
  111.         Serial.println(F("Failed to read from DHT sensor!"));
  112.         return;
  113.     }
  114.  
  115.     // Print the results to the Serial Monitor
  116.     Serial.print(F("Temperature: "));
  117.     Serial.print(temperature);
  118.     Serial.println(F("°C"));
  119.     Serial.print(F("Humidity: "));
  120.     Serial.print(humidity);
  121.     Serial.println(F("%"));
  122.  
  123.     // Read gas concentration from MQ2 sensor
  124.     float gasConcentration = mq2.read(); // Read gas concentration
  125.     Serial.print(F("Gas Concentration: "));
  126.     Serial.println(gasConcentration);
  127.  
  128.     // Check gas concentration and play audio feedback if necessary
  129.     if (gasConcentration > threshold) { // Check against defined threshold
  130.         dfPlayer.play(1); // Play the first audio file
  131.     }
  132. }
  133.  
  134. void updateOutputs()
  135. {
  136.     digitalWrite(Ultra_HC_SR04_Trigger_PIN_D4, Ultra_HC_SR04_Trigger_PIN_D4_rawData);
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement