Extend DHT Sensor for Orange Pi

Hello,

I figured out that the DHT Sensor component only works on a Raspberry Pi and (maybe) Beaglebone.

I want to run this on my Orange Pi One (which is my Home Assistant Server) and would like to be able to use the component.

Actually there is a program written in python which works to read temperature and humidity from my attached sensor.

It’s not harder then this:

git clone https://github.com/duxingkei33/orangepi_PC_gpio_pyH3
cd orangepi_PC_gpio_pyH3
sudo python setup.py install

git clone https://github.com/ionutpi/DHT22-Python-library-Orange-PI
cd DHT22-Python-library-Orange-PI
sudo python dht22_example.py

to get this:

Last valid input: 2018-01-18 18:30:45.093912
Temperature: 20.10 C
Humidity: 59.00 %
Last valid input: 2018-01-18 18:30:46.174023
Temperature: 20.00 C
Humidity: 58.90 %

I think this is the quick and dirty solution (and should be sufficient for a quick win)

There is also a more proper and general approach (supporting a wide range of boards) over here:

GitHub - chwe17/pyGPIO

You could create two python scripts. One for temperature and one for the humidity, and then use the Command Line Sensor. With the command line sensor you will be able to run a command and get the “result” in hass.

Example for CPU temp:

  - platform: command_line
    name: CPU Temp
    command: "cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ value|multiply(0.001)|round }}'

That examble gets you the CPU temperature in hass. Instead of the cat /sys… you could run your python script python ./humidity.py and it would show the humidity.

If the script requires sudo, you will have to add the home assistant user to sudo by following this guide. Remember to explicit select what sudo command home assistant is allowed to run.

Start with the DHT example you already found here.

I know it’s pretty hacky, and I wish I was more skilled so I would be able to help you better.

1 Like

Thank’s a lot for this idea and guidance. Right now I’m hacking this together with 0 (zero) python knowledge…

I have this now in my configuration.yaml

  - platform: command_line
    name: DHT Temperature
    command: "python ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
    unit_of_measurement: "°C"
    value_template: '{{ value|multiply(0.001)|round }}'

and the script: dht22_temp.py

from pyA20.gpio import gpio
from pyA20.gpio import port

#import RPi.GPIO as GPIO
import dht22
import time
import datetime

# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()


# read data using pin 14
instance = dht22.DHT22(pin=PIN2)

while True:
    result = instance.read()
    if result.is_valid():
        print("%.2f" % result.temperature)

    time.sleep(1)

which outputs (continually)

19.60
19.70
19.60
19.60
19.60

Even though the yaml is correct (passed the check), my hass webui isn’t coming up anymore.

In the hass log it’s only written:

2018-01-20 11:58:13 WARNING (MainThread) [homeassistant.components.sensor] Setup of platform command_line is taking over 10 seconds.

but the script is running (when I check with htop - with root privileges)
any ideas?

Your command should return a value that will be used by hass, not continually print it to console output.
Since your python script doesn’t return any value and keep running, hass might be blocked on the script.

Try to just get the value 1 time, and exit the script.

1 Like

Thank’s for the clarification. Problem is that I really have no python or any programming skills at all.

I managed to get only one output and let the script crashing afterwards with this (wrong) code in the last line:

while True:
    result = instance.read()
    if result.is_valid():
        print("%.2f" % result.temperature)

    time.stop(1)

The output is this one:

19.60
Traceback (most recent call last):
File “dht22_temp.py”, line 25, in
time.stop(1)
AttributeError: ‘module’ object has no attribute ‘stop’

It only ‘forced’ stop (or crashes) because the command does not exist.

Can somebody tell me how I can proper stop and close the program?

Have you tried sys.exit(1) or os._exit(1) (don’t forget to import sys or os).

And you don’t need the while True since you only want to read it once.
You could use a

while result.is_valid() is False:
    result = instance.read()
print("%.2f" % result.temperature)

With this variant I’m not sure you need to call exit. The script will reach an end and stop by itself (to confirm, I’m also no python expert).

1 Like

I went with this code now (using sys.exit(1) in the end):

from pyA20.gpio import gpio
from pyA20.gpio import port

#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys

# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()

# read data using pin 14
instance = dht22.DHT22(pin=PIN2)

while True:
    result = instance.read()
    if result.is_valid():
        print("%.2f" % result.temperature)

    sys.exit(1)

At it returns on line:

20.20

before it closes :+1:

I tried to use your cleaner code:

while result.is_valid() is False:
    result = instance.read()
print("%.2f" % result.temperature)

but I get this error as output

Traceback (most recent call last):
File “dht22_temp.py”, line 20, in
while result.is_valid() is False:
NameError: name ‘result’ is not defined

I have the problem now that the value in hass stays empty (-)

my yaml:

# DHT Sensor via command_line

  - platform: command_line
    name: DHT Temperature
    command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
    unit_of_measurement: "°C"
    value_template: '{{ value|multiply(0.001)|round }}'

coming up without problems.

My /DHT22-Python-library-Orange-PI/dht22_temp.py is like this:

from pyA20.gpio import gpio
from pyA20.gpio import port

#import RPi.GPIO as GPIO
import dht22
import time
import datetime
import sys

# initialize GPIO
#gpio.setwarnings(False)
#gpio.setmode(GPIO.BCM)
PIN2 = port.PA6
gpio.init()
#gpio.cleanup()

# read data using pin 14
instance = dht22.DHT22(pin=PIN2)

while True:
    result = instance.read()
    if result.is_valid():
        print("%.2f" % result.temperature)

    sys.exit(1)

an outputs a proper value when called (python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py)

20.10

but in the logfile from hass it says:

Command failed: python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py
1:50 PM components/sensor/command_line.py (ERROR)

I don’t know what’s wrong? :confused:

Call this:

result = instance.read()

One time before your while

Yes, using sys.exit(1) might be wrong, try with sys.exit(0) instead.
The code 1 signal an execution error, 0 is the default code. Sorry for giving the wrong code ^^

No need to say sorry! I say thank’s a lot for investing your time helping me! :smiley:

Got it working now! :upside_down_face:

I needed to edit the last line in this code snippet (configuration.yaml) to get the right numbers

# DHT Sensor via command_line

  - platform: command_line
    name: DHT Temperature
    command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
    unit_of_measurement: "°C"
    value_template: '{{ value }}'

I will now try to use your (proper) code and not the ‘while’ hacky thing. Then I will try to do the same for the humidity!

Nice! It works! And humidity is online as well :relaxed:

So here is my sum up (important is that I’m running hass as root - if you don’t you maybe add some sudo commands and add your hass user to the sudo group!)

  1. Clone & install this

    git clone GitHub - duxingkei33/orangepi_PC_gpio_pyH3: python control orangepi_PC ext GPIO ALLwinner H3 base on pyA20 0.2.1
    cd orangepi_PC_gpio_pyH3
    sudo python setup.py install

  2. Clone & run this to check you get an output

    git clone GitHub - ionutpi/DHT22-Python-library-Orange-PI: Read temperature and humidity values from DHT22 sensor on Orange PI.
    cd DHT22-Python-library-Orange-PI
    sudo python dht22_example.py

  3. Output should be like this:

    Last valid input: 2018-01-18 18:30:45.093912
    Temperature: 20.10 C
    Humidity: 59.00 %

  4. In this folder you are (DHT22-Python-library-Orange-PI) you create a new file called dht22_temp.py adding this code:

    from pyA20.gpio import gpio
    from pyA20.gpio import port
    
    #import RPi.GPIO as GPIO
    import dht22
    import time
    import datetime
    import sys
    
    # initialize GPIO
    #gpio.setwarnings(False)
    #gpio.setmode(GPIO.BCM)
    PIN2 = port.PA6
    gpio.init()
    #gpio.cleanup()
    
    
    # read data using pin 14
    instance = dht22.DHT22(pin=PIN2)
    
    result = instance.read()
    while result.is_valid() is False:
        result = instance.read()
    print("%.2f" % result.temperature)
    
  5. And another file called dht22_hum.py adding this code:

    from pyA20.gpio import gpio
    from pyA20.gpio import port
    
    #import RPi.GPIO as GPIO
    import dht22
    import time
    import datetime
    import sys
    
    # initialize GPIO
    #gpio.setwarnings(False)
    #gpio.setmode(GPIO.BCM)
    PIN2 = port.PA6
    gpio.init()
    #gpio.cleanup()
    
    
    # read data using pin 14
    instance = dht22.DHT22(pin=PIN2)
    
    result = instance.read()
    while result.is_valid() is False:
        result = instance.read()
    print("%.2f" % result.humidity)
    
  6. Last but not least you need to touch your configuriatons.yaml adding this:

    # DHT Sensor via command_line: Temperature
    
      - platform: command_line
        name: DHT Temperature
        command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_temp.py"
        unit_of_measurement: "°C"
        value_template: '{{ value }}'
    
    # DHT Sensor via command_line: Humidity
    
      - platform: command_line
        name: DHT Humidity
        command: "python2 ~/DHT22-Python-library-Orange-PI/dht22_hum.py"
        unit_of_measurement: "%"
        value_template: '{{ value }}'
    

So now it’s time for a tea! Enjoy!

3 Likes

I wonder what’s needed to get this as a component into home assistant? The library dependencies should be not problem (all foss)…

This script should serve all Orange PI SBC’s with H3 & H2+ SoC (there is around 10 or more of them).

Till now this is first (one and only) hacky implemation in my HASS setup. And I wan’t to get rid of it :stuck_out_tongue_winking_eye:

1 Like

This is for Orange Pi’s with Allwinner H3 SoC, if you want to have this functionally on Orange Pi with Allwinner H5 SoC use this github instead:

git clone https://github.com/herzig/orangepi_PC_gpio_pyH5

the rest is equal. Good luck!

1 Like