#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Servo.h>
#include <FastLED.h>
// defining pin
#define DHTPIN 12
#define SERVO_PIN 2 // servo motor pin
#define LED_PIN 4 // led strip pin
#define NUM_LEDS 16 // no. of led in strip
// dht parameters
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
// Servo motor
Servo servo; // Corrected: Added semicolon and capitalization convention
// WS2812 LED strip
CRGB leds[NUM_LEDS];
const char* ssid = "Wokwi-GUEST"; // Corrected: Proper const char* syntax
const char* password = ""; // Corrected: Proper const char* syntax
const char* mqttServer = "broker.hivemq.com";
const char* topic = "Tempdata"; // publish topic
const char* clientID = "ESP32-wokwi";
// parameters for non-blocking delay
unsigned long previousMillis = 0;
const long interval = 1000;
String msgStr = ""; // MQTT message buffer
float temp, hum;
// Setting up WiFi and MQTT client
WiFiClient espClient;
PubSubClient client(espClient);
// Forward declaration of callback function
void callback(char* topic, byte* payload, unsigned int length);
void setup_wifi() {
delay(10);
Serial.begin(115200);
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)) {
Serial.println("MQTT connected");
client.subscribe("lights");
client.subscribe("servo"); // Subscribe to servo topic
client.subscribe("lights/neopixel"); // Subscribe to neopixel topic
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
}
}
}
// Callback function to handle incoming MQTT messages
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
// You can add logic here to control your servo and LEDs based on the topic and payload
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)payload[i];
}
Serial.println(messageTemp);
if (String(topic) == "servo") {
int angle = messageTemp.toInt();
servo.write(angle);
Serial.print("Servo moved to: ");
Serial.println(angle);
} else if (String(topic) == "lights") {
// Implement LED control logic here
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqttServer, 1883);
client.setCallback(callback); // Set the callback function
dht.begin(); // Initialize DHT sensor
servo.attach(SERVO_PIN); // Initialize servo motor
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); // Initialize FastLED strip
delayMS = dht.getMinimumSamplingPeriod();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop(); // Required for PubSubClient to handle incoming messages
// Read DHT sensor and publish temperature and humidity
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
} else {
temp = event.temperature;
Serial.print("Temperature: ");
Serial.println(temp);
}
dht.humidity().getEvent(&event);
if (isnan(event.humidity)) {
Serial.println("Error reading humidity!");
} else {
hum = event.humidity;
Serial.print("Humidity: ");
Serial.println(hum);
}
// Publish data to MQTT
if (client.connected()) {
msgStr = "{\"temperature\":" + String(temp) + ",\"humidity\":" + String(hum) + "}";
client.publish(topic, msgStr.c_str());
}
}
}