Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

AM2302 temp/humidty python error
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

AM2302 temp/humidty python error

by dougskis on Mon Jul 27, 2020 12:24 pm

I am having a problem getting errors when I try to run python script I copied from Adafruit website. Errors I receive are;
sudo ./dht_simpletest.py
./dht_simpletest.py: 1: ./dht_simpletest.py: import: not found
./dht_simpletest.py: 2: ./dht_simpletest.py: import: not found
./dht_simpletest.py: 3: ./dht_simpletest.py: import: not found
./dht_simpletest.py: 6: ./dht_simpletest.py: Syntax error: "(" unexpected

I do not know what "import" it cannot find. Also why I am getting "(" unexpected? Has an equal amount of ( )

I am running this on a Pi Zero

I copied this code from your website

import time
import board
import adafruit_dht

# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D4) <<< I tried changing DHT22 to DHT2302 since that's what I have

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)

Any help will be greatly appreciated, Doug

dougskis
 
Posts: 17
Joined: Tue Nov 21, 2017 9:39 pm

Re: AM2302 temp/humidty python error

by adafruit_support_carter on Mon Jul 27, 2020 1:05 pm

You are trying to execute the script directly, which would required adding a shebang as the first line.

Try just running the script with Python:
Code: Select all | TOGGLE FULL SIZE
python3 dht_simpletest.py

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

Re: AM2302 temp/humidty python error

by dougskis on Mon Jul 27, 2020 5:31 pm

That did the trick!!

python3 dht_simpletest.py
Temp: 77.9 F / 25.5 C Humidity: 60.9%

Thank you very much, Doug

dougskis
 
Posts: 17
Joined: Tue Nov 21, 2017 9:39 pm

Please be positive and constructive with your questions and comments.