#include <WiFi.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define LED 15
#define btn 13
// the Wifi radio's status
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.print(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
// initialize serial for debugging
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(btn, INPUT);
}
void loop() {
int status = digitalRead(btn);
if(status == HIGH && WiFi.status() != WL_CONNECTED){
InitWiFi();
}
else if(status == LOW && WiFi.status() == WL_CONNECTED){
Serial.print("WiFi sudah terkoneksi dengan ");
Serial.println(WIFI_SSID);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
}
}