Sensore digitale di temperatura ed umidità DHT22 su CM3 Panel

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

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

Collegamento su connettore EXP2 - GPIO 27.

Datasheet

Sensore digitale DHT22

Occorre impostare in pullup il GPIO 27.

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

/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 */
            };
        };
    };
};

Lanciare il comando per compilare il dtbo:

sudo dtc -@ -I dts -O dtb -o /boot/overlays/dht22.dtbo /boot/overlays/dht22.dts

Questo un esempio per impostare con stesso file dts i GPIO della scheda come pullup, pulldown, input o output.

# =========================================================
# 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 */
            };
        };
    };
};

Ora modificare il file con il comando sudo nano /boot/config.txt
Inserire nell'ultima riga:

# DHT22
device_tree_overlay=overlays/dht22.dtbodevice_tree_overlay=overlays/dht22.dtbo

Ed effettuare il reboot.

sudo reboot

Collegamenti tra il sensore e la CM3 Panel.

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

File di esempio in Node Red.

Download file json

Invece di utilizzare Node Red, ora descrivo l'utilizzo in Python. Installazione dei pacchetti necessari:

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

Creare lo script dht22.py

nano /home/pi/dht22.py
#!/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 ')
    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)

Links

Mauro Tocci
CMMS (Computerized Maintenance Management System) & CAFM (Computer Aided Facility Management) Specialist Project Manager
Analyst & Developer - Building Automation & Access Control Systems Integrator
Linux Server Administrator - KVM Kernel-based Virtual Machine - Red Hat Enterprise - Fedora - Centos

http://www.coolprojects.it - http://www.multiconproject.com - http://www.multiconproject.it - https://github.com/maurotocci