#include <ESPping.h>
#define LED_Green 12
#define LED_Red 14
#define LED_yellow 13
int a=0;
int y=0;
const char* ssid     = "Wokwi-GUEST";
const char* password = "";
const char* remote_host = "jokovlog.my.id";
int cek_wifi;
String status_wifi; 

void setup() {
  pinMode(LED_Green, OUTPUT);
  pinMode(LED_Red, OUTPUT);
  pinMode(LED_yellow, OUTPUT);
  Serial.begin(9600);
  delay(10);
  Serial.println("");
  Serial.println("");
  Serial.println("Connecting to WiFi");  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED) { 
    delay(100);
    Serial.print("conect to ");
    Serial.println(ssid);     
  }
  Serial.println("");
  Serial.print("Local IP Address: ");  
  Serial.print(WiFi.localIP());
  Serial.println("");
  Serial.print("Pinging: ");
  Serial.print(remote_host);
  Serial.println("");
  Serial.println("");
cek_wifi=WiFi.status();
if (cek_wifi = 3)  {
   //menghidupkan led di pin14
    digitalWrite(LED_yellow, HIGH);}
  else{
 digitalWrite(LED_yellow, LOW);
  }  
}

void loop() { 
    Serial.print("Status wifi=");
    Serial.print(status_wifi);
    Serial.print(" ");
    Serial.print(y);
    Serial.print("=>Y, X<= "); 
    Serial.println(a);
  delay(1000);
    if (y>=1){
    //delay(1000);
    digitalWrite(LED_Red, LOW); 
    y++;
      if (y>=20){ 
      y=0;
      status_wifi="relay-off";
      }
  }
  
  if(Ping.ping(remote_host)) {
    status_wifi="Online";
    digitalWrite(LED_Green, HIGH);
    digitalWrite(LED_Red, LOW);
    a=0;
    y=0;    
  } else {    
    if (y<=0){
      a++;  //increment      
      if (a>=10){
      digitalWrite(LED_Green, LOW);
      digitalWrite(LED_Red, HIGH);
      a=0;
      y=1;
      status_wifi="Offline";
      delay(5000);
      }
    }
  }
}