Advertisement
pleasedontcode

"H₂S Sensor" rev_03

Jun 9th, 2024
327
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: "H₂S Sensor"
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-06-09 07:51:42
  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>  // For WiFi connectivity to send data to cloud server
  32. #include <HTTPClient.h>  // For HTTP requests to send data to cloud server
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void sendDataToCloud(float ppm);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t myHydro_MQ136_DOUT_PIN_D2 = 2;
  41. const uint8_t buzzerPin = 3;  // Pin for buzzer
  42.  
  43. /***** DEFINITION OF ANALOG INPUT PINS *****/
  44. const uint8_t myHydro_MQ136_AOUT_PIN_A0 = A0;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  48. const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  49. const uint8_t LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // 39 in decimal
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Definitions for MQ136 sensor
  53. #define placa "Arduino UNO"
  54. #define Voltage_Resolution 5
  55. #define type "MQ-136"  // MQ136
  56. #define ADC_Bit_Resolution 10  // For Arduino UNO/MEGA/NANO
  57. #define RatioMQ136CleanAir 3.6  // RS / R0 = 3.6 ppm
  58.  
  59. // Declare Sensor
  60. MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);
  61.  
  62. // Declare LCD
  63. LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  64.  
  65. // WiFi credentials
  66. const char* ssid = "your_SSID";
  67. const char* password = "your_PASSWORD";
  68.  
  69. // Cloud server URL
  70. const char* serverName = "http://yourserver.com/api/data";
  71.  
  72. void setup(void)
  73. {
  74.   // Initialize the serial port communication
  75.   Serial.begin(9600);
  76.  
  77.   // Initialize the LCD
  78.   if (lcd.begin()) {
  79.     lcd.print("Initializing...");
  80.   }
  81.  
  82.   // Initialize WiFi
  83.   WiFi.begin(ssid, password);
  84.   while (WiFi.status() != WL_CONNECTED) {
  85.     delay(1000);
  86.     Serial.println("Connecting to WiFi...");
  87.     lcd.setCursor(0, 1);
  88.     lcd.print("Connecting WiFi...");
  89.   }
  90.   Serial.println("Connected to WiFi");
  91.   lcd.clear();
  92.   lcd.print("WiFi Connected");
  93.  
  94.   // Set math model to calculate the PPM concentration and the value of constants
  95.   MQ136.setRegressionMethod(1);  // _PPM = a*ratio^b
  96.   MQ136.setA(36.737);
  97.   MQ136.setB(-3.536);  // Configure the equation to calculate H2S Concentration
  98.  
  99.   // Initialize the sensor
  100.   MQ136.init();
  101.  
  102.   // Calibrate the sensor
  103.   Serial.print("Calibrating please wait.");
  104.   lcd.setCursor(0, 1);
  105.   lcd.print("Calibrating...");
  106.   float calcR0 = 0;
  107.   for(int i = 1; i <= 10; i++) {
  108.     MQ136.update();  // Update data, the Arduino will read the voltage from the analog pin
  109.     calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
  110.     Serial.print(".");
  111.   }
  112.   MQ136.setR0(calcR0 / 10);
  113.   Serial.println("  done!");
  114.   lcd.clear();
  115.   lcd.print("Calibration done!");
  116.  
  117.   // Check for connection issues
  118.   if(isinf(calcR0)) {
  119.     Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
  120.     lcd.clear();
  121.     lcd.print("Conn issue: R0 inf");
  122.     while(1);
  123.   }
  124.   if(calcR0 == 0) {
  125.     Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
  126.     lcd.clear();
  127.     lcd.print("Conn issue: R0 0");
  128.     while(1);
  129.   }
  130.  
  131.   // Enable serial debugging
  132.   MQ136.serialDebug(true);
  133.  
  134.   // Initialize pins
  135.   pinMode(myHydro_MQ136_DOUT_PIN_D2, INPUT_PULLUP);
  136.   pinMode(myHydro_MQ136_AOUT_PIN_A0, INPUT);
  137.   pinMode(buzzerPin, OUTPUT);
  138. }
  139.  
  140. void loop(void)
  141. {
  142.   MQ136.update();  // Update data, the Arduino will read the voltage from the analog pin
  143.   MQ136.readSensor();  // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
  144.   MQ136.serialDebug();  // Will print the table on the serial port
  145.  
  146.   // Display PPM concentration on LCD
  147.   lcd.setCursor(0, 0);
  148.   lcd.print("H2S PPM: ");
  149.   lcd.print(MQ136.getPPM());
  150.  
  151.   // Send data to cloud server
  152.   sendDataToCloud(MQ136.getPPM());
  153.  
  154.   // Check if H2S concentration is above threshold and activate buzzer
  155.   if (MQ136.getPPM() > 10) {  // Threshold value for H2S concentration
  156.     digitalWrite(buzzerPin, HIGH);  // Turn on buzzer
  157.   } else {
  158.     digitalWrite(buzzerPin, LOW);  // Turn off buzzer
  159.   }
  160.  
  161.   delay(500);  // Sampling frequency
  162. }
  163.  
  164. void sendDataToCloud(float ppm) {
  165.   if (WiFi.status() == WL_CONNECTED) {
  166.     HTTPClient http;
  167.     http.begin(serverName);
  168.     http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  169.  
  170.     String httpRequestData = "ppm=" + String(ppm);
  171.     int httpResponseCode = http.POST(httpRequestData);
  172.  
  173.     if (httpResponseCode > 0) {
  174.       String response = http.getString();
  175.       Serial.println(httpResponseCode);
  176.       Serial.println(response);
  177.     } else {
  178.       Serial.print("Error on sending POST: ");
  179.       Serial.println(httpResponseCode);
  180.     }
  181.     http.end();
  182.   } else {
  183.     Serial.println("Error in WiFi connection");
  184.   }
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement