Remotely Updating Arduino mega Sketch using enc28j60 ethernet Module

Hello,

I'am still new to arduino and i am currently working on a project that is gonna be included in my thesis. I am using an Arduino Mega 2560 hooked to an enc28j60 ethernet Module. It send every 5 mins a json file to the cloud. The project will be shipped to another country and i am asking if there is a way to update the arduino sketch using ethernet connection. There is no SD card slot , so basically an arduino mega hooked to an enc28j60. Is there a way to update the sketch or this is impossible.

Thanks.

yes

I have put the .bin file on the cloud but I always get status Code -1, I think the sketch doesn't find the file somehow

it is an example. modify it as you need. it looks for a bin file named update-v1.bin in the doc root of the webserver

I have modified it and I have put the path of the bin file yet I only got statusCode -1. I tried to print the body of the response but I got an empty line. Somehow using postman I tried testing the Get Method and I got the bin file back but I don't know why on the Arduino IDE it Stops at that point

the code doesn't run in Arduino IDE. it runs on the Arduino.

maybe show here your test sketch

this is the sketch that i am using:
#include <EthernetENC.h>
#include <ArduinoOTA.h> // only for InternalStorage
#include <Ethernet.h>
#include <ArduinoHttpClient.h>
#include <EthernetClientSecure.h>

const short VERSION = 1;

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

#ifdef ARDUINO_SAM_ZERO // M0
#define Serial SerialUSB
#endif

const char* SERVER = "212.24.108.195"; // must be string for HttpClient
const unsigned short SERVER_PORT = 5001;
const char* URL = "https://2ii5.l.time4vps.cloud/webUI/update.bin"; // fixed URL for update file
const unsigned long CHECK_INTERVAL = 5000;

static unsigned long previousMillis;

void handleSketchDownload() {
unsigned long currentMillis = millis();

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

EthernetClientSecure transport;
transport.setInsecure();

HttpClient client(transport, SERVER, SERVER_PORT);

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

client.get(URL);
Serial.println(client.get(URL));
Serial.println(client.responseBody());

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 (!InternalStorage.open(length)) {
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;
InternalStorage.write(b);
length--;
}
InternalStorage.close();
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();
InternalStorage.apply(); // this doesn't return
}

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

ArduinoHttpClient doesn't take a whole URL. server and port are provided in begin.

https doesn't work with Ethernet or EthernetENC

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