Page 1 of 1

Hardware ID

Posted: Tue Dec 13, 2016 10:01 pm
by tillorly
Hi everyone,

is there a way to get the hardware ID of the chip from micropython? We have a project, were we want to identify some dozen micro:bits.

Thanks guys.

Re: Hardware ID

Posted: Wed Dec 14, 2016 1:53 pm
by shaoziyang
in ESP8266 and STM32 port, in machine mudule, it can get ID use machine.unique_id(). But microbit doesn't has this feature.

Re: Hardware ID

Posted: Thu Dec 15, 2016 3:48 am
by jpj
I have an ESP8266 on an Adafruit HUZZA board and ran it:

Code: Select all

>>> import machine
>>> machine.unique_id()
b'~\xd8\xc6\x00'
>>>
I searched for a way to make the byte string more human readable but could only come up with this:

Code: Select all

>>> machine.unique_id().decode("utf-8")
'~\x18\x06\x00
Is there some other method to make the byte string more readable? I was thinking maybe a string of hex values without the \x

Thanks

Re: Hardware ID

Posted: Thu Dec 15, 2016 4:11 am
by dhylands
That's a string of binary bytes. You can get a hex version by doing something like this:

Code: Select all

>>> import ubinascii
>>> ubinascii.hexlify(b'~\xd8\xc6\x00')
b'7ed8c600'
>>> ubinascii.hexlify(b'~\xd8\xc6\x00').decode('utf-8')
'7ed8c600'
You could also do something like:

Code: Select all

>>> id = b'~\xd8\xc6\x00'
>>> '{:02x}{:02x}{:02x}{:02x}'.format(id[0], id[1], id[2], id[3])
'7ed8c600'

Re: Hardware ID

Posted: Thu Dec 15, 2016 4:14 am
by jpj
Excellent. Thanks Dave!