von-b
December 12, 2015, 6:22am
1
Hi,
When reading out the temperature and humidity, the temperature is always rounded to whole degrees Celcius. I saw the sensor has a resolution of 0.1 degree, so how can I get a more detailed readout or am I overlooking something.
[temp, hum] = grovepi.dht(sensor, 0)
Regards,
Von.B
karan
December 14, 2015, 2:22am
2
The DHT senor does return a value in float and should have a decimal value. Can you check if you are printing it as a float and not an integer. Also, can you post the output that you are getting along with the code.
-karan
von-b
December 19, 2015, 5:31am
3
Hi, apologies for the delay. Below the code. Running on Python 2.7.10. Can’t get it to work on Python 3.4.3.
import datetime
import json
import logging
import paho.mqtt.client as mqtt
import ssl
import time
import grovepi
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
def on_connect(client, userdata, flags, rc):
logging.info("Connected with result code " + str(rc))
def create_payload(temp, humidity):
data = {}
key = {}
attr = {}
key['location'] = 'KW-Attic-01'
key['timestamp'] = datetime.datetime.now().isoformat()
attr['temperature'] = "%.2f" % temp
attr['humidity'] = "%.1f" % humidity
data['key'] = key
data['attr'] = attr
json_str = json.dumps(data)
logging.info(json_str)
return json_str
client = mqtt.Client("KW-Attic-01")
client.tls_set(ca_certs="rootCA.pem", certfile="cert1.pem", keyfile="privkey1.pem", tls_version=ssl.CERT_REQUIRED)
client.on_connect = on_connect
client.loop_start()
client.connect(host="XXX.amazonaws.com", port=8883, keepalive=60, bind_address='')
topic = "kw/house/measurement"
sensor = 2
time.sleep(1)
while True:
try:
[temp, hum] = grovepi.dht(sensor, 0)
print("Temp: %.2f" % temp)
print("Humi: %.1f" % hum)
pl = create_payload(temp, hum)
r = client.publish(topic=topic, payload=pl, qos=0)
if r[0] == mqtt.MQTT_ERR_SUCCESS:
logging.info("Success [%s, %s]" % r)
else:
logging.info("Failure!!! [%s, %s]" % r)
time.sleep(60)
except TypeError:
pass
except KeyboardInterrupt:
break
except IOError:
print("IOError")
karan
December 21, 2015, 12:13am
4
Hi Von,
The code looks good to me. What output are getting with the sensor. Can you post a screenshot of it.
-Karan
von-b
December 21, 2015, 7:23am
5
Him below the logging output. Is running on Ubuntu Mate 15.10.
‘Temp: 17.00
Humi: 46.0
12/21/2015 01:43:25 PM {“attr”: {“temperature”: “17.00”, “humidity”: “46.0”}, “key”: {“timestamp”: “2015-12-21T13:43:25.982512”, “location”: “KW-Attic-01”}}
12/21/2015 01:43:25 PM Success [0, 2944]
Temp: 18.00
Humi: 46.0
12/21/2015 01:44:26 PM {“attr”: {“temperature”: “18.00”, “humidity”: “46.0”}, “key”: {“timestamp”: “2015-12-21T13:44:26.692866”, “location”: “KW-Attic-01”}}
12/21/2015 01:44:26 PM Success [0, 2945]
Temp: 17.00
Humi: 46.0
12/21/2015 01:45:27 PM {“attr”: {“temperature”: “17.00”, “humidity”: “46.0”}, “key”: {“timestamp”: “2015-12-21T13:45:27.422807”, “location”: “KW-Attic-01”}}
12/21/2015 01:45:27 PM Success [0, 2946]
’
karan
December 23, 2015, 3:17am
6
Hi,
Can you try running this example and check what if it gives any better data: https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_dht_pro.py .
-karan