Example code for a neat OTA update ESP32 web server:
- Replace your credentials at line 11-12 in
ota_example.ino
- Upload the code to your ESP32 with any C-based compiler.
- Then wait for it to boot.
- And go to esp32.local. On Windows you might need to install Apple's Bonjour Print Service to use mDNS.
- This is not a secure login, but partially obfuscated: username and password are
program
- Click on "Login". You'll be sent to a URL. Make sure not to share it or let people access your browser history (if they have the URL, they can access the login, and upload programs).
- Outside of code
- Make a new sketch like normal.
- Copy ota.h and otaroute.h into your sketch's directory.
- Code, before
setup
- First, add these to the top of your code:
#include <WiFi.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <WebServer.h>
#include "ota.h"
- Then, after that, make sure you declare these:
WebServer server(80);
const char* host = "esp32";
const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";
- Modify
host
andssid
- Inside of
setup
- Set up
WiFi
and make sure you connect to it inside of yoursetup
. - Also inside your
setup
, after that, do a#include "otaroute.h"
.
- Set up
- Inside of
loop
- When you have spare time, call
server.handleClient();
. I have an example here waiting for 20ms each loop:
- When you have spare time, call
while (millis() - tt < 10) {
server.handleClient();
}
- Or you can do it on a seperate core:
// Check for updates loop, outside of setup:
TaskHandle_t Task;
void handleServer(void * pvParameters) {
while(true) {
server.handleClient();
}
}
// Inside of setup:
xTaskCreatePinnedToCore(
handleServer,
"handleServer",
10000,
NULL,
1,
&Task,
(xPortGetCoreID() == 1) ? 0 : 1);
Remember to follow the above guidelines for creating a sketch. If you forget and upload a non-OTA capable sketch, you'll lose OTA access until another one with OTA is uploaded.
- First, make sure you're logged in:
- Go to esp32.local. On Windows you might need to install Apple's Bonjour Print Service to use mDNS.
- This is not a secure login, but partially obfuscated: username and password are
program
. (Feel free to fork and send a PR with more secure functionality!) - Click on "Login". You'll be sent to a URL. Make sure not to share it or let people access your browser history.
- Then build your sketch:
- Choose
Choose File
- Select your
.bin
file - Choose
Upload
- Don't close the tab, and eventually it'll finish!
Any errors? Let me know.