Advertisement
pleasedontcode

H2S Monitor rev_05

Jul 23rd, 2024
169
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 compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-07-23 08:13:10
  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. #include <WiFi.h> // Include WiFi library for cloud communication
  32. #include <HTTPClient.h> // Include HTTP client for sending data to the cloud
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void updateOutputs(void);
  38. void sendDataToCloud(float h2sConcentration);
  39. void alertUser(float h2sConcentration);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t myHydro_MQ136_DOUT_PIN_D2 = 2;
  43.  
  44. /***** DEFINITION OF ANALOG INPUT PINS *****/
  45. const uint8_t myHydro_MQ136_AOUT_PIN_A0 = A0;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t strip_WS2812_DIN_PIN_D3 = 3;
  49. const uint8_t strip_WS2812B_DIN_PIN_D4 = 4;
  50. const uint8_t buzzerPin = 5; // Define buzzer pin
  51.  
  52. /***** DEFINITION OF I2C PINS *****/
  53. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  54. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  55. const uint8_t LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  58. // Definitions for MQ136 sensor
  59. #define placa "Arduino Nano ESP32"
  60. #define Voltage_Resolution 5
  61. #define ADC_Bit_Resolution 10 // For Arduino Nano ESP32
  62. #define RatioMQ136CleanAir 3.6 // RS / R0 = 3.6 ppm
  63. #define type "MQ-136" // MQ136
  64.  
  65. // Declare Sensor
  66. MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);
  67.  
  68. // Declare LCD object using the correct constructor
  69. LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // I2C address, width, height
  70.  
  71. // WiFi credentials
  72. const char* ssid = "your_SSID"; // Replace with your network SSID
  73. const char* password = "your_PASSWORD"; // Replace with your network password
  74.  
  75. void setup(void)
  76. {
  77.     // Initialize digital input and output pins
  78.     pinMode(myHydro_MQ136_DOUT_PIN_D2, INPUT_PULLUP);
  79.     pinMode(myHydro_MQ136_AOUT_PIN_A0, INPUT);
  80.     pinMode(strip_WS2812_DIN_PIN_D3, OUTPUT);
  81.     pinMode(strip_WS2812B_DIN_PIN_D4, OUTPUT);
  82.     pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
  83.  
  84.     // Initialize the MQ136 sensor
  85.     MQ136.init();
  86.  
  87.     // Set regression method and coefficients for MQ136
  88.     MQ136.setRegressionMethod(1); // _PPM = a * ratio^b
  89.     MQ136.setA(36.737);
  90.     MQ136.setB(-3.536); // Configure the equation to calculate H2S Concentration
  91.  
  92.     // Calibrate the sensor
  93.     Serial.begin(9600); // Initialize serial communication
  94.     Serial.print("Calibrating please wait.");
  95.     float calcR0 = 0;
  96.     for(int i = 1; i <= 10; i++) {
  97.         MQ136.update(); // Update data, the Arduino will read the voltage from the analog pin
  98.         calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
  99.         Serial.print(".");
  100.     }
  101.     MQ136.setR0(calcR0 / 10);
  102.     Serial.println("  done!");
  103.  
  104.     // Check for connection issues
  105.     if(isinf(calcR0)) {
  106.         Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
  107.         while(1);
  108.     }
  109.     if(calcR0 == 0) {
  110.         Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
  111.         while(1);
  112.     }
  113.  
  114.     // Enable serial debugging
  115.     MQ136.serialDebug(true);
  116.  
  117.     // Initialize the LCD
  118.     lcd.begin();
  119.     lcd.print("Initializing...");
  120.  
  121.     // Connect to WiFi
  122.     WiFi.begin(ssid, password);
  123.     while (WiFi.status() != WL_CONNECTED) {
  124.         delay(1000);
  125.         Serial.println("Connecting to WiFi...");
  126.     }
  127.     Serial.println("Connected to WiFi");
  128. }
  129.  
  130. void loop(void)
  131. {
  132.     // Refresh output data
  133.     updateOutputs();
  134.     MQ136.update(); // Update data from the sensor
  135.     MQ136.readSensor(); // Read sensor data
  136.     MQ136.serialDebug(); // Print sensor data to serial
  137.  
  138.     // Get H2S concentration in PPM
  139.     float h2sConcentration = MQ136.readSensor();
  140.     lcd.setCursor(0, 0);
  141.     lcd.print("H2S: ");
  142.     lcd.print(h2sConcentration);
  143.     lcd.print(" ppm");
  144.  
  145.     sendDataToCloud(h2sConcentration); // Send data to cloud
  146.     alertUser(h2sConcentration); // Alert user if concentration is high
  147.  
  148.     delay(1000); // Delay for a second before next loop
  149. }
  150.  
  151. void updateOutputs()
  152. {
  153.     // Update digital outputs (example placeholder, replace with actual data)
  154.     digitalWrite(strip_WS2812_DIN_PIN_D3, LOW); // Placeholder for WS2812 strip data
  155.     digitalWrite(strip_WS2812B_DIN_PIN_D4, LOW); // Placeholder for WS2812B strip data
  156. }
  157.  
  158. void sendDataToCloud(float h2sConcentration) {
  159.     // Create HTTP client
  160.     HTTPClient http;
  161.     http.begin("http://your-cloud-server.com/api/h2s"); // Replace with your cloud server URL
  162.     http.addHeader("Content-Type", "application/json");
  163.  
  164.     // Create JSON payload
  165.     String jsonPayload = "{\"h2s_concentration\": ";
  166.     jsonPayload += h2sConcentration;
  167.     jsonPayload += "}";
  168.  
  169.     // Send POST request
  170.     int httpResponseCode = http.POST(jsonPayload);
  171.  
  172.     if (httpResponseCode > 0) {
  173.         Serial.printf("HTTP Response code: %d\n", httpResponseCode);
  174.     } else {
  175.         Serial.printf("Error sending data: %s\n", http.errorToString(httpResponseCode).c_str());
  176.     }
  177.  
  178.     http.end(); // Free resources
  179. }
  180.  
  181. void alertUser(float h2sConcentration) {
  182.     if (h2sConcentration > 10.0) { // Threshold for alerting
  183.         digitalWrite(buzzerPin, HIGH); // Turn on buzzer
  184.         delay(1000); // Buzzer on for 1 second
  185.         digitalWrite(buzzerPin, LOW); // Turn off buzzer
  186.     }
  187. }
  188.  
  189. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement