#include <Adafruit_Sensor.h>
#include <WiFi.h>
#include <ESP32Servo.h>
#include <DHT_U.h>
#include <PubSubClient.h>
#include <FastLED.h>
//Defining the pin for input and output
#define LED 26 // output led pin
#define DHTPIN 12 // Temp and humidity pin
#define SERVO_PIN 2 //servo motor
#define LED_PIN 4 //neon rgb light
#define NUM_LEDS 16 //rgb light leds in it
//DHT PRAMETERS
#define DHTTYPE DHT22 //type of tem &humidity sensor //Dht11
DHT_Unified dht(DHTPIN,DHTTYPE);
uint32_t delayMS;
//Servo motor
Servo servo;
//W2812 LED Strip
CRGB leds[NUM_LEDS];
//Wifi_credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//MQTT Credentials
const char* mqttServer = "test.mosquitto.org";
const char* clientID = "ksdfkl"; //Client id
const char* topic = "Tempdata"; //publish topic
//parametrs to use in program
unsigned long previousMillis = 0;
const long interval = 1000;
String msgStr = "";
float temp,hum;
//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.println(".");
}
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("Topic Subscribed");
}
else {
Serial.print("failed, rc =");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
// 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 ON");
digitalWrite(LED,HIGH);
}
else {
Serial.println("LED OFF");
digitalWrite(LED, LOW);
}
}
else if(String(topic) == "servo") {
int degree = data.toInt(); //converting data to 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);
Serial.print("Setting NeoPixel color (RGB): ");
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 to specified color code
FastLED.show(); //Update the led strip with the new color
}
}
void setup() {
Serial.begin(115200);
//intialize device
dht.begin();
//get temperature sensor details
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
//initalazie led
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
//Setting up servo motor
servo.attach(SERVO_PIN,500,2400);
servo.write(0);
//Setting up WS2812 led strip
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds,NUM_LEDS);
//wifi_connect
setup_wifi();
client.setServer(mqttServer,1883); //Setting MQTT server
client.setCallback(callback);//Define function which will be called when a message is recived
}
void loop(){
if (!client.connected()){
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"));
}
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);
}
}