#include <WiFi.h>
#include <PubSubClient.h>
const char* wifiNome = "Wokwi-GUEST";
const char* wifiSenha = "";
const char* mqtt_server = "mqtt.eclipseprojects.io";
WiFiClient espClient;
PubSubClient client(espClient);
int PinoBotao = 19;
int UltimoStatusBotao = HIGH;
void reconectar()
{
while (!client.connected())
{
Serial.print("\nTentando conexão");
if (client.connect("ESP32Client33333"))
{
Serial.println("\nConectado ao eclipseprojects🌒");
} else
{
Serial.print("\n⚠️ Falha ⚠️");
delay(3000);
}}}
void setup()
{
Serial.begin(115200);
pinMode(PinoBotao, INPUT);
WiFi.begin(wifiNome, wifiSenha);
Serial.print("conectando a rede 🔄");
Serial.println("\nRede conectada ✅");
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected())
{
reconectar();
}
client.loop();
int BotaoStatus = digitalRead(PinoBotao);
if (BotaoStatus != UltimoStatusBotao) {
if (BotaoStatus == LOW) {
client.publish("🖖👽 ", "LIGADO");
Serial.println("LIGADO");
} else {
client.publish("🖖👽 ", "DESLIGADO");
Serial.println("DESLIGADO");
}
delay(50);
}
UltimoStatusBotao = BotaoStatus;
}