Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

DHT22 Works as User but Not as Sudo on Pi Zero W
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

DHT22 Works as User but Not as Sudo on Pi Zero W

by brocktucker on Thu Aug 06, 2020 5:15 pm

This is the first time I've run into a problem I couldn't google, often finding answers on this lovely forum. Hopefully someone can help me before I pull the rest of my hair out.

I have a python script using the adafruit-circuitpython-dht module on a Pi Zero W. The script does exactly what is should when run as user, but when run as Sudo I get an error from libgpoid. Script and error messages below. There's a bunch of extra includes here as well as in my setup because the full script does a lot more than just read from the DHT22, I've paired it down for troubleshooting. Thank you in advance for any help anyone can provide, if I can resolve this issue I can finally put this project in an enclosure!

My Script:
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/env python3
from gpiozero import OutputDevice, Button
import time
import subprocess
from board import SCL, SDA
import busio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import board
import adafruit_dht
import neopixel
import paho.mqtt.client as mqtt

#Set DHT22 Pin
DHT_SENSOR = adafruit_dht.DHT22(board.D4)
current_temp = 0

temp_tick = 30 # how often we publish temp data in seconds
   
# DHT22 Sensor Functions
def publish_temp_humid():
   global current_temp
   temp = None
   humid = None
   try:
      humid = DHT_SENSOR.humidity
      temp = DHT_SENSOR.temperature
   except:
      pass
   if humid is not None and temp is not None:
      temp = temp * 9 / 5 + 32
      current_temp = temp
      current_temp = int(current_temp)
      current_temp = str(current_temp)
      print("Temp={}*F  Humidity={}%".format(current_temp, humid))
   else:
      print("Failed to retrieve data from DHT22 sensor")
      temp = "NR"
      humid = "NR"

while True:
   publish_temp_humid()
   time.sleep(temp_tick)


Successful Run:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~/tstat $ python3 dht.py
Temp=79*F  Humidity=55.0%


Failed Run:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~/tstat $ sudo python3 dht.py
Traceback (most recent call last):
  File "dht.py", line 15, in <module>
    DHT_SENSOR = adafruit_dht.DHT22(board.D4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 273, in __init__
    super().__init__(False, pin, 1000)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 69, in __init__
    self.pulse_in = PulseIn(self._pin, 81, True)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 71, in __init__
    message = self._wait_receive_msg()
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 89, in _wait_receive_msg
    "Timed out waiting for PulseIn message. Make sure libgpiod is installed."
RuntimeError: Timed out waiting for PulseIn message. Make sure libgpiod is installed.


The full install process of this Pi from blank SD:
Code: Select all | TOGGLE FULL SIZE
== Loading Operating System ==

* Download latest version of rasbian lite https://downloads.raspberrypi.org/raspbian_lite_latest
* Burn to SD card with Etcher https://www.balena.io/etcher/
* Unplug SD card and plug it back in to mount
* Enable SSH by putting blank file with no extension named SSH in root directory
* Setup WiFi by putting wpa_supplicant.conf in the root directory
** set ssid and psk (password) in file
* Put the SD card in the pi, boot it up, and it should be detectable on the network
* Give it a reserved IP and reboot
* SSH in through putty, username pi, default password raspberry
* Change password: $sudo passwd pi
* Update and upgrade
** $sudo apt-get update
** $sudo apt-get upgrade -y
** $sudo apt-get dist-upgrade -y
* install git
**$sudo apt-get install -y git
*Make python3 default
**$sudo update-alternatives --list python
**$sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
***use python --version to get correct 2.x
**$sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
***use $sudo python3 --version to get correct 3.x and verify it worked
*Install pip
**$sudo apt install python3-pip
* Change hostname (raspberrypi) to a unique identifier in the following files
**$sudo nano /etc/hostname
***legal characters 0-9 a-z and -
**$sudo nano /etc/hosts
** $sudo reboot

== Installing Mosquitto MQTT ==

*get repo key
**$wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
*add repo
**$sudo apt-key add mosquitto-repo.gpg.key
*download appropriate lists file
**$cd /etc/apt/sources.list.d/
**$sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list
*update caches and install
**$apt-cache search mosquitto
**$sudo apt-get update
**$sudo apt-get install -f libmosquitto-dev mosquitto mosquitto-clients libmosquitto1

=== Install Bluetooth Proximity ===
* Install bluetooth drivers
** $sudo apt-get install pi-bluetooth
** $sudo reboot
*Install dependencies
**$sudo apt-get install -y bluetooth
**$sudo apt-get install -y python3-bluez
**$sudo apt-get install -y python3-setuptools
*Install Bluetooth Proximity
**$git clone https://github.com/frederikbolding/bluetooth-proximity.git
**$cd bluetooth-proximity
**$sudo python3 setup.py install
*Install paho-mqtt
**$sudo apt-get install -y python3-paho-mqtt

=== Install Thermostat ===

*Install GPIO Zero Library
**$sudo apt install -y python3-gpiozero

*Install Blinka needed for all adafruit-circuitpython libraries
**$sudo python3 -m pip install --force-reinstall adafruit-blinka

*Install neopixel library
**$sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel

*Install DHT Library
**$sudo pip3 install adafruit-circuitpython-dht
**$sudo apt-get install -y libgpiod2

*Install I2C OLED Library, RPI.GPIO, and PIL for text display
**$sudo pip3 install adafruit-circuitpython-ssd1306
**$sudo pip3 install RPI.GPIO
**$sudo apt-get install -y python3-pil
*Enable I2C
**$sudo apt-get install -y python3-smbus
**$sudo apt-get install -y i2c-tools
**$sudo raspi-config
***5 Interfacing Options
***5 I2C
***Yes
***Ok
***Finish
**$sudo reboot

brocktucker
 
Posts: 4
Joined: Thu Jul 30, 2020 5:28 pm

Re: DHT22 Works as User but Not as Sudo on Pi Zero W

by mikeysklar on Thu Aug 06, 2020 6:02 pm

I've seen this issue pop up in our forums and on github. I suspect is just a difference between ENV variables between running sudo and running as yourself.

viewtopic.php?f=19&t=160892&start=15
https://github.com/adafruit/Adafruit_Ci ... /issues/44

I think we should take a look at the results from:

Code: Select all | TOGGLE FULL SIZE
printenv


Could you run that both as sudo / su - root / yourself. Maybe there is a library path we need to fix. PYTHONPATH might be the var to look at. Also could you run python with the '-d' debug output maybe we can see the difference in a diff there.

Thank you for providing such detailed install notes and example code. I'm sure we can figure this out.

mikeysklar
 
Posts: 6242
Joined: Mon Aug 01, 2016 8:10 pm

Re: DHT22 Works as User but Not as Sudo on Pi Zero W

by brocktucker on Thu Aug 06, 2020 6:22 pm

Edit: no joy for this problem from the linked threads. I've started documenting all my projects like this, solves a lot of frustration later on and helps with repeatability for similar projects in the future.

Thanks for the quick reply. Here's what I've got from your suggestions. I don't see any relevant differences, but hopefully something in here means something to you. I'll go read that thread you linked and see if anything there helps.

I had to delete a 3 letter word from the output below due to the spam filter. I don't think it was a relevant detail.

Running as Pi:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~ $ printenv
SHELL=/bin/bash
NO_AT_BRIDGE=1
PWD=/home/pi
LOGNAME=pi
XDG_SESSION_TYPE=tty
HOME=/home/pi
LANG=en_GB.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
SSH_CONNECTION=192.168.86.21 50936 192.168.86.200 22
XDG_SESSION_CLASS=user
TERM=xterm
USER=pi
SHLVL=1
XDG_SESSION_ID=c5
XDG_RUNTIME_DIR=/run/user/1000
SSH_CLIENT=192.168.86.21 50936 22
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
MAIL=/var/mail/pi
SSH_TTY=/dev/pts/0
TEXTDOMAIN=Linux-PAM
_=/usr/bin/printenv


And running as sudo:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~ $ sudo printenv
NO_AT_BRIDGE=1
LANG=en_GB.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
TERM=xterm
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAIL=/var/mail/root
LOGNAME=root
USER=root
HOME=/root
SHELL=/bin/bash
SUDO_COMMAND=/usr/bin/printenv
SUDO_USER=pi
SUDO_UID=1000
SUDO_GID=1000


I don't see PYTHONPATH in there but I ran this in case it helps:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~/tstat $ python --version
Python 3.7.3
pi@living-tstat:~/tstat $ sudo python --version
Python 3.7.3


And adding the -d flag to python:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~/tstat $ sudo python3 -d dht.py
Traceback (most recent call last):
  File "dht.py", line 15, in <module>
    DHT_SENSOR = adafruit_dht.DHT22(board.D4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 273, in __init__
    super().__init__(False, pin, 1000)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 69, in __init__
    self.pulse_in = PulseIn(self._pin, 81, True)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 71, in __init__
    message = self._wait_receive_msg()
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 89, in _wait_receive_msg
    "Timed out waiting for PulseIn message. Make sure libgpiod is installed."
RuntimeError: Timed out waiting for PulseIn message. Make sure libgpiod is installed.


brocktucker
 
Posts: 4
Joined: Thu Jul 30, 2020 5:28 pm

Re: DHT22 Works as User but Not as Sudo on Pi Zero W

by brocktucker on Fri Aug 07, 2020 6:33 pm

Well now I'm even more stumped. Trying to troubleshoot I wiped the pi and restarted from scratch, only doing the things I though were essential for the dht to work.

The full install process of this Pi from blank SD:

Code: Select all | TOGGLE FULL SIZE
== Loading Operating System ==

* Download latest version of rasbian lite https://downloads.raspberrypi.org/raspbian_lite_latest
* Burn to SD card with Etcher https://www.balena.io/etcher/
* Unplug SD card and plug it back in to mount
* Enable SSH by putting blank file with no extension named SSH in root directory
* Setup WiFi by putting wpa_supplicant.conf in the root directory [http://www.brocktucker.com/wiki/project_files/pi_zero_w/wpa_supplicant.conf File Download]
** set ssid and psk (password) in file
* Put the SD card in the pi, boot it up, and it should be detectable on the network
* Give it a reserved IP and reboot
* SSH in through putty, username pi, default password raspberry
* Change password: $sudo passwd pi
* Update and upgrade
** $sudo apt-get update
** $sudo apt-get upgrade -y
** $sudo apt-get dist-upgrade -y
* install git
**$sudo apt-get install -y git
*Make python3 default
**$sudo update-alternatives --list python
**$sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
***use python --version to get correct 2.x
**$sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
***use $sudo python3 --version to get correct 3.x and verify it worked
*Install pip
**$sudo apt install python3-pip
* Change hostname (raspberrypi) to a unique identifier in the following files
**$sudo nano /etc/hostname
***legal characters 0-9 a-z and -
**$sudo nano /etc/hosts
** $sudo reboot

*Install Blinka needed for all adafruit-circuitpython libraries
**$sudo python3 -m pip install --force-reinstall adafruit-blinka

*Install DHT Library
**$sudo pip3 install adafruit-circuitpython-dht
**$sudo apt-get install -y libgpiod2



And here's the result:
Code: Select all | TOGGLE FULL SIZE
pi@living-tstat:~/tstat $ sudo python3 dht.py
Traceback (most recent call last):
  File "dht.py", line 7, in <module>
    DHT_SENSOR = adafruit_dht.DHT22(board.D4)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 273, in __init__
    super().__init__(False, pin, 1000)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_dht.py", line 69, in __init__
    self.pulse_in = PulseIn(self._pin, 81, True)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 71, in __init__
    message = self._wait_receive_msg()
  File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/bcm283x/pulseio/PulseIn.py", line 89, in _wait_receive_msg
    "Timed out waiting for PulseIn message. Make sure libgpiod is installed."
RuntimeError: Timed out waiting for PulseIn message. Make sure libgpiod is installed.

pi@living-tstat:~/tstat $ python3 dht.py
Temp=77*F  Humidity=58.2%


Modified python script with no includes that aren't relevant:
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/env python3
import time
import board
import adafruit_dht

#Set DHT22 Pin
DHT_SENSOR = adafruit_dht.DHT22(board.D4)
current_temp = 0

temp_tick = 30 # how often we publish temp data in seconds
   
# DHT22 Sensor Functions
def publish_temp_humid():
   global current_temp
   temp = None
   humid = None
   try:
      humid = DHT_SENSOR.humidity
      temp = DHT_SENSOR.temperature
   except:
      pass
   if humid is not None and temp is not None:
      temp = temp * 9 / 5 + 32
      current_temp = temp
      current_temp = int(current_temp)
      current_temp = str(current_temp)
      print("Temp={}*F  Humidity={}%".format(current_temp, humid))
   else:
      print("Failed to retrieve data from DHT22 sensor")
      temp = "NR"
      humid = "NR"

while True:
   publish_temp_humid()
   time.sleep(temp_tick)

brocktucker
 
Posts: 4
Joined: Thu Jul 30, 2020 5:28 pm

Re: DHT22 Works as User but Not as Sudo on Pi Zero W

by brocktucker on Sat Aug 08, 2020 10:29 am

I've found success by switching to the depreciated Adafruit_Python_DHT library. https://github.com/adafruit/Adafruit_Python_DHT

Would love to know what was going on with libgpiod2 in the new library, but I've spent about 12 hours fighting this and I need to move on with the solution I've got.

For anyone coming behind me, things I tried that didn't work:

    Completely removing Python2 from the pi.
    Commenting out Defaults env_reset and Defaults secure_path =... from /etc/sudoers using visudo
    Editing PulseIn.py to extend the timeout window
    Setting Debug = TRUE in PulseIn.py to see if that would yield any clues (it didn't)

brocktucker
 
Posts: 4
Joined: Thu Jul 30, 2020 5:28 pm

Please be positive and constructive with your questions and comments.


cron