Skip to content

onewire

Boris Lovosevic edited this page Jan 28, 2018 · 10 revisions

machine Module

Class Onewire

This class includes full support for Onewire devices using the ESP32 RMT peripheral to achieve very precise timing

1-Wire is a device communications bus system designed by Dallas Semiconductor Corp. that provides low-speed data, signaling, and power over a single conductor.
1-Wire is similar in concept to I²C, but with lower data rates and longer range. It is typically used to communicate with small inexpensive devices such as digital thermometers and weather instruments. A network of 1-Wire devices with an associated master device is called a MicroLAN.
One distinctive feature of the bus is the possibility of using only two wires: data and ground. To accomplish this, 1-Wire devices include an 800 pF capacitor to store charge, and to power the device during periods when the data line is active.

Connecting 1-wire device.

ESP32 Device
any ouput capable port
External pull-up resistor (1K - 4.7K)
to +3.3V is needed
D (data pin)
GND GND
+3.3V Vdd (optional)

The 1-wire devices can be connected in two power modes:

  • Normal power, +3.3V is connected to the device's Vdd pin
    Normal Power
  • Parasite power, device's Vdd pin is connected to the GND pin, the needed power is supplied by the data pin
    Parasite power
  • The used Power mode is detected automatically by the driver
  • All devices connected to the same 1-wire bus must operate in the same mode

Create the Onewire instance object

ow = machine.Onewire(pin)

The new 1-wire bus is created on ESP32 gpio pin.
The bus is scanned for attached devices.

>>> import machine
>>> 
>>> ow = machine.Onewire(25)
>>> ow
Onewire(Pin=25, RMTChannels=7&6, Devices=2, Used=0)

Methods

ow.deinit()

Deinitialize the 1-wire bus and free the used resources.


ow.scan([asnum])

Scan 1-wire bus for attached devices.

Returns the tuple of detected devices ROM codes.

Optional asnum argument defines the format of the rom code returned.
If False (default), the rom code is returned as hex string.
If True, the rom code is returned as tuple: (family_code, serial_number, crc)

>>> ow.scan()
('6e000000c86a8e28', '02000000c82a8928')
>>> ow.scan(True)
((40, 13134478, 110), (40, 13118089, 2))

ow.search([asnum])

Same as ow.scan.


ow.rom_code(dev)

Return the ROM code of the device dev as hex string.

>>> ow.rom_code(0)
'6e000000c86a8e28'

ow.crc8(buf)

Returns the crc8 checksum byte for the buf argument

>>> ow.crc8('123456abcdef')
12
>>> buf = bytearray([0x11,0x12,0x13,0x67,0xf7])
>>> ow.crc8(buf)
227

The following low-level methods can be used
to implement Python drivers for various 1-wire devices.

ow.reset()

Reset the 1-wire bus.

ow.readbyte()

Read one byte from the 1-wire device.

ow.writebyte(val)

write one byte val to the 1-wire device.

ow.readbytes(len)

Read len bytes from the 1-wire device.
Returns string of read bytes.

ow.writebytes(buf)

Write multiple bytes to 1-wire device from string or bytearray buf.




Class Onewire.ds18x20

This class includes support for DS18xxx family of temperature sensors.

Supported devices: DS18B20, DS18S20, DS1822 and DS28EA00

Create the ds18x20 instance object

ds1 = machine.Onewire.ds18x20(ow, dev)

ow is the Onewire instance
dev is the device number on 1-wire bus.

>>> ds0 = machine.Onewire.ds18x20(ow, 0)
>>> ds0
ds18x20(Pin=25, OW Device=0, Type: DS18B20, Resolution=10bits, ROM code=6e000000c86a8e28, Parasite power: True)
>>> ds1 = machine.Onewire.ds18x20(ow, 0)
>>> ds1
ds18x20(Pin=25, OW Device=1, Type: DS18B20, Resolution=10bits, ROM code=02000000c82a8928, Parasite power: True)

Methods


ds.deinit()

Deinitialize the ds18x20 object, free the resources.

Note: Onewire object can be deinitialized only if all ds18x20 object using it are deinitialized.


ds.read_temp()

Return the last measured temperature from DS18xxx device.
The measurement cycle is not started.
The temperature is returned as the float value in °C


ds.read_tempint()

Return the last measured value from DS18xxx device.
The measurement cycle is not started.
The temperature is returned as the raw integer value.


ds.convert_read()

Return the temperature from DS18xxx device.
Start the measurement, wait until done.
The temperature is returned as the float value in °C

>>> ds1.convert_read()
24.75
>>> 

ds.convert_readint()

Return the temperature value from DS18xxx device.
Start the measurement, wait until done.
The temperature is returned as the raw integer value.

>>> ds1.convert_readint()
396

ds.convert([wait])

Start the temperature measurement on all attached devices.

If the optional argument wait is True, the function will wait until the measurement is done.
If wait argument is False (default), the function will exit immediately.
The measurement progress can be checked using ds-conv_time() method.


ds.conv_time()

Returns the elapsed time since last ds.convert(False).
If the returned value is negative, the conversion is still in progress.
If positive, the conversion is finished and the temperature valu can be read.

def wait_temp():
    ds1.convert(False)
    et = ds1.conv_time()
    while et < 50:
        print("waiting", et)
        time.sleep_ms(50)
        et = ds1.conv_time()
    print("finished, temp =",ds1.read_temp())

>>> wait_temp()
waiting -189
waiting -139
waiting -89
waiting -39
waiting 11
finished, temp = 24.75
>>> 

ds.get_res()

Return the current sensor resolution.

ds.set_res(res)

Set the sensor's resolution res bits.
Higher resolution will require longer conversion time.

>>> ds1.get_res()
10
>>> ds1.set_res(12)
True
>>> wait_temp()
waiting -752
waiting -702
waiting -652
waiting -602
waiting -552
waiting -502
waiting -452
waiting -402
waiting -352
waiting -302
waiting -252
waiting -202
waiting -152
waiting -102
waiting -52
waiting -2
waiting 48
finished, temp = 24.75
>>> 

ds.rom_code()

Return the device's ROM code as hex string.

>>> ds1.rom_code()
'6e000000c86a8e28'

ds.get_pwrmode()

Return device pover mode:
0 device operates in parasite power mode
255 device operates in normal power mode
2 device not responding
3 error

Clone this wiki locally