Onewire scan question

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.
Post Reply
Wim
Posts: 2
Joined: Thu Mar 11, 2021 12:35 pm

Onewire scan question

Post by Wim » Thu Mar 11, 2021 1:08 pm

Hello all,

i am playing a bit with micropython and a ds18b20 i use the following short program:

Code: Select all

import time
import onewire, ds18x20

from machine import Pin

# the device is on B12
dat = Pin('B12')

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

roms = ds.scan()
print('found devices:', roms)

ds.convert_temp()

time.sleep_ms(750)

rom_code = roms[0]

print(ds.read_temp(rom_code), end=' ')
print()
print (rom_code)

for i in range(8):
    print (hex(rom_code[i]) , end=' ')


The program works ok and reads the temperature fine but i do not understand why i only get 7 bytes back from ds.scan() but when i read them back seperate i get 8 bytes .

The output of the program is:

found devices: [bytearray(b'(\x82\x9b\x94\x05\x00\x00\x87')]

19.0

bytearray(b'(\x82\x9b\x94\x05\x00\x00\x87')

0x28 0x82 0x9b 0x94 0x5 0x0 0x0 0x87

I am using MicroPython v1.14-82-gcdaec0dca on 2021-02-27; WeAct F411CE with STM32F411CE

Regards
Wim

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

Re: Onewire scan question

Post by dhylands » Thu Mar 11, 2021 2:27 pm

You are getting back 8 bytes. The first byte (after the quote) is an opening parenthesis ( which has a hex representation of 0x28.

When python tries to print a string it will use ASCII bytes whenever possible and only uses hex values for non-ASCII bytes.

Wim
Posts: 2
Joined: Thu Mar 11, 2021 12:35 pm

Re: Onewire scan question

Post by Wim » Thu Mar 11, 2021 3:03 pm

Now i see it it is the second ( before the /x82.
I overlooked a parenthesis i should have counted them :oops:
Thanks a lot.

Wim

Post Reply