//Sematec_2023 - Pratica ESP32 - PG/T5 - IoT 9mar24
//push-button em D5 Comanda o Led Vermelho em D4 (modo LOCAL).
//Led Azul em D18 controlado via MQTT
//D32 mede tensao de 0 a 3,3 V V
//LM35 mede temperatura (10 mV/C) em D34 (simbolo do potenciometro)
//Instalar as bibliotecas: (instalação na aba "Library Manager")
   //PubSubClient
  
//Adaptado a partir de RandomNerdTutorials (Rui Santos)
#include <WiFi.h>
#include <PubSubClient.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "CVOR-IOT";
const char* password = "abc@1234";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
long scanSensores = 0;
char msg[50];
int value = 0;
float temperature;
float tensao3_3V;
// LED Pin
const int ledRedPin = 4;
const int PBLigaLedRed = 5;
const int ledBlueMqttPin = 18;
bool statusPB5;
float voltage32;  //tensao 3,3 V medida em D32

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(ledRedPin, OUTPUT);
  pinMode(PBLigaLedRed, INPUT);
  pinMode(ledBlueMqttPin, OUTPUT);
  delay(500);
}

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(1500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* 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];
  }
  Serial.println();
  if (String(topic) == "sematec23/ledBlue") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      digitalWrite(ledBlueMqttPin, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      digitalWrite(ledBlueMqttPin, LOW);
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("sematec23Clientggo")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("sematec23/ledBlue");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void leSensores(){
  if(digitalRead(PBLigaLedRed) == HIGH){
    statusPB5=1;
    digitalWrite(ledRedPin,HIGH);
  } else{
    statusPB5=0;
    digitalWrite(ledRedPin,LOW);
  }
  int v32 = analogRead(32);        //Tensao 3,3 V em D32
  voltage32 = map(v32,0,4095,0,3300)/1.0;
  Serial.println(voltage32);  
  int lm35_D34 = analogRead(34);   //LM35 em D34
  temperature = map(lm35_D34,0,4095,0,3300)/1000.0;
  Serial.println(temperature);  
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  if(now - scanSensores > 450){
    scanSensores = now;
    leSensores();
  }
  if (now - lastMsg > 1000) {
    lastMsg = now;
    // Convert the value to a char array
    char tempString[8];
    dtostrf(temperature, 1, 2, tempString);   //LM35
    Serial.print("Temperature: ");
    Serial.println(tempString);
    client.publish("sematec23/temp", tempString);
    // Convert the value to a char array
    char cvoltage[8];
    dtostrf(voltage32, 1, 2, cvoltage);      //3,3 V
    Serial.print("voltage32: ");
    Serial.println(cvoltage);
    client.publish("sematec23/voltage32", cvoltage);
    //myString.toCharArray(buf, len)
    char s_temp[]={};
    unsigned int len = 10;
    String s_Chave="";
    if(statusPB5){
      s_Chave = "Ligada";
      } else {
      s_Chave = "Desligada";
    }
    s_Chave.toCharArray(s_temp,len);
    client.publish("sematec23/chave",s_temp);
    Serial.print("Status_chave: ");
    Serial.println(s_temp);
  }
}