#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define ADC_REF 3.3
#define ADC_RESOLUTION 4095
#define DHT_PIN 4
#define DHT_TYP DHT22
#define MQD_PIN 3
#define MQA_PIN A0
#define BUZ_PIN 5
DHT dht(DHT_PIN, DHT_TYP);
LiquidCrystal_I2C lcd(0x27, 16, 2);
uint32_t previousMillis = 0;
const uint16_t interval = 1000;
float ppm; //Stores parts per million value for gas concentration.
//long previous_millis = 0; //previous_millis: Used for timing the main loop operations.
//int interval = 1000; //interval: Defines the delay between consecutive gas checks in milliseconds.
void setup() {
Serial.begin(115200);
dht.begin();
lcd.init();
lcd.backlight();
lcd.print("Hello, World!");
}
void loop() {
//uint32_t currentMillis = millis();
//if (currentMillis - previousMillis <= interval) return;
//previousMillis = currentMillis;
// Membaca kelembapan dan suhu
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Gagal membaca dari sensor DHT!");
delay(2000);
return;
}
lcd.setCursor(0, 0);
lcd.print("Suhu : " + String(temperature) + " ");
GASLevel();
}
// GASLevel Function
// Function to convert ADC value to ppm (assuming a linear relationship for demonstration)
// You may need to adjust the calibration values based on your sensor's datasheet and environment.
float convertToPPM(int sensorValue) {
float voltage = sensorValue * (ADC_REF / ADC_RESOLUTION);
// Assuming a linear relationship: ppm = k * voltage (you should calibrate this properly)
float ppm = voltage * 1000; // Adjust the factor based on calibration
return ppm;
}
// Function to read gas level, update the LCD, and handle MQTT communication
void GASLevel() {
int sensorValue = analogRead(MQA_PIN);
int gasLevel = map(sensorValue, 0, 4095, 0, 100);
//adc_value = analogRead(POT_PIN); // Read the analog value from the potentiometer
//Serial.print("ADC Value: "); // Print the ADC value for debugging
//Serial.println(sensorValue);
ppm = convertToPPM(sensorValue);
//Serial.print("PPM Value:"); // Print the PPM value for debugging
//Serial.println(ppm);
// lcd.clear(); // Clear the LCD
lcd.setCursor(0, 1);
lcd.print("Gas : ");
lcd.print(ppm);
lcd.print("ppm ");
if (ppm >= 1800) { //it will trigger alarm at 55%
// Adjust threshold as needed
digitalWrite(BUZ_PIN, HIGH);
tone(BUZ_PIN, 200);
// lcd.setCursor(0, 1);
// lcd.print("Gas detected!!!");
//Serial.println("Gas detected");
// client.publish("IOT/group5/project", "Gas detected");
//Red_Led_state = HIGH;
//Green_Led_state = LOW;
//Yellow_Led_state = LOW;
} else if ((ppm >= 900) && (ppm <= 1800)) { //28% - 55% under normal
digitalWrite(BUZ_PIN, LOW);
noTone(BUZ_PIN);
// lcd.setCursor(0, 1);
// lcd.print("Warning ! ! ! ");
//Serial.println("Normal");
// client.publish("IOT/group5/project", "Warning");
//Red_Led_state = LOW;
//Green_Led_state = LOW;
//Yellow_Led_state = HIGH;
} else { // 0% - 27% is safe and will trigger Green Led
digitalWrite(BUZ_PIN, LOW);
noTone(BUZ_PIN);
// lcd.setCursor(0, 1);
// lcd.print("Safe ");
//Serial.println("Safe");
// client.publish("IOT/group5/project", "Safe");
//Red_Led_state = LOW;
//Green_Led_state = HIGH;
//Yellow_Led_state = LOW;
}
}