Koding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Library | |
#include <WiFi.h> | |
// PIN LED | |
#define LED_BUILTIN 2 | |
// SSID dan Password WiFi | |
const char *ssid = "RobotikID"; | |
const char *password = "123456789"; | |
// Objek kelas WiFiServer | |
// 80 adalah port | |
WiFiServer server(80); | |
// variabel kontrol LED dan Penampil di client | |
bool isLive = false; | |
String header; | |
void setup() { | |
// Atur PIN LED jadi Output | |
pinMode(LED_BUILTIN, OUTPUT); | |
// Serial Monitor | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.println("Mulai Konfigurasi"); | |
// Menghubungkan ke WiFi | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Menampilkan Alamat IP dan Memulai web server | |
Serial.println(""); | |
Serial.println("WiFi connected."); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
server.begin(); | |
Serial.println("Memulai Server"); | |
} | |
void loop() { | |
// Mengecek apakai ada client | |
WiFiClient client = server.available(); | |
if (client) { | |
Serial.println("Client Baru"); | |
String currentLine = ""; | |
while (client.connected()) { | |
if (client.available()) { | |
// menerima dan koleksi data dari client request | |
char c = client.read(); | |
header += c; | |
Serial.write(c); | |
if (c == '\n') { | |
if (currentLine.length() == 0) { | |
Serial.println(); | |
// header untuk http | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println("Connection: close"); | |
client.println(); | |
// Display the HTML web page | |
client.println("<!DOCTYPE html><html>"); | |
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); | |
client.println("<link rel=\"icon\" href=\"data:,\">"); | |
// CSS to style the on/off buttons | |
// Feel free to change the background-color and font-size attributes to fit your preferences | |
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); | |
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;"); | |
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); | |
client.println(".button2 {background-color: #555555;}</style></head>"); | |
// Heading Web | |
client.println("<body><h1>ESP32 Web Server</h1>"); | |
// cek header dan memberikan perintah, led hidup / mati | |
if (header.indexOf("GET /26/H") >= 0) { | |
isLive = true; | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
else if (header.indexOf("GET /27/L") >= 0) { | |
isLive = false; | |
digitalWrite(LED_BUILTIN, LOW); | |
} | |
// cek yang akan ditampilkan ke client | |
if (isLive == false) { | |
client.print("<p><a href=\"/26/H\"><button class=\"button\"> Hidupkan LED </button></a></p><br>"); | |
} | |
else { | |
client.print("<p><a href=\"/27/L\"><button class=\"button button2\">Matikan LED </button></a></p><br>"); | |
} | |
client.println("</body></html>"); | |
client.println(); | |
break; | |
} else { | |
// mengosongkan nilai | |
currentLine = ""; | |
} | |
} else if (c != '\r') { | |
// mengumpulkan karakter dari client request | |
currentLine += c; | |
} | |
} | |
} | |
// Tutup Koneksi | |
header = ""; | |
client.stop(); | |
Serial.println("Client Disconnected."); | |
Serial.println(); | |
} | |
} |
Video
https://linktr.ee/robotikid
Youtube: https://www.youtube.com/robotikid
Instagram: https://www.instagram.com/robotikid/
Facebook: https://www.facebook.com/RobotikID/
Website: https://www.robotikindonesia.com/
Tokopedia: http://tokopedia.com/instrumentrobot
0 Comments