Skip to content

Conversation

jpwsutton
Copy link

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
  • mqtt_host - This is the hostname or IP address of the server where your MQTT broker is.
  • mqtt_port - This is the port number for the broker, usually 1883.
  • mqtt_client_id - This is the Client ID for your monitor service, can be ignored if you dont mind what the broker sees this device as.
  • mqtt_username - If you have authentication enabled, this is the username.
  • mqtt_password - If you have authentication enabled, this is the password.
  • mqtt_topic_root - This is the MQTT topic where the sensor data will be sent.
  • mqtt_qos - The QoS Level to publish messages at. Default: 2.
  • mqtt_interval - This is the time interval between publishing readings, in seconds.

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
Copy link
Author

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
Copy link
Member

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 - #20

@ikornaselur
Copy link

ikornaselur commented Jul 21, 2022

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
Copy link
Author

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
Copy link
Member

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
Copy link
Contributor

damacus commented Nov 28, 2023

@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 👌🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants