-->
Page 1 of 1

Irrelevant WiFiClient disconnected by ESPhttpUpdate.update

PostPosted: Sun Jan 29, 2023 1:43 am
by snakeninny
Hi!
I was testing OTA with NodeMCU and espClient was disconnected by ESPhttpUpdate.update as you can see this sketch:

Code: Select all#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
#include <PubSubClient.h>

#define ssid "SSID" // Change this
#define password "PASSWORD" // Change this

String upUrl = "https://bin.bemfa.com/b/3BcN2Q1NGY4NWFmNDI5NzZlZTNjMjY5M2U2OTJhNmJiNTk=light002.bin";
char serverName[] = "baidu.com";

WiFiClient espClient;

void onStart()
{
  Serial.println("onStart");
  Serial.printf("WiFiClient is %sconnected.\r\n", espClient.connected() ? "" : "NOT ");
}

void onEnd()
{
  Serial.println("onEnd");
}

void onProgress(int cur, int total)
{
  Serial.printf("onProgress at %d of %d bytes.\r\n", cur, total);
  Serial.printf("WiFiClient is %sconnected.\r\n", espClient.connected() ? "" : "NOT ");
}

void onError(int err)
{
  Serial.printf("onError, err code = %d.\r\n", err);
}

void updateBin()
{
  WiFiClientSecure updateClient;
  updateClient.setInsecure();

  ESPhttpUpdate.onStart(onStart);
  ESPhttpUpdate.onEnd(onEnd);
  ESPhttpUpdate.onProgress(onProgress);
  ESPhttpUpdate.onError(onError);

  t_httpUpdate_return ret = ESPhttpUpdate.update(updateClient, upUrl);
  switch (ret)
  {
  case HTTP_UPDATE_FAILED:
    Serial.println("HTTP_UPDATE_FAILED");
    break;
  case HTTP_UPDATE_NO_UPDATES:
    Serial.println("HTTP_UPDATE_NO_UPDATES");
    break;
  case HTTP_UPDATE_OK:
    Serial.println("HTTP_UPDATE_OK");
    break;
  }
}

void setup()
{
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");

  if (espClient.connect(serverName, 443))
  {
    if (espClient.connected())
    {
      Serial.println("Connected to https://baidu.com.");
    }
    updateBin();
  }
}

void loop()
{
}


Code: Select all...........
Connected to https://baidu.com.
onStart
WiFiClient is connected.
onProgress at 0 of 305120 bytes.
WiFiClient is NOT connected.
onProgress at 0 of 305120 bytes.
WiFiClient is NOT connected.
onProgress at 4096 of 305120 bytes.
WiFiClient is NOT connected.
...


Did I miss anything or this is normal?
How should I keep espClient CONNECTED while ESPhttpUpdate.update working?

Thanks,
Yinglu