“How can I set up notifications when the flespi stream encounters an error?”
We’ve got this question from one of our users and would like to share the solution with all of you.
Sure you can monitor the Toolbox logs and the flespi panel dashboard to be informed of such occurrences. But staring at the screen is not the most productive pastime. So we suggest a handier option to get notifications of critical events once they occur thus saving time and minimizing the time-to-fix.
Here’s the diagram of the main dataflow (actual work that flespi does) and auxiliary dataflow (dedicated to catching errors and sending email notifications):
We will focus on the auxiliary dataflow since the main dataflow may vary depending on your needs.
Create an MQTT channel
Let’s assume that we want to monitor the health of an HTTP flespi stream forwarding data to the target platform.
Create a flespi channel of MQTT type.
Put the mqtt.flespi.io:1883 of the flespi MQTT broker in the MQTT broker URI field.
Replace %ident% in Device identification field with anything without %%, e.g. “stream_<stream_id>”.
Paste a valid flespi token into the Username field; leave the Password blank.
Subscribe it to the logs of the target stream by specifying the following MQTT topic: flespi/log/gw/streams/{stream_id}/# (replace the {stream_id} with the specific ID of the stream in the flespi panel).
Save changes.
Check the channel logs to see that you start getting the logs:
Set up a Thingsboard device
Thingsboard is an open-source IoT platform (we’ve already mentioned it in one of our blog posts). You can easily create a free account at demo.thingsboard.io.
Login with Thingsboard, go to Devices page, and create a new device.
Leave the device type as default.
Check the “Is gateway” checkbox.
Save.
Click on the Manage credentials icon on the device card and copy the Access token.
Create a flespi stream to Thingsboard
To generate alarms we need Thingsboard to receive the logs data. Therefore, we need to forward to log records collected in the channel to the Thingsboard platform.
Create a flespi stream of a Thingsboard type.
Paste the URI of your Thingsboard server (for free version it’s demo.thingsboard.io:1883).
Insert the access token that you copied in Thingsboard (see the screenshot above).
Switch to the Subscriptions tab and subscribe this stream to the channel with logs.
Check the Toolbox for the stream to make sure it started sending messages:
The gateway device we previously created in Thingsboard will automatically create a specific device instance that will collect messages from a particular ident (unique ID).
If we open this device and navigate to the Latest telemetry tab, we will see the values from the last log record received:
We will base our alarms and notifications on the event_code field — 404 tells that the message was sent successfully, while 403 tells that the stream couldn’t connect to the target.
Create Rule Chains in Thingsboard
To catch the 403 code and initiate email sending, we need to set up the logic. Thingsboard has rule chains for this purpose.
Below is the rule chain for checking the error_code value, generating an alarm if necessary, and sending an email:
Double click on the script block to edit filtering condition:
Double click on to email block to change the email settings:
Then we need to add the rule chain above (Create/Clear Alarm & Send Email) into the root rule chain in the Post telemetry branch:
Note: Download the JSON files for the Rule Chains necessary to implement the email notifications in Thingsboard: Root Rule Chain, Create/Clear Alarm & Send Email and import them on the Rule Chains page.
Getting a notification
If something goes wrong with the HTTP stream destination, meaning it won’t be able to send data, the stream log will throw a “403: stream failed to connect” error. This will trigger our Create/Clear Alarm & Send Email rule chain and you will get an email notification about this occurrence:
Now you are informed about any critical issues happening in your main dataflow and can react swiftly.
***
This may not be the most straightforward or intuitive way to get notifications about flespi stream errors, however, it shows the flexibility of the flespi platform and a variety of ways it can interact with other applications.