#include <CoreX.h>
#include "Connection.h"

int BUILTIN_LED = 34;

const char* AUTH_TOKEN = "1oLiw09sPiMfImb";
const char* DEVICE_ID = "lYnpFz0To";

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

void receive(String &bucket, String &data) {
  if(bucket == "led"){
    digitalWrite(BUILTIN_LED, data.toInt());
  
    if(data == "1"){
      Serial.println("LED ON");
      digitalWrite(BUILTIN_LED, HIGH);
    } else {
      Serial.println("LED OFF");
      digitalWrite(BUILTIN_LED,LOW);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  corex.begin(net);

  corex.onMessage(receive);       // Lakukan receive pada fungsi receive().

  setupCoreX();
}

void loop() {
  corex.loop();
  //delay(10);                          // Hapus komentar untuk memberikan delay 10 milidetik jika terjadi kendala pada stabilitas WiFi.

  if (!corex.connected()) {
    setupCoreX();
  }
}