#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""

#define SERIAL_DEBUG_BAUD 115200

#define LED_Merah 13

int status = WL_IDLE_STATUS;

void InitWiFi(){
  Serial.println("Connecting to AP ... ");

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to AP");
}

void reconnect(){
  status = WiFi.status();
  if (status != WL_CONNECTED){
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.println(".");
    }
    Serial.println("Connected to AP");
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(SERIAL_DEBUG_BAUD);
  Serial.println();
  InitWiFi();
  pinMode(LED_Merah, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //delay(10); // this speeds up the simulation
  delay(1000);

  if (WiFi.status() != WL_CONNECTED){
    reconnect();
  } else {
    Serial.print("Wifi sudah terkoneksi dengan ");
    Serial.println(WIFI_SSID);
    Serial.print("IP : ");
    Serial.println(WiFi.localIP());  
    digitalWrite(LED_Merah, HIGH);
    delay(200);
    digitalWrite(LED_Merah, LOW);
  }
}