1
0
Fork 0

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
Dustin 2021-06-18 18:31:34 -05:00
parent 2afae103d9
commit 990de7fbb9
1 changed files with 7 additions and 3 deletions

View File

@ -96,9 +96,9 @@ class Daemon:
while 1:
data = bme280.sample(bus, SENSOR_ADDR, params)
values = {
"temperature": int(data.temperature),
"pressure": int(data.pressure),
"humidity": int(data.humidity),
"temperature": adj(data.temperature),
"pressure": adj(data.pressure),
"humidity": adj(data.humidity),
}
client.publish(TOPIC, json.dumps(values))
ready = select.select((self.quitpipe[0],), (), (), 10)[0]
@ -142,6 +142,10 @@ class Daemon:
print("Message", client, userdata, msg)
def adj(value, p=10):
return int(value * p) / p
def main():
logging.basicConfig(level=logging.DEBUG)
daemon = Daemon()