garyns / pigpio-dht

DHT11 Temperature and Humidity Sensor using pigpio
GNU General Public License v3.0
14 stars 6 forks source link

Constant timeout when accessing remote pigpio #5

Open davemacrae opened 3 years ago

davemacrae commented 3 years ago

I have two DHT22 sensors attached to a Pi Zero.

Sample Code:

from pigpio_dht import DHT11, DHT22
import pigpio
import time

gpio_1 = 4 # BCM Numbering
gpio_2 = 17 # BCM Numbering

pi = pigpio.pi('pitemp')

sensor_1 = DHT22(gpio_1, timeout_secs=5, pi=pi)
sensor_2 = DHT22(gpio_2, timeout_secs=5, pi=pi)

result = sensor_1.read()
print(result)
time.sleep(5)
result = sensor_2.read()
print(result)

Running the above on the Pi Zero W pretty much always results in two successful readings. However, if I attempt to run the code from another device on my network (in this case a Pi 4), then the attempt to read the sensors will often (pretty much always) results in a Timeout.

The Pi Zero is connected to my network via inbuilt WiFi.

Traceback (most recent call last):
  File "./fred.py", line 13, in <module>
    result = sensor_1.read()
  File "/home/pi/.local/lib/python3.7/site-packages/pigpio_dht/dhtxx.py", line 74, in read
    result = self._read()
  File "/home/pi/.local/lib/python3.7/site-packages/pigpio_dht/dhtxx.py", line 256, in _read
    raise TimeoutError("{} sensor on GPIO {} has not responded in {} seconds. Check sensor connection.".format(self.__class__.__name__, self.gpio, self.timeout_secs))
TimeoutError: DHT22 sensor on GPIO 4 has not responded in 5 seconds. Check sensor connection.

or

{'temp_c': 21.3, 'temp_f': 70.3, 'humidity': 37.7, 'valid': True}
Traceback (most recent call last):
  File "./fred.py", line 17, in <module>
    result = sensor_2.read()
  File "/home/pi/.local/lib/python3.7/site-packages/pigpio_dht/dhtxx.py", line 74, in read
    result = self._read()
  File "/home/pi/.local/lib/python3.7/site-packages/pigpio_dht/dhtxx.py", line 256, in _read
    raise TimeoutError("{} sensor on GPIO {} has not responded in {} seconds. Check sensor connection.".format(self.__class__.__name__, self.gpio, self.timeout_secs))
TimeoutError: DHT22 sensor on GPIO 17 has not responded in 5 seconds. Check sensor connection.

Any ideas how I can resolve this?

Thanks

Dave

garyns commented 3 years ago

Hi,

On investigation and reflection, I don't think Remote GPIO will work with the DHT sensors. If it does work, it would be erratic and unreliable and in all probability return incorrect readings at best.

DHT sensors communicate data using a time-based protocol that requires microsecond accuracy. I'd expect latency added by the network will interfere with the timing causing the library to time out.

Further to this, part of the protocol is a timing-based "handshake". Latency that interferes with this means the library never receives the expected handshake and thus times out. Increasing the timeout will not help here since the library is not waiting for communication to finish, it's waiting for (and not receiving) the handshake timing signal.

Given this situation, the pi parameter should be removed from the constructor.

Use an API based approach or something similar to remotely access DHT data on a remote Pi. Network Zero (https://networkzero.readthedocs.io) is handy for quick PoC and small projects.

davemacrae commented 3 years ago

Thanks for the feedback Gary.

I resolved my problem by just reading the devices locally and using Prometheus to gather the data from the devices to a central point.

Dave