#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>

#define DHTPIN 13
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

const char* ssid = "Wokwi-GUEST";
const char* password = "";

const char* mqtt_broker = "broker.hivemq.com";
const char* mqtt_topic = "ucol/iot/lali";
const char* mqtt_client_id = "cliente_lali";
const int mqtt_port = 1883;

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  dht.begin();

  Serial.println("Conectando a WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }

  Serial.print("Conectado con la IP: ");
  Serial.println(WiFi.localIP());

  mqttClient.setServer(mqtt_broker, mqtt_port);
}

void loop() {
  if (!mqttClient.connected()) {
    reconnect();
  }
  mqttClient.loop();

  delay(5000); // this speeds up the simulation
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  Serial.printf("Temperatura: %f C, humedad: %f %\n", t, h);

  char json[200];
  sprintf(json, "{\"temperature\": %f, \"humidity\": %f}", t, h);
  Serial.println(json);

  mqttClient.publish(mqtt_topic, json);
}

void reconnect() {
  while (!mqttClient.connected()) {
    Serial.println("Conectando con el broker MQTT...");

    if (mqttClient.connect(mqtt_client_id)) {
      Serial.println("Conectado al broker MQTT.");
      delay(5000);
    }
  }
}