#include <WiFi.h>
#include <FirebaseESP32.h>
#include <DHT.h>
#include <Stepper.h>
#include <Ultrasonic.h>
#define DHT_PIN 2 // Chân kết nối của cảm biến DHT
DHT dht(DHT_PIN, DHT22); // Khởi tạo đối tượng DHT
/* 2. Define the API Key */
#define API_KEY "AIzaSyACr-0jAZ4ZHAzqDkjipZc0mDzNkQMWmrY"
/* 3. Define the RTDB URL */
#define DATABASE_URL "https://baocaoiot-5c55e-default-rtdb.firebaseio.com/"
/* 4. Define the user Email and password that already registered or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "Thao2003@"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
FirebaseData firebasedata;
FirebaseJson json;
String path = "/";
WiFiClient client;
byte x = 0;
unsigned long previousMillis = 0;
const long interval = 5000; // interval at which to blink (milliseconds)
bool ledState = false;
int led1 = 13;
int led2 = 25;
int led3 = 33;
int loa = 15;
int sensorPin = 4;
int gas = 32;
const int ledPin = 2;
int trig = 23;
int echo = 22;
Ultrasonic kc(trig, echo);
const int stepsPerRevolution = 100;
//ham tinh trung binh
const int numReadings = 7;
int readings[numReadings];
float total = 0;
float average = 0;
bool motor = false;
bool den = false;
bool motornhietdo = false;
Stepper myStepper(stepsPerRevolution, 2, 18, 19, 21);
Stepper nhietdoStepper(stepsPerRevolution, 12, 14, 27, 26);
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 setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
myStepper.setSpeed(60);
nhietdoStepper.setSpeed(60);
dht.begin();
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
WIFIConnect();
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(loa, OUTPUT);
pinMode(gas, OUTPUT);
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
// Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
Firebase.reconnectNetwork(true);
// Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
// Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred.
fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);
// Limit the size of response payload to be collected in FirebaseData
fbdo.setResponseSize(2048);
Firebase.begin(&config, &auth);
Firebase.setDoubleDigits(5);
config.timeout.serverResponse = 10 * 1000;
}
void loop() {
unsigned long currentMillis = millis();
long duration = pulseIn(echo, HIGH);
float distance = kc.read();
int anhsang = analogRead(A0);
int Gas = analogRead(gas);
int val = digitalRead(sensorPin);
int dosang = map(anhsang, 32, 4063, 100, 0);
Serial.println(anhsang);
int doAm = dht.readHumidity(); // Đọc độ ẩm
int nhietDo = dht.readTemperature();
Firebase.setInt(fbdo, "NhietDo", nhietDo);
Firebase.setInt(fbdo, "DoAm", doAm);
Firebase.setInt(fbdo, "Anhsang", anhsang);
Firebase.setInt(fbdo, "Chuyendong", val);
Firebase.setInt(fbdo, "Khoangcach", distance);
Firebase.setInt(fbdo, "Gas", Gas);
Serial.print("Khoang cach: ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Gas: ");
Serial.println(Gas);
Serial.print("Chuyen dong : ");
Serial.println(val);
if (Firebase.ready() && (currentMillis - previousMillis >= interval)) {
previousMillis = currentMillis;
// Toggle the LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Update the state to Firebase
if (Firebase.setInt(fbdo, "led/state", ledState)) {
Serial.println("Firebase updated successfully");
} else {
Serial.println(fbdo.errorReason().c_str());
}
}
if (Firebase.getInt(firebasedata, path + "khoangcach"))
{
x = firebasedata.intData();
Serial.println(x);
Serial.println("");
}
// // Reset total to zero before calculating average
total = 0;
for (int i = 0; i < numReadings; i++) {
readings[i] = analogRead(A0);
total += readings[i];
delay(100); // Đợi 100ms giữa các lần đo để có kết quả chính xác hơn
}
average = total / numReadings;
Serial.print("Gia tri nhiet do trung binh: ");
Serial.println(average);
if (average >= 1000) {
digitalWrite(led2, HIGH);
Serial.println("led nhiet do bat");
nhietdoStepper.step(stepsPerRevolution);
} else {
digitalWrite(led2, LOW);
Serial.println("led nhiet do tắt");
}
// cảm biến ánh sáng
if (anhsang <= 50) {
digitalWrite(led1, HIGH);
Serial.println("led độ sáng bật");
} else {
digitalWrite(led1, LOW);
}
if (distance < 100)
{
digitalWrite(loa, HIGH);
Serial.println("Cảnh báo nguy hiểm!");
}
else
{
digitalWrite(loa, LOW);
}
}