Báo cáo Bài tập 4 (Bài tập 4) - Nguyễn Hà Sơn
Báo cáo bài tập 4 của sinh viên Nguyễn Hà Sơn, bao gồm mã nguồn chương trình ESP32 thu thập dữ liệu cảm biến DHT và PIR, mô phỏng và link bài tập.
Generating preview...
Báo cáo Bài tập 4 Họ và tên: Nguyễn Hà Sơn MSSV: 20252120M 1. Code của chương trình #include <DHT.h> #include <LiquidCrystal_I2C.h> #include <Wire.h> // --- Định nghĩa chân kế t nố i --#define DHT_PIN 15 // Chân GPIO 15 cho DHT22 #define PIR_PIN 12 // Chân GPIO 12 cho PIR Motion Sensor #define LED_PIN 32 // Chân GPIO 32 cho LED // --- Cấ u hình Cảm biế n DHT --#define DHT_TYPE DHT22 DHT dht(DHT_PIN, DHT_TYPE); // --- Cấ u hình Màn hình LCD I2C --// Địa chỉ I2C phổbiế n là 0x27 hoặc 0x3F. LiquidCrystal_I2C lcd(0x27, 16, 2); // --- Cấ u hình thời gian đọc DHT --const long readInterval = 2000; // Đọc dữ liệu 2 giây/lầ n unsigned long previousMillis = 0; // --- Setup --void setup() { Serial.begin(115200); Serial.println("--- Bài 4: Thu thập dữ liệu ESP32 Sensors (Setup moi) ---"); // Khởi tạo các chân I/O pinMode(PIR_PIN, INPUT); pinMode(LED_PIN, OUTPUT); // Khởi tạo cảm biế n DHT dht.begin(); // Khởi tạo và làm sạch LCD lcd.init(); lcd.backlight(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("System Ready..."); } // --- Loop --void loop() { unsigned long currentMillis = millis(); // 1. Đọc dữ liệu DHT định kỳ if (currentMillis - previousMillis >= readInterval) { previousMillis = currentMillis; // Đọc nhiệt độ và độ ẩ m float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); // Kiể m tra dữ liệu hợp lệ if (isnan(humidity) || isnan(temperature)) { Serial.println("DHT Data | Failed to read from DHT sensor!"); // Log lỗ i lcd.setCursor(0, 0); lcd.print("Temp: Read Error!"); lcd.setCursor(0, 1); lcd.print("Humidity: Error!"); } else { // Hiể n thị lên LCD (16x2) lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(temperature, 1); lcd.print((char)223); // Ký tự độ lcd.print("C "); lcd.setCursor(0, 1); lcd.print("Humidity: "); lcd.print(humidity, 1); lcd.print("% "); // Gử i log dữ liệu qua Serial Serial.print("DHT Data | Temp: "); Serial.print(temperature); Serial.print((char)248); Serial.print("C, Humidity: "); Serial.print(humidity); Serial.printl
… Download the original file to read the full document.
- Document name
- Báo cáo Bài tập 4 (Bài tập 4) - Nguyễn Hà Sơn
- School / Course
- Đại học Bách khoa Hà Nội · Internet of things
- Author (in document)
- Nguyễn Hà Sơn
- Content
- Tài liệu mô tả bài tập lập trình ESP32 thu thập dữ liệu nhiệt độ, độ ẩm từ DHT22 và phát hiện chuyển động bằng PIR để điều khiển LED. Mã nguồn, mô phỏng và link bài tập được cung cấp.
- Table of contents
- This document has no clear table of contents.
- Pages
- 3 pages
- Uploaded by
- Giang Le
Frequently asked questions
Is this document free?
Yes. “Báo cáo Bài tập 4 (Bài tập 4) - Nguyễn Hà Sơn” is free — just sign in and click Download to get the original file.
How many pages is this document?
The document has 3 pages, for the course Internet of things. You can preview it online before downloading.
Can I preview before downloading?
Yes. You can preview this document right on this page with the online reader, then decide whether to download.

Comments (0)
No comments yet. Be the first!