#include <WiFi.h>
#include <PubSubClient.h>
#include <NewPing.h>
#include <ESP32Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <AccelStepper.h>
// Định nghĩa các chân kết nối đến động cơ
#define motorPin1 25 // IN1 trên module ULN2003
#define motorPin2 33 // IN2 trên module ULN2003
#define motorPin3 32 // IN3 trên module ULN2003
#define motorPin4 35 // IN4 trên module ULN2003
// Khởi tạo đối tượng AccelStepper
AccelStepper stepper(AccelStepper::FULL4WIRE, motorPin1, motorPin2, motorPin3, motorPin4);
const char* SSID = "Wokwi-GUEST";
const char* PASSWORD = "";
const char* MQTT_SERVER = "broker.emqx.io";
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
int LDR_PIN = 34;
int LED_PIN = 23;
#define TRIGGER_PIN 18 // Chân Trigger
#define ECHO_PIN 5 // Chân Echo
#define MAX_DISTANCE 400 // Khoảng cách tối đa để đo, đơn vị cm
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
#define SERVO_PIN 15 // Chân điều khiển Servo
#define OPEN_ANGLE 90 // Góc mở của servo
#define CLOSE_ANGLE 0 // Góc đóng của servo
Servo servo;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(115200);
pinMode(LDR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
setupWiFi();
mqttClient.setServer(MQTT_SERVER, 1883);
servo.attach(SERVO_PIN); // Kết nối servo với chân điều khiển
servo.write(CLOSE_ANGLE); // Đặt servo ở góc đóng ban đầu
//LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Thung rac");
// Đặt tốc độ và gia tốc cho động cơ
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(100.0);
stepper.setSpeed(200);
stepper.moveTo(10000);
// Khởi tạo hàm random
randomSeed(analogRead(0));
}
void setupWiFi() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnectMQTT() {
while (!mqttClient.connected()) {
Serial.println("Attempting MQTT connection...");
if (mqttClient.connect("ESP32Client")) {
Serial.println("Connected to MQTT broker");
mqttClient.publish("esp32/status", "hello from ESP32");
mqttClient.subscribe("esp32/receive");
} else {
Serial.print("MQTT connection failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" Retrying in 5 seconds");
}
}
}
void loop() {
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.loop();
//hong ngoai
// Mô phỏng giá trị từ cảm biến hồng ngoại bằng cách tạo số ngẫu nhiên
int irValue = random(1000);
Serial.println(irValue);
// Nếu giá trị từ cảm biến hồng ngoại mô phỏng lớn hơn 500
if (irValue > 500) {
// if (stepper.distanceToGo() == 0) {
// delay(5000); // Dừng 5 giây
stepper.run();
delay(5000);
// stepper.moveTo(-stepper.currentPosition()); // Đảo chiều
}
// Tiếp tục chạy động cơ
// stepper.run();
else {
// Dừng động cơ
stepper.stop();
}
///anh sang
int ldrValue = analogRead(LDR_PIN);
Serial.println(ldrValue);
digitalWrite(LED_PIN, ldrValue >= 500 ? HIGH : LOW);
//// sieu am
unsigned int distance = sonar.ping_cm();
// In ra Serial Monitor
Serial.print("Khoang cach: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 15) { // Nếu khoảng cách nhỏ hơn 10cm
openDoor(); // Mở cửa
} else {
closeDoor(); // Đóng cửa
}
delay(1000);
if (mqttClient.connected()) {
mqttClient.publish("esp32/ldrsensor", String(ldrValue).c_str());
mqttClient.subscribe("esp32/receive");
}
}
void openDoor() {
servo.write(OPEN_ANGLE); // Mở cửa
delay(1000); // Chờ 1 giây
}
void closeDoor() {
servo.write(CLOSE_ANGLE); // Đóng cửa
delay(1000); // Chờ 1 giây
}