#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Servo.h> // Make sure this is the correct include
#include <FastLED.h>
#define DHTPIN 12
#define LED 26
#define SERVO_PIN 2
#define LED_PIN 4
#define NUM_LEDS 16
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
ESP32Servo servo; // Create an instance of the ESP32Servo class
CRGB leds[NUM_LEDS];
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi password (leave empty if no password)
const char* mqttServer = "Mosquitto"; // Change to your MQTT broker IP
const char* topic = "Tempdata"; // MQTT topic for temperature and humidity data
const char* clientID = "ESP32-wokwi"; // Client ID for MQTT connection
unsigned long previousMillis = 0; // Variable to store the previous time
const long interval = 1000; // Interval for sensor readings
String msgStr = ""; // Message string for MQTT publishing
float temp, hum; // Variables to store temperature and humidity
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)) {
Serial.println("MQTT connected");
client.subscribe("lights");
client.subscribe("servo");
client.subscribe("lights/neopixel");
Serial.println("Topics Subscribed");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received on topic: ");
Serial.println(topic);
Serial.print("Message: ");
String message; // To hold the received message
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
message += (char)payload[i]; // Construct message
}
Serial.print(message);
Serial.print(" (");
Serial.print(length);
Serial.println(")");
if (String(topic) == "lights") {
if (message == "ON") {
Serial.println("LED ON");
digitalWrite(LED, HIGH); // Turn LED on
} else if (message == "OFF") {
Serial.println("LED OFF");
digitalWrite(LED, LOW); // Turn LED off
}
} else if (String(topic) == "servo") {
int degree = message.toInt();
Serial.print("Moving servo to ");
Serial.print(degree);
Serial.println(" degrees");
servo.write(degree); // Move the servo
} else if (String(topic) == "lights/neopixel") {
int red, green, blue;
sscanf(message.c_str(), "%d,%d,%d", &red, &green, &blue);
Serial.print("Setting NeoPixel to (");
Serial.print(red);
Serial.print(", ");
Serial.print(green);
Serial.print(", ");
Serial.print(blue);
Serial.println(")");
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue));
FastLED.show(); // Update the LED colors
}
}
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
servo.attach(SERVO_PIN); // Attach the servo to the defined pin
servo.write(0); // Start position
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS); // Initialize LED strip
setup_wifi(); // Connect to WiFi
client.setServer(mqttServer, 1883); // Set up MQTT server
client.setCallback(callback); // Set the callback function for MQTT
}
void loop() {
if (!client.connected()) {
reconnect(); // Reconnect to MQTT if not connected
}
client.loop(); // Handle MQTT communication
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
} else {
Serial.print(F("Temperature: "));
temp = event.temperature;
Serial.print(temp);
Serial.println(F(" °C"));
}
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
} else {
Serial.print(F("Humidity: "));
hum = event.relative_humidity;
Serial.print(hum);
Serial.println(F(" %"));
}
msgStr = String(temp) + "," + String(hum); // Prepare message
char msg[msgStr.length() + 1];
msgStr.toCharArray(msg, sizeof(msg)); // Convert String to char array
client.publish(topic, msg); // Publish temperature and humidity data
Serial.print("PUBLISHED DATA: ");
Serial.println(msgStr);
msgStr = ""; // Reset message string
delay(1); // Short delay for stability
}
}