#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
//
// WIFI
//
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//
// MQTT
//
const char* mqtt_server = "broker.mqttdashboard.com";
WiFiClient espClient;
PubSubClient client(espClient);
//
// LCD
//
LiquidCrystal_I2C lcd(0x27, 20, 4);
//
// DHT22
//
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//
// SENSOR
//
#define PIR_PIN 33
#define LDR_PIN 32
//
// RELAY
//
#define RELAY1 5
#define RELAY2 18
#define RELAY3 19
#define RELAY4 4
#define RELAY5 27
//
// STATUS LED
//
#define STATUS_LED 2
unsigned long lastMsg = 0;
//
// WIFI CONNECT
//
void setup_wifi() {
Serial.println();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
}
//
// MQTT CALLBACK
//
void callback(char* topic, byte* payload, unsigned int length) {
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.print("Topic: ");
Serial.println(topic);
Serial.print("Message: ");
Serial.println(message);
//
// RELAY 1
//
if (String(topic) == "dalat/smarthome/relay1/set") {
digitalWrite(RELAY1, message == "ON");
client.publish(
"dalat/smarthome/relay1/state",
message.c_str(),
true
);
}
//
// RELAY 2
//
if (String(topic) == "dalat/smarthome/relay2/set") {
digitalWrite(RELAY2, message == "ON");
client.publish(
"dalat/smarthome/relay2/state",
message.c_str(),
true
);
}
//
// RELAY 3
//
if (String(topic) == "dalat/smarthome/relay3/set") {
digitalWrite(RELAY3, message == "ON");
client.publish(
"dalat/smarthome/relay3/state",
message.c_str(),
true
);
}
//
// RELAY 4
//
if (String(topic) == "dalat/smarthome/relay4/set") {
digitalWrite(RELAY4, message == "ON");
client.publish(
"dalat/smarthome/relay4/state",
message.c_str(),
true
);
}
//
// RELAY 5
//
if (String(topic) == "dalat/smarthome/relay5/set") {
digitalWrite(RELAY5, message == "ON");
client.publish(
"dalat/smarthome/relay5/state",
message.c_str(),
true
);
}
}
//
// MQTT RECONNECT
//
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting MQTT...");
if (client.connect("DALAT_ESP32")) {
Serial.println("Connected");
client.subscribe("dalat/smarthome/relay1/set");
client.subscribe("dalat/smarthome/relay2/set");
client.subscribe("dalat/smarthome/relay3/set");
client.subscribe("dalat/smarthome/relay4/set");
client.subscribe("dalat/smarthome/relay5/set");
} else {
Serial.print("Failed MQTT: ");
Serial.println(client.state());
delay(2000);
}
}
}
//
// SETUP
//
void setup() {
Serial.begin(115200);
//
// RELAY
//
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(RELAY3, OUTPUT);
pinMode(RELAY4, OUTPUT);
pinMode(RELAY5, OUTPUT);
//
// SENSOR
//
pinMode(PIR_PIN, INPUT);
//
// LED
//
pinMode(STATUS_LED, OUTPUT);
//
// LCD
//
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("SMART HOME");
//
// DHT
//
dht.begin();
//
// WIFI
//
setup_wifi();
//
// MQTT
//
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
//
// LOOP
//
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
if (now - lastMsg > 3000) {
lastMsg = now;
//
// DHT
//
float temp = dht.readTemperature();
float hum = dht.readHumidity();
//
// PIR
//
int motion = digitalRead(PIR_PIN);
//
// LDR
//
int lightValue = analogRead(LDR_PIN);
//
// MQTT PUBLISH
//
client.publish(
"dalat/smarthome/sensor/temperature",
String(temp).c_str(),
true
);
client.publish(
"dalat/smarthome/sensor/humidity",
String(hum).c_str(),
true
);
client.publish(
"dalat/smarthome/sensor/motion",
String(motion).c_str(),
true
);
client.publish(
"dalat/smarthome/sensor/light",
String(lightValue).c_str(),
true
);
//
// LCD
//
lcd.clear();
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print(temp);
lcd.setCursor(0,1);
lcd.print("H:");
lcd.print(hum);
lcd.setCursor(0,2);
lcd.print("L:");
lcd.print(lightValue);
lcd.setCursor(0,3);
if (motion) {
lcd.print("Motion:YES");
} else {
lcd.print("Motion:NO");
}
Serial.println("Published");
}
}