0

I am currently trying to implement an over-the-air update for my micro controller. I found an example which I can use. The problem is I would like to use it without access to the internet. The problem it is written in jQuery and I did not work with jQuery so far. Using jQuery does not work with the project since it has no internet access.

jQuery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form method="POST" action="#" enctype="multipart/form-data" id="upload_form">
  <input type="file" name="update" />
  <input type="submit" value="Update" />
</form>
<div id="prg">progress: 0%</div>
<script>
  $("form").submit(function (e) {
    e.preventDefault();
    var form = $("#upload_form")[0];
    var data = new FormData(form);
    $.ajax({
      url: "/update",
      type: "POST",
      data: data,
      contentType: false,
      processData: false,
      xhr: function () {
        var xhr = new window.XMLHttpRequest();
        xhr.upload.addEventListener(
          "progress",
          function (evt) {
            if (evt.lengthComputable) {
              var per = evt.loaded / evt.total;
              $("#prg").html("progress: " + Math.round(per * 100) + "%");
            }
          },
          false
        );
        return xhr;
      },
      success: function (d, s) {
        console.log("success!");
      },
      error: function (a, b, c) {},
    });
  });
</script>
";

And I tried to get the upload part with that but it seems I missing some properties or did set wrong parameters

Plain javascript:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Upload</title>
    <script>
      async function uploadFile() {
        let formData = new FormData();
        formData.append("file", document.getElementById('fileupload').files[0]);
        await fetch("/upload", {
          method: "POST", // *GET, POST, PUT, DELETE, etc.
          body: formData, // body data type must match "Content-Type" header
        });
        alert("The file has been uploaded successfully.");
      }
    </script>
  </head>
  <body>
    <p>Click on the "Choose File" button to upload a file:</p>

    <input id="fileupload" type="file" name="fileupload" />
    <button id="upload-button" onclick="uploadFile()">Upload</button>
  </body>
</html>

I would like to upload the new firmware which is a .bin file does that require some special settings?

I am using this the code for that example (c++): https://github.com/espressif/arduino-esp32/blob/master/libraries/ArduinoOTA/examples/OTAWebUpdater/OTAWebUpdater.ino

I modified it a little bit so it can be used in the access point mode. I Can post the modified code too but since it does not work with html code there should be no error since it works with the jQuery code. (I posted that code here too if it is really needed: https://forum.arduino.cc/t/why-can-i-do-not-perform-an-ota-update-when-i-am-in-the-ap-mode-it/952874/3)

jQuery request console output jQuery request

JavaScript request console output JavaScript request

This is where the website code gets replaced This is where the website code gets replaced

DJPX
  • 56
  • 1
  • 8
  • 1
    I'm not 100% sure I follow. But jQuery does not require access to the internet. You can download _jquery.min.js_ and store it locally so your HTML page doesn't have to download it from a remote site. – Marc Jan 29 '22 at 19:50
  • 1
    Content Type for FormData is not application/json. Do a search for sending FormData with `fetch()` – charlietfl Jan 29 '22 at 20:04
  • @Marc The problem is I am limited in space and that JQuery is a huge file which takes alot space on my microcontroller so that soulutuion does not work for me. – DJPX Jan 29 '22 at 21:25
  • Does the [info here](https://stackoverflow.com/questions/46640024/how-do-i-post-form-data-with-fetch-api) help? – Paul T. Jan 29 '22 at 23:48
  • @charlietfl I still do not know what I need it seems that the post request does not even contain the file. I used this for the code https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch . Are blob and data Content Type too? And if they are it did not work for me (end of the side or I used the incorrect) – DJPX Jan 30 '22 at 01:30
  • @PaulT. Yes it does it makes it more clear that I do not have to use the Content Type ( I rode something similar before about that but I was not sure if it may messes stuff up if it is a binary file), – DJPX Jan 30 '22 at 01:47
  • Well, about all you can do is try it to see if the binary is affected, or not. – Paul T. Jan 30 '22 at 03:22
  • Looking at the code from the link you quoted on [github](https://github.com/espressif/arduino-esp32/blob/master/libraries/ArduinoOTA/examples/OTAWebUpdater/OTAWebUpdater.ino) is the javascript that you wrote ( using fetch) to be written to your version of that `OTAWebUpdater.ino` file as the current version is (using jQuery) ?? – Professor Abronsius Jan 30 '22 at 09:57
  • @ProfessorAbronsius I am not sure what you want to know but I try to reply as good as I can. The code which I quoted on github is from the ESP-Adroino-Framework. To be more precise it is form the OTP libery example. I did not write that code. The OTAWebUpdater.ino currently using JQuery but I need to convert that in JavaScript so I can use it for my project. My version of the OTAWebUpdater.ino can work with or without router. But when I would like to use it without router I can not access jQuery so it needs to be javascript or it does not work (with internet it works with the jQuery code ). – DJPX Jan 30 '22 at 17:06
  • The above code using `fetch` appears to work 100% OK for uploading a file. I copied it and ran it to upload various files - all ok. Could it be that the problem is within the endpoint, `/upload` rather than this ajax code? Have you looked in the console to analyse the request? Have you put debug statements in `/upload` to see what happens? – Professor Abronsius Jan 30 '22 at 18:59
  • Which problem can I look at the endpoint when the jQuery code works? (Like extra settings?). Should they not do the same? When I use the console I always get a ok "200" with the jQuery code but an error or 404 when I use the JavaScript code (Chromium). On firefox is does not even upload the file (like I commented earlyer). I hope the pictures help – DJPX Jan 31 '22 at 10:24

0 Answers0