Advertisement
mudhita_triari

Access Point - OTA Update

Apr 21st, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <ESPAsyncWebServer.h>
  3. #include <ESPmDNS.h>
  4. #include <Update.h>
  5.  
  6. const char* host = "esp32";
  7. const char* ssid = "AP_punye_dani";
  8. const char* pass = "test";
  9. const char* updateUsername = "admin";
  10. const char* updatePassword = "admin123";
  11.  
  12. AsyncWebServer server(80);
  13.  
  14. void setup() {
  15.   Serial.begin(115200);
  16.  
  17.   WiFi.mode(WIFI_AP);
  18.  
  19.   if (!WiFi.softAP(ssid, pass)) {
  20.     Serial.println("Gagal mengatur Titik Akses.");
  21.     return;
  22.   }
  23.  
  24.   Serial.println();
  25.   Serial.print("Titik Akses Berjalan, SSID: ");
  26.   Serial.println(ssid);
  27.  
  28.   IPAddress IP = WiFi.softAPIP();
  29.   Serial.print("Alamat IP: ");
  30.   Serial.println(IP);
  31.  
  32.   if (!MDNS.begin(host)) {
  33.     Serial.println("Kesalahan dalam mengatur responder mDNS!");
  34.     return;
  35.   }
  36.   Serial.println("Responder mDNS dimulai (http://esp32.local)");
  37.  
  38.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  39.     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>");
  40.   });
  41.  
  42.   server.on("/update", HTTP_POST, [updateUsername, updatePassword](AsyncWebServerRequest *request) {
  43.     if (!request->authenticate(updateUsername, updatePassword)) {
  44.       return request->requestAuthentication();
  45.     }
  46.     request->send(200);
  47.   }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  48.     if (!index) {
  49.       Serial.printf("Mulai Pembaruan: %s\n", filename.c_str());
  50.       if (!Update.begin()) {
  51.         Update.printError(Serial);
  52.       }
  53.     }
  54.     if (!Update.hasError()) {
  55.       if (Update.write(data, len) != len) {
  56.         Update.printError(Serial);
  57.       }
  58.     }
  59.     if (final) {
  60.       if (Update.end(true)) {
  61.         Serial.printf("Pembaruan Berhasil: %uB\n", index + len);
  62.       } else {
  63.         Update.printError(Serial);
  64.       }
  65.     }
  66.   });
  67.  
  68.   server.begin();
  69. }
  70.  
  71. void loop() {
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement