Pi IoT In Python Using Linux Drivers -The DHT22
Written by Harry Fairhead & Mike James   
Monday, 15 March 2021
Article Index
Pi IoT In Python Using Linux Drivers -The DHT22
Using The Driver
The Program

 A function to check for the dht11 overlay and only load it if it isn’t already loaded is easy enough with just some string handling and the use of the Popen method:

import subprocess 
import io import fcntl def checkDht11(): indicator = "dht11 gpiopin=4" command =["sudo", "dtoverlay", "dht11","gpiopin=4"] temp = subprocess.Popen(["sudo", "dtparam", "-l"], stdout = subprocess.PIPE) output = str(temp.communicate()) print(output) if output.find(indicator)==-1: temp = subprocess.Popen(command,
stdout = subprocess.PIPE)
output = str(temp.communicate()) print(output) return checkDht11() 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(
"/sys/bus/iio/devices/iio:device0/in_temp_input",
"rb", buffering=0) temp = fdr.readline().decode("utf-8") print("temp=", int(temp)/1000, "C") fdr.close() fdr = io.open("/sys/bus/iio/devices/iio:device0/ in_humidityrelative_input", "rb", buffering=0) hum = fdr.readline().decode("utf-8") print("Humidity=",int(hum)/1000,"%") fdr.close()

The indicator string is used to specify the string that is in the overlay listing when the driver is loaded and this isn’t necessarily the same as the string in the command string, which is the command to install the driver with any parameters that are needed.

You can run this program without having to change the config.txt file. The driver remains loaded until the next reboot and is reinstalled when you next run the program. Notice that the program has to have root permissions to be able to run sudo, even if the program isn’t being run as root.

Summary

  • The Device Tree is the modern way to specify the hardware configuration of a machine and to load and configure drivers.

  • The boot disk contains device trees for each of the different versions of the Pi and the loader selects the appropriate device tree for the Pi model that is booting.

  • The loader merges the device tree with the overlays listed in the config.txt file to produce a customized final device tree used to boot the system.

  • Overlays are fragments of the device tree which can be used to modify and add to the basic device tree.

  • Overlays can themselves be customized by specifying parameters.

  • When the loader parses the DT and applies all the overlays you have specified, the final DT is represented by the folders in /proc/overlay.

  • The DHT22 Humidity and Temperature Sensor provides an example of loading and customizing a driver.

  • A fairly recent innovation is the ability to load and customize drivers after the system has booted using the dtoverlay and dtparam commands.

  • You can easily write a program to load and configure missing drivers at runtime.

  • Dynamic loading of overlays doesn’t always work and unloading overlays, while possible, isn’t a good idea.

Raspberry Pi IoT In PythonUsing Linux Drivers
Second Edition

By Harry Fairhead & Mike James

DriverPython2e360

Buy from Amazon.

Contents

  1.  Choosing A Pi For IoT
  2.  Getting Started With Python
  3.   Drivers: A First Program
  4.  The GPIO Character Driver 
  5.  GPIO Using Ioct ***NEW!!
  6.  GPIO Events
  7.  The Device Tree
       Extract: The DHT22
  8.  Some Electronics
  9.  Pulse Width Modulation
       Extract: PWM *
  10. SPI Devices
  11. I2C Basics
       Extract: I2C *
  12. The I2C Linux Driver
  13. Advanced I2C
  14. Sensor Drivers
  15. 1-Wire Bus
       Extract 1-Wire And The DS18B20 *
  16. Going Further With Drivers
  17. Appendix I

*From the first edition waiting for update.

 <ASIN:B0CT46R6LF>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Looking Forward To NAO 7
03/11/2024

Introduced to the world in 2004 by its creator Bruno Maisonnier the kid-sized, autonomous humanoid robot NAO, turns 20 this year. At less than 2 ft tall, it is small in stature, but plays a big r [ ... ]



Google Updates Responsible AI Toolkit
01/11/2024

Google has announced updates to the Responsible Generative AI Toolkit to enable it to be used with any LLM model. The Responsible GenAI Toolkit provides resources to design, build, and evaluate open A [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962587>

<ASIN:B07S1K8KLW>

<ASIN:1871962455>

<ASIN:1871962463>



Last Updated ( Monday, 15 March 2021 )