Hello guys,
One small help
I have written a code
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
and it is giving me an error
Unknown format code 'f' for object of type 'str'
I tried float (temperature), float (temperature) even but it doesn't seems to work.
Kindly help.
(Aug-12-2018, 03:49 AM)Prashant93 Wrote: [ -> ]I tried float (temperature), float (temperature) even but it doesn't seems to work.
the error is clear. you need numeric value when using format code f in formating string.
show us minimal, runnable snippet that reproduce the error.
How to create a Minimal, Complete, and Verifiable example
#!/usr/bin/python
import sys
import Adafruit_DHT
while True:
humidity, temperature = Adafruit_DHT.read_retry(11, 4)
print 'Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(temperature, humidity)
---Here I am facing problem
Adafruit_DHT.read_retry()
will return either tuple of floats or tuple of None's, i.e.
(None, None)
if it is not able to get correct reading from the sensor. In your case it returns
(None, None)
for some reason it's not able to get good reading from your sensor.
print 'Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(None, None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'f' for object of type 'str'
the docstrin for read_retry:
Quote:"""Read DHT sensor of specified sensor type (DHT11, DHT22, or AM2302) on
specified pin and return a tuple of humidity (as a floating point value
in percent) and temperature (as a floating point value in Celsius).
Unlike the read function, this read_retry function will attempt to read
multiple times (up to the specified max retries) until a good reading can be
found. If a good reading cannot be found after the amount of retries, a tuple
of (None, None) is returned. The delay between retries is by default 2
seconds, but can be overridden.
"""
By the way, why do you use python2? In python3 you will get
>>> print('Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(None, None))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
Actually, I am using Grove Pi and my sensor is on port D4, Does that matter?
(Aug-12-2018, 05:37 PM)Prashant93 Wrote: [ -> ]Actually, I am using Grove Pi and my sensor is on port D4, Does that matter?
I have no idea. But it looks like you don't get good readings from it
Hi, I tried using python 3. I am getting the same error, How can I fix this??
TypeError: non-empty format string passed to object.__format__
it looks they have their own code base
https://github.com/DexterInd/GrovePi
there are installation instruction, example code and project.
Note that they import gruvipi (I expect it is installed when you install from their repository)
I don't know if Adafruit_DHT can handle their sensors correctly.
(Aug-12-2018, 06:12 PM)Prashant93 Wrote: [ -> ]Hi, I tried using python 3. I am getting the same error, How can I fix this??
TypeError: non-empty format string passed to object.__format__
The problem is not in the code, but because you don't get good readings, thus it returns None, None
Hi I am using grovepi code for sensors and want to store in sqlite3 and so I am using this code below:
import grovepi
import time
import math
dbname='sensorsData.db'
import sqlite3
sampleFreq=1*60
def getDHTdata():
blue=0
sensor = 4 # The Sensor goes on digital port 4.
while True:
try:
# This example uses the blue colored sensor.
# The first parameter is the port, the second parameter is the type of sensor.
[temp,humidity] = grovepi.dht(sensor,blue)
if math.isnan(temp) == False and math.isnan(humidity) == False:
print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
except IOError:
print ("Error")
I am getting error name sensor is not defined. Please help.