#include <Adafruit_Sensor.h> // Include Adafruit sensor library
#include <DHT_U.h> // Include DHT sensor library
#include <WiFi.h> // Include ESP32 WiFi library
#include <PubSubClient.h> // Include MQTT PubSubClient library
#include <ESP32Servo.h> // Include ESP32 Servo library
#include <FastLED.h> // Include FastLED library
// Defining Pins
#define DHTPIN 12 // Pin for DHT sensor
#define LED 26 // Pin for LED
#define SERVO_PIN 2 // Pin for Servo motor
#define LED_PIN 4 // Pin for WS2812 LED strip
#define NUM_LEDS 16 // Number of LEDs in the strip
// DHT parameters
#define DHTTYPE DHT22 // DHT sensor type
DHT_Unified dht(DHTPIN, DHTTYPE); // DHT sensor object
uint32_t delayMS; // Delay for DHT sensor reading
// Servo motor
Servo servo; // Servo motor object
// WS2812 LED strip
CRGB leds[NUM_LEDS]; // LED strip object
// MQTT Credentials
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi password
const char* mqttServer = "broker.hivemq.com"; // MQTT broker server
const char* clientID = "ujaisldaaasdfgh;laslksdja1"; // MQTT client ID
const char* mqttUserName = "hivemq.webclient.1715055613390";
const char* mqttPwd = "WBf$,3tGyQ85.0AHxb:d";
const char* topic = "Tempdata"; // MQTT publish topic
// Parameters for using non-blocking delay
unsigned long previousMillis = 0; // Previous time
const long interval = 1000; // Interval for publishing data
String msgStr = ""; // MQTT message buffer
float temp, hum; // Temperature and humidity values
// Setting up WiFi and MQTT client
WiFiClient espClient; // WiFi client
PubSubClient client(espClient); // MQTT client
// Function to setup WiFi connection
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());
}
// Function to reconnect MQTT client
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
}
}
}
// Subscribe callback
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 (String(topic) == "lights") {
if (data == "ON") {
Serial.println("LED");
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
}
else if (String(topic) == "servo") {
int degree = data.toInt(); // Convert the received data to an integer
Serial.print("Moving servo to degree: ");
Serial.println(degree);
servo.write(degree); // Move the servo to the specified degree
}
else if (String(topic) == "lights/neopixel") {
int red, green, blue;
sscanf(data.c_str(), "%d,%d,%d", &red, &green, &blue); // Parse the received data into RGB values
Serial.print("Setting NeoPixel color to (R,G,B): ");
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.println(blue);
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue)); // Set all LEDs in the strip to the specified color
FastLED.show(); // Update the LED strip with the new color
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue)); // Set all LEDs in the strip to the specified color
FastLED.show(); // Update the LED strip with the new color
}
}
void setup() {
Serial.begin(115200); // Begin serial communication
// Initialize DHT sensor
dht.begin();
// Get temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
// Setup servo
servo.attach(SERVO_PIN, 500, 2400);
servo.write(0);
// Setup WS2812 LED strip
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
setup_wifi(); // Setup WiFi connection
client.setServer(mqttServer, 1883); // Setting MQTT server
client.setCallback(callback); // Define function which will be called when a message is received.
}
void loop() {
if (!client.connected()) { // If client is not connected
reconnect(); // Try to reconnect
}
client.loop();
unsigned long currentMillis = millis(); // Read current time
if (currentMillis - previousMillis >= interval) { // If current time - last time > 5 sec
previousMillis = currentMillis;
// Read temperature and humidity
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"));
}
// Get humidity event and print its value
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) + ",";
byte arrSize = msgStr.length() + 1;
char msg[arrSize];
Serial.print("PUBLISH DATA: ");
Serial.println(msgStr);
msgStr.toCharArray(msg, arrSize);
client.publish(topic, msg);
msgStr = "";
delay(1);
}
}