#include "WiFi.h"
#include <PubSubClient.h> //library for MQTT
#include <ESP32Servo.h>
#include <DHT.h>
#define DHTPIN 2 // Define the pin to which the DHT22 sensor is connected
#define DHTTYPE DHT22 // Define the type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
Servo myServo;
int pos = 0;
int servo1 = 23;
int LED = 22;
int sensorValue;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//MQTT Credentials
const char* mqttServer = "broker.mqttdashboard.com"; //MQTT URL
const char* mqttUserName = "led1"; // MQTT username
const char* mqttPwd = "led1"; // MQTT password
const char* clientID = "led1"; // client id username+0001
const char* topic = "led1"; //publish topic
const char* topic2 = "led3";
//parameters for using non-blocking delay
unsigned long previousMillis = 0;
const long interval = 5000;
String msgStr = ""; // MQTT message buffer
//setting up wifi and mqtt client
WiFiClient espClient;
PubSubClient client(espClient);
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());
}
void reconnect() {
while (!client.connected()) {
if (client.connect(clientID, mqttUserName, mqttPwd)) {
Serial.println("MQTT connected");
client.subscribe("led1");
Serial.println("Topic Subscribed");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // wait 5sec and retry
}
}
}
//subscribe call back
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("-----------------------");
Serial.println(data);
if(data=="ON"){
Serial.println("LED ON");
Serial.println("SERVO ON");
digitalWrite(LED, HIGH);
for (pos = 0; pos <= 180; pos += 1)
{
myServo.write(pos);
delay(10);
}
}
if(data=="OFF"){
Serial.println("LED OFF");
Serial.println("SERVO ON");
digitalWrite(LED, LOW);
for (pos = 180; pos >= 0; pos -= 1)
{
myServo.write(pos);
delay(10);
}
}
}
void setup() {
Serial.begin(9600);
// Initialize device.
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
setup_wifi();
client.setServer(mqttServer, 1883); //setting MQTT server
client.setCallback(callback); //defining function which will be called when message is received.
myServo.setPeriodHertz(50); // standard 50 hz servo
myServo.attach(servo1, 500, 2400); // attaches the servo on pin 23 to the servo object
}
void loop() {
if (!client.connected()) { //if client is not connected
reconnect(); //try to reconnect
}
client.loop();
float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity
// Check if the readings are valid
if (!isnan(temperature) && !isnan(humidity)) {
// Print the temperature and humidity readings
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\t");
sensorValue = temperature;
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000); // Delay for 2 seconds before taking the next reading
} else {
Serial.println("Error reading data from DHT sensor");
}
msgStr = String(sensorValue);
byte arrSize = msgStr.length() + 1;
char msg[arrSize];
Serial.print("PUBLISH DATA:");
Serial.println(msgStr);
msgStr.toCharArray(msg, arrSize);
client.publish(topic2, msg);
msgStr = "";
delay(50);
}