#include <WiFi.h>
#include <MQTT.h>

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

WiFiClient net;
MQTTClient client;

void connect(){
  Serial.print("Menghubungkan ke WiFi");
  while(WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.println("Behasil Terhubung ke WiFi");

  Serial.print("Menghubungkan ke Server");
  while(!client.connect("TIDAK BOLEH SAMA!!")){
    Serial.print(".");
  }
  Serial.println("Berhasil Terhubung Ke Server");
  client.subscribe("kelasiot/lampu/#", 1);
}

void diterima(String &topic, String &data){
  if(topic == "kelasiot/lampu/dapur"){
    if(data == "true"){
      digitalWrite(33, 1);
      Serial.println("nyala");
    } else {
      digitalWrite(33, 0);
      Serial.println("mati");
    }
  }
}

void setup() {
  pinMode(33, OUTPUT);
  Serial.begin(9600);
  WiFi.begin(ssid, pass);
  client.begin("broker.emqx.io", net);

  client.onMessage(diterima);

  connect();
}

void loop() {
  client.loop();
  delay(10);
  // client.publish("kelasiot/mantap", "ini datanya");
}
NOCOMNCVCCGNDINLED1PWRRelay Module