Reprogram mbed via Ethernet?

09 Oct 2010

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.

09 Oct 2010

Hi Rob

I have a similar setup to you, but at the moment I have the luxury of a PC next to the mbed under the stairs which I remote desktop into for programming. I have also used a USB extender over CAT5 to connect to my mbed. Is that an option for you?

With regards to a software solution, I think all the bits are in the cookbook (eg. download file to mbed file system etc). For the software reset, you can put extern "C" void mbed_reset(); in your code and then call mbed_reset()

I have to say, I do like having the serial connection for debugging, which is mainly what has put me off implementing the code download at start up as you describe it. I think I'd want a debug over ethernet sorted first.

Regards
Daniel

09 Oct 2010

Hi Daniel,

A USB extender over cat5 would be nice, but I'm a cheap-skate... :-)

Thanks for the tips about downloading and mbed_reset - I'll look into those.

I'm already sending debug by multicasting onto the network.

Rob.

09 Oct 2010

Hi Rob

I got one of these USB extenders. I thought it was cheap enough, I know they used to cost a lot more, like 50 GBP upwards. But the challenge is half the fun sometimes, so I hope you succeed!

I posted some of my early work on home energy monitoring here if you are interested.

I'd be interested in the results of your experiments with programming the mbed over the internet and also the debug multicasting. I've spent my time so far on the network stack and now playing with the scmRTOS, and the next thing is to write the central heating controller (I have just installed temperature sensors on the flow and return pipes).

Regards
Daniel

09 Oct 2010

The USB extender shown above will not work over ethernet (i.e. you can't plug it in through a network switch as a part of the network.) It just matches the impedence of the USB device/port to the impedance of the network cable. It's still the same USB signaling that travels down the cat5 cable. So if you want to use this kind of usb extender, you have to run a seperate cable from the mbed to the computer.

You can a usb print server. Most of them have the capability to share other usb devices (scanner, usb hard drive, webcam etc..) but these need stable drivers to work. I have a netgear ps121v2 usb print server that allows you to share usb devices over the network, but I never got it to work with windows 7.

You can program the mbed by downloading the file to the flash and then reseting, but the problem then is what if in your new new code doesn't work, or the mbed locks up..

09 Oct 2010
user Daniel Peter wrote:

Hi Rob

I got one of these USB extenders..

That is cheaper than I was thinking, and I could use one of those as all my RJ45 sockets go to a patch panel, so I could route it all the way to the PC.  Maybe if I don't succeed to reprogram it via the network...

Thanks to the pointer to your home energy monitoring site.  Here's mine :-) : http://ideasandbox.blogspot.com/search/label/gas%20meter%20monitor

Rob.

10 Oct 2010

So far I've managed to establish that the HTTPClient doesn't work with a minimalist netcat-based HTTP web server as described here: http://en.wikipedia.org/wiki/Netcat#Setting_up_a_one-shot_webserver_on_port_8080_to_present_a_file I'll have to use a proper web-server instead.

Rob.

10 Oct 2010

Hi Rob

Is there some secret society for people who have a patch panel under the stairs at home? This was mine yesterday whilst adding a couple of satellite cables and wiring in some extra alarm and temperature signals.

There's an mbed in there somewhere as well ...

Regards
Daniel


10 Oct 2010 . Edited: 10 Oct 2010

Do you live next door to Bill Gates or something? ;)

edit/update: nice to see all that wiring right next to your gas meter, hehe :p

10 Oct 2010

user Daniel Peter wrote:

Hi Rob

Is there some secret society for people who have a patch panel under the stairs at home? This was mine yesterday whilst adding a couple of satellite cables and wiring in some extra alarm and temperature signals.

Wow!  What is all that?  Especially the big box at the top...?  And what's the fat cable labelled "3" that looks like it has something glassy on the end?

Rob.

12 Oct 2010

Hi Rob

The big box is the security alarm panel. Sitting on top of the battery at the bottom in there is an I2C I/O extender board I made with some relays and opto-isolators (so the mbed can handle the 12V signals). The cable marked 3 is one of a pair of satellite feeds (the "glassy end" is copper braid reflecting the light) for connection to a home theatre PC.

Regards
Daniel

12 Oct 2010

Woohoo!  My mbed can now be reprogrammed over the network.

I've learnt a few new things in the process:

1) The mbed can't read the datestamps of files.  Instead, I have a separate file containing a version number that sits beside the .bin file on both the mbed and the webserver that holds mbed program updates. During bootup, the version numbers are checked, and if different then the new program is downloaded, the stored version number is updated, and then the mbed resets itself.

2) The mbed can't rename files.  The rename() function is there, but it doesn't work.

3) Files downloaded by the HTTPClient have a datestamp of 12pm 1st Jan 2008.  Therefore it's necessary to delete the old program in order to force the mbed to use the new one.

Need to sleep now but tomorrow I'll post the code I'm using in case it's useful to anyone else.

Rob.

ps. Would still like a way to remotely trigger a check of the latest .bin version number, so I don't either have to make it poll from time to time, and I don't have to climb into the back of the understair cupboard to press the reset button.

pps. Often my mbed resets when I lightly touch the reset button, before pressing.  Static I assume?  I hope it doesn't damage it.

13 Oct 2010

Hi Rob

That's good!

With regards to light touches on the reset button, I had the same problem (and worse because of spurious resets). See this helpful post from Igor M for a solution.

Since you've got CAT5 cabling down to the mbed from where you work, why not use a couple of cores and rig up a switch to the nR pin and ground (with the pullup resistor mentioned) - nR = active-low reset pin with identical functionality to the reset button. It will also help with those situations when only a hardware reset will do (eg. you just hard faulted the device).

Even though you're a self-avowed cheap skate, that won't cost too much, you could even dispense with a switch and just touch the cores together (sadly no flying sparks)! Or you could include a nice switch, I recommend this one.

Regards
Daniel

13 Oct 2010

Daniel, you are a veritable mine (or is it understair cupboard?) of useful information and suggestions.  :-)

Not sure about the switch recommendation though.  Bit too ornate for my house.  :-)  Maybe one of these: http://uk.farnell.com/apem/a01essp354b-pea01/emergency-stop-switch/dp/1082380

Rob.

13 Oct 2010

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.

04 Mar 2011

Hi Rob,

I know it's been a long time since you posted that great code, but I wonder if you could share with us the way you implemented the mcast function.

It would be great.

Thank you in advance

Dani.

05 Mar 2011

If you don't mind writing your code in BASIC, you could try the Ethernet-based BASIC language

http://jumentum.sourceforge.net/

Then you could program and update your code over a web server. There is a version you could use on the mbed.

Dan

20 Jul 2011

hi Rob , thank you for the code , i am working on the same idea but i am using an ftp server , regarding the version txt file should I make it before writing this code or it is made automatically ? how do i make it ? is it just a regular text file that has only a number in it and then save it as txt file ?

I will appreciate your response

Tareq Khammash

26 Jun 2014

Rob, your work inspired my follow-on and success with Software Update over Ethernet. The prior versions of HTTPFile that I found all had a bug that prevented success, but with some sleuthing I figured it out.