#include <WiFi.h>
#include "PubSubClient.h"
// const char *ssid = "TrangThuw";
// const char *password = "Kkkkkkkk";
const char * MQTTServer = "broker.emqx.io";
const char * MQTT_Topic_Gas = "sensor/gas";
const char * MQTT_Topic_Flame = "sensor/flame";
const char * MQTT_Topic_ThresholdGas = "sensor/thresholdGas";
const char * MQTT_Topic_Alert = "sensor/alert";
// Tạo ID ngẫu nhiên tại: https://www.guidgen.com/
const char * MQTT_ID = "baoCao";
int Port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
const int gasSensorPin = 32; // Chân đọc giá trị analog từ cảm biến khí gas
const int flameSensorPin = 13; // Chân đọc giá trị kỹ thuật số từ cảm biến lửa (đổi từ 13 sang 33)
const int buzzerGas = 22; // Chân điều khiển còi buzzer
const int buzzerFlame = 21; // Chân điều khiển còi buzzer
const int buttonPin = 13; // Chân kết nối nút bấm
int thresholdGas = 10; // Ngưỡng cảnh báo khí gas (giá trị sau khi dùng hàm map)
int thresholdFlame = LOW; // Ngưỡng cảnh báo lửa (giá trị kỹ thuật số)
bool buttonState = LOW; // Trạng thái hiện tại của nút bấm
bool lastButtonState = LOW; // Trạng thái trước đó của nút bấm
void WIFIConnect() {
Serial.println("Connecting to SSID: Wokwi-GUEST");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected");
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}
void MQTT_Reconnect() {
while (!client.connected()) {
if (client.connect(MQTT_ID)) {
Serial.print("MQTT Topic: ");
Serial.print(MQTT_Topic_Gas);
Serial.print(" connected");
client.subscribe(MQTT_Topic_ThresholdGas);
Serial.println("");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
Serial.print("Message: ");
String stMessage;
for (int i = 0; i < length; i++) {
stMessage += (char)message[i];
}
Serial.println(stMessage);
if (String(topic) == MQTT_Topic_ThresholdGas) {
thresholdGas = stMessage.toInt();
Serial.print("New Gas Threshold: ");
Serial.println(thresholdGas);
}
}
void setup() {
Serial.begin(115200); // Khởi động Serial monitor để hiển thị thông tin
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
pinMode(gasSensorPin, INPUT);
pinMode(flameSensorPin, INPUT);
pinMode(buzzerGas, OUTPUT);
pinMode(buzzerFlame, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Thiết lập chân nút bấm với PULLUP nội bộ
digitalWrite(buzzerGas, LOW); // Tắt còi buzzer ban đầu
digitalWrite(buzzerFlame, LOW); // Tắt còi buzzer ban đầu
}
void loop() {
delay(10);
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
int gasValue = analogRead(gasSensorPin); // Đọc giá trị từ cảm biến khí gas
int gasLevel = map(gasValue, 0, 4095, 0, 100); // Chuyển đổi giá trị analog sang giá trị từ 0 đến 100
int flameValue = digitalRead(flameSensorPin); // Đọc giá trị từ cảm biến lửa
buttonState = digitalRead(buttonPin); // Đọc trạng thái của nút bấm
// Kiểm tra nếu nút bấm được nhấn (từ LOW sang HIGH)
if (buttonState == HIGH && lastButtonState == LOW) {
thresholdFlame = HIGH; // Chuyển thành trạng thái bật
Serial.println("Ngưỡng cảnh báo lửa thay đổi thành: HIGH");
delay(50); // Debounce nút bấm
}
// Cập nhật trạng thái nút bấm trước đó
lastButtonState = buttonState;
// Hiển thị giá trị đọc được từ cảm biến
Serial.print("Gas Level: ");
Serial.print(gasLevel);
Serial.print(" - Flame Value: ");
Serial.println(flameValue);
// Gửi giá trị khí gas và lửa lên máy chủ MQTT
String gasString = String(gasLevel);
String flameString = String(flameValue);
client.publish(MQTT_Topic_Gas, gasString.c_str());
client.publish(MQTT_Topic_Flame, flameString.c_str());
// Kiểm tra nếu giá trị khí gas vượt ngưỡng
if (gasLevel > thresholdGas) {
Serial.println("Cảnh báo: Phát hiện rò rỉ khí gas!");
tone(buzzerGas, 1000); // Phát ra âm thanh cảnh báo với tần số 1000 Hz
String alertMessage = "Gas Leak Detected!";
client.publish(MQTT_Topic_Alert, alertMessage.c_str());
} else {
noTone(buzzerGas); // Tắt còi buzzer nếu không có cảnh báo
}
// Kiểm tra nếu giá trị cảm biến lửa vượt ngưỡng
if (flameValue == thresholdFlame) {
Serial.println("Cảnh báo: Phát hiện cháy!");
tone(buzzerFlame, 2000); // Phát ra âm thanh cảnh báo với tần số 2000 Hz
String alertMessage = "Fire Detected!";
client.publish(MQTT_Topic_Alert, alertMessage.c_str());
} else {
noTone(buzzerFlame); // Tắt còi buzzer nếu không có cảnh báo
}
// Kiểm tra nếu cả hai giá trị đều vượt ngưỡng
// if (gasLevel > thresholdGas && flameValue == thresholdFlame) {
// tone(buzzerGas, 1000); // Đảm bảo rằng buzzerGas vẫn kêu
// tone(buzzerFlame, 2000); // Đảm bảo rằng buzzerFlame vẫn kêu
// String alertMessage = "Both Gas Leak and Fire Detected!";
// client.publish(MQTT_Topic_Alert, alertMessage.c_str());
// }
delay(1000);
}