#include <WiFi.h>
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"
#include <PubSubClient.h>
// Adafruit_SHT31 sht31 = Adafruit_SHT31();


#define mqttServer    "broker.emqx.io"
#define mqttPort      1883
#define mqttUser      ""
#define mqttPassword  ""

String msg;
unsigned long myTime = 0;

WiFiClient wificlient;
PubSubClient mqttClient(wificlient);

char ssid[] = "Wokwi-GUEST";
char pass[] = "";

#define Relay01 32
#define Relay02 33 //14
#define led  17

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  setupMQTT();


  // if (! sht31.begin(0x44)) {
  //   Serial.println("Check circuit. SHT31 not found!");
  //   while (1) delay(1);
  // }

  pinMode(Relay01, OUTPUT);
  pinMode(Relay02, OUTPUT);
  pinMode(led, OUTPUT);
  digitalWrite(Relay01, 0);
  digitalWrite(Relay02, 0);
}

void loop() {
  if (!mqttClient.connected()) {
    reconnect();
  }
  if ((millis() - myTime) > 10000) { // 10S = 10000. 22000-11000 > 10000 = true
    mqttClient.publish("CPE345IoT/65006866/msg/spu", String(millis() / 1000).c_str());
    myTime = millis();
  }
  mqttClient.loop();

}
void setupMQTT() {
  mqttClient.setServer(mqttServer, mqttPort);
  // set the callback function
  mqttClient.setCallback(callback);
}

void reconnect() {
  Serial.println("Connecting to MQTT Broker...");
  while (!mqttClient.connected()) {
    Serial.println("Reconnecting to MQTT Broker..");
    String clientId = "ESP32Client-";
    clientId += String(random(0xffff), HEX);

    if (mqttClient.connect(clientId.c_str(), mqttUser, mqttPassword)) {
      Serial.println("Connected.");
      Serial.println(clientId);
      // subscribe to topic
      mqttClient.subscribe("CPE345IoT/65006866/msg/#");
    }
  }
}

void callback(char *topic, byte *payload, unsigned int length) {
  String sTopic = String(topic);
  Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    msg += (char)payload[i];
  }


  if (sTopic == "CPE345IoT/65006866/msg/Relay01") {
    //ON / OFF
    if (msg == "ON") {
      digitalWrite(Relay01, 1);
      Serial.println("Relay01 ON");
    }
    else if (msg == "OFF") {
      digitalWrite(Relay01, 0);
      Serial.println("Relay01 OFF");
    }
  }

  if (sTopic == "CPE345IoT/65006866/msg/Relay02") {
    //ON / OFF
    if (msg == "ON") {
      digitalWrite(Relay02, 1);
      Serial.println("Relay02 ON");
    }
    else if (msg == "OFF") {
      digitalWrite(Relay02, 0);
      Serial.println("Relay02 OFF");
    }
  }

  if (sTopic == "CPE345IoT/65006866/msg/led") {
    //ON / OFF

    if (msg == "ON") {
      digitalWrite(led, 1);
      Serial.println("LED ON");
    }
    else if (msg == "OFF") {
      digitalWrite(led, 0);
      Serial.println("LED OFF");
    }
  }
  sTopic = "";
  Serial.println("\n======================================");
}

// void readSht31(){
//   //READ SHT31 ------------------
//   float temp = sht31.readTemperature();
//   float hum = sht31.readHumidity();

//   if (! isnan(temp)) {
//     Serial.print("Temperature *C = ");
//     Serial.print(temp);
//     Serial.print("\t\t");
//   } else {
//     Serial.println("Failed to read temperature!");
//   }

//   if (! isnan(hum)) {
//     Serial.print("Humidity % = ");
//     Serial.println(hum);
//   } else {
//     Serial.println("Failed to read humidity!");
//   }

//   mqttClient.publish("CPE345IoT/65006866/msg/Sensor", String(31.c_str());
//   mqttClient.publish("CPE345IoT/65006866/msg/Sensor", String(44.c_str());

// }
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module