#include <Adafruit_Sensor.h>
#include <DHT_U.h>
#include <Wifi.h>
#include <PubSubClient.h>
#include <Servo.h>
#include <FastLED.h>
#define DHTPIN 12
#define LED 26
#define SERVO_PIN 2
#define LED_PIN 4
#define NUM_LEDS 16
#define DHTTYPE DHT22 //DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
Servo servo;
CRGB leds[NUM_LEDS];
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqttServer = "broker.hivemq.com";
const char* topic = "Tempdata";
const char* clienId = "ESP32-wokwi";
unsigned long previousMillis = 0;
const long interval = 1000;
String msgStr = "";
float temp, hum;
WifiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
Wifi.begin(ssid, password);
while (Wifi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("");
Serial.print(Wifi connected);
Serial.print("IP address");
Serial.print(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 Subscibed");
}
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 lenght) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message: ");
string data = "";
for(int i = 0; i < lenght; i++) {
Serial.print((char)payload[i]);
data += (char)payload[i];
}
Serial.println();
Serial.print("Message size: ");
Serial.println(lenght);
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();
Serial.print("Moving servo to degree: ");
Serial.println(degree);
servo.write(degree);
}
else if (String(topic) == "servo: "){
int red, green, blue;
sscanf(data.c_str(), "%d,%d,%d", %red, %green, %blue);
Serial.print("Setting NeoPixel color to (R,G,B): ");
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.print(blue);
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue));
FastLED.show();
fill_solid(leds, NUM_LEDS, CRGB(red, green, blue));
FastLED.show();
}
}
void setup() {
Serial.begin(115200);
//Initialize device.
dht.begin();
// Get temperature sensor details.
sensor_t sensor;
dht.temperature().getsensor(&sensor);
dht.humidity().getsensor(&sensor);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
servo.attach(SERVO_PIN);
servo.write(0);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
setup_wifi();
client.setServer(mqttServer, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
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.lenght() + 1;
char msg[arrSize];
Serial.print("PUBLISH DATA: ");
Serial.println(msgStr);
msgStr.toCharArray(msg, arrSize);
client.publish(topic, msg);
msgStr = "";
delay(1);
}
}