pythonraspberry-piraspberry-pi4adafruitadafruit-circuitpython

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


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?


Solution

  • 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.