convert ds18b20 serial number

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
c.man
Posts: 21
Joined: Thu Jul 06, 2017 9:12 pm

convert ds18b20 serial number

Post by 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: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: convert ds18b20 serial number

Post by 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: 21
Joined: Thu Jul 06, 2017 9:12 pm

Re: convert ds18b20 serial number

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

thanks !

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: convert ds18b20 serial number

Post by ajocius » 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

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: convert ds18b20 serial number

Post by navy » Fri May 17, 2019 9:00 am

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()

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: convert ds18b20 serial number

Post by kevinkk525 » 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))
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

BigStupidBeast
Posts: 9
Joined: Thu May 28, 2020 5:40 pm
Location: sorry for my English)

Re: convert ds18b20 serial number

Post by BigStupidBeast » Fri Jun 19, 2020 9:20 pm

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:

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: convert ds18b20 serial number

Post by kevinkk525 » Sat Jun 20, 2020 9:58 am

Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply