【问题标题】:Arduino OTA upload firmware from a server to ESP32Arduino OTA 将固件从服务器上传到 ESP32
【发布时间】:2022-01-12 22:47:03
【问题描述】:

我创建了一个本地服务器,我需要上传一个 .bin 文件到本地服务器,然后 ESP32 必须将固件切换到该文件,我如何才能正确实现本地服务器和 esp 之间的通信?这是 ESP 代码中的 /update 端点:

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);
      }
    }
  });

这是本地服务器的端点,我尝试在 ajax 中通过 url: 'http://192.168.4.1/update'(esp 端点) 与 ESP 通信,看起来它可以通信,但固件不会改变

<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>

【问题讨论】:

  • 你的代码中有调试信息。它输出什么? ESP 会重启吗?
  • @romkey a got this [WebServer.cpp:633] _handleRequest(): request handler not found, and not the esp doesn't restart
  • esp32 应该是从服务器下载更新 bin 的客户端。查看 HTTPUpdate 库

标签: html arduino esp32 ota


【解决方案1】:

您是否要让 ESP32 从服务器下载新固件?
如果是这样,您将需要创建一个 wifi 客户端并将文件下载到 SPI 文件系统。
为此,您需要:WifiClient.hSPIFFS.h

下载完成后,将文件复制到固件位置。
为此,您需要:Update.h

就我而言,我将 .bin 文件存储在 github 上与我的代码相同的存储库中。要访问它,您需要一个 secure 客户端。我使用了:WifiClientSecure.h,它可以让您访问公共网站,甚至是那些需要用户名和密码的网站。

让这一切顺利进行确实是一项艰巨的任务,但最终还是值得的。如果您在起步阶段需要更多帮助,请告诉我。

【讨论】:

  • 嗨@Michael Ward 所以,我需要获取文件的服务器是使用python创建的本地服务器,所以现在我正在尝试使用 HttpClient 获取二进制文件,我正在尝试与我在服务器中上传文件的端点建立连接connect():fd 54 上的套接字错误,errno:104,“对等方重置连接”
  • 为什么要将它存储在 SPIFFS 中? HTTPUpdate 库做得更好
  • @MarkosAlonsoMoscosoOcampo, 127.0.0.1 是环回IP地址
  • @Juraj 你是对的。 HTTPUpdate 更容易。
猜你喜欢
  • 2019-09-14
  • 1970-01-01
  • 2019-04-21
  • 2021-05-25
  • 2021-01-07
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多