Hardware ID

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
tillorly
Posts: 1
Joined: Tue Jan 26, 2016 5:11 pm

Hardware ID

Post by tillorly » Tue Dec 13, 2016 10:01 pm

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.

shaoziyang
Posts: 363
Joined: Sun Apr 17, 2016 1:55 pm

Re: Hardware ID

Post by shaoziyang » Wed Dec 14, 2016 1:53 pm

in ESP8266 and STM32 port, in machine mudule, it can get ID use machine.unique_id(). But microbit doesn't has this feature.

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Hardware ID

Post by jpj » Thu Dec 15, 2016 3:48 am

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Hardware ID

Post by dhylands » Thu Dec 15, 2016 4:11 am

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'

jpj
Posts: 60
Joined: Sat Dec 10, 2016 3:07 pm

Re: Hardware ID

Post by jpj » Thu Dec 15, 2016 4:14 am

Excellent. Thanks Dave!

Post Reply