homekit-enviroplus icon indicating copy to clipboard operation
homekit-enviroplus copied to clipboard

[WiP] Read values from local sensors

Open rubiojr opened this issue 4 years ago • 13 comments

Use bme280 and ltr559 Go modules to read sensors locally, instead of pulling values from the Prometheus exporter.

The sensors are sampled every second instead of every 5 seconds by default -at the expense of some extra power consumption-, leading to more accurate motion detection.

Reduces power consumption a bit, even with the faster sampling:

~0.93W homekit-enviroplus reading from local sensors ~1.1W homekit-enviroplus + Prometheus exporter

Note: probably not very accurate readings, using a cheap USB digital multimeter.

Saves ~30MB of memory, removing the need for the Prometheus exporter and reducing the daemon memory footprint a bit.

:warning: Notes

  • PMS5003 readings still not available. I'm waiting for my PMS5003 sensor to arrive, so I can write and test the Go driver that will read the air quality values required here.
  • I haven't found a MICS6814 (gas) sensor Go driver, so I've started writing one.

rubiojr avatar Jan 24 '21 21:01 rubiojr

Part of https://github.com/sighmon/homekit-enviroplus/issues/5

rubiojr avatar Jan 24 '21 21:01 rubiojr

@sighmon my plan is to remove the code to read values from the Prometheus exporter to simplify it, but if you want to keep it around (say to run the daemon and the exporter in different machines) and use a flag to switch to local readings instead, I can do that too.

rubiojr avatar Jan 24 '21 21:01 rubiojr

@rubiojr It'd be great if you could leave the option to read values from a Prometheus exporter, or else include a Prometheus exporter here... whichever is easier.

I haven't tested the code on my Pi yet, but I'll endeavour to do that tomorrow. 💅

sighmon avatar Jan 26 '21 10:01 sighmon

@rubiojr It'd be great if you could leave the option to read values from a Prometheus exporter, or else include a Prometheus exporter here... whichever is easier.

:+1:, I'll add flag to optionally run a exporter also, I found some code from Mark Hansen that I can easily reuse.

I've been running this branch over the the last 24 hours and I haven't found any memory leaks, process RSS values (go 1.16beta1) look reasonable:

Screenshot from 2021-01-26 12-27-00

RSS measured in MB

Haven't measured what happens with master though, to confirm there's indeed some kind of leak.

Got my PMS5003 sensor yesterday and quickly added basic PMS5003 support in 79614a5 . The plan is to work on the code to read the MICS6814 today, I think that's that's the only sensor we're missing in this branch.

rubiojr avatar Jan 26 '21 11:01 rubiojr

741b276 added gas readings.

af814c3 added a basic Prometheus exporter. A couple of things worth highlighting here:

  • Changed default exporter port from 1006 (privileged) to 10006 (unprivileged), so you don't need privilege elevation if you have user access to the SPI, I2C and serial ports.
  • Tried to keep backwards compat with enviroplus_exporter keeping the same metric names etc, but I don't have a lot of experience with Prometheus, so worth an extra :eye:
  • The exporter is disabled by default, added a new --prom-exporter flag to enable it

rubiojr avatar Jan 26 '21 18:01 rubiojr

0a0442a adds an optional InfluxDB exporter and a tentative interface to easily register more exporters that simplifies the code a bit.

If you're happy with that extra, I'll update documentation accordingly.

rubiojr avatar Jan 27 '21 14:01 rubiojr

@rubiojr That's so great you added an influxdb exporter too, amazing. Yes please do update the docs and I'll give it a run. I'm currently travelling, so might be a few days between replies sorry, but can't wait to try it out.

sighmon avatar Jan 29 '21 23:01 sighmon

~I tried this branch, but the get the following error on start~ ~2021/04/10 15:37:04 error initializing ltr559 sensor: i2creg: no bus found; did you forget to call Init()?~

UPDATE: never mind I missed to use sudo

timoschilling avatar Apr 10 '21 14:04 timoschilling

@sighmon I'll try to het back to this this week, but feel free to push changes to the branch.

rubiojr avatar Apr 10 '21 17:04 rubiojr

@rubiojr The Prometheus exporter works perfectly. Couple of sensor issues:

  • I'm seeing the Gas sensor always read 25177.25 for all three gasses
  • The pressure seems to be out by a factor of 100. e.g. 101481.37 instead of 1023

I've added some more logging to the branch: https://github.com/sighmon/homekit-enviroplus/tree/rubiojr-local-readings

$ ./homekit-enviroplus-v2 -prom-exporter=true -prom-exporter-address=:8000 -sleep=5s

2021/04/11 16:52:16 Prometheus Exporter starting on port :8000
INFO 2021/04/11 16:52:16 ip_transport.go:184: Listening on port 44875

2021/04/11 16:59:06 Temperature: 26.92°C
2021/04/11 16:59:06 Humidity: 24.11 RH
2021/04/11 16:59:06 Pressure: 101481.37 hPa
2021/04/11 16:59:06 Air Quality: 1 (PM1.0 0.000000, PM2.5 1.000000, PM10 1.000000, CO 9.748228, NO2 2.517725)
2021/04/11 16:59:06 Gas: 25177.25 Oxidising, 25177.25 Reducing, 25177.25 NH3
2021/04/11 16:59:06 Light: 106.48 lux
2021/04/11 16:59:06 Motion: true (10.00)
2021/04/11 16:59:06 ---

sighmon avatar Apr 11 '21 07:04 sighmon

Is the local sensor access working? I would prefer to use this instead of the current method.

Thanks!

jonathanjacob avatar Aug 16 '21 18:08 jonathanjacob

@jonathanjacob it should work. Try using this branch. I did not have the time and/or energy to fix the two issues @sighmon mentioned though.

rubiojr avatar Aug 17 '21 11:08 rubiojr

@jonathanjacob it should work. Try using this branch. I did not have the time and/or energy to fix the two issues @sighmon mentioned though.

Thanks, I did, was having an issue when building/running. I will try building on a mac instead, maybe the PI couldn't build for some reason.

jonathanjacob avatar Aug 17 '21 19:08 jonathanjacob