欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

通过网络服务器升级 Arduino ESP8266 固件,成功率达 100%

最编程 2024-03-28 14:06:48
...

Arduino 使用ESP8266httpUpdate库进行固件升级,一开始总是返回http错误,网上给的结果是换成手机WIFI,试了的确可以,但是这也没有解决问题啊,经过不断的尝试,终于成功了。

先看结果:

CALLBACK:  HTTP update process started
CALLBACK:  HTTP update process at 0 of 331216 bytes...
CALLBACK:  HTTP update process at 0 of 331216 bytes...
CALLBACK:  HTTP update process at 4096 of 331216 bytes...
CALLBACK:  HTTP update process at 8192 of 331216 bytes...
CALLBACK:  HTTP update process at 12288 of 331216 bytes...
CALLBACK:  HTTP update process at 16384 of 331216 bytes...
CALLBACK:  HTTP update process at 20480 of 331216 bytes...
CALLBACK:  HTTP update process at 24576 of 331216 bytes...
CALLBACK:  HTTP update process at 28672 of 331216 bytes...
CALLBACK:  HTTP update process at 32768 of 331216 bytes...
CALLBACK:  HTTP update process at 36864 of 331216 bytes...
CALLBACK:  HTTP update process at 40960 of 331216 bytes...
CALLBACK:  HTTP update process at 45056 of 331216 bytes...
CALLBACK:  HTTP update process at 49152 of 331216 bytes...
CALLBACK:  HTTP update process at 53248 of 331216 bytes...
CALLBACK:  HTTP update process at 57344 of 331216 bytes...
CALLBACK:  HTTP update process at 61440 of 331216 bytes...
CALLBACK:  HTTP update process at 65536 of 331216 bytes...
CALLBACK:  HTTP update process at 69632 of 331216 bytes...
CALLBACK:  HTTP update process at 73728 of 331216 bytes...
CALLBACK:  HTTP update process at 77824 of 331216 bytes...
CALLBACK:  HTTP update process at 81920 of 331216 bytes...
CALLBACK:  HTTP update process at 86016 of 331216 bytes...
CALLBACK:  HTTP update process at 90112 of 331216 bytes...
CALLBACK:  HTTP update process at 94208 of 331216 bytes...
CALLBACK:  HTTP update process at 98304 of 331216 bytes...
CALLBACK:  HTTP update process at 102400 of 331216 bytes...
CALLBACK:  HTTP update process at 106496 of 331216 bytes...
CALLBACK:  HTTP update process at 110592 of 331216 bytes...
CALLBACK:  HTTP update process at 114688 of 331216 bytes...
CALLBACK:  HTTP update process at 118784 of 331216 bytes...
CALLBACK:  HTTP update process at 122880 of 331216 bytes...
CALLBACK:  HTTP update process at 126976 of 331216 bytes...
CALLBACK:  HTTP update process at 131072 of 331216 bytes...
CALLBACK:  HTTP update process at 135168 of 331216 bytes...
CALLBACK:  HTTP update process at 139264 of 331216 bytes...
CALLBACK:  HTTP update process at 143360 of 331216 bytes...
CALLBACK:  HTTP update process at 147456 of 331216 bytes...
CALLBACK:  HTTP update process at 151552 of 331216 bytes...
CALLBACK:  HTTP update process at 155648 of 331216 bytes...
CALLBACK:  HTTP update process at 159744 of 331216 bytes...
CALLBACK:  HTTP update process at 163840 of 331216 bytes...
CALLBACK:  HTTP update process at 167936 of 331216 bytes...
CALLBACK:  HTTP update process at 172032 of 331216 bytes...
CALLBACK:  HTTP update process at 176128 of 331216 bytes...
CALLBACK:  HTTP update process at 180224 of 331216 bytes...
CALLBACK:  HTTP update process at 184320 of 331216 bytes...
CALLBACK:  HTTP update process at 188416 of 331216 bytes...
CALLBACK:  HTTP update process at 192512 of 331216 bytes...
CALLBACK:  HTTP update process at 196608 of 331216 bytes...
CALLBACK:  HTTP update process at 200704 of 331216 bytes...
CALLBACK:  HTTP update process at 204800 of 331216 bytes...
CALLBACK:  HTTP update process at 208896 of 331216 bytes...
CALLBACK:  HTTP update process at 212992 of 331216 bytes...
CALLBACK:  HTTP update process at 217088 of 331216 bytes...
CALLBACK:  HTTP update process at 221184 of 331216 bytes...
CALLBACK:  HTTP update process at 225280 of 331216 bytes...
CALLBACK:  HTTP update process at 229376 of 331216 bytes...
CALLBACK:  HTTP update process at 233472 of 331216 bytes...
CALLBACK:  HTTP update process at 237568 of 331216 bytes...
CALLBACK:  HTTP update process at 241664 of 331216 bytes...
CALLBACK:  HTTP update process at 245760 of 331216 bytes...
CALLBACK:  HTTP update process at 249856 of 331216 bytes...
CALLBACK:  HTTP update process at 253952 of 331216 bytes...
CALLBACK:  HTTP update process at 258048 of 331216 bytes...
CALLBACK:  HTTP update process at 262144 of 331216 bytes...
CALLBACK:  HTTP update process at 266240 of 331216 bytes...
CALLBACK:  HTTP update process at 270336 of 331216 bytes...
CALLBACK:  HTTP update process at 274432 of 331216 bytes...
CALLBACK:  HTTP update process at 278528 of 331216 bytes...
CALLBACK:  HTTP update process at 282624 of 331216 bytes...
CALLBACK:  HTTP update process at 286720 of 331216 bytes...
CALLBACK:  HTTP update process at 290816 of 331216 bytes...
CALLBACK:  HTTP update process at 294912 of 331216 bytes...
CALLBACK:  HTTP update process at 299008 of 331216 bytes...
CALLBACK:  HTTP update process at 303104 of 331216 bytes...
CALLBACK:  HTTP update process at 307200 of 331216 bytes...
CALLBACK:  HTTP update process at 311296 of 331216 bytes...
CALLBACK:  HTTP update process at 315392 of 331216 bytes...
CALLBACK:  HTTP update process at 319488 of 331216 bytes...
CALLBACK:  HTTP update process at 323584 of 331216 bytes...
CALLBACK:  HTTP update process at 327680 of 331216 bytes...
CALLBACK:  HTTP update process at 331216 of 331216 bytes...
CALLBACK:  HTTP update process at 331216 of 331216 bytes...
CALLBACK:  HTTP update process at 331216 of 331216 bytes...
CALLBACK:  HTTP update process finished

升级的步骤:

1、WIFI连接路由器,这个就非常简单了;

2、连接MQTT服务器,订阅消息;

3、在非8266的MQTT客户端发布升级消息;

4、ESP8266收到消息经过版本比对符合条件后开始升级。

重点来了,上述一切就绪后调用ESPhttpUpdate.update 总是返回失败,经过多次尝试跟踪调试,发现是HTTP链接返回失败。

所以把httpclient拿出来单独处理,成功后再开始升级,成功率就是100%了,

关键代码:

HTTPClient http;
  int retry=0;
  while(!http.begin(UpdateClient, otaUrl))
  {
    delay(200);
    if(retry++>10)
    {
      Serial1.println("connect failed");
      return;
    }
  }

完整的升级代码如下:

#include <ESP8266httpUpdate.h>

// 当升级开始时,打印日志
void update_started()
{
  Serial1.println("CALLBACK:  HTTP update process started");
}

// 当升级结束时,打印日志
void update_finished()
{
  Serial1.println("CALLBACK:  HTTP update process finished");
}

// 当升级中,打印日志
void update_progress(int cur, int total)
{
  Serial1.printf("CALLBACK:  HTTP update process at %d of %d bytes...\n", cur, total);
}

// 当升级失败时,打印日志
void update_error(int err)
{
  Serial1.printf("CALLBACK:  HTTP update fatal error code %d\n", err);
}

/**
 * 固件升级函数
 * 在需要升级的地方,加上这个函数即可,例如setup中加的updateBin();
 * 原理:通过http请求获取远程固件,实现升级
 */
void updateBin()
{
  Serial1.println("start update");

  WiFiClient UpdateClient;

  ESPhttpUpdate.onStart(update_started);     // 当升级开始时
  ESPhttpUpdate.onEnd(update_finished);      // 当升级结束时
  ESPhttpUpdate.onProgress(update_progress); // 当升级中
  ESPhttpUpdate.onError(update_error);       // 当升级失败时

  Serial1.println(otaUrl);

  HTTPClient http;
  int retry=0;
  while(!http.begin(UpdateClient, otaUrl))
  {
    delay(200);
    if(retry++>10)
    {
      Serial1.println("connect failed");
      return;
    }
  }

  Serial1.println("start update");
  String strCurVersion = ESP.getSdkVersion();
  t_httpUpdate_return ret = ESPhttpUpdate.update(http, strCurVersion);
  Serial1.println(ret);
  switch (ret)
  {
  case HTTP_UPDATE_FAILED: // 当升级失败
    Serial1.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
    break;
  case HTTP_UPDATE_NO_UPDATES: // 当无升级
    Serial1.println("[update] Update no Update.");
    break;
  case HTTP_UPDATE_OK: // 当升级成功
    Serial1.println("[update] Update ok.");
    break;
  }
}