/********* 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: H2S Monitor
- Source Code NOT compiled for: Arduino Nano ESP32
- Source Code created on: 2024-07-23 08:08:09
********* 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 // Include WiFi library for cloud communication
#include // Include HTTP client for sending data to the cloud
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void sendDataToCloud(float h2sConcentration);
void alertUser(float h2sConcentration);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t myHydro_MQ136_DOUT_PIN_D2 = 2;
/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t myHydro_MQ136_AOUT_PIN_A0 = A0;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t strip_WS2812_DIN_PIN_D3 = 3;
const uint8_t strip_WS2812B_DIN_PIN_D4 = 4;
const uint8_t buzzerPin = 5; // Define buzzer pin
/***** 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 = 39;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Definitions for MQ136 sensor
#define placa "Arduino Nano ESP32"
#define Voltage_Resolution 5
#define ADC_Bit_Resolution 10 // For Arduino Nano ESP32
#define RatioMQ136CleanAir 3.6 // RS / R0 = 3.6 ppm
#define type "MQ-136" // MQ136
// Declare Sensor
MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);
// Declare LCD object using the correct constructor
LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // I2C address, width, height
// WiFi credentials
const char* ssid = "your_SSID"; // Replace with your network SSID
const char* password = "your_PASSWORD"; // Replace with your network password
void setup(void)
{
// Initialize digital input and output pins
pinMode(myHydro_MQ136_DOUT_PIN_D2, INPUT_PULLUP);
pinMode(myHydro_MQ136_AOUT_PIN_A0, INPUT);
pinMode(strip_WS2812_DIN_PIN_D3, OUTPUT);
pinMode(strip_WS2812B_DIN_PIN_D4, OUTPUT);
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
// Initialize the MQ136 sensor
MQ136.init();
// Set regression method and coefficients for MQ136
MQ136.setRegressionMethod(1); // _PPM = a * ratio^b
MQ136.setA(36.737);
MQ136.setB(-3.536); // Configure the equation to calculate H2S Concentration
// Calibrate the sensor
Serial.begin(9600); // Initialize serial communication
Serial.print("Calibrating please wait.");
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!");
// Check for connection issues
if(isinf(calcR0)) {
Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
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");
while(1);
}
// Enable serial debugging
MQ136.serialDebug(true);
// Initialize the LCD
lcd.begin();
lcd.print("Initializing...");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop(void)
{
// Refresh output data
updateOutputs();
MQ136.update(); // Update data from the sensor
MQ136.readSensor(); // Read sensor data
MQ136.serialDebug(); // Print sensor data to serial
float h2sConcentration = MQ136.getPPM(); // Get H2S concentration in PPM
lcd.setCursor(0, 0);
lcd.print("H2S: ");
lcd.print(h2sConcentration);
lcd.print(" ppm");
sendDataToCloud(h2sConcentration); // Send data to cloud
alertUser(h2sConcentration); // Alert user if concentration is high
delay(1000); // Delay for a second before next loop
}
void updateOutputs()
{
// Update digital outputs (example placeholder, replace with actual data)
digitalWrite(strip_WS2812_DIN_PIN_D3, LOW); // Placeholder for WS2812 strip data
digitalWrite(strip_WS2812B_DIN_PIN_D4, LOW); // Placeholder for WS2812B strip data
}
void sendDataToCloud(float h2sConcentration) {
// Create HTTP client
HTTPClient http;
http.begin("http://your-cloud-server.com/api/h2s"); // Replace with your cloud server URL
http.addHeader("Content-Type", "application/json");
// Create JSON payload
String jsonPayload = "{\"h2s_concentration\": ";
jsonPayload += h2sConcentration;
jsonPayload += "}";
// Send POST request
int httpResponseCode = http.POST(jsonPayload);
if (httpResponseCode > 0) {
Serial.printf("HTTP Response code: %d\n", httpResponseCode);
} else {
Serial.printf("Error sending data: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end(); // Free resources
}
void alertUser(float h2sConcentration) {
if (h2sConcentration > 10.0) { // Threshold for alerting
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
delay(1000); // Buzzer on for 1 second
digitalWrite(buzzerPin, LOW); // Turn off buzzer
}
}
/* END CODE */