(Adafruit_Python_DHT - Raspberry Pi) in get_platform RuntimeError: Unknown platform

6.8k views Asked by At

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?

2

There are 2 answers

1
Quisp On BEST ANSWER

Thank Tms91 for posting this solution as it helped find the heart of a problem I've been running into with the DHT22 sensor.

I was having issues reading the DHT22 sensor on a Raspberry Pi 4B.

The platform_detect.py file returns a value to the Adafruit_DHT routine read_retry() call to identify the platform being used (should return 3 for RPi4) when it tries to read temp and humidity.

A modification to @tms91 solution that worked for me was to add the BCM (Broadcom chip identifier) value for RPi 4B to the platform_detect.py file, (rather than failing out and supplying a value)

For example:

{your path}/Adafruit_Python_DHT/Adafruit_DHT/

Edit platform_detect.py: - in the function pi_version() [around line 112 ] add the BCM device value for RPI-4b (BCM2711) to the if/elif list of BCM types. For Raspberry Pi 4B, BCM2711 is an updated value to check for:

# Adding the following elif to accommodate RPi4B Broadcomchip
elif match.group(1) == 'BCM2711':
    # Pi 4B
    return 3

No need to change the final else statement or edit the common.py file.

Next, return to the {your path}/Adafruit_python_DHT/folder and, as Tms91 suggested, run the setup.py again:

python3 setup.py install 

to re-install the platform_detect.py file. 

I'm guessing this might work for other similar platform errors if you're able to find the right value for your platform.

0
Tms91 On

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%