RaspberryPi DHT11 temperature and humidity sensor driver

To build a kernel mode driver you need to have available the kernel headers. Instructions on how to set up the headers can be found here Compiling kernel modules for Raspbian / Raspberry Pi
To help you understand how to write kernel mode drivers (where I sourced my information) was here
There is a link at the bottom of the page to a pre-built driver and to the source code for the project.

The DHT11 sensor is connected to GPIO 0 GPIO connector of the Raspberry pi - this has an internal 2.2K pullup so an external resistor is not needed. Power for the device is taken from the GPIO connector 3V3 pin. From the command line you can select a different GPIO port if you want to use a different one.

Driver file


By default the DHT11 is connected to GPIO pin 0 (pin 3 on the GPIO connector).
I know very little about Linux so getting this to work is a bit of a hack - I know there are other ways (with modprobe) but I have yet to explore this art..
The following works on my system...
The driver Major version default is 80 but can be set via the command line.

Command line parameters:
gpio_pin=X - a valid GPIO pin value
driverno=X - value for Major driver number
format=X - format of the output from the sensor

Usage:


Load driver: insmod ./dht11km.ko
i.e. insmod ./dht11km.ko gpio_pin=2 format=3

Set up device file to read from (i.e.):
mknod /dev/dht11 c 80 0
mknod /dev/myfile c 0 - to set the output to your own file and driver number

To read the values from the sensor: cat /dev/dht11

Validity of the return results are indicated ny OK or BAD as the last field in the results string.

Issues

Sometimes the readings are a bit odd. It appears a bit gets mis-interpreted or lost; the timing is critical and the microsecond delays are too fast for the RaspberryPi at times.
There is a retry strategy built into the driver but sometimes a bad result is still returned.

Driver code


Source code and make file plus a ready built driver are available here:

Disclaimer


Use the driver as you wish but if it breaks something dont blame me! I knew nothing about Linux before writing it - I know a little bit more now.
Any suggestions for improvements or how to do it better can be sent to my email (included in the source code).