Unique sequential ID on a network

I am currently working on a project where I need several arduino's to "automatically" number themselves,

  1. plug arduino 1 in to the network and it gets the ID 1
  2. plug arduino 2 in to the network and it gets the ID 2
  3. plug arduino 3 in to the network and you know the score here
  4. and on
  5. and on
  6. and on
  7. remove arduino 2 from the network and plug in another, this gets the ID 2

I've managed to get arduino's talking to each other via UDP so when an arduino is plugged in to the network it sends out a broadcast announcing itself to the network, this would be a lot simpler if I had a "control" node to allocate id's but I need the same software on each device.

I'm thinking if the device being plugged in sent a "who's alive" request and any installed boxes replied with their ID, the new box should wait 20 seconds for all the replies then allocate itself the lowest available number, this would mean that if a node is removed and replaced with another box the new box takes the old boxes ID, these need to be mac independant and independant of IP too.

Any suggestions or does anyone know of any libraries to make this easier?

Cheers

Mike

    MyID = 0;
    do {
        MyID++;
        Broadcast: Is MyID in use on the net?
    }  while (ReplyReceived);

void loop() {
    If (Receive("Is MyID in use on the net)"))
         Reply "Yes" to sender;

Cheers for the reply John,

I'm trying to get my head round timings, so basically if I understand the flow of this correctly it goes as follows,

  1. Box starts as box 0
  2. box sends out an "anyone using 0" request
  3. box gets no reply so stays as box 0
  4. next box is plugged in and sends "anyone using box 0"
  5. box zero replies with a yes
  6. new box increments by 1 and says "anyone using 1"
  7. box gets no reply so sets id to 1

is there a way to set a timeout as theres going to be UDP data on the network all the time pretty much

This sounds like DHCP. An Arduino comes on line with ethernet and is automatically assigned an IP address by the router. The range of addresses would be configured in the router.

If you don't use DHCP you need to be careful with race conditions. If one or more devices comes online at the same time (e.g. after power outage) they could all check to see if "ID 1" is in use and finding that it's not they would all start using "ID 1" at the same time. DHCP was designed by smart people and already has that type of problem covered.

I have actually already used DHCP to kind of prove this out, my router is set to assign IP's from the range 100 to 199, I then simply grab the last octet deduct 100 from it and that's my ID, unique from other devices and sequential, till I remove a box from the system, even with a low lease time it's still holding the removed boxes ID, so say I take box four out and there are eight boxes, the next box gets 9 instead of four, even if I leave it for a while it still seems to do this.

Typically the router will not reassign the address for a period of days waiting for the machine that used it previously to come back. If you had a router running OpenWRT or such you'd have the configurability to knock that down to much shorter times -- down to just a few seconds if it was required.

But you highlight an important point; if boxes 1 to 4 go down, what period of time do you want to wait before boxes 5 to 8 reassign themselves as boxes 1 to 3? Figuring out the algorithm for all that stuff to work is not at all a trivial problem; the best solution is to avoid the requirement that the live boxes are always numbered starting at 1.