Circuit Documentation
Summary
This circuit is designed to create a "Smart Fridge" system using an ESP32 microcontroller, an RFID reader (RFID-RC522), and a 20x4 I2C LCD display. The system reads RFID tags to identify items, displays information on the LCD, and stores data in the ESP32's SPIFFS file system.
Component List
Vietduino Wifi BLE ESP32
- Description: A versatile microcontroller with WiFi and Bluetooth capabilities.
- Pins: GND, 5V, IO_0, 18, 19, 23, 5, 13, 2, 14, 27, 26, 25, 17, 16, 1, 3, 3V3, RST, VIN, 35, 34, 33, 32, 4
Lcd 20x4 i2c
- Description: A 20x4 character LCD display with I2C interface.
- Pins: GND, 5V, SCA, SCL
RFID-RC522
- Description: An RFID reader module used for reading RFID tags.
- Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA
Wiring Details
Vietduino Wifi BLE ESP32
- GND: Connected to GND of RFID-RC522 and Lcd 20x4 i2c
- 5V: Connected to 5V of Lcd 20x4 i2c
- 3V3: Connected to VCC (3.3V) of RFID-RC522
- Pin 18: Connected to SCK of RFID-RC522
- Pin 19: Connected to MISO of RFID-RC522
- Pin 23: Connected to MOSI of RFID-RC522
- Pin 5: Connected to SDA of RFID-RC522 and SCL of Lcd 20x4 i2c
- Pin 13: Connected to RST of RFID-RC522
- Pin 4: Connected to SCA of Lcd 20x4 i2c
Lcd 20x4 i2c
- GND: Connected to GND of Vietduino Wifi BLE ESP32
- 5V: Connected to 5V of Vietduino Wifi BLE ESP32
- SCA: Connected to Pin 4 of Vietduino Wifi BLE ESP32
- SCL: Connected to Pin 5 of Vietduino Wifi BLE ESP32
RFID-RC522
- VCC (3.3V): Connected to 3V3 of Vietduino Wifi BLE ESP32
- RST: Connected to Pin 13 of Vietduino Wifi BLE ESP32
- GND: Connected to GND of Vietduino Wifi BLE ESP32
- SCK: Connected to Pin 18 of Vietduino Wifi BLE ESP32
- MISO: Connected to Pin 19 of Vietduino Wifi BLE ESP32
- MOSI: Connected to Pin 23 of Vietduino Wifi BLE ESP32
- SDA: Connected to Pin 5 of Vietduino Wifi BLE ESP32
Code Documentation
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MFRC522.h>
#include <FS.h>
#include <SPIFFS.h>
// Khai báo các chân kết nối
#define RST_PIN 13
#define SS_PIN 5
// Khai báo LCD và RFID
LiquidCrystal_I2C lcd(0x27, 20, 4); // Địa chỉ I2C là 0x27
MFRC522 rfid(SS_PIN, RST_PIN);
void setup() {
Wire.begin(4 ,15);
// Cấu hình Serial
Serial.begin(115200);
// Cấu hình LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart Fridge");
// Cấu hình RFID
SPI.begin();
rfid.PCD_Init();
delay(1000);
// Khởi tạo SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("Failed to initialize SPIFFS!");
lcd.setCursor(0, 1);
lcd.print("SPIFFS Error!");
while (true);
}
// Hướng dẫn sử dụng
Serial.println("===== Smart Fridge =====");
Serial.println("1. Scan card to read/write data.");
Serial.println("2. Data will be saved to the card's UID.");
Serial.println("3. Enter DELETE:<UID> to delete data.");
}
void loop() {
// Kiểm tra thẻ RFID
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
// Đọc UID của thẻ
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
uid += String(rfid.uid.uidByte[i], HEX);
}
uid.toUpperCase();
Serial.println("Card UID: " + uid);
// Kiểm tra xem file UID đã tồn tại chưa
String fileName = "/" + uid + ".txt";
if (SPIFFS.exists(fileName)) {
// Đọc file và hiển thị thông tin
File file = SPIFFS.open(fileName, "r");
if (!file) {
Serial.println("Failed to open file!");
return;
}
// Đọc dữ liệu từ file
String name = file.readStringUntil('\n');
String addedDate = file.readStringUntil('\n');
String expiryDate = file.readStringUntil('\n');
// Loại bỏ khoảng trắng thừa
name.trim();
addedDate.trim();
expiryDate.trim();
// Hiển thị lên LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Name: " + name);
lcd.setCursor(0, 1);
lcd.print("Added: " + addedDate);
lcd.setCursor(0, 2);
lcd.print("Expiry: " + expiryDate);
file.close();
delay(5000);
} else {
// Yêu cầu nhập thông tin và lưu file mới
Serial.println("New card detected. Enter product information.");
Serial.println("Enter product name:");
String productName = readSerialInput();
Serial.println("Enter date added (DD/MM/YYYY):");
String dateAdded = readSerialInput();
Serial.println("Enter expiry date (DD/MM/YYYY):");
String expiryDate = readSerialInput();
// Lưu thông tin vào file
File file = SPIFFS.open(fileName, "w");
if (!file) {
Serial.println("Failed to create file!");
return;
}
file.println(productName);
file.println(dateAdded);
file.println(expiryDate);
file.close();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Info Saved!");
delay(2000);
}
// Quay lại trạng thái "Smart Fridge"
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart Fridge");
delay(1000);
// Dừng quét thẻ
rfid.PICC_HaltA();
// Kiểm tra lệnh xóa từ Serial Monitor
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n');
command.trim();
if (command.startsWith("DELETE:")) {
String deleteUID = command.substring(7); // Lấy UID từ lệnh "DELETE:<UID>"
deleteUID.toUpperCase(); // Đảm bảo UID là chữ hoa
String deleteFileName = "/" + deleteUID + ".txt";
if (SPIFFS.exists(deleteFileName)) {
// Xóa file nếu thẻ đã tồn tại
Serial.println("Deleting file: " + deleteFileName); // Debugging thông báo
bool deleted = SPIFFS.remove(deleteFileName);
if (deleted) {
Serial.println("File deleted: " + deleteFileName);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Card Deleted!");
} else {
Serial.println("Error deleting file.");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Error Deleting!");
}
delay(2000);
} else {
// Nếu thẻ không tồn tại
Serial.println("No data found for UID: " + deleteUID);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No data found!");
delay(2000);
}
}
}
}
// Hàm đọc chuỗi từ Serial Monitor với xử lý lỗi WDT
String readSerialInput() {
String input = "";
while (true) {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n')
