Hi why do i have this error? It is from barometric pressure sensor hp206c.
File “xxx.py”, line 55, in
temp=h.ReadTemperature()
AttributeError: ‘str’ object has no attribute ‘ReadTemperature’
Thank you.
Hi why do i have this error? It is from barometric pressure sensor hp206c.
File “xxx.py”, line 55, in
temp=h.ReadTemperature()
AttributeError: ‘str’ object has no attribute ‘ReadTemperature’
Thank you.
Please post your code.
Best to put three back quotes
```
around your code
```
(The error message is telling that “h” was not properly initialized to be a sensor class object.)
Perhaps include all the output before the error also - and what is output when you try running the high_accuracy_barometer_example.py program
Hi
The code you gave was working fine till i add dht11 code and error came out.
Can you help to show how to combine dht11 and barometric pressure sensor codes?
thank yo
Please post your code and the console output.
from __future__ import print_function
import paho.mqtt.publish as publish
import time
import hp206c
import psutil
import string
import random
import grovepi
import math
from time import sleep
h= hp206c.hp206c()
ret=h.isAvailable()
sensor = 7
blue = 0
blue = 1
string.alphanum='1234567890avcdefghijklmnopqrstuvwxyzxABCDEFGHIJKLMNOPQRSTUVWXYZ'
channelID = "xxx"
writeAPIKey = "xx"
mqttHost = "mqtt.thingspeak.com"
mqttUsername = "xx
mqttAPIKey ="xx"
tTransport = "websockets"
tPort = 80
topic = "channels/" + channelID + "/publish/" + writeAPIKey
if h.OK_HP20X_DEV == ret:
print("HP20x_dev is available.")
else:
print("HP20x_dev isn't available.")
while True:
try:
[temp,humidity] = grovepi.dht(sensor,blue)
if math.isnan(temp) == False and math.isnan(humidity) == False:
print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
sleep(1)
temp=h.ReadTemperature()
pressure=h.ReadPressure()
altitude=h.ReadAltitude()
print("Temperature\t: %.2f C\nPressure\t: %.2f hPa\nAltitude\t: %.2f m" %(temp,pressure,altitude))
time.sleep(5)
#Prep connection to Thingspeak
clientID=''
# Create a random clientID.
for x in range(1,16):
clientID+=random.choice(string.alphanum)
t = str(temp)
p = str(pressure)
a = str(altitude)
# t1 = str(temp)
h = str(humidity)
# build the payload string for Thingspeak
payload = "field1=" + t + "&field=2" + h + "&field3=" + p + "&field4=" + a
# attempt to publish data to the topic on Teamspeak.
try:
publish.single(topic, payload, hostname=mqttHost, transport=tTransport, port=tPort,auth={'username':mqttUsername,'password':mqttAPIKey})
print (" Published to host: " , mqttHost , " clientID= " , clientID)
except (KeyboardInterrupt):
break
except:
print ("There was an error while publishing the data.")
time.sleep(5)
except (IOError,TypeError) as e:
print("Error")
pi@raspberrypi:~ $ python 1.py
HP20x_dev is available.
Temperature : 26.68 C
Pressure : 999.26 hPa
Altitude : 117.11 m
Published to host: mqtt.thingspeak.com clientID= k4ufsEyLe4O0Bs5
Traceback (most recent call last):
File “1.py”, line 44, in
temp=h.ReadTemperature()
AttributeError: ‘str’ object has no attribute ‘ReadTemperature’
You already have used h for the sensor, make this “hum” or “strHumidity”
Hi
It doesn’t work even with pressure or altitude
Traceback (most recent call last):
File “1.py”, line 45, in
Pressure=h.ReadPressure()
AttributeError: ‘str’ object has no attribute ‘ReadPressure’
Did you change the line: h = str(humidity) ???
strHumidity = str(humidity)
# build the payload string for Thingspeak
payload = "field1=" + t + "&field2=" + strHumidity + "&field3=" + p + "&field4=" + a
(Note the error you had field=2 should be field2= No?)
h starts out as your hp206c sensor:
h= hp206c.hp206c()
then later you are assigning a string to h:
h = str(humidity)
then the loop repeats and you try to read the hp206c sensor using “h” but it no longer contains the sensor, you overwrote a string into “h”, so when you try to get temp from the sensor, it tells you that you have confused it.
Hi thanks , no error now but dht11 not updating in thingspeak. hp206c did update
‘’’
while True:
try:
air_quality = grovepi.analogRead(Airquality)
light_intensity = grovepi.analogRead(LightSensor)
sound_level = grovepi.analogRead(SoundSensor)
if sound_level > 0:
last_sound=sound_level
[temp,humidity] = grovepi.dht(sensor,blue)
if math.isnan(temp) == False and math.isnan(humidity) == False:
print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))
sleep(1)
temphp206c=h.ReadTemperature()
pressure=h.ReadPressure()
altitude=h.ReadAltitude()
thp = str(temphp206c)
p = str(pressure)
a = str(altitude)
t = str(temp)
hum = str(humidity)
aq = str(air_quality)
l = str(light_intensity)
s = str(last_sound)
# build the payload string for Thingspeak
payload = "field1=" + t + "&field=2" + hum + "&field3=" + p + "&field4=" + a + "&field5=" + aq + "&field6=" + s + "&field7=" +l + "&field8=" + thp
‘’’
@RobertLucian please mark this one solved.
@azizahsham I think you can and should figure out Thingspeak yourself. We got your GrovePi reading the sensors; up to you to decide what to do with the data.