sensor: Improve reported value precision
Casting the measured values to `int` loses a lot of precision. Sending the raw values, however, results in uselessly over-precise values. Thus, we need to compromise by truncating the values at the tenths place.master
parent
2afae103d9
commit
990de7fbb9
|
@ -96,9 +96,9 @@ class Daemon:
|
||||||
while 1:
|
while 1:
|
||||||
data = bme280.sample(bus, SENSOR_ADDR, params)
|
data = bme280.sample(bus, SENSOR_ADDR, params)
|
||||||
values = {
|
values = {
|
||||||
"temperature": int(data.temperature),
|
"temperature": adj(data.temperature),
|
||||||
"pressure": int(data.pressure),
|
"pressure": adj(data.pressure),
|
||||||
"humidity": int(data.humidity),
|
"humidity": adj(data.humidity),
|
||||||
}
|
}
|
||||||
client.publish(TOPIC, json.dumps(values))
|
client.publish(TOPIC, json.dumps(values))
|
||||||
ready = select.select((self.quitpipe[0],), (), (), 10)[0]
|
ready = select.select((self.quitpipe[0],), (), (), 10)[0]
|
||||||
|
@ -142,6 +142,10 @@ class Daemon:
|
||||||
print("Message", client, userdata, msg)
|
print("Message", client, userdata, msg)
|
||||||
|
|
||||||
|
|
||||||
|
def adj(value, p=10):
|
||||||
|
return int(value * p) / p
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
daemon = Daemon()
|
daemon = Daemon()
|
||||||
|
|
Loading…
Reference in New Issue