0

I'm using a Pi 3 running Raspbian with a DHT22 sensor. When I run the DHT22 code:

import Adafruit_DHT
from time import sleep

DELAY_INTERVAL = 0.3
dht_sensor = Adafruit_DHT.DHT22
DHT_PIN = 4


while True:
  humidity, temperature = Adafruit_DHT.read_retry(dht_sensor, DHT_PIN)

  print "Humidity: {:0.1f}%, Temperature: {:0.2f}*C".format(humidity, temperature)

  sleep(DELAY_INTERVAL) 

I get this error:

print "Humidity: {:0.1f}%, Temperature: {:0.2f}*C".format(humidity,
temperature) ValueError: Unknown format code 'f' for object of type
'str'

When I tried the same code on a NOOBS installation I got no error.

  • 1
    I think you should test values before printing if humidity is not None and temperature is not None: And not sure but do you have to specify the parameter order # as in Humidity: {0:0.1f}%, Temperature: {1:0.2f} – dbmitch Jun 14 '18 at 4:50
  • # Attempraturet to get sensor reading. humidity, temprature = Adafruit_DHT.read(DHT_TYPE, DHT_PIN) # can't be reliably read (timing is critical to read the sensor). if humidity is None or temprature is None: time.sleep(1) continue print('temprature: {0:0.1f} C'.format(temprature)) print('humidity: {0:0.1f} %'.format(humidity)) yeah i am change the code its working well but slow log's worken slow. – noorkhokhar noorkhokhar Jun 15 '18 at 2:27
  • 2
    Don't do what you just did - Instead paste any code into your question with a note that you're editing or Updating your question. So the code's working? read the docs for speed - you have to wait for it to stabilize and ignore bad readings. – dbmitch Jun 15 '18 at 3:43
  • If you go back to the NOOBs installation, does the sensor work? If so, then please rename your question because it sounds like the sensor works, just that something isn't connecting right. – NomadMaker Jun 15 '18 at 17:01
  • Could you please let us know what the string(s) are that it is outputing? This could be useful for debugging. – NomadMaker Jun 15 '18 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.