简体   繁体   中英

Gathering temperature from raspberry pi (DHT22) through api failes on second attempt

I have a DHT22 connected to my raspberry pi 4 model B, connected like this: 在此处输入图像描述

And I wrote a small script for the raspberry pi that is supposed to get the temperature and the humidity. I made this script available through the ip address of my local network. To do so, I've used two libraries in an attempt to figure out the error: Flask and Jaymon/Endpoints.

When running the script locally, everything works great. When I call the endpoint using Postman, I get the temperature and humidity, just as expected. However, when I call the same endpoint a second time I receive the following error:

RuntimeError: Timed out waiting for PulseIn message. Make sure libgpiod is installed.

When this happens, I need to reboot the webserver and it starts all over again: I can call the api once with correct results, and it fails on the second attempt.

Here's one of the scripts I've tried:

from endpoints import Controller

import adafruit_dht import board

class Default(Controller):   def GET(self):
    dht_device = adafruit_dht.DHT22(board.D4)

    data = {
        "temperature": dht_device.temperature,
        "humidity": dht_device.humidity
    }

    return data

The problem occurs specifically when calling the dht_device.temperature/dht_device.humidity. When I leave this out, I can call the endpoint without issues.

To be clear: I did install libgpiod on the Raspberry Pi. EDIT : I installed libgpiod2 , as one of the answers suggested.

Does anybody have an idea on what the issue might be here? Thanks in advance!

This was solved elsewhere for the Pi3 by installing libgpiod2:

https://github.com/adafruit/Adafruit_Blinka/issues/259

You wrote:

class Default(Controller):
  def GET(self):
    dht_device = adafruit_dht.DHT22(board.D4)

    data = {
        "temperature": dht_device.temperature,
        "humidity": dht_device.humidity
    }

    return data

You need something like:

class Default(Controller):
  def __init__(self):
    super().__init__(self)
    self.dht_device = adafruit_dht.DHT22(board.D4)

  def GET(self):
    data = {
        "temperature": self.dht_device.temperature,
        "humidity": self.dht_device.humidity
    }
    return data

The problem you have is that you are creating each time a new instance when calling the endpoint. The first instance is created properly and has access to the information but the second and consecutive instances are blocked by the first instance.

I encountered this error in Raspbian stretch OS of RaspberryPi. Solved using below steps

You can try installing libgpiod2 package using below command

sudo apt-get install libgpiod2

If you get error as "E: Unable to locate package libgpiod2"

Then, Install the package using the deb package

Package url: https://packages.debian.org/buster/libgpiod2

wget http://ftp.cn.debian.org/debian/pool/main/libg/libgpiod/libgpiod2_1.2-3_armhf.deb

sudo dpkg -i libgpiod2_1.2-3_armhf.deb

If you get error as conflicting packages with libgpiod1, then uninstall the libgpiod1 package

sudo apt-get remove libgpiod1

and then try to install the package

sudo dpkg -i libgpiod2_1.2-3_armhf.deb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM