PWA + ESP32 communication #7912
Replies: 2 comments
-
I just found a workaround. Maybe more an temporary hack than a solution. On your server (where your PWA is hosted) you need a function openLink(link)
{
console.log("Open " + link + "...");
window.location.href = link;
}
function fetchLink(link)
{
console.log("fetching link..." + link);
const t1 = new Date();
fetch(link, {method: "HEAD",mode: "cors"}).then(c=>{
console.log("fetch success: " + link);
openLink(link);
}).catch(e=>{
if(((new Date()) - t1) / 1000 > 3)
{
console.warn("Timeout: " + link);
}
else
{
console.log("fetch error, but available? " + link);
openLink(link);
}
});
}
window.setTimeout(()=>{
fetchLink("http://192.168.1.10");
}, 1000); With this code, a https-server redirects the browser to an insecure http page. This is possible at the moment. On your ESP, you have to deliver just this page: ...
const char *SERVER_URL = "https://WEBSERVER.COM/";
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", "<!DOCTYPE html><html><head><script src='" + SERVER_URL+ "loader.js' async></script></head><body></body></html>");
});
... And finally, generate the entire webpage over loader.js. Why the first redirect? Why loader.js? I don't know if this can help someone. I am now able to write my PWA on the server and open insecure sockets and make insecure http requests now :D |
Beta Was this translation helpful? Give feedback.
-
Sorry for the monologue... If someone want to create a PWA for an ESP32 without hosting it on the ESP32 itself but online, you can use my GitHub project: |
Beta Was this translation helpful? Give feedback.
-
Hi,
My ESP32 is in my local network and I am able to update it with the OTA directly from the Arduino IDE (so I don't need to connect it over the serial for each update and I don't need to export the build-file).
Now I would like to send and receive just some bytes from this module over my mobile phone and don't know what is the right approach. I was thinking about a simple PWA, so I can connect my ESP with any device.
Websocket:
http/GET request:
Bluetooth is still an experiment library in HTML5, so I can't send the data over BT.
Is there another option to send the data from a PWA to the ESP32 without a secure connection? Or is there a way to implement a secure connection? I can find some examples for the ESP8266, but they doesn't work on the ESP32.
Beta Was this translation helpful? Give feedback.
All reactions