首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DHT22在Pi Zero W上以用户身份工作,而不是以Sudo身份工作

DHT22在Pi Zero W上以用户身份工作,而不是以Sudo身份工作
EN

Stack Overflow用户
提问于 2020-08-07 05:48:41
回答 1查看 357关注 0票数 2

我有一个python脚本,它使用了Pi Zero W上的adafruit-adafruit python-dht模块。该脚本在以用户身份运行时会执行正确的操作,但在以sudo身份运行时会收到来自libgpoid的错误。下面的脚本和错误消息。

这里以及我的设置中都有一堆额外的includes,因为完整的脚本不仅仅是从DHT22中读取,我已经将其删减以进行故障排除。

我的脚本:

代码语言:javascript
复制
#!/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)

成功运行:

代码语言:javascript
复制
pi@living-tstat:~/tstat $ python3 dht.py
Temp=79*F  Humidity=55.0%

运行失败:

代码语言:javascript
复制
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.

此Pi从空白SD的完整安装过程:

代码语言:javascript
复制
== 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
EN

回答 1

Stack Overflow用户

发布于 2021-09-15 18:41:00

作为参考,我为您所面临的问题创建了这个GitHub问题:https://github.com/adafruit/Adafruit_CircuitPython_DHT/issues/73让我们希望我们能从Adafruit得到一个错误修复。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63292226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档