/* ESP32 WiFi Scanning example */

#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;

//#####################Configuração do Sensor PIN movimento
int PIR_SENSOR_PIN = 35; // Pino do sensor PIR (análogo)
int PIR_SENSOR_STATUS = 0; // Variável para armazenar o status do sensor
// Essas constantes devem corresponder aos atributos "gama" e "rl10" do fotoresistor
const float GAMMA = 0.7;
const float RL10 = 33;
int previousStatus = -1; // Variável para armazenar o status anterior do sensor
unsigned long lastChangeTime = 0; // Variável para armazenar o tempo da última alteração

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Olá, ESP32!");
  
  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() {
  // put your main code here, to run repeatedly:
  int movimento = Sensor_movimento();//recebe informação do sensor PIR
  
  // 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

//Aguarda 15 segundos para enviar uma nova mensagem
if(millis() -lastMillis > 15000) {
  lastMillis = millis();
  sprintf(mensagem, "%d", movimento);
  //sprintf(mensagem, "MQTT ESP32 -Mensagem no. %d", contador); 
  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++;
  }
}

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());
}

// Process incoming MQTT message and control the servo motor
void callback(char* topic, byte* payload, unsigned int length) {
  String string;
  Serial.print("Chegou a mensagem [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
     string+=((char)payload[i]); 
}
Serial.print(string);
}
// 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");   
 }
  }
}

int Sensor_movimento()
{
  unsigned long currentTime = millis(); // Obtém o tempo atual

// Verifica se passaram pelo menos 5 segundos desde a última alteração

if(currentTime -lastChangeTime >= 5000) {
PIR_SENSOR_STATUS = digitalRead(PIR_SENSOR_PIN); // Lê o valor digital do sensorfloat resistance = 2000* voltage / (1-voltage / 3.3);

// Verifica se houve uma alteração no valor do sensor
if(PIR_SENSOR_STATUS != previousStatus) {
// Atualiza o valor anterior
  previousStatus = PIR_SENSOR_STATUS;

  // Mostra o valor apenas se houver uma alteração
  if(PIR_SENSOR_STATUS !=0){
   Serial.print("Movimento detectado -");
   Serial.println(PIR_SENSOR_STATUS);
} 
else{
  Serial.print("Sem movimento -");
  Serial.println(PIR_SENSOR_STATUS);
}

// Atualiza o tempo da última alteração
lastChangeTime = currentTime;
 }
}
 return PIR_SENSOR_STATUS;
}