ThingsBoard and Raspberry PI (half 2): Add Gadget telemetry by MTTQ and Python

Making a tenant and including a primary system is the subsequent step when you put in ThingsBoard on Raspberry PI. A easy monitoring job is by checking inside CPU temperature

On this tutorial, I’m going to indicate you easy methods to create a tenant, add a tool and configure it to message by MTTQ and Python with Thingsboard on Raspberry PI. We’ll begin from a recent ThingsBoard set up, in accordance with my ThingsBoard and Raspberry PI: getting a Professional IoT dashboard tutorial.

Raspberry PI 4 model B image

There are other ways to make use of Mosquitto (MQTT) messages by way of Python. On this case, I’m going to put in Mosquitto to the Raspberry PI OS and use it from a python script that simulates console instructions. On this approach, you should use the identical python script to ship any desired bash output to ThingsBoard in a simple and totally customizable approach.

As necessities and set up have been already defined within the earlier submit, we’ll go immediately on the process.

Step-by-Step Process

Login as System Administrator with the credentials you get within the earlier submit. Please additionally keep in mind as quickly to alter System Admin electronic mail/password along with your customized ones, as it's a finest follow in your safety.

Create a ThingsBoard Tenant

We’ll have to create a Tenant in an effort to create dashboards. From System Admin dashboard, go to tenants by clicking the associated icon or sidebar hyperlink:

thingsboard click tenant

Click on the cross image on top-right to create a brand new tenant:

thingsboard add new tenant

Please fill obligatory fields (marked by an asterisk). I’ve used “Dwelling” as Title and I left “Default” as Tenant Profile:

thingsboard add tenant info

Then click on the “Add” button in bottom-right a part of this window.

Create a Tenant Admin

The brand new tenant now seems in our checklist. Use the hamburger menù (highlighted within the following image) to open accessible choices and click on “Handle tenant admins”:

thingsboard tenant list options

On the subsequent web page, please use the button with the cross image so as to add a brand new tenant admin:

thingsboard add tenant admin

On the next web page, please fill in not less than the e-mail subject. As we don’t have nonetheless configured the e-mail sending, we are able to additionally proceed with an activation hyperlink, by maintaining positive that the “Activation technique” is about to “Show Activation Hyperlink”:

thingsboard tenant admin info

Lastly, please click on the “Add” button. It will generate an activation hyperlink to get your tenant admin lively:

thingsboard tenant admin activation link

Copy the activation hyperlink and use it in a brand new browser tab. You may be requested first to create a password in your tenant admin:

thingsboard tenant admin create password

Fill within the password you want after which click on the “Create Password” button. Please save in a secure place your electronic mail and password registered right here as you'll use these for all the subsequent entry.

You may be logged in to the tenant admin house web page. From right here you possibly can affiliate belongings, gadgets and create dashboards.

thingsboard tenant admin home

Create a brand new Gadget

Now, let’s create our first system, which would be the similar Raspberry PI working ThingsBoard. We’ll monitor CPU temperature. Click on on the Units hyperlink from the sidebar menu:

thingsboard tenant admin home Device highlighted

From the Units web page, which might be empty, please click on the button with the cross image after which “Add new Gadget”, as highlighted within the following image:

thingsboard add Device

Fill within the identify subject one thing helpful to establish the system you might be including (I used “RPI ThingsBoard Server”). You possibly can depart all the opposite fields as default and immediately click on the “Add” button at bottom-right:

thingsboard add Device info

You'll find now the primary system accessible from the Units checklist. Use the hamburger menu in the identical row as our Raspberry PI system to open the sub-menu the place you must click on on the “Handle credentials” hyperlink:

thingsboard Device Manage Credentials

You'll get the entry token for the Raspberry PI system:

thingsboard Raspberry PI token

Copy it as we’ll want this string for the next steps.

Now you can shut the Gadget Credentials window by clicking the Cancel button.

Set up and Take a look at Mosquitto

sudo apt set up mosquitto-clients -y

To check that MQTT communication is working appropriately, we’ll ship a message to ThingsBoard together with a telemetry measure (manually set to worth 25). From the terminal, please use the next command (please keep in mind to make use of your token as a substitute of mine, in crimson):

mosquitto_pub -d -q 1 -h "localhost" -p "1883" -t "v1/gadgets/me/telemetry" -u "UaknVYwlFnLgwJZdzSPw" -m {"CPU_temp":25}

Word that the “CPU_temp” label may be no matter you need, but it surely should NOT embrace areas contained in the string. The next is the anticipated consequence:

pi@raspberrypi:~ $ mosquitto_pub -d -q 1 -h "localhost" -p "1883" -t "v1/gadgets/me/telemetry" -u "UaknVYwlFnLgwJZdzSPw" -m {"CPU_temp":25}
Consumer (null) sending CONNECT
Consumer (null) obtained CONNACK (0)
Consumer (null) sending PUBLISH (d0, q1, r0, m1, 'v1/gadgets/me/telemetry', ... (13 bytes))
Consumer (null) obtained PUBACK (Mid: 1, RC:0)
Consumer (null) sending DISCONNECT

This can be a right communication log. We are able to examine that the worth has been appropriately obtained and registered from ThingsBoard by going again to “Units” checklist in ThingsBoard dashboard, clicking (single left click on with the mouse) on our Raspberry PI system on this checklist, and eventually switching to “Newest Telemetry” tab. You will notice the CPU_temp telemetry registration:

thingsboard Raspberry PI CPU temp test MQTT

Create the Python Script

Create a python script named “”:


And use the next content material. Please keep in mind, once more, to make use of your token as a substitute of crimson one:

from subprocess import Popen, PIPE

def console(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    out, err =
    return out.decode('ascii').strip()

param_cmd="cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1/1000}'"


publish='mosquitto_pub -d -q 1 -h "'+host+'" -p "'+port+'" -t "v1/gadgets/me/telemetry" -u "'+token+'" -m {"'+param_name+'":'+worth+'}'

This script simply defines a customized perform named “console()” that retains as enter a string and executes that string on the host bash shell.

One of many methods to get CPU temperature on Raspberry PI is by working on shell the “cat /sys/class/thermal/thermal_zone0/temp” command. This ends in a quantity that, divided by 1000, provides CPU temperature in levels. However the cat consequence isn’t an actual integer quantity, so piping the built-in awk perform each converts it to floating and divides by 1000.

The publish variable shops the Mosquitto string to run in an effort to publish values. It's executed with the ultimate “console(publish)” line.

Please notice you can customise this script by altering “param_name” along with your favourite telemetry label and “param_cmd” with the shell command to be executed.

Take a look at the script by working, from the terminal:


A brand new CPU_temp studying might be recorded in our ThingsBoard tenant, this time with an actual CPU temperature worth:

thingsboard Raspberry PI CPU temp test MQTT #2

Set Telemetry Measures Periodic

For this job we’ll use the built-in cron service. From terminal, open the cron jobs configuration file with the next command:

crontab -e

Please notice that these cron jobs are executed as root, so we’ll want to supply the total path to execute our script.

The shell will present an possibility checklist to decide on your favourite textual content editor, press 1 to make use of nano as editor or choose your most popular one. Append the next line, supposing that “” script is situated in your pi person house folder:

* * * * * python3 /house/pi/

This cron job will execute a CPU temperature measurement each minute and ship it to our ThingsBoard tenant. Return to Raspberry PI telemetry to examine that “Final replace time” modifications each minute with a brand new temperature pattern.

Create CPU Temperature Widget on a Dashboard

From ThingsBoard, please click on on the CPU_temp telemetry. It will activate the “Present on widget” button:

thingsboard Raspberry PI CPU temp click telemetry

Click on it and choose “Charts” on “Present Bundle” checklist menu. Right here you possibly can select from completely different charts by switching with the left/proper buttons on chart preview sides. I'll use the “Timeseries Bar Chart”.

thingsboard Raspberry PI CPU temp select chart

As soon as completed, click on the “Add to Dashboard” button. It'll suggest so as to add the widget to an present dashboard or to immediately create a brand new dashboard:

thingsboard Raspberry PI CPU temp add dashboard

As our set up is new, choose Create New dashboard, sort your favourite identify for it and click on “Add”. The brand new widget is now added to a brand new house.

From the left aspect menu, go to Dashboards. It will checklist all of your accessible dashboard areas. If it's a new set up like on this tutorial, you'll find only one accessible. Click on it (“My house Raspberry PI” in my case) after which the button “Open Dashboard”:

thingsboard Raspberry PI CPU temp open dashboard

You'll find the widget with our timeseries bar chart:

thingsboard Raspberry PI CPU temp dashboard

The pencil icon on the bottom-right aspect will allow you to maneuver/resize all of the widgets in your dashboard. Furthermore, the clock icon will allow you to set the correct timeframe to indicate:

thingsboard Raspberry PI CPU temp dashboard clock

By setting it to five hours I received the next:

thingsboard Raspberry PI CPU temp 5 hours timeframe

Get pleasure from!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *