#include <WiFi.h>
#include <HTTPClient.h>
// Firebase config
#define FIREBASE_HOST "https://test1-9b7bb-default-rtdb.firebaseio.com/"
#define FIREBASE_AUTH "qNl9Iel3G2XzIjtvSX1ne7F6xfod6iZc3FYFVnby"
// ThingSpeak config
#define THINGSPEAK_API_KEY "2769390" // Thay bằng API key của bạn
#define THINGSPEAK_SERVER "ATLGBUDWE05E5VS9"
// Wi-Fi config
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// DHT sensor config
#define DHTPIN 12
// HC-SR04 config
#define TRIG_PIN 5
#define ECHO_PIN 4
// LED config
#define LED_RED 26
#define LED_GREEN 14
// PIR sensor config
#define PIR_PIN 27
float temperature = 0.0;
float humidity = 0.0;
bool motionDetected = false; // Trạng thái chuyển động
// Kết nối Wi-Fi
void connectWiFi() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(" Connected to Wi-Fi!");
}
// Đọc dữ liệu từ cảm biến DHT22
bool readDHT22(float &temp, float &hum) {
uint8_t data[5] = {0, 0, 0, 0, 0};
uint8_t checksum;
pinMode(DHTPIN, OUTPUT);
digitalWrite(DHTPIN, LOW);
delay(20);
digitalWrite(DHTPIN, HIGH);
delayMicroseconds(30);
pinMode(DHTPIN, INPUT_PULLUP);
unsigned long start = micros();
while (digitalRead(DHTPIN) == HIGH) {
if (micros() - start > 40) return false;
}
for (int i = 0; i < 40; i++) {
while (digitalRead(DHTPIN) == LOW);
start = micros();
while (digitalRead(DHTPIN) == HIGH);
unsigned long duration = micros() - start;
data[i / 8] <<= 1;
if (duration > 50) {
data[i / 8] |= 1;
}
}
checksum = data[0] + data[1] + data[2] + data[3];
if (checksum != data[4]) return false;
hum = ((data[0] << 8) | data[1]) * 0.1;
temp = ((data[2] & 0x7F) << 8 | data[3]) * 0.1;
if (data[2] & 0x80) temp = -temp;
return true;
}
// Đọc khoảng cách từ cảm biến HC-SR04
float readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration * 0.034) / 2;
return distance;
}
// Đọc dữ liệu từ cảm biến PIR
bool readPIR() {
return digitalRead(PIR_PIN) == HIGH;
}
// Gửi dữ liệu lên Firebase
void sendDataToFirebase(float temp, float hum, float distance, bool motion) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Gửi nhiệt độ
String url = String(FIREBASE_HOST) + "/Temperature.json?auth=" + FIREBASE_AUTH;
http.begin(url);
http.addHeader("Content-Type", "application/json");
String payload = "{\"temperature\": " + String(temp) + "}";
int httpCode = http.POST(payload);
Serial.print("Temperature HTTP Response: ");
Serial.println(httpCode);
http.end();
// Gửi độ ẩm
url = String(FIREBASE_HOST) + "/Humidity.json?auth=" + FIREBASE_AUTH;
http.begin(url);
payload = "{\"humidity\": " + String(hum) + "}";
httpCode = http.POST(payload);
Serial.print("Humidity HTTP Response: ");
Serial.println(httpCode);
http.end();
// Gửi khoảng cách
url = String(FIREBASE_HOST) + "/Distance.json?auth=" + FIREBASE_AUTH;
http.begin(url);
payload = "{\"distance\": " + String(distance) + "}";
httpCode = http.POST(payload);
Serial.print("Distance HTTP Response: ");
Serial.println(httpCode);
http.end();
// Gửi trạng thái chuyển động
url = String(FIREBASE_HOST) + "/Motion.json?auth=" + FIREBASE_AUTH;
http.begin(url);
payload = "{\"motion\": " + String(motion ? "true" : "false") + "}";
httpCode = http.POST(payload);
Serial.print("Motion HTTP Response: ");
Serial.println(httpCode);
http.end();
} else {
Serial.println("Wi-Fi not connected!");
}
}
// Gửi dữ liệu lên ThingSpeak
void sendDataToThingSpeak(float temp, float hum, float distance, bool motion) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Xây dựng URL cho ThingSpeak
String url = String(THINGSPEAK_SERVER) +
"?api_key=" + THINGSPEAK_API_KEY +
"&field1=" + String(temp) +
"&field2=" + String(hum) +
"&field3=" + String(distance) +
"&field4=" + String(motion ? "1" : "0");
http.begin(url);
int httpCode = http.GET(); // Gửi GET request
if (httpCode > 0) {
Serial.print("ThingSpeak HTTP Response: ");
Serial.println(httpCode);
} else {
Serial.println("Failed to send data to ThingSpeak!");
}
http.end();
} else {
Serial.println("Wi-Fi not connected!");
}
}
void setup() {
Serial.begin(115200);
connectWiFi();
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_RED, OUTPUT); // Cài đặt LED đỏ là OUTPUT
pinMode(LED_GREEN, OUTPUT); // Cài đặt LED xanh là OUTPUT
pinMode(PIR_PIN, INPUT); // Cài đặt cảm biến PIR là INPUT
digitalWrite(LED_RED, LOW); // Tắt LED đỏ ban đầu
digitalWrite(LED_GREEN, LOW); // Tắt LED xanh ban đầu
}
void loop() {
// Đọc DHT22
if (readDHT22(temperature, humidity)) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" \u00b0C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
Serial.println("Failed to read from DHT sensor!");
}
// Đọc HC-SR04
float distance = readDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Đọc PIR
motionDetected = readPIR();
Serial.print("Motion detected: ");
Serial.println(motionDetected ? "Yes" : "No");
// Gửi dữ liệu lên Firebase
sendDataToFirebase(temperature, humidity, distance, motionDetected);
// Gửi dữ liệu lên ThingSpeak
sendDataToThingSpeak(temperature, humidity, distance, motionDetected);
// Điều kiện LED đỏ: chuyển động + độ ẩm < 50%
if (motionDetected && humidity < 50) {
digitalWrite(LED_RED, HIGH);
delay(500);
digitalWrite(LED_RED, LOW);
delay(500);
}
// Điều kiện LED xanh: nhiệt độ 20-50\u00b0C, độ ẩm > 60%, không có chuyển động
else if (!motionDetected && temperature >= 20 && temperature <= 50 && humidity > 60) {
digitalWrite(LED_GREEN, HIGH);
} else {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
}
delay(2000); // Đợi 2 giây trước khi đọc lại
}