#define THINGER_SERIAL_DEBUG

#include <ThingerESP32.h>
#include "WiFi.h"
#include "WiFiClient.h"

#define USERNAME "RahmanHK"
#define DEVICE_ID "ESP32"
#define DEVICE_CREDENTIAL "online course iot"

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

#define LED1 12
#define LED2 14

ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  // open serial for debugging
  Serial.begin(115200);
  Serial.print("Materi 1 LED");

  Serial.println();
  Serial.print("Menghubungkan dengan WIFI : ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while(WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
    Serial.println("ESP32 sudah terhubung dg Wifi!");
    Serial.println("IP address ESP32 : ");
    Serial.println(WiFi.localIP());
  delay(1000);
  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  thing.add_wifi(ssid, password);

  // digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["LED 1"] << digitalPin(12);
    
  thing["LED 2"] << [](pson& in){
    if(in.is_empty()){
      in = (bool) digitalRead(14);
      }
    else{
      digitalWrite(14, in ? HIGH : LOW);
      }
    }; 

  // resource output example (i.e. reading a sensor value)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}