/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-light-sensor
*/
#include <PubSubClient.h>
#include <WiFi.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "broker.emqx.io";
const char* mqtt_user = "lidiamariano";
const char* mqtt_password = "Li15404628!";
const char* temp_topic = "tempdht22";
const char* servo_topic = "servomqtt1";
#define LIGHT_SENSOR_PIN 36 // ESP32 pin GIOP36 (ADC0)
WiFiClient espClient;
PubSubClient client(espClient);
Servo meuServo;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.begin(115200);
meuServo.attach(2);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
client.subscribe(temp_topic);
}
void loop() {
// reads the input on analog pin (value between 0 and 4095)
int analogValue = analogRead(LIGHT_SENSOR_PIN);
Serial.print("Analog Value = ");
Serial.print(analogValue); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (analogValue < 40) {
Serial.println(" => Dark");
} else if (analogValue < 800) {
Serial.println(" => Dim");
} else if (analogValue < 2000) {
Serial.println(" => Light");
} else if (analogValue < 3200) {
Serial.println(" => Bright");
} else {
Serial.println(" => Very bright");
}
delay(500);
clientconnected();
}
bool superando32Graus = false;
void setup_wifi() {
Serial.println("Conectando ao WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConectado ao WiFi");
}
void receiveMQTTMessage(String message) {
if (message == "Superando 32 graus!") {
superando32Graus = true;
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String receivedMessage;
for (int i = 0; i < length; i++) {
receivedMessage += (char)payload[i];
}
Serial.print("Mensagem recebida: ");
Serial.println(receivedMessage);
if (strcmp(topic, temp_topic) == 0) {
receiveMQTTMessage(receivedMessage);
}
}
void publishServoStatus(bool ligado) {
if (ligado) {
client.publish(servo_topic, "Ligado");
} else {
client.publish(servo_topic, "Desligado");
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Conectando ao MQTT...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
Serial.println("Conectado");
client.subscribe(temp_topic);
} else {
Serial.print("Falha, rc=");
Serial.print(client.state());
Serial.println(" Tentando novamente em 5 segundos");
delay(5000);
}
}
}
void clientconnected() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (superando32Graus) {
for (int pos = 0; pos <= 180; pos += 1) {
meuServo.write(pos);
delay(15);
}
delay(1000);
for (int pos = 180; pos >= 0; pos -= 1) {
meuServo.write(pos);
delay(15);
}
} else {
meuServo.write(0); // Define a posição inicial do servomotor quando não está superando 32 graus
}
publishServoStatus(superando32Graus);
}