#include <WiFi.h>
#include <WebServer.h>
// Pastikan untuk mengganti "NamaWiFi" dan "PasswordWiFi"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//const char* ssid = "ESP32-AP";
//const char* password = "password";
// Put IP Address details
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
WebServer server(80);
uint8_t LED1pin = 2;
uint8_t LED2pin = 4;
bool LED1status = LOW;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
Serial.println();
Serial.println("Mengatur ESP32 sebagai titik akses WiFi...");
WiFi.softAP(ssid, password);
//WiFi.softAPConfig(local_ip, gateway, subnet);
Serial.println("ESP32 diatur sebagai titik akses dengan SSID: " + String(ssid));
Serial.print("Alamat IP ESP32: ");
Serial.println(WiFi.softAPIP());
/*Serial.println("Connecting to ");
Serial.println(ssid);
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Gagal terhubung ke WiFi. Mencoba lagi...");
//Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());*/
server.on("/", handle_OnConnect);
server.on("/led1on", handle_led1on);
server.on("/led1off", handle_led1off);
server.on("/led2on", handle_led2on);
server.on("/led2off", handle_led2off);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(){
server.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
void handle_OnConnect() {
LED1status = LOW;
Serial.println("GPIO2 Status: OFF");
LED2status = LOW;
Serial.println("GPIO4 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status, LED2status));
}
void handle_led1on() {
LED1status = HIGH;
Serial.println("GPIO2 Status: ON");
server.send(200, "text/html", SendHTML(true,LED1status));
}
void handle_led1off() {
LED1status = LOW;
Serial.println("GPIO2 Status: OFF");
server.send(200, "text/html", SendHTML(false,LED1status));
}
void handle_led2on() {
LED2status = HIGH;
Serial.println("GPIO4 Status: ON");
server.send(200, "text/html", SendHTML(LED1status, LED2status));
}
void handle_led2off() {
LED2status = LOW;
Serial.println("GPIO4 Status: OFF");
server.send(200, "text/html", SendHTML(LED1status, LED2status));
}
void handle_NotFound(){
server.send(404, "text/plain", "Not found");
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = "<!DOCTYPE html> <html>\n";
ptr +="<head><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0, user-scalable=no\">\n";
ptr +="<title>LED Control</title>\n";
ptr +="<style>html { font-family: Helvetica; display: inline-block;margin: 0px auto; text-align: center;}\n";
ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
ptr +=".button {display: block;width: 80px;background-color:#3498db;border: none;color: white;padding: 13px 30px;text-decoration:none;font-size: 25px;margin: 0px auto 35px;cursor: pointer;border-radius: 4px;}\n";
ptr +=".button-on {background-color: #3498db;}\n";
ptr +=".button-on:active {background-color: #2980b9;}\n";
ptr +=".button-off {background-color: #34495e;}\n";
ptr +=".button-off:active {background-color: #2c3e50;}\n";
ptr +="p {font-size: 14px;color: #888;margin-bottom: 10px;}\n";
ptr +="</style>\n";
ptr +="</head>\n";
ptr +="<body>\n";
ptr +="<h1>ESP32 Web Server</h1>\n";
ptr +="<h3>Using Station(STA) Mode</h3>\n";
if (led1stat) {
ptr +="<p>LED1 Status: ON</p><a class=\"button button-off\"href=\"/led1off\">OFF</a>\n";
} else {
ptr +="<p>LED1 Status: OFF</p><a class=\"button button-on\"href=\"/led1on\">ON</a>\n";
}
if (led2stat) {
ptr +="<p>LED2 Status: ON</p><a class=\"button button-off\"href=\"/led2off\">OFF</a>\n";
} else {
ptr +="<p>LED2 Status: OFF</p><a class=\"button button-on\"href=\"/led2on\">ON</a>\n";
}
ptr +="</body>\n";
ptr +="</html>\n";
return ptr;
}
/* untuk menjalankan simulasi ESP32 wokwi dari localhost /
lokal komputer tanpa memakai hardware asli,
artinya code dan akses dari komputer yang sama,
maka baca dulu ini juragan:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
ekstrak file wokwigw_v1.2.0_Linux_64bit.zip dan eksekusi:
./wokwigw-linux
When you run the gateway, it should print a logo, its version,
and say: "Listening on TCP port 9011".
Hooray, you've completed the setup!
After running the gateway, open any project in Wokwi,
go to the code editor,
press "F1" and select "Enable Private Wokwi IoT Gateway".
You'll be prompted if you want to enable the gateway.
Answer "OK" to enable the Private Gateway,
or "Cancel" to disable it and switch back to the Public Gateway.
Then run any ESP32 project that uses the WiFi.
Look at the gateway output, it should say "Client connected".
This means you are using the Private Gateway.
*/