#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include "DHTesp.h"
#include <Stepper.h>
//-----------------------------------------------------------//
//chân cảm biến DHT22
const int DHT_PIN = 15;
//chân Led
const int LED_PIN = 13;
//chân cảm biến ánh sáng
const int photoresistorPin = 33;
//chân cảm biến siêu âm
const int trigPin = 5;
const int echoPin = 18;
//xác định tốc độ âm thanh tính bằng cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
//chân cảm biến hồng ngoại thụ động
const int pir = 19;
//chân motor
float stepmax = 600;
Stepper Stepper1(stepmax, 12, 14, 27, 26);
int i = 0;
float step1;
float steptarget1 = 500;
float deltaT = 5;
float speed1;
//chân buzzer
const int Buzzer = 21;
//-----------------------------------------------------------//
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int WIFI_CHANNEL = 6;
const char* server = "api.thingspeak.com";
const String apiKey = "AJ1RPII8U9WHAKH7";
const String channelId = "2514553";
DHTesp dhtSensor;
WiFiClient client;
//-----------------------------------------------------------//
void setup() {
Serial.begin(115200);
//khai báo DHT22
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//khai báo Led
pinMode(LED_PIN, OUTPUT);
//khai báo cảm biến ánh sáng
pinMode(photoresistorPin, INPUT);
//khai báo cảm biến siêu âm
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
//khai báo cảm biến cảm biến hồng ngoại thụ động
pinMode(pir,INPUT);
//khai báo motor
speed1 = ((steptarget1 * 60) / (deltaT * stepmax));
Stepper1.setSpeed(speed1);
//khai báo buzzer
pinMode(Buzzer, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASSWORD, WIFI_CHANNEL);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
}
void gerak(float k) {
step1 = (steptarget1) * (k / (1000));
Stepper1.step(step1);
}
void stop() {
step1 = step1;
}
//-----------------------------------------------------------//
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
// Read value from photoresistor
int lightIntensity = analogRead(photoresistorPin);
// Control LED based on temperature and humidity
if (data.temperature > 35 || data.temperature < 12 || data.humidity > 70 || data.humidity < 40) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance
float distanceCm = duration * SOUND_SPEED / 2;
if (distanceCm < 200) {
// Nếu khoảng cách nhỏ hơn 200cm, motor sẽ chạy theo chiều thuận
Stepper1.step(steptarget1);
step1 += steptarget1; // Cập nhật vị trí hiện tại của motor
} else {
// Nếu khoảng cách lớn hơn hoặc bằng 200cm, motor sẽ quay ngược lại và dừng lại ở vị trí 0
Stepper1.step(-step1); // Di chuyển motor về vị trí 0 ngay lập tức
delay(100); // Đợi một lát để motor dừng lại
stop(); // Dừng motor và đặt vị trí hiện tại về 0
}
const int IP = digitalRead(pir);
if (IP == HIGH) {
// Phát ra tiếng từ buzzer với tần số 1000Hz trong 100ms
tone(Buzzer, 1000);
} else {
// Tắt tiếng từ buzzer
noTone(Buzzer);
}
// Send data to ThingSpeak
String url = "http://api.thingspeak.com/update?api_key=" + apiKey + "&field1=" + String(data.temperature) +
"&field2=" + String(data.humidity) + "&field3=" + String(lightIntensity) + "&field4=" + String(distanceCm) +
"&field5=" + String(IP);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(httpCode));
}
http.end();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Light Intensity: " + String(lightIntensity));
Serial.println("Distance: " + String(distanceCm) + "Cm");
Serial.println("Pir: " + String(IP));
Serial.println("---");
delay(10000);
}