#include <DHT_U.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ESP32Servo.h>
// Defining the pins
#define DHTPIN 12
#define LED 33
#define SERVO_PIN 4
// DHT parameters
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
// Servo motor
Servo servo;
const char* ssid = "Wokwi-GUEST"; // Setting AP SSID
const char* password = ""; // Setting AP password
const char* mqttServer = "broker.hivemq.com"; // MQTT broker
const char* topic = "weather"; // Publish topic
const char* clientID = "ESP32-wokwi";
// Parameters for using non-blocking delay
unsigned long previousMillis = 0;
const long interval = 1000;
float temp, hum;
// Setting up the WiFi and MQTT client
WiFiClient espClient; // Creates a WiFi client (espClient) to connect to a WiFi network.
PubSubClient client(espClient); // Creates an MQTT client (client) that uses the WiFi client (espClient) for network communication.
// CONNECTING TO WIFI
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address:");
Serial.println(WiFi.localIP());
}
// RECONNECTS TO MQTT BROKER IF THE BROKER IS DISCONNECTED.
void reconnect() {
while (!client.connected()) {
if (client.connect(clientID)) {
Serial.println("MQTT connected");
client.subscribe("lights");
client.subscribe("servo");
Serial.println("Topics subscribed");
} else {
Serial.print("Failed rc=");
Serial.print(client.state());
Serial.println(" Try again in 5 seconds");
delay(5000);
}
}
}
// Subscribe callback, HANDLES INCOMING MQTT Messages.
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message: ");
String data = "";
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
data += (char)payload[i];
}
Serial.println();
Serial.print("Message size: ");
Serial.println(length);
Serial.println(".............");
Serial.println(data);
if (String(topic) == "lights") {
if (data == "ON") {
Serial.println("LED ON");
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
} else if (String(topic) == "servo") {
int degree = data.toInt(); // Convert degree to integer
Serial.print("Moving servo to degree: ");
Serial.println(degree);
servo.write(degree); // Move to the specified degree
}
}
// INITIATING the ESP32, sensors, and Wi-Fi
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
servo.attach(SERVO_PIN);
dht.begin();
delayMS = 2000;
setup_wifi();
client.setServer(mqttServer, 1883);
client.setCallback(callback);
}
// Runs continuously, reading sensor data and publishing to MQTT
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Reading temperature and humidity from DHT22 sensor
sensors_event_t event;
dht.temperature().getEvent(&event);
if (!isnan(event.temperature)) {
temp = event.temperature;
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
}
dht.humidity().getEvent(&event);
if (!isnan(event.relative_humidity)) {
hum = event.relative_humidity;
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
}
// Publishing temperature and humidity data to MQTT in JSON format
String msgStr = "{\"temperature\":" + String(temp) + ", \"humidity\":" + String(hum) + "}";
client.publish(topic, msgStr.c_str());
// Check temperature and humidity conditions
if (temp > 30 || hum > 30) {
Serial.println("Conditions met: Turning on LED and adjusting servo");
digitalWrite(LED, HIGH); // Turn on the LED
servo.write(90); // Move servo to 90 degrees
} else {
digitalWrite(LED, LOW); // Turn off the LED
servo.write(0);
}
}
}