#include "SPI.h"
#include "DHT.h"
#include <WiFi.h>
#include <MQTT.h>
#include <WiFiClient.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "ESP32Servo.h"
#define TFT_DC 4
#define TFT_CS 5
#define TFT_MOSI 6
#define TFT_CLK 7
#define TFT_CTRL 45
#define TFT_RST 48
#define DHTTYPE DHT22
#define DHTPIN 20
int LED = 9;
int mSensor1 = 11;
int mSensor2 = 13;
int pirState = LOW;
int val = 0;
int sensor1Count = 0;
int sensor2Count = 0;
float previousHumidity = NAN;
float previousTemperature = NAN;
bool sensor1First = false;
bool sensor2First = false;
unsigned long previousMillis = 0;
const unsigned long interval = 60000;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST);
DHT dht(DHTPIN, DHTTYPE);
WiFiClient TCPclient;
MQTTClient mqtt;
Servo sm;
void onMessageReceived(String &topic, String &payload) {
if (topic == "/yuki/feeder") {
Feeder1(payload);
} else if (topic == "/yuki/light") {
Light(payload);
}
}
void setup() {
Serial.begin(115200);
dht.begin();
tft.begin();
pinMode(LED, OUTPUT);
pinMode(mSensor1, INPUT);
pinMode(mSensor2, INPUT);
pinMode(10, INPUT_PULLUP);
WiFi.mode(WIFI_STA);
sm.attach(19);
sm.write(0);
const uint8_t mode = 0xc8;
tft.sendCommand(ILI9341_MADCTL, &mode, 1);
tft.setCursor(96, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Smart Home");
tft.setCursor(10, 200);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
tft.setCursor(10, 200);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
tft.setCursor(10, 200);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connected");
mqtt.begin("yuki-smart-home.cloud.shiftr.io", 1883, TCPclient);
while(!mqtt.connect("SmartHome", "yuki-smart-home", "FIeSpBqRvxpyXFXd")) {
Serial.print(".");
delay(1000);
}
tft.setCursor(10, 200);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print("Connected");
tft.setCursor(10, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("H+");
tft.setCursor(10, 60);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Humidity: ");
tft.setCursor(10, 90);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Temperature: ");
tft.setCursor(10, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Feeder: ");
tft.setCursor(10, 150);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Light: ");
mqtt.onMessage(onMessageReceived);
mqtt.subscribe("/yuki/feeder");
mqtt.subscribe("/yuki/light");
}
void loop() {
mqtt.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Humidity();
Temperature();
}
int value = digitalRead(10);
Feeder(value);
int sensor1State = digitalRead(mSensor1);
int sensor2State = digitalRead(mSensor2);
// Перевірка активації першого датчика
if (sensor1State == HIGH && !sensor1First && !sensor2First) {
sensor1First = true;
sensor1Count++;
delay(500); // Затримка для уникнення багаторазової активації від одного переривання
while(digitalRead(mSensor1) == HIGH); // Очікування поки датчик повернеться в LOW
}
// Перевірка активації другого датчика після першого
if (sensor2State == HIGH && sensor1First && !sensor2First) {
sensor2First = true;
sensor2Count++;
digitalWrite(LED, HIGH);
delay(500); // Затримка для уникнення багаторазової активації від одного переривання
while(digitalRead(mSensor2) == HIGH); // Очікування поки датчик повернеться в LOW
}
// Перевірка активації другого датчика
if (sensor2State == HIGH && !sensor2First && !sensor1First) {
sensor2First = true;
sensor2Count--;
delay(500); // Затримка для уникнення багаторазової активації від одного переривання
while(digitalRead(mSensor2) == HIGH); // Очікування поки датчик повернеться в LOW
}
// Перевірка активації першого датчика після другого
if (sensor1State == HIGH && sensor2First && !sensor1First) {
sensor1First = true;
sensor1Count--;
delay(500); // Затримка для уникнення багаторазової активації від одного переривання
while(digitalRead(mSensor1) == HIGH); // Очікування поки датчик повернеться в LOW
}
// Скидання послідовності після відпускання датчиків
if (sensor1State == LOW && sensor2State == LOW) {
sensor1First = false;
sensor2First = false;
}
if(sensor1Count == 0 && sensor2Count == 0) {
digitalWrite(LED, LOW);
}
}
void Humidity() {
delay(100);
tft.setCursor(130, 60);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
float h = dht.readHumidity();
if (isnan(h)) {
tft.print(("Failed to read from DHT sensor!"));
return;
}
if (!isnan(previousHumidity)) {
tft.setTextColor(ILI9341_BLACK);
tft.print(previousHumidity);
tft.print(" %");
}
delay(100);
tft.setCursor(130, 60);
tft.setTextColor(ILI9341_WHITE);
tft.print(h);
tft.print(" %");
previousHumidity = h;
}
void Temperature() {
tft.setCursor(160, 90);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
float t = dht.readTemperature();
if (isnan(t)) {
tft.print(("Failed to read from DHT sensor!"));
return;
}
if (!isnan(previousTemperature)) {
tft.setTextColor(ILI9341_BLACK);
tft.print(previousTemperature, 0);
tft.print(" C");
}
delay(100);
tft.setCursor(160, 90);
tft.setTextColor(ILI9341_WHITE);
tft.print(t, 0);
tft.print(" C");
previousTemperature = t;
delay(100);
}
void Feeder(int value) {
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("disabled");
if(value == LOW)
{
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.println("disabled");
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.println("in process");
delay(1000);
sm.write(120);
delay(2000);
sm.write(0);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.println("in process");
delay(100);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.print("ok!");
delay(1000);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.print("ok!");
delay(1000);
}
}
void Feeder1(String &payload) {
int activate = payload.toInt();
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("disabled");
if(activate == LOW)
{
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.println("disabled");
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.println("in process");
delay(1000);
sm.write(120);
delay(2000);
sm.write(0);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.println("in process");
delay(100);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_WHITE);
tft.print("ok!");
delay(1000);
tft.setCursor(110, 120);
tft.setTextColor(ILI9341_BLACK);
tft.print("ok!");
delay(1000);
}
}
void Light(String &payload) {
int activated = payload.toInt();
if(activated == 1) {
digitalWrite(LED, HIGH);
sensor1Count++;
sensor2Count++;
} else if(activated == 0) {
digitalWrite(LED, LOW);
sensor1Count--;
sensor2Count--;
}
}Loading
esp32-s3-box
esp32-s3-box