How to set Unique ID to Arduino

arduinouuid

In the new Ardulink 2.0.0 release distribution (download it here) there is an example that shows how is simple with Ardulink set an unique id to Arduino. Arduino boards haven’t an unique id like the MAC address for network interfaces. This can be a problem for some systems where several Arduino based boards have to interact. One example maybe the Leader Election problem in distributed systems.

Into the Ardulink 2.0.0 distribution you can find UniqueID example with a specific sketch.

The most important row in the example is:

RplyEvent rplyEvent = ResponseAwaiter.onLink(link)
                    .withTimeout(500, MILLISECONDS)
                    .waitForResponse(sendUniqueIdCustomMsg(link));

ResponseAwaiter is able to catch the response coming from Arduino about a specific request.

sendUniqueIdCustomMsg method is:

private long sendUniqueIdCustomMsg(Link link) throws IOException {
   return link.sendCustomMessage(GET_UNIQUE_ID_CUSTOM_MESSAGE,
                                       getSuggestedUniqueID());
}
    
private String getSuggestedUniqueID() {
   sugestedUniqueID = UUID.randomUUID().toString();
   return sugestedUniqueID;
}

where GET_UNIQUE_ID_CUSTOM_MESSAGE is a constant equal to “getUniqueID” and the suggestedUniqueID is an UUID generated by the example.

The example sketch receives the “getUniqueID” custom message together with the suggested unique id parameter. So it controls if into the Arduino’s EEPROM there is already stored an UUID. If yes it returns the stored UUID to Ardulink with a reply event message otherwise it stores the suggested UUID and then returns this UUID that now owns.

The ResponseAwaiter catches the response coming from Arduino and you can get the Arduino’s UUID with this statement:

String uniqueID = rplyEvent.getParameterValue("UniqueID");