#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);
void DHTReady(){
dht.begin();
}
// Cấu hình RTC DS1307
RTC_DS1307 rtc;
void RTCReady(){
Wire.begin(RTC_SDA, RTC_SCL);
// while (!rtc.begin()) {
// Serial.println("Couldn't find RTC");
// }
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// Uncomment the next line to set the date and time to the time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
// Cấu hình màn SSD1306 OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void OLEDReady(){
Wire.begin(OLED_SDA, OLED_SCL);
// while(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // or Address 0x3D for 128x64
// Serial.println(F("SSD1306 allocation failed"));
// }
display.display();
delay(2000);
display.clearDisplay();
}
String soilStatus(){
int soilValue = analogRead(SOIL_PIN);
String msg = soilValue < 2165 ? "WET" : soilValue > 3135 ? "DRY" : "OK";
return msg;
}
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(115200);
while(!Serial);
dht.begin();
//I2C_1.begin(RTC_SDA, RTC_SCL);
I2C_2.begin(OLED_SDA, OLED_SCL);
I2C_2.beginTransmission(0x3C);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
I2C_2.endTransmission();
pinMode(aLED_PIN, OUTPUT);
pinMode(wLED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.print("Connecting to WiFi");
wifiConnect();
analogWrite(aLED_PIN, HIGH); // Thông báo việc khởi động hoàn thành
}
void loop()
{
// Đọc dữ liệu từ các cảm biến và hiển thị lên OLED (hoặc thực hiện các hành động khác)
float h = dht.readHumidity();
float t = dht.readTemperature();
int ldrValue = analogRead(LDR_PIN);
Serial.print("Temp: ");
Serial.print(t);
Serial.println(" C");
I2C_2.beginTransmission(0x3C);
// 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());
display.display();
I2C_2.endTransmission();
delay(5000); // Chờ 2 giây trước khi lặp lại
}