/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "H₂S Sensor"
- Source Code NOT compiled for: Arduino Nano ESP32
- Source Code created on: 2024-06-09 07:51:42
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Design an H2S detection system with the MQ136 */
/* sensor. The system should capture digital and */
/* analog outputs, send data to a cloud server for */
/* remote monitoring, and use an LCD and buzzer to */
/* alert users of high H2S concentrations. */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include // https://github.com/miguel5612/MQSensorsLib
#include // https://github.com/offcircuit/LCDIC2
#include // For WiFi connectivity to send data to cloud server
#include // For HTTP requests to send data to cloud server
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void sendDataToCloud(float ppm);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myHydro_MQ136_DOUT_PIN_D2 = 2;
const uint8_t buzzerPin = 3; // Pin for buzzer
/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t myHydro_MQ136_AOUT_PIN_A0 = A0;
/***** DEFINITION OF I2C PINS *****/
const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t LCD1602_Display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // 39 in decimal
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Definitions for MQ136 sensor
#define placa "Arduino UNO"
#define Voltage_Resolution 5
#define type "MQ-136" // MQ136
#define ADC_Bit_Resolution 10 // For Arduino UNO/MEGA/NANO
#define RatioMQ136CleanAir 3.6 // RS / R0 = 3.6 ppm
// Declare Sensor
MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);
// Declare LCD
LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
// WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// Cloud server URL
const char* serverName = "http://yourserver.com/api/data";
void setup(void)
{
// Initialize the serial port communication
Serial.begin(9600);
// Initialize the LCD
if (lcd.begin()) {
lcd.print("Initializing...");
}
// Initialize WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
lcd.setCursor(0, 1);
lcd.print("Connecting WiFi...");
}
Serial.println("Connected to WiFi");
lcd.clear();
lcd.print("WiFi Connected");
// Set math model to calculate the PPM concentration and the value of constants
MQ136.setRegressionMethod(1); // _PPM = a*ratio^b
MQ136.setA(36.737);
MQ136.setB(-3.536); // Configure the equation to calculate H2S Concentration
// Initialize the sensor
MQ136.init();
// Calibrate the sensor
Serial.print("Calibrating please wait.");
lcd.setCursor(0, 1);
lcd.print("Calibrating...");
float calcR0 = 0;
for(int i = 1; i <= 10; i++) {
MQ136.update(); // Update data, the Arduino will read the voltage from the analog pin
calcR0 += MQ136.calibrate(RatioMQ136CleanAir);
Serial.print(".");
}
MQ136.setR0(calcR0 / 10);
Serial.println(" done!");
lcd.clear();
lcd.print("Calibration done!");
// Check for connection issues
if(isinf(calcR0)) {
Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
lcd.clear();
lcd.print("Conn issue: R0 inf");
while(1);
}
if(calcR0 == 0) {
Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
lcd.clear();
lcd.print("Conn issue: R0 0");
while(1);
}
// Enable serial debugging
MQ136.serialDebug(true);
// Initialize pins
pinMode(myHydro_MQ136_DOUT_PIN_D2, INPUT_PULLUP);
pinMode(myHydro_MQ136_AOUT_PIN_A0, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop(void)
{
MQ136.update(); // Update data, the Arduino will read the voltage from the analog pin
MQ136.readSensor(); // Sensor will read PPM concentration using the model, a and b values set previously or from the setup
MQ136.serialDebug(); // Will print the table on the serial port
// Display PPM concentration on LCD
lcd.setCursor(0, 0);
lcd.print("H2S PPM: ");
lcd.print(MQ136.getPPM());
// Send data to cloud server
sendDataToCloud(MQ136.getPPM());
// Check if H2S concentration is above threshold and activate buzzer
if (MQ136.getPPM() > 10) { // Threshold value for H2S concentration
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
} else {
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
delay(500); // Sampling frequency
}
void sendDataToCloud(float ppm) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "ppm=" + String(ppm);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("Error in WiFi connection");
}
}