|Pi IoT In Python Using Linux Drivers -1-Wire And The DS18B20|
|Written by Harry Fairhead & Mike James|
|Monday, 12 July 2021|
Page 3 of 3
Once the driver is loaded and the device is recognized you will find a set of new folders in the w1/devices folder:
Most of the files have functions that are obvious from their names but there are some points of detail:
It is also worth knowing that the device is also added to the hwmon folder and that folder is replicated in the devices folder. The reason for this is to allow integration with any hwmon software you may have – there are no advantages over using the device directly.
The 1-wire master also has some useful files in the w1_bus_master folder:
therm_bulk_read Takes a temperature from all devices
w1_master_add Manually registers a slave device
w1_master_attempts Number of times a search was attempted
w1_master_max_slave_count Maximum number of slaves to search for
w1_master_name Name of the device (w1_bus_masterX)
w1_master_pullup 5V strong pull-up 0 enabled, 1 disabled
w1_master_remove Manually remove a slave device
w1_master_search Number of searches left to do
w1_master_slave_count Number of slaves found
w1_master_slaves Names of the slaves, one per line
w1_master_timeout Delay in seconds between searches
w1_master_timeout_us Delay in microseconds between searches
Normally a temperature conversion is triggered when you read the appropriate file but if you write trigger to the therm_bulk_read file all of the connected devices are read and the readings stored for the next time you read the device. Reading the file returns 0 if no bulk conversion is in progress, -1 if at least one device is still converting and 1 if conversion is complete but there is still data to be read from the devices.
You can set the w1_master_search to a small number if the attached devices rarely change. If your devices never change you could set it to zero and use w1_master_add to add the serial numbers.
The w1_master_timeout and w1_master_timeout_us determine the interval between searches for devices. Each time a search occurs w1_master_search is decremented and w1_master_attempts is incremented.
You can use the w1_master_slave_count and w1_master_slaves as an alternative way of discovering what devices are installed:
def getDevices(): fdr = io.open("/sys/bus/w1/drivers/w1_master_driver/ w1_bus_master1/w1_master_slaves", "r") buffer=fdr.read() fdr.close() return buffer.split()
Notice that the names are separated by newline characters and this is where the split function splits the string.
A complete program that reads and displays the data of the first device connected to the 1-Wire bus is:
import subprocess import io def load1w(pin): indicator = "w1-gpio" command =["sudo", "dtoverlay", "w1-gpio",
In Chapter But Not In This Extract
Raspberry Pi IoT In Python Using Linux Drivers
By Harry Fairhead & Mike James
Buy from Amazon.
or email your comment to: email@example.com
|Last Updated ( Monday, 12 July 2021 )|