Issue
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!
Solution
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.
Answered By - Tin Nguyen