Due to high demand expect some shipping delays at this time, orders may not ship for 1-2 business days.
0

DHT22 sensor Unable to set line XX to input
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

DHT22 sensor Unable to set line XX to input

by Trollpatrol on Mon Feb 08, 2021 8:11 pm

Hello everyone,

i am having the same issue as this post:
viewtopic.php?f=19&t=169472

the first time that i run the code on a new pin it is working. although, when changing the pin wiring i get the message "Unable to set line XX to input".

Has this problem been solved by Adafruit?
Python Code:
Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
 
import time
import board
import adafruit_dht
 
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D10)
 
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)
 
while True:
    try:
        # Print the values to the serial port
        temperature_c = dhtDevice.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = dhtDevice.humidity
        print(
            "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                temperature_f, temperature_c, humidity
            )
        )
 
    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue
    except Exception as error:
        dhtDevice.exit()
        raise error
 
    time.sleep(2.0)

Thanks for your help.
Jean

Trollpatrol
 
Posts: 2
Joined: Sat Feb 06, 2021 9:32 pm

Re: DHT22 sensor Unable to set line XX to input

by FrankBS on Wed Mar 03, 2021 5:06 pm

Same problem. Using Pie v4. Works first time then error when re-running. Switched pin and it worked first time, then error again.

Python 3.7.3 (/usr/bin/python3)
>>> %Run working_dht_simpletest.py
Temp: 73.9 F / 23.3 C Humidity: 24.2%
Temp: 73.6 F / 23.1 C Humidity: 24.9%
Temp: 73.6 F / 23.1 C Humidity: 24.9%
Temp: 73.6 F / 23.1 C Humidity: 24.9%
Temp: 73.4 F / 23.0 C Humidity: 24.8%
Temp: 73.4 F / 23.0 C Humidity: 24.8%

────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Python 3.7.3 (/usr/bin/python3)
>>> %Run working_dht_simpletest.py
Unable to set line 4 to input

FrankBS
 
Posts: 4
Joined: Sat Feb 20, 2021 12:27 pm

Re: DHT22 sensor Unable to set line XX to input

by adafruit_support_carter on Fri Mar 05, 2021 4:44 pm

There's an open issue tracking this here:
https://github.com/adafruit/Adafruit_Ci ... /issues/63

adafruit_support_carter
 
Posts: 20103
Joined: Tue Nov 29, 2016 2:45 pm

Re: DHT22 sensor Unable to set line XX to input

by adafruit_support_carter on Fri Mar 05, 2021 5:37 pm

I think the general issue here is that these codes are all infinite loops that are being aborted using <CTRL><C>. As a result, the DHT exit() never gets called:
https://github.com/adafruit/Adafruit_Ci ... dht.py#L59
and so the pulseio deinit() does not get called:
https://github.com/adafruit/Adafruit_Bl ... eIn.py#L99
and so the background process handling the pulsein does not get terminated. So when the code is run a second time, that still running process still has the pin taken.

Further, the current example has some try/except logic:
https://github.com/adafruit/Adafruit_Ci ... pletest.py
but this will not catch a <CTRL><C> event since KeyboardInterrupt inherits from BaseException, not Exception:
https://docs.python.org/3/library/excep ... dInterrupt

So....just need to somehow make sure the deinit stuff gets called. Try the code below. This is not tested, but attempts to restructure things to try and catch the <CTRL><C> and call the deinit stuff.

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
   
import time
import board
import adafruit_dht
   
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D10)
   
# you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio.
# This may be necessary on a Linux single board computer like the Raspberry Pi,
# but it will not work in CircuitPython.
# dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False)

try:
    while True:
        try:
            # Print the values to the serial port
            temperature_c = dhtDevice.temperature
            temperature_f = temperature_c * (9 / 5) + 32
            humidity = dhtDevice.humidity
            print(
                "Temp: {:.1f} F / {:.1f} C    Humidity: {}% ".format(
                    temperature_f, temperature_c, humidity
                )
            )
            time.sleep(2.0)
        except RuntimeError as error:
            # Errors happen fairly often, DHT's are hard to read, just keep going
            print(error.args[0])
            time.sleep(2.0)
            continue
except KeyboardInterrupt as error:
    print("CTRL-C pressed. Deiniting everything...")
    dhtDevice.exit()
    raise error

adafruit_support_carter
 
Posts: 20103
Joined: Tue Nov 29, 2016 2:45 pm

Re: DHT22 sensor Unable to set line XX to input

by FrankBS on Fri Mar 05, 2021 10:58 pm

Is there no way to initialize the board to break the sequence when the program is re-run after interrupt short-circuits the code?

When my error occurs, I am stopping the code in Thonny and then re-running the code.

FrankBS
 
Posts: 4
Joined: Sat Feb 20, 2021 12:27 pm

Re: DHT22 sensor Unable to set line XX to input

by adafruit_support_carter on Sat Mar 06, 2021 11:19 am

When my error occurs, I am stopping the code in Thonny and then re-running the code.

There are ways of registering callback functions for various signals, like SIGINT, which is what <CTRL><C> generates. How do you "stop code" in Thonny?

adafruit_support_carter
 
Posts: 20103
Joined: Tue Nov 29, 2016 2:45 pm

Re: DHT22 sensor Unable to set line XX to input

by FrankBS on Sat Mar 06, 2021 3:16 pm

I click the red stop button (Red circle with white square) in the Thonny menu while running the referenced code.

FrankBS
 
Posts: 4
Joined: Sat Feb 20, 2021 12:27 pm

Re: DHT22 sensor Unable to set line XX to input

by adafruit_support_carter on Thu Mar 18, 2021 7:38 pm

I can't recreate this. I'm running the example from the first post above, which is the simpletest example from the library:
https://github.com/adafruit/Adafruit_Ci ... pletest.py

I can run it, abort with <CTRL><C>, and run again.
Code: Select all | TOGGLE FULL SIZE
(blinka) pi@raspberrypi:~ $ python3 dht_simpletest.py
Temp: 77.7 F / 25.4 C    Humidity: 30.0%
Temp: 77.7 F / 25.4 C    Humidity: 30.0%
Temp: 77.7 F / 25.4 C    Humidity: 30.0%
^CTraceback (most recent call last):
  File "dht_simpletest.py", line 37, in <module>
received SIGINT

    time.sleep(2.0)
KeyboardInterrupt
(blinka) pi@raspberrypi:~ $ python3 dht_simpletest.py
Temp: 77.7 F / 25.4 C    Humidity: 29.9%
Temp: 77.7 F / 25.4 C    Humidity: 29.8%
Temp: 77.7 F / 25.4 C    Humidity: 29.7%
^Creceived SIGINT
Traceback (most recent call last):
  File "dht_simpletest.py", line 37, in <module>

    time.sleep(2.0)
KeyboardInterrupt
(blinka) pi@raspberrypi:~ $


Is this only happening with people using Thonny?

adafruit_support_carter
 
Posts: 20103
Joined: Tue Nov 29, 2016 2:45 pm

Re: DHT22 sensor Unable to set line XX to input

by FrankBS on Fri Mar 19, 2021 9:54 am

I tried your new code in Thonny. Same issue. It runs once and then no matter how I stop it, STOP or CTRL-C, it will not run again unless I reboot the Pi.
Also tried in MU, same results.

FrankBS
 
Posts: 4
Joined: Sat Feb 20, 2021 12:27 pm

Please be positive and constructive with your questions and comments.