Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- #include <ESPmDNS.h>
- #include <Update.h>
- const char* host = "esp32";
- const char* ssid = "AP_punye_dani";
- const char* pass = "test";
- const char* updateUsername = "admin";
- const char* updatePassword = "admin123";
- AsyncWebServer server(80);
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_AP);
- if (!WiFi.softAP(ssid, pass)) {
- Serial.println("Gagal mengatur Titik Akses.");
- return;
- }
- Serial.println();
- Serial.print("Titik Akses Berjalan, SSID: ");
- Serial.println(ssid);
- IPAddress IP = WiFi.softAPIP();
- Serial.print("Alamat IP: ");
- Serial.println(IP);
- if (!MDNS.begin(host)) {
- Serial.println("Kesalahan dalam mengatur responder mDNS!");
- return;
- }
- Serial.println("Responder mDNS dimulai (http://esp32.local)");
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(200, "text/html", "<!DOCTYPE html><html><head><style>body {font-family: Arial, Helvetica, sans-serif;} h1 {color: #3498db;} form {border: 3px solid #f1f1f1;margin: auto;width: 50%;padding: 10px;} input[type=file], input[type=submit] {width: 100%;padding: 12px 20px;margin: 8px 0;display: inline-block;border: 1px solid #ccc;box-sizing: border-box;} input[type=submit] {background-color: #4CAF50;color: white;border: none;} input[type=submit]:hover {opacity: 0.8;}</style></head><body><h1>Selamat datang di ESP32 - Indobot Academy OTA Update </h1><form method='POST' action='/update' enctype='multipart/form-data' id='upload_form'><input type='file' name='update'><input type='submit' value='Update'></form></body></html>");
- });
- server.on("/update", HTTP_POST, [updateUsername, updatePassword](AsyncWebServerRequest *request) {
- if (!request->authenticate(updateUsername, updatePassword)) {
- return request->requestAuthentication();
- }
- request->send(200);
- }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
- if (!index) {
- Serial.printf("Mulai Pembaruan: %s\n", filename.c_str());
- if (!Update.begin()) {
- Update.printError(Serial);
- }
- }
- if (!Update.hasError()) {
- if (Update.write(data, len) != len) {
- Update.printError(Serial);
- }
- }
- if (final) {
- if (Update.end(true)) {
- Serial.printf("Pembaruan Berhasil: %uB\n", index + len);
- } else {
- Update.printError(Serial);
- }
- }
- });
- server.begin();
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement