#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
///########### configuração do wifi
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
int status = WL_IDLE_STATUS; // the Wifi radio's status
///########## configuração do MQTT
const char* mqtt_server = "test.mosquitto.org";// MQTT broker
char* my_topic_SUB = "FIT/SUB";// the chosen topic
char* my_topic_PUB = "FIT/PUB";// the chosen topic
/////##### configuração do wifi e MQTT
WiFiClient espClient;
PubSubClient client(espClient);
/////###configuração do tempo de leitura
int contador = 1;
char mensagem[30];
unsigned long lastMillis = 0;
int LED = 2;
int LED2 = 4;
int PIR_SENSOR_PIN = 14;
int PIR_SENSOR_STATUS=0;
int ultimovalor=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
conectawifi();
client.setServer(mqtt_server, 1883); // Set the MQTT broker server and port
client.setCallback(callback); // Set the callback function for incoming MQTT messages
}
void loop() {
// Check MQTT connection and process incoming messages
if (!client.connected()) {
reconnect();
}
client.loop();
delay(100); // Add a delay to avoid flooding the MQTT broker with messages
PIR_SENSOR_STATUS= digitalRead(PIR_SENSOR_PIN);
if(PIR_SENSOR_STATUS != ultimovalor)
{
Serial.print(PIR_SENSOR_STATUS);
if(PIR_SENSOR_STATUS==0)
{
Serial.println("- Sem movimento");
digitalWrite(LED, LOW);
// Define
String str = "Sem movimento";
// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;
// Prepare the character array (the buffer)
mensagem[str_len];
// Copy it over
str.toCharArray(mensagem, str_len);
client.publish(my_topic_PUB, mensagem);
Serial.print("Mensagem enviada: ");
Serial.println(mensagem);
}
else{
Serial.println("- Movimento detectado");
digitalWrite(LED, HIGH);
// Define
String str2 = "Movimento detectado";
// Length (with one extra character for the null terminator)
int str_len = str2.length() + 1;
// Prepare the character array (the buffer)
char mensagem2[str_len];
client.publish(my_topic_PUB, mensagem2);
Serial.print("Mensagem enviada: ");
Serial.println(mensagem);
}
//Envia a mensagem ao broker
//client.publish(my_topic_PUB, mensagem);
Serial.print(contador);
Serial.println("-Mensagem enviada com sucesso...");
//Incrementa o contador
contador++;
ultimovalor=PIR_SENSOR_STATUS;
}
}
void conectawifi()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Conectando ao WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Conectado!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
String string;
Serial.print("Chegou a mensagem [");
Serial.print(my_topic_SUB);
Serial.print("] ");
for (int i = 0; i < length; i++) {
string+=((char)payload[i]);
}
Serial.print(string);
if(string == "1"){
digitalWrite(LED2, HIGH);
}
if(string == "0"){
digitalWrite(LED2, LOW);
}
}
// Attempt to reconnect to the MQTT broker if the connection is lost
void reconnect() {
while (!client.connected()) {
Serial.print("Tentando conectar ao MQTT ...");
if (client.connect("ESPClient")) {
Serial.println("Conectado");
client.subscribe(my_topic_SUB);
} else {
Serial.print("falhou, rc=");
Serial.print(client.state());
Serial.println(" Tente novamente em 5 segundos");
}
}
}