I’ve been reading through the documentation and your other post about MQTT, but I think at this point I might just need to see some basic examples/sketches of how to implement it, and then I can start experimenting on my own.
Ex: I’m running pioreactor1 on a Leader+Worker image. I just manually started the default chemostat dosing automation. I want to create a basic script at ~/my_script.py
(hosted on pioreactor1), and when I run this script, it will: (1) get the current volume or duration from the default chemostat; and (2) update the current published settings so volume = 1 and duration = 2.
I am not sure how to do the first, but I believe I can do the second as shown below.
from pioreactor.background_jobs.base import BackgroundJobContrib
from pioreactor.whoami import get_unit_name, get_latest_experiment
class Brains(BackgroundJobContrib):
def __init__(self):
def set_value(self, attribute, value):
self.publish(f"pioreactor/pioreactor1/{get_latest_experiment()}/dosing_automation/{attribute}/set", value)
def get_value(self, attribute) -> int | float:
# Retrieve value of 'attribute' from pioreactor1 here.
return value
b = Brains()
b.get_value("volume") # returns value of pioreactor1 published attribute volume
b.set_value("volume", 1.5) # Set's published attribute "volume" to 1.5
b.block_until_disconnected()
I am wondering how I might accomplish this by directly asking for the value of a published attribute. Is this currently possible?
I understand that the leader acts as the MQTT broker, and I am picturing this as a message board (hosted by the leader) from which all pioreactors can post messages to and read messages from. I’m not sure if this message board also stores any other data, such as the most recent values of published settings or a list of all pioreactors that are currently connected to the MQTT broker.
Edit: I ran pio mqtt -t "pioreactor/+/+/+/#"
and this looks relevant.
pio mqtt -t "pioreactor/+/+/+/volume/#"
2023-03-17T12:27:35 | pioreactor/pioreactor1/experiment/dosing_automation/volume 0.0
2023-03-17T12:27:35 | pioreactor/pioreactor1/experiment/dosing_automation/volume/$settable True
2023-03-17T12:27:35 | pioreactor/pioreactor1/experiment/dosing_automation/volume/$datatype float
2023-03-17T12:27:35 | pioreactor/pioreactor1/experiment/dosing_automation/volume/$unit mL
Here I can see that “pioreactor/pioreactor1/experiment/dosing_automation/duration 20.0” shows that the value of duration is 20. However, I found this information by running the “pio mqtt -t *” command from terminal. I’m not sure how I would do the same thing from a script, and save the value ‘20’ to a variable.