#include "EspMQTTClient.h"
#include "DHTesp.h"
#define LEDPIN_vm 12
#define LEDPIN_az 14
#define Buzzer_PIN 27
#include <Arduino.h>
#include <ESP32Servo.h>
EspMQTTClient client(
"Wokwi-GUEST",
"",
"broker.mqttdashboard.com",
"mqtt-wokwi",
1883
);
const int servoPin = 4;
Servo servo;
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup()
{
Serial.begin(115200);
pinMode(LEDPIN_vm,OUTPUT);
pinMode(LEDPIN_az,OUTPUT);
pinMode(Buzzer_PIN, OUTPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
servo.attach(servoPin, 500, 2400);
client.enableHTTPWebUpdater();
client.enableOTA();
client.enableLastWillMessage("TestClient/lastwill", "Vou ficar offline");
}
int posL = 0;
int posD = 180;
void onConnectionEstablished()
{
client.subscribe("topicowokwi/msgRecebida/#", [](const String & topic, const String & payload) {
Serial.println("Mensagem recebida no topic: " + topic + ", payload: " + payload);
});
}
void loop(){
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidade: " + String(data.humidity, 1) + "%");
Serial.println("---");
if(data.temperature>25 || data.humidity<60 ){
digitalWrite(LEDPIN_vm,HIGH);
digitalWrite(Buzzer_PIN,HIGH);
servo.write(posL);
delay(200);
digitalWrite(Buzzer_PIN,LOW);
digitalWrite(LEDPIN_az, LOW);
}
else{
digitalWrite(LEDPIN_az,HIGH);
digitalWrite(LEDPIN_vm,LOW);
servo.write(posD);
delay(200);
}
client.publish("topicowokwi/Temp", String(data.temperature, 2) + "°C");
client.publish("topicowokwi/Humidade", String(data.humidity, 1) + "%");
client.loop();
}