Program Unique Serial Number to Hardware

techElder

Well-known member
While creating an RF networked sensor system, I needed each hardware sensor to include its own identifier (serial number) with the transmission of data. That way I knew which sensor the data came from. There was a need for uniqueness in the local network, but there wasn't any need from a security standpoint and there wasn't going to be 1000's of sensors.

My initial thought was to simply adjust an EEPROM location with the identifier while programming the sensor processor (08M2). Then the program would read this value at startup and transmit it with the data. The thought of doing this for 20 or 30 sensors and keeping up with it got me to ask about a solution on the forum. I still store the identifier in an EEPROM location, but getting it there happens during programming of the processor.

Here is the final result which works for a single session of programming a batch of sensors (and probably over additional time also) because it uses the preprocessor "ppp_time". Any identifiers that are duplicates can be adjusted by reprogramming. This is better than the time it would take to adjust 20 or 30 sensors. Just reprogram any conflicting devices.

It works by substituting "ppp_time" into the macro during programming. Then, upon first execution, the macro is executed to calculate the unique identifier and store it in EEPROM. From then on (until reprogramming) the program reads this location to obtain the "serial number."

Thanks go to all that participated, and, of course, thanks to Hippy for the final solution.

Here is the subroutine version:

Code:
' EXAMPLE

symbol serialNumber = w1
symbol serialNumberAddress = 0               ' location in EEPROM where serialNumber is stored

' #Define pppTime ppp_time
'  #Define pppTime "18:12:15"
'  #Define pppTime "22:12:15"


'---------------------------------
'------[ READ SERIALNUMBER ]------
'---------------------------------


read serialNumberAddress, WORD serialNumber
if serialNumber = 0 then
   gosub UID ' uses b0 and w1
   write serialNumberAddress, WORD serialNumber
endif

end

UID:
   '  Create a unique ID number (serial number) for small network sensor ID.
   '  Using the PPP_* pre-processor symbols it is possible to program the time of download into the PICAXE 
   '     and then use that to create the ID. Presumably one would check a value in EEPROM to see if it is
   '     zero or has one of these IDs saved at that location as the sensor serial number.
   '  Based on posts started on the PICAXE FORUM at:
   '     http://www.picaxeforum.co.uk/showthread.php?29537-Serial-Number
   '  Final code is from Hippy's post #19 part B.

   '  The unique number is returned in w1.
   '  #Define pppTime ppp_time
   '  Uses b0, w1
   '  The value returned in w1 isn't random nor vastly different, just unique.
   '  Unless two devices are programmed at exactly the same time of day there won't be a duplicate.
   '  This allows times 00:00:00 to 18:12:15 to give as a unique code, 0 to 65535 however
   '     adding the "- 4" allows times from 04:00:00 to 22:12:15.
   '     It still works outside of 04:00:00 to 22:12:15 but there's more chance of a clash.
   '
   Lookup 0, (ppp_time), b0 : w1 =           b0 - "0"
   Lookup 1, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0" - 4
   Lookup 3, (ppp_time), b0 : w1 = w1 *  6 + b0 - "0"
   Lookup 4, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0"
   Lookup 6, (ppp_time), b0 : w1 = w1 *  6 + b0 - "0"
   Lookup 7, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0"

   return
Here is the macro version:


Code:
' EXAMPLE

symbol serialNumber = w1
symbol serialNumberAddress = 0               ' location in EEPROM where serialNumber is stored

' #Define pppTime ppp_time
'  #Define pppTime "18:12:15"
'  #Define pppTime "22:12:15"

#Macro UID(idWord,idAddr)
   '  Create a unique ID number (serial number) for small network sensor ID.
   '  Using the PPP_* pre-processor symbols it is possible to program the time of download into the PICAXE 
   '     and then use that to create the ID. Presumably one would check a value in EEPROM to see if it is
   '     zero or has one of these IDs saved at that location as the sensor serial number.
   '  Based on posts started on the PICAXE FORUM at:
   '     http://www.picaxeforum.co.uk/showthread.php?29537-Serial-Number
   '  Final code is from Hippy's post #19 part B.

   '  The unique number is returned in idWord.
   '  #Define pppTime ppp_time
   '  The value returned in idWord isn't random nor vastly different, just unique.
   '  Unless two devices are programmed at exactly the same time of day there won't be a duplicate.
   '  This allows times 00:00:00 to 18:12:15 to give as a unique code, 0 to 65535 however
   '     adding the "- 4" allows times from 04:00:00 to 22:12:15.
   '     It still works outside of 04:00:00 to 22:12:15 but there's more chance of a clash.
   '  Uses b0, w1

   read idAddr, WORD idWord
   if idWord = 0 then
      Lookup 0, (ppp_time), b0 : w1 =           b0 - "0"
      Lookup 1, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0" - 4
      Lookup 3, (ppp_time), b0 : w1 = w1 *  6 + b0 - "0"
      Lookup 4, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0"
      Lookup 6, (ppp_time), b0 : w1 = w1 *  6 + b0 - "0"
      Lookup 7, (ppp_time), b0 : w1 = w1 * 10 + b0 - "0"
      idWord = w1
      write idAddr, WORD idWord
   endif
#endm

'---------------------------------
'------[ READ SERIALNUMBER ]------
'---------------------------------

UID(serialNumber,serialNumberAddress)

end
 
Last edited:
Top