0

I created a local server and I need to upload a .bin file to the local server and then the ESP32 has to switch the firmware to that file, How I can achive the comunication between the local server and the esp correctly? This is the /update endpoint in the ESP code:

void SetupServer() {

  server.on("/update", HTTP_POST, []() {
      server.sendHeader("Connection", "close");
      server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
      ESP.restart();
  }, []() {
    HTTPUpload & upload = server.upload();
    if (upload.status == UPLOAD_FILE_START) {
      Serial.printf("Update: %s\n", upload.filename.c_str());
      if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
        Update.printError(Serial);
      }
    } else if (upload.status == UPLOAD_FILE_WRITE) {
      if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
        Update.printError(Serial);
      }
    } else if (upload.status == UPLOAD_FILE_END) {
      if (Update.end(true)) {
        Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
      } else {
        Update.printError(Serial);
      }
    }
  });

This is the endpoint of the local server, I try to comunicate with the ESP through url: 'http://192.168.4.1/update'(the esp endpoint) in the ajax thing and it seems like it comunicates but the firmware dosn't change

<div class="container mt-5 mb-5 px-10 ">
    <div class="row">
        <div class="col-md-6 offset-md-3 text-center">
            <h2>Actualizar Firmware</h2><br>
            <form action="/update" method="POST" enctype="multipart/form-data" id='upload_form' role="form">
                <div class="form-group"> 
                    <input type="file" name="binfile" class="form-control" required><br>
                    <button type="submit" class="btn btn-secondary">Actualizar</button>
                </div><br>
                <div id='prg'>Progress: 0%</div>
            </form>
            {% if message  %}
                    <p class="alert alert-danger">{{ message }}</p>
            {% endif %}
        </div>
    </div>
</div>

<script>
$('form').submit(function(e){
    e.preventDefault();
    var form = $('#upload_form')[0];
    var data = new FormData(form);
    $.ajax({
    url: 'http://192.168.4.1/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>

1 Answers1

-1

Are you trying to make the ESP32 download new firmware from a server?
If so, you will need to create a wifi client and download the file to the SPI file system.
For this you need: WifiClient.h and SPIFFS.h

After the download has finished, you copy the file to the firmware location.
For this you need: Update.h

In my case, I stored my .bin file on github in the same repo as my code. To access that you need a secure client. I used: WifiClientSecure.h which lets you access public webites, even those that require a username and password.

It really was a mamoth task to get it all working, but worth it in the end. Let me know if you need any more help getting off the ground.

  • hi @Michael Ward so, the server where I need to take the file is a local one created with python, so now I'm trying to use HttpClient to get the binary file, I'm trying to stablish connection with the endpoin where I'm uploading the file in the server witch is `http://127.0.0.1:5000/upload-file` using `client.begin(HOST);` and then using a client.get function but I've got an error [E][WiFiClient.cpp:258] connect(): socket error on fd 54, errno: 104, "Connection reset by peer" – Markos Alonso Moscoso Ocampo Dec 08 '21 at 17:22
  • 1
    why would you store it in SPIFFS? the HTTPUpdate library does it better – Juraj Dec 09 '21 at 04:59
  • @MarkosAlonsoMoscosoOcampo, 127.0.0.1 is the loopback IP address – Juraj Dec 09 '21 at 05:00
  • @Juraj you are correct. [HTTPUpdate](https://github.com/espressif/arduino-esp32/tree/master/libraries/HTTPUpdate) is much easier. – Michael Ward Dec 09 '21 at 06:42