Advertisement
pleasedontcode

H2S Monitor rev_04

Jul 23rd, 2024
188
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-07-23 08:08:09
  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.     float h2sConcentration = MQ136.getPPM(); // Get H2S concentration in PPM
  139.     lcd.setCursor(0, 0);
  140.     lcd.print("H2S: ");
  141.     lcd.print(h2sConcentration);
  142.     lcd.print(" ppm");
  143.  
  144.     sendDataToCloud(h2sConcentration); // Send data to cloud
  145.     alertUser(h2sConcentration); // Alert user if concentration is high
  146.  
  147.     delay(1000); // Delay for a second before next loop
  148. }
  149.  
  150. void updateOutputs()
  151. {
  152.     // Update digital outputs (example placeholder, replace with actual data)
  153.     digitalWrite(strip_WS2812_DIN_PIN_D3, LOW); // Placeholder for WS2812 strip data
  154.     digitalWrite(strip_WS2812B_DIN_PIN_D4, LOW); // Placeholder for WS2812B strip data
  155. }
  156.  
  157. void sendDataToCloud(float h2sConcentration) {
  158.     // Create HTTP client
  159.     HTTPClient http;
  160.     http.begin("http://your-cloud-server.com/api/h2s"); // Replace with your cloud server URL
  161.     http.addHeader("Content-Type", "application/json");
  162.  
  163.     // Create JSON payload
  164.     String jsonPayload = "{\"h2s_concentration\": ";
  165.     jsonPayload += h2sConcentration;
  166.     jsonPayload += "}";
  167.  
  168.     // Send POST request
  169.     int httpResponseCode = http.POST(jsonPayload);
  170.  
  171.     if (httpResponseCode > 0) {
  172.         Serial.printf("HTTP Response code: %d\n", httpResponseCode);
  173.     } else {
  174.         Serial.printf("Error sending data: %s\n", http.errorToString(httpResponseCode).c_str());
  175.     }
  176.  
  177.     http.end(); // Free resources
  178. }
  179.  
  180. void alertUser(float h2sConcentration) {
  181.     if (h2sConcentration > 10.0) { // Threshold for alerting
  182.         digitalWrite(buzzerPin, HIGH); // Turn on buzzer
  183.         delay(1000); // Buzzer on for 1 second
  184.         digitalWrite(buzzerPin, LOW); // Turn off buzzer
  185.     }
  186. }
  187.  
  188. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement