//Instalar as bibliotecas: (instalação na aba "Library Manager")
   //PubSubClient
   //Adafruit Unified Sensor
   //DHT sensor library

#include <WiFi.h>
#include <PubSubClient.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "Wokwi-GUEST";
const char* password = "";
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 humidity;
// LED Pin
const int ledRedPin = 4;
const int ledRedChave = 5;
const int ledBlueMqttPin = 18;
bool statusChaveLedRed;
float voltage34;

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  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(500);
    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) == "luminosidade/AMT") {
    Serial.print("Changing output to ");
    if(messageTemp == "on"){
      Serial.println("on");
      //digitalWrite(led, HIGH);
    }
    else if(messageTemp == "off"){
      Serial.println("off");
      //digitalWrite(led, LOW);
    }
  }
}

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

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  if (now - lastMsg > 1000) {  //publica a cada segundo
    lastMsg = now;
    int luminosidade=map(analogRead(34),0,4095,0,100);
    // Convert the value to a char array
    char lumString[8];
    dtostrf(luminosidade, 1, 2, lumString);
    Serial.print("luminosidade: ");
    Serial.println(lumString);
    client.publish("luminosidade/ldr", lumString);
   /*
    //myString.toCharArray(buf, len)
    char s_temp[]={};
    unsigned int len = 10;
    String s_Chave="";
    if(statusChaveLedRed){
      s_Chave = "Ligada";
      } else {
      s_Chave = "Desligada";
    }
    s_Chave.toCharArray(s_temp,len);
    client.publish("sematec23/chaveX",s_temp);
    Serial.print("Status_chave: ");
    Serial.println(s_temp);
  */
  }
}