#include <WiFi.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define LED 2
#define Tombol 13
int status = WL_IDLE_STATUS;
void connect_WiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
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) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Connected to AP");
}
}
void setup() {
// put your setup code here, to run once:
Serial.println();
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(Tombol, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Tombol)== HIGH)
connect_WiFi();
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
else {
Serial.print("WiFi sudah terkoneksi dengan: ");
Serial.println(WIFI_SSID);
Serial.println(WiFi.localIP());
digitalWrite(LED, HIGH);
}
}