convert ds18b20 serial number

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
User avatar
c.man
Posts: 18
Joined: Thu Jul 06, 2017 9:12 pm

convert ds18b20 serial number

Postby c.man » Sat Aug 12, 2017 8:04 pm

Hi,
I'm running a code for temperature acquisition (with digital sensor ds18b20)

Code: Select all

import time
import machine
import onewire, ds18x20

# the device is on GPIO12
dat = machine.Pin(12)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# loop 10 times and print all temperatures
for i in range(10):
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        print(ds.read_temp(rom), end=' ')
    print()


in output I have:

Code: Select all

found devices: [bytearray(b'(\xb4\x95f\x05\x00\x00\x0c')]
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25
temperatures: 27.25


I want to convert devices from bytearray in string (in ubuntu/raspberry I have "28-0000056695b4").
How I to do ?

User avatar
deshipu
Posts: 1118
Joined: Thu May 28, 2015 5:54 pm

Re: convert ds18b20 serial number

Postby deshipu » Sat Aug 12, 2017 11:19 pm

Code: Select all

>>> hex(int.from_bytes(b'(\xb4\x95f\x05\x00\x00\x0c', 'little'))
'0xc0000056695b428'

User avatar
c.man
Posts: 18
Joined: Thu Jul 06, 2017 9:12 pm

Re: convert ds18b20 serial number

Postby c.man » Sun Aug 13, 2017 6:09 am

thanks !


Return to “Programs, Libraries and Tools”

Who is online

Users browsing this forum: No registered users and 1 guest