This plugin allows you to send data to an MQTT message broker using a CraftBeerPi background task.
cd craftbeerpi3/modules/plugins/
git clone https://github.com/peakMeissner/cbpi-MQTTClient
cd cbpi-MQTTClient
In case you miss the eclipse phao library. Install the dependencies using:
pip install paho-mqtt
Based on the requirement you can define a background task in the __init__.py
file. To get started check out the following example:
mqttc = MQTTClient().connect() # initialize the MQTT client
@cbpi.backgroundtask(key='mqtt_client', interval=2.5) # create bg job with an interval of 2.5 seconds
def mqtt_client_background_task(api):
sensors = cbpi.cache.get('sensors') # read available sensors
for key, value in sensors.iteritems(): # loop over the sensors
topic = 'CraftBeerPi/sensor/' + str(value.instance.id) # define the MQTT topic
data = { # define the playload
'id': value.instance.id,
'value': value.instance.last_value,
'unit': value.instance.get_unit()
}
payload = json.dumps(data, ensure_ascii=False) # convert payload to JSON
mqttc.publish(topic, payload) # publish the payload
In order to test the client in a local environment you'll need an MQTT message broker such as Mosquitto. You can easily install the message broker on your local CraftBeerPi by using the following command.
sudo apt-get install mosquitto mosquitto-clients
If you want to subscribe to the published content I recomend the MQTT.fx client.
Integrating CraftBeerPi through cbpi-MQTTClient with SAP IoT Services.