Markdown source

#Sensore digitale di temperatura ed umidità DHT22 su CM3 Panel

<abstract>
Appunti collegamento sensore digitale temperatura ed umidità DHT22 (Node Red o Python) su CM3 Panel.
</abstract>

### Testato su Raspbian GNU/Linux 9.4 (stretch) Kernel 4.14.34-v7+

Collegamento su connettore EXP2 - GPIO 27.

### Datasheet
<a href="./D000000000566.pdf">Sensore digitale DHT22</a>

Occorre impostare in pullup il GPIO 27.

Editare file :
sudo nano /boot/overlays/dht22.dts

<pre class="terminal">
/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    fragment@0 {
        target = <&gpio>;
        __overlay__ {
            pinctrl-names = "default";
            pinctrl-0 = <&my_pins>;

            my_pins: my_pins {
                brcm,pins = <27>;     /* gpio no. */
                brcm,function = <0>; /* 0:in, 1:out */
                brcm,pull = <2>;     /* 2:up 1:down 0:none */
            };
        };
    };
};
</pre>

Lanciare il comando per compilare il dtbo:
<pre class="terminal">
sudo dtc -@ -I dts -O dtb -o /boot/overlays/dht22.dtbo /boot/overlays/dht22.dts
</pre>

Questo un esempio per impostare con stesso file dts i GPIO della scheda come pullup, pulldown, input o output.
<pre class="terminal">
# =========================================================
# Esempio per impostare più GPIO nel DTS
# =========================================================
#	GPIO  	Type		 	Pull Type
#	7 		Input 			pulldown
#	8 		Output 			pulldown
#	9 		Input			pullup

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    fragment@0 {
        target = <&gpio>;
        __overlay__ {
            pinctrl-names = "default";
            pinctrl-0 = <&my_pins>;

            my_pins: my_pins {
                brcm,pins = <7 8 9>;     /* gpio no. */
                brcm,function = <0 1 0>; /* 0:in, 1:out */
                brcm,pull = <1 1 2>;     /* 2:up 1:down 0:none */
            };
        };
    };
};
</pre>

Ora modificare il file con il comando sudo nano /boot/config.txt<br>
Inserire nell'ultima riga:
<pre class="terminal">
# DHT22
device_tree_overlay=overlays/dht22.dtbodevice_tree_overlay=overlays/dht22.dtbo
</pre>

Ed effettuare il reboot.
<pre class="terminal">
sudo reboot
</pre>

Collegamenti tra il sensore e la CM3 Panel.
<img src="./Connessioni.jpg" class="img-responsive left-block" />

Esempio in Node Red, installare node-red-contrib-dht-sensor

### File di esempio in Node Red.
<a href="./dht22.json">Download file json</a>

<img src="./NodeRed.jpg" class="img-responsive left-block" />

Invece di utilizzare Node Red, ora descrivo l'utilizzo in Python.
Installazione dei pacchetti necessari:
<pre class="terminal">
sudo apt-get update
sudo python -m pip install --upgrade pip setuptools wheel
sudo pip install Adafruit_DHT

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

cd Adafruit_Python_DHT
sudo python setup.py install
</pre>

Creare lo script dht22.py
<pre class="terminal">
nano /home/pi/dht22.py
</pre>

<pre class="prettyprint">
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys

import Adafruit_DHT

if len(sys.argv) == 2:
    sensor = 22
    pin = sys.argv[1]
else:
    print('Usage: sudo ./dht22 <GPIO pin number>')
    sys.exit(1)

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!

if humidity is not None and temperature is not None:
    print('Read:{0:0.1f}:{1:0.1f}'.format(temperature, humidity))
else:
    print('Error reading.')
    sys.exit(1)
</pre>

## Links
* CM3-PANEL - Panel PC based on Raspberry Compute Module 3 lite <https://www.acmesystems.it/CM3-PANEL>
* Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or Beaglebone Black <https://github.com/adafruit/Adafruit_Python_DHT>

@include='bio_mauro_tocci'