Page 1 of 1

convert ds18b20 serial number

Posted: Sat Aug 12, 2017 8:04 pm
by c.man
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 ?

Re: convert ds18b20 serial number

Posted: Sat Aug 12, 2017 11:19 pm
by deshipu

Code: Select all

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

Re: convert ds18b20 serial number

Posted: Sun Aug 13, 2017 6:09 am
by c.man
thanks !

Re: convert ds18b20 serial number

Posted: Sat Mar 03, 2018 9:54 am
by ajocius
How to add this to the code? Testing this sensor as well.

print('found devices:', hex(int.from_bytes(b'(roms', 'little'))))

does not work

Re: convert ds18b20 serial number

Posted: Fri May 17, 2019 9:00 am
by navy
ajocius wrote:
Sat Mar 03, 2018 9:54 am
How to add this to the code? Testing this sensor as well.

print('found devices:', hex(int.from_bytes(b'(roms', 'little'))))

does not work
roms is an array so you should use it like this:

Code: Select all

roms = ds.scan()
print('found devices:', roms)
#serialnum = hex(int.from_bytes(roms[0], 'little'))
#print('serial:', serialnum)
or in example loop:

Code: Select all

for i in range(5):
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        serialnum = hex(int.from_bytes(rom, 'little'))
        print('sensor #', serialnum)
        print(ds.read_temp(rom), end=' ')
    print()

Re: convert ds18b20 serial number

Posted: Fri May 17, 2019 6:37 pm
by kevinkk525
Always funny when someone revives a thread that is a year old :D

The solution of converting a rom to string is quite simple: ''.join('%02X' % i for i in iter(rom))

Re: convert ds18b20 serial number

Posted: Fri Jun 19, 2020 9:20 pm
by BigStupidBeast
kevinkk525 wrote:
Fri May 17, 2019 6:37 pm
Always funny when someone revives a thread that is a year old :D

The solution of converting a rom to string is quite simple: ''.join('%02X' % i for i in iter(rom))
Every year some nube like me ask something like
Can you take some sample of code? Pleeeeeesssee :roll:

Re: convert ds18b20 serial number

Posted: Sat Jun 20, 2020 9:58 am
by kevinkk525