ESP32 with ENC28j60 module, update via Ethernet

Hi,
Is it possible to update ESP32 firmware via Ethernet (not Wifi) ?
(from an http server).

you can use the Update object in a similar way the HttpUpdate library bundled with the esp32 plaform does

I tried this. With HTTPClient and EthernetClient.
But the Update function only supports WifiClient as a parameter.

What happens if you copy the Update function and change the parameter type?

Compilation errors. I can't get rid of them...
WifiClient is something more special... Many other methods it has :slight_smile:

No one has really written such procedure?
I didn't find it on the net.
It is possible through SPIFFS (or SD Card), but I want to do it directly.
And I thought that I would find it already written and tested, so that I wouldn't have to work anymore :slight_smile:

In this sketch InternalStorage of ArduinoOTA library wraps the esp32 Update object.

and here is the implementation of InternalStorage so you can inline it so you don't need to use it as intermediary:

Ok. I'll try this.
Thanks.

I tried different software found on the net.
Nothing works to solve the case of ESP32 - Ethernet firmware update.
I will try to use EthernetClient and write something.

ok I replaced the InternalStorage for you


#include <Update.h>
#include <EthernetENC.h>
#include <ArduinoHttpClient.h>

const short VERSION = 1;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void handleSketchDownload() {

  const char* SERVER = "192.168.1.108"; // must be string for HttpClient
  const unsigned short SERVER_PORT = 80;
  const char* PATH = "/update-v%d.bin";
  const unsigned long CHECK_INTERVAL = 5000;

  static unsigned long previousMillis;

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis < CHECK_INTERVAL)
    return;
  previousMillis = currentMillis;

  EthernetClient transport;
  HttpClient client(transport, SERVER, SERVER_PORT);

  char buff[32];
  snprintf(buff, sizeof(buff), PATH, VERSION + 1);

  Serial.print("Check for update file ");
  Serial.println(buff);

  client.get(buff);

  int statusCode = client.responseStatusCode();
  Serial.print("Update status code: ");
  Serial.println(statusCode);
  if (statusCode != 200) {
    client.stop();
    return;
  }

  long length = client.contentLength();
  if (length == HttpClient::kNoContentLengthHeader) {
    client.stop();
    Serial.println("Server didn't provide Content-length header. Can't continue with update.");
    return;
  }
  Serial.print("Server returned update file of size ");
  Serial.print(length);
  Serial.println(" bytes");

  if (!Update.begin(length, U_FLASH)) {
    client.stop();
    Serial.println("There is not enough space to store the update. Can't continue with update.");
    return;
  }
  byte b;
  while (length > 0) {
    if (!client.readBytes(&b, 1)) // reading a byte with timeout
      break;
    Update.write(&b, 1);
    length--;
  }
  Update.end(false);
  client.stop();
  if (length > 0) {
    Serial.print("Timeout downloading update file at ");
    Serial.print(length);
    Serial.println(" bytes. Can't continue with update.");
    return;
  }

  Serial.println("Sketch update apply and reset.");
  Serial.flush();
  ESP.restart();
}

void setup() {

  Serial.begin(115200);
  while (!Serial);

  Serial.print("Sketch version ");
  Serial.println(VERSION);

  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true);
  }
  Serial.println("Ethernet connected");
}

void loop() {
  // check for updates
  handleSketchDownload();

  // add your normal loop code below ...
}

It seems you are right.
I used UIPEthernet.h in your example and it seems to be ok.
I updated my esp32 with a text file from server :slight_smile:
I will return tomorrow with the final result.
Many thanks @Juraj !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.