/********* 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-08-03 17:08:47

********* 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 <Wire.h>
#include <MQUnifiedsensor.h>  // https://github.com/miguel5612/MQSensorsLib
#include <LCDIC2.h>          // https://github.com/offcircuit/LCDIC2

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void sendDataToCloud(float ppm); // Function to send data to cloud
void alertUser(float ppm); // Function to alert user

/***** 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 OUTPUT RAW VARIABLES *****/
bool strip_WS2812_DIN_PIN_D3_rawData = 0;
bool strip_WS2812B_DIN_PIN_D4_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float strip_WS2812_DIN_PIN_D3_phyData = 0.0;
float strip_WS2812B_DIN_PIN_D4_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Define constants for the MQ136 sensor
#define placa "Arduino Nano ESP32"
#define Voltage_Resolution 5
#define ADC_Bit_Resolution 10 // For Arduino Nano
#define RatioMQ136CleanAir 3.6 // RS / R0 = 3.6 ppm
#define type "MQ-136" // MQ136

// Declare the MQUnifiedsensor object for the MQ136 sensor
MQUnifiedsensor MQ136(placa, Voltage_Resolution, ADC_Bit_Resolution, myHydro_MQ136_AOUT_PIN_A0, type);

// Declare the LCDIC2 object for the LCD display
LCDIC2 lcd(LCD1602_Display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize with I2C address and dimensions

void setup(void)
{
    // Initialize digital 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); // Initialize buzzer pin

    // Initialize the LCD display
    lcd.begin(); // Initialize the LCD without checking for success
    lcd.print("Initializing..."); // Print a message on the LCD

    // Initialize the MQ136 sensor
    Serial.begin(9600); // Start serial communication
    MQ136.setRegressionMethod(1); // Set regression method for PPM calculation
    MQ136.setA(36.737); 
    MQ136.setB(-3.536); // Configure the equation to calculate H2S Concentration
    MQ136.init(); // Initialize the sensor

    // Calibrate the sensor
    Serial.print("Calibrating please wait.");
    float calcR0 = 0;
    for(int i = 1; i <= 10; i++) {
        MQ136.update(); // Update data from the sensor
        calcR0 += MQ136.calibrate(RatioMQ136CleanAir); // Calibrate the sensor
        Serial.print(".");
    }
    MQ136.setR0(calcR0 / 10); // Set the R0 value
    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);
}

void loop(void)
{
    // Update outputs and read sensor data
    updateOutputs(); // Refresh output data
    MQ136.update(); // Update data from the sensor
    float ppm = MQ136.readSensor(); // Read PPM concentration
    MQ136.serialDebug(); // Print the sensor data to the serial monitor

    // Send data to cloud
    sendDataToCloud(ppm);

    // Alert user if PPM exceeds threshold (e.g., 10 ppm)
    alertUser(ppm);

    delay(500); // Sampling frequency
}

void updateOutputs()
{
    digitalWrite(strip_WS2812_DIN_PIN_D3, strip_WS2812_DIN_PIN_D3_rawData);
    digitalWrite(strip_WS2812B_DIN_PIN_D4, strip_WS2812B_DIN_PIN_D4_rawData);
}

void sendDataToCloud(float ppm) {
    // Simulate sending data to a cloud server
    Serial.print("Sending data to cloud: ");
    Serial.print("H2S Concentration: ");
    Serial.print(ppm);
    Serial.println(" ppm");
    // Here you would add actual cloud communication code
}

void alertUser(float ppm) {
    if (ppm > 10.0) { // Threshold for alert
        lcd.clear();
        lcd.print("Alert! H2S High!");
        digitalWrite(buzzerPin, HIGH); // Activate buzzer
        delay(1000); // Buzzer on for 1 second
        digitalWrite(buzzerPin, LOW); // Deactivate buzzer
    } else {
        lcd.clear();
        lcd.print("H2S Level: ");
        lcd.print(ppm);
    }
}

/* END CODE */