Here is the guts of my Ethernet program-updating code:
#include "mbed.h"
#include "gas.h"
#include "HTTPClient.h"
LocalFileSystem local("local");
void checkUpdate() {
HTTPClient http;
http.setTimeout( 15000 );
char m[100];
char fwfn[32];
char *verfn = "/local/VERSION.TXT";
mcast( "Check for firmware update\n" );
/* Read installed version string */
FILE *fv = fopen( verfn, "r" );
int inst_ver = -1;
fscanf ( fv, "%d", &inst_ver );
fclose( fv );
sprintf( m, "Installed version: %d\n", inst_ver );
mcast( m );
/* Download latest version string */
HTTPText server_ver;
HTTPResult r = http.get( "http://192.168.1.245/mbed/VERSION.TXT", &server_ver );
if ( r == HTTP_OK ) {
int latest_ver = -1;
sscanf( server_ver.gets(), "%d", &latest_ver );
sprintf( m, "Latest version: %d\n", latest_ver );
mcast( m );
if ( inst_ver != latest_ver ) {
mcast( "Downloading new firmware...\n" );
sprintf( fwfn, "/local/%d.BIN", latest_ver );
HTTPFile latest( fwfn );
r = http.get( "http://192.168.1.245/mbed/GasMeter_LPC1768.bin", &latest );
if ( r == HTTP_OK ) {
mcast( "Firmware downloaded, removing old version:\n" );
sprintf( fwfn, "/local/%d.BIN\n", inst_ver );
mcast( fwfn );
if ( remove( fwfn ) ) {
mcast( "Failed to remove old version.\n" );
}
mcast( "Updating stored version number.\n" );
fv = fopen( verfn, "w" );
int fr = fputs( server_ver.gets(), fv );
if ( fr < 0 ) {
sprintf( m, "Failed (%d) to update stored version number.\n", fr );
mcast( m );
fclose( fv );
} else {
fclose( fv );
mcast( "Resetting...\n" );
mbed_reset();
}
} else {
mcast( "Couldn't download lastest firmware.\n" );
}
}
} else {
mcast( "Couldn't check latest firmware version.\n" );
}
}
I call checkUpdate() from main(), after initialising the network connection.
mcast is defined elsewhere - it multicasts the provided debug string onto my home network to be picked up elsewhere, or just to disappear into the ether if no-one is listening.
The rest of it I think is fairly self-explanatory?
Rob.
Hi,
Has anyone worked out a way to reprogram the mbed via Ethernet instead of USB?
My mbed is to be installed into the understair cupboard, near the gas meter (I'm sure you can guess the purpose). It has power and a network connection, but no USB connection to my PC. I'd like to be able to update it over the network so I don't have to keep unplugging it and moving it to the PC and back.
I'm wondering if, during startup, after it gets an IP address, I could get the mbed to poll for a file from somewhere, say via HTTP, and if the file is newer than previously seen, download a copy and store it into the flash filesystem. If it has the file extension, will that then be recognised as a new program to flash in after the next reset?
Better still if I can trigger a reset via the network too, as if the hardware reset button has been pressed, so I don't have to climb into the cupboard to press the reset button...?
Ta, Rob.