DHT sensor on RPi 3

raspberrypi3

#1

Hey there

I recently tried to use a DHT22 temperature and humidity sensor using a RPi3 and Resin OS 2.13.6+rev1.

I tried various method to get the sensor to work:

  1. I tried to add the module Adafruit-DHT==1.3.4 in requirements.txt, but when I tried to push the code to the device, it gives me this error:
    Could not detect if running on the Raspberry Pi or Beaglebone Black. If this failure is unexpected, you can run again with --force-pi or --force-bbb parameter to force using the Raspberry Pi or Beaglebone Black respectively.'

  2. Then I tried to install the module in the dockerfile:

cd /tmp/ && git clone https://github.com/adafruit/Adafruit_Python_DHT.git && \
 cd Adafruit_Python_DHT && python3 setup.py install && 

which successfully deployed the code to the application but in my logger python threw an error:
ImportError: No module named 'Adafruit_DHT' and when I try to install it in the container via the terminal I get the error:
importError: cannot import name 'Raspberry_Pi_2_Driver'

after a lot of trial and error I came up with this solution to install and import the package on the device:

def install(package):
    subprocess.call([sys.executable, "-m", "pip", "install", package])

try:
    install('Adafruit-DHT==1.3.4')
    import Adafruit_DHT
except:
    raise ValueError('Failed to import and install Adafruit.')

I couldn’t find any resources online to assist me with this so I am adding it here for anyone struggling wwith this in the future.