pimoroni / grow-python

Python libs for controlling the Grow HATs
MIT License
57 stars 43 forks source link

Adding MQTT Publish Functionality to monitor service #11

Open jpwsutton opened 3 years ago

jpwsutton commented 3 years ago

The Monitor service is really well made, but I thought it would be good to publish the saturation readings to an MQTT broker as well as onto the display so that they can be logged in a Database.

I've duplicated the existing code pattern as much as possible by adding an MqttController Class. If there is an mqtt category in the settings.yml file, then it will load those settings and connect to the specified MQTT broker on startup. It will then check every loop to see whether it needs to publish the current sensor readings (default every 30 seconds).

I've added the following to the README.md explaining all of the settings:

MQTT Settings

If you want to send the moisture saturation levels somewhere to build a dashboard, or log to a database, MQTT is a great way to do this. You'll need an MQTT broker set up somwhere, Mosquitto is great choice and runs really well on a Raspberry Pi.

Add the following settings to your settings.yml file:

mqtt:
  mqtt_host: localhost
  mqtt_port: 1883
  mqtt_client_id: plantMonitor
  mqtt_username: plantpi
  mqtt_password: pimoroni
  mqtt_topic_root: plants/moisture
  mqtt_qos: 2
  mqtt_interval : 30

When you use this MQTT configuration, every time the mqtt_interval passes, the saturation levels (0% - 100%) will be published to the configured topic.

{
        "channel1": 18.7,
        "channel2": 2.8,
        "channel3": 68.6
}

The only main thing this is missing right now is the configuration logic for TLS settings, but I wanted to get this PR in front of some eyes sooner rather than later to incorporate any feedback.

jpwsutton commented 3 years ago

Ah, just realised I need to add the paho-mqtt python library in, would the service install.sh be the best place to do this?

Gadgetoid commented 2 years ago

It should be added to the bottom of library/setup.cfg in the [pimoroni] section. The install script parses this and installs the deps lists:

[pimoroni]
py2deps =
        python-pip
        python-yaml
        python-smbus
        python-pil
        python-spidev
        python-numpy
        python-rpi.gpio
py3deps =
        python3-pip
        python3-yaml
        python3-smbus
        python3-pil
        python3-spidev
        python3-numpy
        python3-rpi.gpio
commands =
        printf "Setting up i2c and SPI..\n"
        raspi-config nonint do_spi 0
        raspi-config nonint do_i2c 0
configtxt =
        dtoverlay=spi0-cs,cs0_pin=14 # Re-assign CS0 from BCM 8 so that Grow can use it

Sorry this has flown under the radar for a bit and needs a bit of work to rebase/merge. Let me know if you'd rather I take it from here!

And a long overdue thank you. It's clear at least someone will benefit from this - https://github.com/pimoroni/grow-python/issues/20

ikornaselur commented 2 years ago

Are there any outstanding blockers on this PR? I'd love to get MQTT support in to the service and if there's any more work that needs to be done, I'd love to help if I could 🙏

EDIT: Just saw the comment about TLS support, plus the commented out section of the PR! Is that support the only blocker at the moment?

jpwsutton commented 2 years ago

This could probably do with another pass as I wrote these changes over a year ago, so a fresh look might improve it generally. I'm happy to give it a go once I'm back from hols next week.

Gadgetoid commented 1 year ago

A go over and a rebase would be appreciated. I've just fixed our GitHub Action so hopefully it wont all collapse in a heap.

damacus commented 10 months ago

@jpwsutton do you want a hand with this? I was looking at a way of exporting the values to Home Assistant and this would be perfect 👌🏼