#include <Wire.h>
#include <DHT.h>
#include <WiFi.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
/// Đánh dấu các chân cắm của các linh kiện điện tử
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_SCL 22 // Màn SSD1306 OLED
#define OLED_SDA 21 //
#define aLED_PIN 19 // Led thông báo hệ thống đã khởi động
#define wLED_PIN 18 // Led thông báo cần tưới nước
#define RELAY_PIN 5 // Relay cấp điện máy bơm
#define RTC_SDA 26 // RTC DS1307
#define RTC_SCL 27 //
#define BUZZER_PIN 23 // Loa
#define LDR_PIN 32 // Cảm biến cường độ ánh sáng
#define SOIL_PIN 34 // Cảm biến độ ẩm đất
#define DHT_PIN 35 // Cảm biến độ ẩm và nhiệt độ không khí
#define DHTTYPE DHT22
/// Setup kết nối wifi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
void wifiConnect() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
// Cấu hình DHT22
DHT dht(DHT_PIN, DHTTYPE);
// Cấu hình RTC DS1307
RTC_DS1307 rtc;
// Cấu hình màn SSD1306 OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Hai đối tượng TwoWire cho hai giao tiếp I2C
TwoWire I2C_1 = TwoWire(0); // Wire sử dụng giao tiếp I2C đầu tiên
TwoWire I2C_2 = TwoWire(1); // Wire sử dụng giao tiếp I2C thứ hai
void setup() {
Serial.begin(9600);
// Khởi tạo DHT, I2C và OLED
dht.begin();
// Khởi tạo RTC
I2C_1.begin(RTC_SDA, RTC_SCL); // Khởi tạo I2C cho RTC
if (!rtc.begin(&I2C_1)) {
Serial.println("Couldn't find RTC");
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Khởi tạo màn hình OLED
I2C_2.begin(OLED_SDA, OLED_SCL); // Khởi tạo I2C cho OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C, &I2C_2)) {
Serial.println(F("SSD1306 allocation failed"));
}
display.display();
delay(2000);
display.clearDisplay();
// Thiết lập các chân GPIO
pinMode(aLED_PIN, OUTPUT);
pinMode(wLED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(aLED_PIN, HIGH); // Thông báo việc khởi động hoàn thành
Serial.print("Connecting to WiFi");
wifiConnect();
}
String soilStatus() {
int soilValue = analogRead(SOIL_PIN);
String msg = soilValue < 2165 ? "WET" : soilValue > 3135 ? "DRY" : "OK";
return msg;
}
void loop() {
// Đọc dữ liệu từ các cảm biến
float h = dht.readHumidity();
float t = dht.readTemperature();
int ldrValue = analogRead(LDR_PIN);
Serial.print("Temp: ");
Serial.print(t);
Serial.println(" C");
// Hiển thị dữ liệu lên màn hình OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(t);
display.print(" C");
display.setCursor(0, 10);
display.print("Hum: ");
display.print(h);
display.print(" %");
display.setCursor(0, 20);
display.print("LDR: ");
display.print(ldrValue);
display.setCursor(0, 30);
display.print("Soil: ");
display.print(soilStatus());
// Đọc thời gian từ RTC và hiển thị
DateTime now = rtc.now();
display.setCursor(0, 40);
display.print("Time: ");
display.print(now.hour());
display.print(":");
display.print(now.minute());
display.print(":");
display.print(now.second());
display.display();
// Nhấp nháy đèn LED
digitalWrite(wLED_PIN, HIGH);
delay(5000);
digitalWrite(wLED_PIN, LOW);
delay(5000);
}