#include <WiFi.h>
#include <PubSubClient.h>
#define wakeuptime 20
//Definir credenciais do wifi, neste caso dados móveis
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//Definir endereço do MQTT
const char* mqtt_server = "66.29.142.107";
//Iniciar o espClient
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
//This part of the code configures the ESP32 pins to be used as inputs or outputs.
pinMode(LED_BUILTIN, OUTPUT);//Definir Variáveis como Output/Input
int errorCounter=0;
//This part of the code initializes the MPU6050 sensor, displaying an error message if it does not connect.
//This part of the code initializes the Wi-Fi network connection so the ESP8266 can connect to it.
setup_wifi();
//This part of the code initializes the MQTT client, setting the MQTT server address.
client.setServer(mqtt_server, 1883); // uses the string defined with the server address: const char* mqtt_server = "jpsantos.ddns.net"; and the server communication port
client.setCallback(callback); // uses the function with prototype: void callback(String topic, byte* message, unsigned int length);
}
// Don't change the function below. This functions connects your ESP8266 to your router
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// blinks the builtin LED when connected to a WiFi network
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("");
Serial.print("WiFi connected - IP address: ");
Serial.println(WiFi.localIP());
delay(1000);
}
//This part of the code implements the callback function, which detects when a device publishes a message to a topic to which the ESP32 is subscribed.
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
}
int errorCounter=0;
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP32
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
//Pusca duas vezes o led do ESP caso se tenha conetado ao MQTT broker
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("connected");
//É aqui onde é subscrito o tópico do botão que defini no node-red
client.subscribe("dg/lamp");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// espera 0.5 segundos antes de se tentar conectar
delay(500);
}
client.subscribe("esp32/output");
}
}
int counter=0;
//This part of the code implements the loop function, which maintains the connection between the ESP32 and the MQTT broker. It also publishes the values of the data obtained by the sensors and displays this data on the OLED display
void loop()
{
if (!client.connected()) {
reconnect();
}
if (!client.loop())
client.connect("ESP32Client");
char msg[640];
while(Serial.available() >= 1)
{
msg[counter] = (char)Serial.read();
counter++;
}
if(Serial.available() < 1 && counter > 0)
{
counter=0;
char msg1[640];
sprintf(msg1, "%s\n", msg);
client.publish("TTS", msg1);
}
}