Advertisement
pleasedontcode

H2S Monitor rev_06

Aug 3rd, 2024
340
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: H2S Monitor
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-08-03 17:08:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Design an H2S detection system with the MQ136 */
  21.     /* sensor. The system should capture digital and */
  22.     /* analog outputs, send data to a cloud server for */
  23.     /* remote monitoring, and use an LCD and buzzer to */
  24.     /* alert users of high H2S concentrations. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <MQUnifiedsensor.h>  // https://github.com/miguel5612/MQSensorsLib
  30. #include <LCDIC2.h>          // https://github.com/offcircuit/LCDIC2
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36. void sendDataToCloud(float ppm); // Function to send data to cloud
  37. void alertUser(float ppm); // Function to alert user
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t myHydro_MQ136_DOUT_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t myHydro_MQ136_AOUT_PIN_A0 = A0;
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t strip_WS2812_DIN_PIN_D3 = 3;
  47. const uint8_t strip_WS2812B_DIN_PIN_D4 = 4;
  48. const uint8_t buzzerPin = 5; // Define buzzer pin
  49.  
  50. /***** DEFINITION OF I2C PINS *****/
  51. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  52. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  53. const uint8_t LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  54.  
  55. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  56. bool strip_WS2812_DIN_PIN_D3_rawData = 0;
  57. bool strip_WS2812B_DIN_PIN_D4_rawData = 0;
  58.  
  59. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  60. float strip_WS2812_DIN_PIN_D3_phyData = 0.0;
  61. float strip_WS2812B_DIN_PIN_D4_phyData = 0.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. // Define constants for the MQ136 sensor
  65. #define placa "Arduino Nano ESP32"
  66. #define Voltage_Resolution 5
  67. #define ADC_Bit_Resolution 10 // For Arduino Nano
  68. #define RatioMQ136CleanAir 3.6 // RS / R0 = 3.6 ppm
  69. #define type "MQ-136" // MQ136
  70.  
  71. // Declare the MQUnifiedsensor object for the MQ136 sensor
  72. MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);
  73.  
  74. // Declare the LCDIC2 object for the LCD display
  75. LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize with I2C address and dimensions
  76.  
  77. void setup(void)
  78. {
  79.     // Initialize digital pins
  80.     pinMode(myHydro_MQ136_DOUT_PIN_D2, INPUT_PULLUP);
  81.     pinMode(myHydro_MQ136_AOUT_PIN_A0, INPUT);
  82.     pinMode(strip_WS2812_DIN_PIN_D3, OUTPUT);
  83.     pinMode(strip_WS2812B_DIN_PIN_D4, OUTPUT);
  84.     pinMode(buzzerPin, OUTPUT); // Initialize buzzer pin
  85.  
  86.     // Initialize the LCD display
  87.     lcd.begin(); // Initialize the LCD without checking for success
  88.     lcd.print("Initializing..."); // Print a message on the LCD
  89.  
  90.     // Initialize the MQ136 sensor
  91.     Serial.begin(9600); // Start serial communication
  92.     MQ136.setRegressionMethod(1); // Set regression method for PPM calculation
  93.     MQ136.setA(36.737);
  94.     MQ136.setB(-3.536); // Configure the equation to calculate H2S Concentration
  95.     MQ136.init(); // Initialize the sensor
  96.  
  97.     // Calibrate the sensor
  98.     Serial.print("Calibrating please wait.");
  99.     float calcR0 = 0;
  100.     for(int i = 1; i <= 10; i++) {
  101.         MQ136.update(); // Update data from the sensor
  102.         calcR0 += MQ136.calibrate(RatioMQ136CleanAir); // Calibrate the sensor
  103.         Serial.print(".");
  104.     }
  105.     MQ136.setR0(calcR0 / 10); // Set the R0 value
  106.     Serial.println("  done!");
  107.  
  108.     // Check for connection issues
  109.     if(isinf(calcR0)) {
  110.         Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
  111.         while(1);
  112.     }
  113.     if(calcR0 == 0) {
  114.         Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
  115.         while(1);
  116.     }
  117.  
  118.     // Enable serial debugging
  119.     MQ136.serialDebug(true);
  120. }
  121.  
  122. void loop(void)
  123. {
  124.     // Update outputs and read sensor data
  125.     updateOutputs(); // Refresh output data
  126.     MQ136.update(); // Update data from the sensor
  127.     float ppm = MQ136.readSensor(); // Read PPM concentration
  128.     MQ136.serialDebug(); // Print the sensor data to the serial monitor
  129.  
  130.     // Send data to cloud
  131.     sendDataToCloud(ppm);
  132.  
  133.     // Alert user if PPM exceeds threshold (e.g., 10 ppm)
  134.     alertUser(ppm);
  135.  
  136.     delay(500); // Sampling frequency
  137. }
  138.  
  139. void updateOutputs()
  140. {
  141.     digitalWrite(strip_WS2812_DIN_PIN_D3, strip_WS2812_DIN_PIN_D3_rawData);
  142.     digitalWrite(strip_WS2812B_DIN_PIN_D4, strip_WS2812B_DIN_PIN_D4_rawData);
  143. }
  144.  
  145. void sendDataToCloud(float ppm) {
  146.     // Simulate sending data to a cloud server
  147.     Serial.print("Sending data to cloud: ");
  148.     Serial.print("H2S Concentration: ");
  149.     Serial.print(ppm);
  150.     Serial.println(" ppm");
  151.     // Here you would add actual cloud communication code
  152. }
  153.  
  154. void alertUser(float ppm) {
  155.     if (ppm > 10.0) { // Threshold for alert
  156.         lcd.clear();
  157.         lcd.print("Alert! H2S High!");
  158.         digitalWrite(buzzerPin, HIGH); // Activate buzzer
  159.         delay(1000); // Buzzer on for 1 second
  160.         digitalWrite(buzzerPin, LOW); // Deactivate buzzer
  161.     } else {
  162.         lcd.clear();
  163.         lcd.print("H2S Level: ");
  164.         lcd.print(ppm);
  165.     }
  166. }
  167.  
  168. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement