Pi IoT In Python Using Linux Drivers -The DHT22 |
Written by Harry Fairhead & Mike James | ||||
Monday, 15 March 2021 | ||||
Page 2 of 3
If you lookup the details of the dht11 overlay in the ReadMe you will find: Name: dht11 Info: Overlay for the DHT11/DHT21/DHT22 This all very straightforward. If you add: dtoverlay=dht11 gpiopin=4 to the config.txt file then when you reboot, the driver will be loaded and configured to use GPIO4. Change the gpiopin parameter to whichever GPIO line you are using. After a reboot the driver will be ready to use – but where are the device folders? If you look in /sys/class you won’t find any new or obvious folders, but examining the device tree listed in /proc/device-tree you will find a new device, /proc/device-tree/dht11@0. However, looking through the information listed there gives you no clue how to find and use the device folders. This is very typical of using a new device driver – its use is more than obvious to its creators, but a potential user is left out in the cold. The key to using the dht11 device driver is to discover that it is stored in the Pi OS GitHub repository at /linux/drivers/iio/humidity/dht11.c. The iio folder is where drivers written to the Industrial I/O standard are stored. This is covered in more detail in Chapter 14, but for now we can ignore this fact and simply use it to find the device folders. All IIO devices are stored in /sys/bus/iio/devices/ and if you look you will find /sys/bus/iio/devices/iio:device0. This corresponds to the dht11 device you have just added. Of course iio:device0 isn’t a name that is going to ensure that you have the dht11 device. A partial solution is to read the name file to discover the device name. To read the actual data you read in_temp_input for the temperature in millidegree Celsius and in_humidityrelative_input for the percentage humidity times 1000. There is no easy way to find the names of these files in the documentation, but once you have found the correct folder they are fairly obvious. A program to read the name, temperature and humidity is: import io fdr = io.open("/sys/bus/iio/devices/iio:device0/name", "rb", buffering=0) name = fdr.readline().decode("utf-8") print("name=", name) fdr.close() fdr = io.open( If you run the program you will see the name dht11@0 and the temperature and humidity. In a more complete program you would check the name to make sure that it is the device you want and if there are multiple devices you would search through the top-level folder to find the device with the correct name. Dynamic Loading DriversThere are many problems with using drivers as part of an application and one big one is the need to configure the system to install the driver. The simplest solution is to ask the user to edit the config.txt file, but this could be a road block to your app being installed at all and then there is the possibility of user error. A better solution is to include the change in an install script. This is simple but doesn’t allow for the user or some other program changing the config.txt file so that your app no longer works. A good solution in some cases is to make use of the new dynamic overlay loading feature. This is fairly recent but it works with the latest version of Pi OS. Instead of having to load drivers at boot time you can use the dtoverlay or dtparam commands to install and configure drivers. The only problem is that there are some cases where it doesn’t work. Essentially you have to try it out to see it if works. The overlays that are dynamically loaded form a stack and, in theory, you can only remove the overlay on the top of the stack. In practice, removing any overlay often results in a memory leak at best and a crashed system at worse. The dtoverlay command is described in the documentation as: dtoverlay <overlay> [<param>=<val>...] Add an overlay (with parameters) Options applicable to most variants: -d <dir> specify an alternate location for the overlays The dtparam command is: dtparam <param>=<val>… Add an overlay (with parameters) Options applicable to most variants: -d <dir> Specify an alternate location for the overlay The important thing is that you can use the -l option to discover what overlays are already loaded and hence avoid loading the same overlay again. <ASIN:1871962587> <ASIN:B07S1K8KLW> |
||||
Last Updated ( Monday, 15 March 2021 ) |