0

This is what i did:

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

cd Adafruit_Python_DHT

sudo apt-get install build-essential python-dev

sudo python setup.py install

This^ was given in the github link itself. I did this and my code worked perfectly with DHT11 sensor in python 2.x but its failing with python 3. The error that I'm getting is:

RuntimeError: Error accessing GPTO. Make sure program is run as root with sudo!

My code is:

import Adafruit_DHT
import time

while True:
    time.sleep(1)
    humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,4)
    print(temperature)
    print(humidity)

Works perfectly well with python 2, the issue is with python 3. I'm using a Raspberry Pi3 B for the GPIO interface.

Edit: I tried sudo python temper.py and it works again, but sudo python3 temper.py still doesn't work, with one small change, it doesn't give any errors but now the output is

None
None
None
None

basically, 'None' appears in place of the sensor value for Temperature and Humidity.

Krishnakumar M
  • 109
  • 1
  • 5
  • 12
  • Well, did you do what the error message tells you to do? If you did and it doesn't help, probably you can get some help on the project's [issues page](https://github.com/adafruit/Adafruit_Python_DHT/issues). – spectras May 22 '18 at 09:49
  • Nope, i didn't. How do i do this - "RuntimeError: Error accessing GPTO. Make sure program is run as root with sudo!" ? – Krishnakumar M May 22 '18 at 09:51
  • My obersvation is quite obvious but... Did you try using sudo? – Pitto May 22 '18 at 09:53
  • @Pitto Should i try 'sudo python3 ' ? – Krishnakumar M May 22 '18 at 09:54
  • Check 'Edit:' in my description. – Krishnakumar M May 22 '18 at 10:01
  • Ok 1st issue seems to be permission related and now solved. Now if I was you I'd go and recheck very carefully my wiring and my resistance value. – Pitto May 22 '18 at 10:13
  • I'd do that too, but if the same code works with python 2 and if it doesn't with python 3, don't you think it is something to do with the library/installation part? Because wiring must be right, if not how did it work with python 2? – Krishnakumar M May 22 '18 at 10:17
  • Did you try: sudo python3 setup.py install? – Pitto May 22 '18 at 21:24
  • Just did that and it works! Thanks :) – Krishnakumar M May 23 '18 at 07:04
  • You did the install of library for python 2, the same package is not available for python3, you can check the available library installed using `pip list`, and compare it with `pip3 list` (which are the list of library under python3 installation). – hcheung May 25 '18 at 08:25
  • I am happy that this solved your problem, @KrishnakumarM! If this is the case please select and/or upvote my answer. Have a nice day. – Pitto May 28 '18 at 12:22
  • The Adafruit_DHT library is deprecated, therefore, use the CircuitPython library. https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup – Fakruddin Mohammed Aug 24 '20 at 20:25

2 Answers2

2

Since you are using Python 3 please install the library using the right command:

sudo python3 setup.py install
Pitto
  • 8,229
  • 3
  • 42
  • 51
1

For Python3 you would need to use brackets after the print word.

print('Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity))
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Tina
  • 11
  • 1