问题
I have a Raspberry Pi 4 connected with a DHT22 sensor, and I want to read data from my sensor.
So I installed the library Adafruit_DHT
sudo pip3 install Adafruit_DHT
then, I navigate to the directory Adafruit_Python_DHT/examples/
, and then,
since I have a DHT22 sensor connected to GPIO pi n° 4,
I run
python AdafruitDHT.py 22 4
and I get
(lab_app) root@Raspberry100:/var/www/lab_app/Adafruit_Python_DHT/examples# python AdafruitDHT.py 2302 4
Traceback (most recent call last):
File "AdafruitDHT.py", line 41, in <module>
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 80, in read
platform = get_platform()
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 63, in get_platform
raise RuntimeError('Unknown platform.')
RuntimeError: Unknown platform.
(lab_app) root@Raspberry100:/var/www/lab_app/Adafruit_Python_DHT/examples# python AdafruitDHT.py 22 4
Traceback (most recent call last):
File "AdafruitDHT.py", line 41, in <module>
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 80, in read
platform = get_platform()
File "/var/www/lab_app/lib/python3.8/site-packages/Adafruit_DHT-1.4.0-py3.8-linux-armv7l.egg/Adafruit_DHT/common.py", line 63, in get_platform
raise RuntimeError('Unknown platform.')
RuntimeError: Unknown platform.
(lab_app) root@Raspberry100:/var/www/lab_app/Adafruit_Python_DHT/examples#
Since the traceback indicates
'Unknown platform.'
I did a little research on the github repository of Adafruit_Python_DHT library, and I found the script Adafruit_Python_DHT/Adafruit_DHT/common.py.
Here I see there is an If/elif structure def get_platform()
that aims to identify the device calling the library, but there is value assignment only for RPi 1, 2 and 3, while RPi 4 is missing.
So I bet this is the reason why the error unknown platform
occurs.
I navigated the library source code and I found out the directory Adafruit_Python_DHT/Adafruit_DHT/ , in which the last commit says "included Raspberry Pi 4".
Here is a module platform_detect.py that seems to be designed to somehow "upgrade" the library in order to recognize Raspberry Pi 4.
So I tryed to "upgrade" my library by doing this:
In (lab_app) root@Raspberry100:/var/www/lab_app/Adafruit_Python_DHT/Adafruit_DHT#
,
I run
platform_detect.py
And I don't get any output from the prompt, so I guess everything has gone right.
Then I navigate to the directory Adafruit_Python_DHT/examples/
and run again
python AdafruitDHT.py 22 4
but I still get the same error.
So how can I get data from a DHT22 sensor connected to GPIO pi n° 4 by using Adafruit_Python_DHT library?
回答1:
SOLVED:
This isn't a clean method, but it solved my problem.
By using Filezilla, I connected to my Raspberry Pi 4, I navigated to Adafruit_Python_DHT/Adafruit_DHT/
and downloaded platform_detect.py
and common.py
on my local pc.
Then I edited these two files with Notepad++ as follows:
platform_detect.py
At line 112-144 I substituted
else:
# Something else, not a pi.
return None
with:
else:
# Something else, like PI 4 MODEL B
# my personal changes to the code
return 3
common.py
at lines 62-63 I substituted:
else:
raise RuntimeError('Unknown platform.')
with:
else:
#raise RuntimeError('Unknown platform.')
"""Use Pi 2 driver even though running on Pi 4"""
#my personal changes to the code, do it only if you are using a RPi 2, 3 or 4.
from . import Raspberry_Pi_2
return Raspberry_Pi_2
Then I saved the two files and uploaded them on the same directory on my RPi4, thus rewriting the older ones.
Then, with my command prompt, I run
sudo su
then I turn up the virtual environment and navigate to
/var/www/lab_app/Adafruit_Python_DHT
, and here I run:
python setup.py install
And now the library gets installed without my prompt showing any error of platform detecting.
In the end I navigate to /var/www/lab_app/Adafruit_Python_DHT/examples
and run:
python AdafruitDHT.py 22 4
And the library works, returning me the values tracked by my sensor
Temp=22.3* Humidity=54.1%
来源:https://stackoverflow.com/questions/64093805/adafruit-python-dht-raspberry-pi-in-get-platform-runtimeerror-unknown-platf