MQTT and Nebri

Notice: This article is maintained for historical purposes. The Bixly Automate/Nebri OS platform that Bixly built, and this article features, is no longer available. Checkout Serverless Providers as an alternative.

What is MQTT?

MQTT is a network protocol used for device to device messaging. Once a server is set up, you can subscribe to topics. Once a subscription is in place, you’ll receive any messages published to that topic. You can also publish messages to said topic. These published messages are sent to all devices subscribed to that particular topic.

Using MQTT with Nebri

We recommend the package paho-mqtt for utilizing MQTT on a Nebri instance. This package can be pip installed over an SSH connection to your Nebri Instance. There are a few modifications that need to be taken into consideration when implementing MQTT on a Nebri instance versus a regular python application. We don’t recommend using loop_forever() without adding a disconnect to one of your callback functions as it will cause timeouts, which will lead to your instance being restarted. Instead, we recommend using a manual loop that will timeout and calling loop() within it.

The above example shows one way to implement a loop that will not result in the internal time out being triggered. Keep in mind that any script whose functionality takes longer than 5 minutes to complete will be forced to time out. If a script times out, it will be marked as inactive and won’t rerun automatically. Here’s another way to implement this in a Nebri rule script.

The nice thing about using MQTT in a rule script is you can set variables when you trigger said script. In the above example, self.timeout can be sent via debug mode when you trigger a script.