{"id":28,"date":"2023-06-01T14:48:22","date_gmt":"2023-06-01T18:48:22","guid":{"rendered":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/?post_type=chapter&#038;p=28"},"modified":"2023-09-22T12:06:47","modified_gmt":"2023-09-22T16:06:47","slug":"chapter-3","status":"publish","type":"chapter","link":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/chapter\/chapter-3\/","title":{"raw":"Connecting a Virtual Client to TB","rendered":"Connecting a Virtual Client to TB"},"content":{"raw":"&nbsp;\r\n<p style=\"text-align: justify\">In TB, the registered device serves as the connection point between the telemetry data and the platform, utilizing the MQTT (Message Queue Telemetry Transport) protocol. However, TB also supports other communication protocols such as NB-IoT, Sigfox, and LoRa <span style=\"color: #3366ff\"><span style=\"color: #000000\">[footnote]https:\/\/thingsboard.io\/docs\/samples\/[\/footnote]<\/span><\/span>. A detailed description of these protocols is beyond the scope of this chapter.<\/p>\r\n<p style=\"text-align: justify\">As mentioned in the previous chapter, displaying telemetry data on the dashboard involves four basic steps: 1) adding the device, 2) adding the dashboard, 3) adding the alias, and 4) adding the widget. Once these settings are configured, TB is ready to establish communication with the device to receive the telemetry data. In this chapter, we will explore how to use Python programming to publish data to TB. Before delving into the Python code, let's take a brief overview of the MQTT protocol.<\/p>\r\n\r\n<h1 style=\"text-align: justify\"><strong><span style=\"color: #3366ff\">3.1\u00a0 MQTT Protocol<\/span><\/strong><\/h1>\r\n<p style=\"text-align: justify\">The MQTT protocol is widely used messaging protocol in the Internet of Things (IoT) domain. It defines a set of rules that govern the exchange of data between IoT devices over the Internet. MQTT is commonly used for both consumer and industrial IoT (IIoT) applications. The protocol follows an event-based communication model, employing the concepts of publishers and subscribers to establish a Pub\/Sub (Publish\/Subscribe) pattern for device connectivity <span style=\"color: #3366ff\"><span style=\"color: #000000\">[footnote]https:\/\/thingsboard.io\/docs\/samples\/[\/footnote] <\/span><\/span><span style=\"color: #3366ff\"><span style=\"color: #000000\">[footnote]https:\/\/thingsboard.io\/docs\/samples\/esp8266\/[\/footnote] [footnote]https:\/\/thingsboard.io\/docs\/user-guide\/integrations\/send-data-external-mqtt-brokers\/[\/footnote] .<\/span><\/span><\/p>\r\n<p style=\"text-align: justify\">Publishing refers to the process where a device sends data to a server, also known as a broker. On the other hand, subscription refers to the process of receiving data from the broker. MQTT client libraries can be found on the Internet, catering to various types of publishers and subscribers. data is differentiated from one another using \"topics\", which serve as identifiers within the MQTT ecosystem. The broker acts as an intermediary, managing the communication between IoT devices. It functions as a server, while the connected devices operate as clients. The MQTT broker ensures that they are delivered correctly to the subscribed clients.<\/p>\r\n\r\n<h2 style=\"text-align: justify\"><span style=\"color: #000000\"><strong>1.\u00a0\u00a0\u00a0 MQTT Client<\/strong><\/span><\/h2>\r\n<p style=\"text-align: justify\">In the MQTT protocol, both publishers and subscribers are referred to as clients. The distinction between a publisher and a subscriber lies in their respective roles in the message exchange process. Publishers are clients that send messages or data to the broker server, while subscribers are clients that have subscribed to the broker server to receive messages.<\/p>\r\n<p style=\"text-align: justify\">The MQTT client can encompass a wide range of devices, from small microcontrollers to large servers capable of handling the TCP\/IP stack required for network communication with the MQTT server. For instance, an MQTT client could be a resource-constrained device connected via a wireless network, running minimal code. Since MQTT is built on top of TCP\/IP, both the client and the broker must support this network stack.<\/p>\r\n<p style=\"text-align: justify\">Implementing the MQTT protocol in a client is relatively straightforward and efficient compared to other protocols like HTTP, requiring minimal resources. Therefore, MQTT is particularly suitable for IoT solutions with limited resources. Numerous programming languages, such as Python, Java, JavaScript, and C\/C++, have developed libraries to support MQTT. The MQTT wiki <span style=\"color: #3366ff\"><span style=\"color: #000000\">[footnote]https:\/\/thingsboard.io\/docs\/iot-gateway\/config\/mqtt\/[\/footnote]<\/span><\/span> provides a comprehensive list of these libraries, enabling developers to leverage MQTT in their preferred programming languages.<\/p>\r\n\r\n<h2 style=\"text-align: justify\"><span style=\"color: #000000\"><strong>2.\u00a0\u00a0\u00a0 MQTT Broker<\/strong><\/span><\/h2>\r\n<p style=\"text-align: justify\">In the MQTT protocol, the broker plays a central role in facilitating publish\/subscribe communication. A single broker has the capability to handle a large number of connected MQTT clients simultaneously. In the MQTT architecture, clients do not directly communicate with each other but instead communicate through the broker. The broker performs several essential functions, including:<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">Message Acquisition: The broker receives and collects all published messages from the publishing clients.<\/li>\r\n \t<li style=\"text-align: justify\">Session Management: The broker manages session data for each connected client, maintaining information about client subscriptions and message delivery.<\/li>\r\n \t<li style=\"text-align: justify\">Message Filtering: The broker filters incoming messages based on their topic and determines the subscribers for each message.<\/li>\r\n \t<li style=\"text-align: justify\">Message Distribution: The broker distributes the published messages to the appropriate subscribing clients.<\/li>\r\n \t<li style=\"text-align: justify\">Authentication and Authorization: The broker is responsible for authenticating and authorizing clients. This is particularly critical in MQTT since data is transmitted over public networks like the Internet. Each client is assigned an access token to ensure secure communication with the broker.<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">The broker can be implemented as a server hosted in a cloud platform, providing scalable and reliable MQTT communication infrastructure. Alternatively, the broker can also function as an intermediate gateway that facilitates communication between different devices, allowing for seamless connectivity across diverse IoT devices and networks.<\/p>\r\n\r\n\r\n[caption id=\"attachment_491\" align=\"aligncenter\" width=\"939\"]<img class=\"size-full wp-image-491\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/2-2.gif\" alt=\"\" width=\"939\" height=\"464\" \/> <span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 13. Different types of MQTT brokers<\/em><\/span>[\/caption]\r\n<h1><\/h1>\r\n<h1><strong><span style=\"color: #3366ff\">3.2\u00a0 Telemetry Data<\/span><\/strong><\/h1>\r\n<p style=\"text-align: justify\">In the TB platform, a comprehensive range of features is available to handle telemetry data published from devices with various types and attributes. Telemetry data in TB is represented in JavaScript Object Notation (JSON) format and can be published or subscribed to by clients. A typical example of telemetry data in TB would be a list of key-value pairs, structured as follows:<\/p>\r\n<img class=\" wp-image-205 alignleft\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u0635\u0648\u062a\u06cc-\u0646\u0641\u06cc\u0633-1.gif\" alt=\"\" width=\"345\" height=\"180\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The JSON format allows for organizing and structuring the data in a readable and standardized manner. Each data point can be associated with a timestamp, capturing the time of its occurrence or recording. This timestamp provides valuable temporal information and enhances the context and usefulness of the telemetry data. Whether it is the value of a variable, the status of a switch, or any other relevant information, the inclusion of timestamps enables efficient time-series analysis and facilitates tracking data trends and patterns over time. Throughout the rest of the chapter, we will utilize this JSON formatting in Python to transmit telemetry data to TB, enabling seamless integration and communication between the Python code and the TB platform.<\/p>\r\n\r\n<h1 style=\"text-align: justify\"><strong><span style=\"color: #3366ff\">3.3\u00a0 \u00a0The Python Code<\/span><\/strong><\/h1>\r\n<p style=\"text-align: justify\">To send telemetry data to TB using Python programming, you need to publish the data in JSON format.<\/p>\r\n<strong>Opening the MQTT port<\/strong>\r\n\r\nBefore starting the coding process, it is important to check the client port used for communication with TB over MQTT. The default port for MQTT communication is 1883. To check if this port is open, you can follow these steps:\r\n\r\n1- Open the Command Prompt (cmd) on your Windows PC or laptop with administrative privileges.\r\n\r\n2- Enter the command \"netstat -a\" and press Enter.\r\n\r\n3- Look for the printed results to find the open ports.\r\n\r\n4- Check if port 1883 is listed among the open ports. If it is listed, it means that the port is open and available for MQTT communication with TB.\r\n\r\n[caption id=\"attachment_207\" align=\"aligncenter\" width=\"906\"]<img class=\"size-full wp-image-207\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644.jpg\" alt=\"\" width=\"906\" height=\"279\" \/> <em><span style=\"color: #3366ff\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Figure 14. List of open ports<\/span><\/em>[\/caption]\r\n<p style=\"text-align: justify\">5- If you come across a closed port that isn't included in the list, it's necessary to open it. To do so, follow these steps: Go to \"Windows Defender Firewall\" by entering the \"firewall.cpl\" command in the Run command window. In the opened window, click on \"Advanced settings\" on the left side. From the options on the left, select \"Inbound Rules\". On the right side, choose \"New Rule\". Finally, proceed with the remaining steps in order to successfully open the port.<\/p>\r\nAfter opening the \"New Inbound Rule\" window, follow these steps:\r\n<ul>\r\n \t<li>Choose \"Port\" and click \"Next\".<\/li>\r\n \t<li>Select \"TCP\" and enter \"1883\" in the \"Specific local ports\" field. Click \"Next\".<\/li>\r\n \t<li>Allow the connection by selecting \"Allow the connection\" and click \"Next\".<\/li>\r\n \t<li>Check all the boxes for \"Domain\", \"Private\", and \"Public\" profiles, and click \"Next\".<\/li>\r\n \t<li>In the \"Name\" field, enter \"MQTT Service Port\" and click \"Finish\".<\/li>\r\n \t<li>Once completed, you will find port 1883 listed in the main ports as described in Step 4. You can refer to figure below for a visual representation of this step.<\/li>\r\n<\/ul>\r\n<img class=\"size-full wp-image-258 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13.jpg\" alt=\"\" width=\"504\" height=\"295\" \/><img class=\"size-full wp-image-261 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12.jpg\" alt=\"\" width=\"638\" height=\"299\" \/>\r\n\r\n<img class=\"size-full wp-image-238 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2.jpg\" alt=\"\" width=\"939\" height=\"405\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"wp-image-251 size-full aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11.jpg\" alt=\"\" width=\"614\" height=\"448\" \/>\r\n\r\n<img class=\"size-full wp-image-249 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9.jpg\" alt=\"\" width=\"573\" height=\"449\" \/><img class=\"size-full wp-image-250 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10.jpg\" alt=\"\" width=\"592\" height=\"447\" \/>\r\n\r\n<span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 15. Opening the MQTT client port in Windows<\/em><\/span>\r\n\r\n<strong>Publishing Data to TB<\/strong>\r\n<p style=\"text-align: justify\">1- To install the Paho MQTT library in Python, assuming you have Python already installed on your client machine, open a command prompt or terminal window and execute the command \"pip install paho-mqtt\". This will download and install the Paho MQTT library along with its dependencies. Once the installation is complete, you'll be able to utilize the library in your Python projects.<\/p>\r\npip install paho_mqtt\r\n<p style=\"text-align: left\">2- Import the following required libraries into your code:<\/p>\r\n\r\n<div>\r\n<div>import paho.mqtt.client as paho \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #mqtt library<\/div>\r\n<div>import os<\/div>\r\n<div>import json<\/div>\r\n<div>import time<\/div>\r\n<div>from datetime import datetime<\/div>\r\n<\/div>\r\n<p style=\"text-align: justify\">3- To define the \u201c<strong>ACCESS_TOKEN<\/strong><strong>\u201d<\/strong> variable along with the address of the broker and the client port, you should include the following lines in your code:<\/p>\r\n\r\n<div>\r\n<div>ACCESS_TOKEN='my18rTF09PA4WgKIfAtf' \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #Token of your device<\/div>\r\n<div>broker=\"thingsboard.cloud\" \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#host name<\/div>\r\n<div>port=1883 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #data listening port<\/div>\r\n<\/div>\r\n<p style=\"text-align: justify\">Replace \"<strong>your_access_token<\/strong>\" with the actual value of the access token derived from the TB platform, as it was shown in Figure 6. Similarly, replace \"<strong>broker_address<\/strong>\" with the address of your MQTT broker which in our case is \u201c<strong>thingsboard.cloud<\/strong>\u201d. Assign the appropriate port number (i.e., 1883) to \"<strong>broker_port<\/strong>\". By defining these variables, you will have the necessary information to establish a connection between your code and the MQTT broker.<\/p>\r\n<p style=\"text-align: justify\">4- To define the callback method for publishing data and handle the response when telemetry data is successfully published to ThingsBoard (TB), you can include the following code snippet:<\/p>\r\n\r\n<div>\r\n<div>def on_publish(client,userdata,result): \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #create function for callback<\/div>\r\n<div>\u00a0 \u00a0 print(\"data published to thingsboard \\n\")<\/div>\r\n<div>\u00a0 \u00a0 pass<\/div>\r\n<\/div>\r\n<p style=\"text-align: justify\">5- Define a client object in your code and configure its parameters:<\/p>\r\n\r\n<div>client1= paho.Client(\"control1\") \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#create client object<\/div>\r\n<div>client1.on_publish = on_publish \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #assign function to callback<\/div>\r\n<div>client1.username_pw_set(ACCESS_TOKEN) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #access token from thingsboard device<\/div>\r\n<div>client1.connect(broker,port,keepalive=60) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #establish connection<\/div>\r\n<p style=\"text-align: justify\">6- Publish sample telemetry data that is written in JSON format in an infinite loop:<\/p>\r\n\r\n<div>\r\n<div>while True:<\/div>\r\n<div>\u00a0 \u00a0 # {'Humidity\": + \"60\"; \u00a0\"Temperature\": \"10\"}<\/div>\r\n<div>\u00a0 \u00a0payload=\"{\"<\/div>\r\n<div>\u00a0 \u00a0payload+=\"\\\"Humidity\\\":\" + str(hmnt) + \",\"<\/div>\r\n<div>\u00a0 \u00a0payload+=\"\\\"temperature\\\":\" + str(temp)<\/div>\r\n<div>\u00a0 \u00a0payload+=\"}\"<\/div>\r\n<div>\u00a0 \u00a0ret= client1.publish(\"v1\/devices\/me\/telemetry\",payload) #topic-v1\/devices\/me\/telemetry<\/div>\r\n<div>\u00a0 \u00a0print(\"Please check LATEST TELEMETRY field of your device\")<\/div>\r\n<div>\u00a0 \u00a0print(payload);<\/div>\r\n<div>\u00a0 \u00a0time.sleep(5)<\/div>\r\n<\/div>\r\nIn this step, you can put your telemetry data, i.e., <strong><span style=\"color: #008080\">Humidity <span style=\"color: #000000\">=<\/span> 55 <\/span><\/strong>(%) and <span style=\"color: #008080\"><strong>Temperature<\/strong><\/span> = <span style=\"color: #008080\"><strong>24<\/strong><\/span> (0C), in a variable named payload, publishing it to the following topic: \u201c<span style=\"color: #008080\"><strong>v1\/devices\/me\/telemetry<\/strong><\/span>\u201d.\r\n<p style=\"text-align: justify\">7- Aggregating the aforementioned Python scripts, the final code will be as follow. Running this code will publish the telemetry data every five seconds.<\/p>\r\n\r\n<div>\r\n<div>import paho.mqtt.client as paho \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #mqtt library<\/div>\r\n<div>import os<\/div>\r\n<div>import json<\/div>\r\n<div>import time<\/div>\r\n<div>from datetime import datetime<\/div>\r\n<div>ACCESS_TOKEN='my18rTF09PA4WgKIfAtf' \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #Token of your device<\/div>\r\n<div>broker=\"thingsboard.cloud\" \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#host name<\/div>\r\n<div>port=1883 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #data listening port<\/div>\r\n<div>def on_publish(client,userdata,result): \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #create function for callback<\/div>\r\n<div>\u00a0 \u00a0 print(\"data published to thingsboard \\n\")<\/div>\r\n<div>\u00a0 \u00a0 pass<\/div>\r\n<div>client1= paho.Client(\"control1\") \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#create client object<\/div>\r\n<div>client1.on_publish = on_publish \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #assign function to callback<\/div>\r\n<div>client1.username_pw_set(ACCESS_TOKEN) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #access token from thingsboard device<\/div>\r\n<div>client1.connect(broker,port,keepalive=60) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #establish connection<\/div>\r\n<div>temp = 86<\/div>\r\n<div>hmnt = 40<\/div>\r\n<div>while True:<\/div>\r\n<div>\u00a0 \u00a0 # {'Humidity\": + \"60\"; \u00a0\"Temperature\": \"10\"}<\/div>\r\n<div>\u00a0 \u00a0payload=\"{\"<\/div>\r\n<div>\u00a0 \u00a0payload+=\"\\\"Humidity\\\":\" + str(hmnt) + \",\"<\/div>\r\n<div>\u00a0 \u00a0payload+=\"\\\"temperature\\\":\" + str(temp)<\/div>\r\n<div>\u00a0 \u00a0payload+=\"}\"<\/div>\r\n<div>\u00a0 \u00a0ret= client1.publish(\"v1\/devices\/me\/telemetry\",payload) #topic-v1\/devices\/me\/telemetry<\/div>\r\n<div>\u00a0 \u00a0print(\"Please check LATEST TELEMETRY field of your device\")<\/div>\r\n<div>\u00a0 \u00a0print(payload);<\/div>\r\n<div>\u00a0 \u00a0time.sleep(5)<\/div>\r\n<div><\/div>\r\n<\/div>\r\n<p style=\"text-align: justify\">8- To view the latest telemetry data of defined devices in your TB account, navigate to the \"Latest Telemetry\" section. This section provides you with the most recent telemetry data sent by your devices, allowing you to monitor the values of attributes or parameters being tracked.<\/p>\r\n\r\n\r\n[caption id=\"attachment_231\" align=\"aligncenter\" width=\"821\"]<img class=\"wp-image-231 size-full\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mi2_1818.gif\" alt=\"\" width=\"821\" height=\"475\" \/> <span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 16. Latest telemetry data<\/em><\/span>[\/caption]","rendered":"<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In TB, the registered device serves as the connection point between the telemetry data and the platform, utilizing the MQTT (Message Queue Telemetry Transport) protocol. However, TB also supports other communication protocols such as NB-IoT, Sigfox, and LoRa <span style=\"color: #3366ff\"><span style=\"color: #000000\"><a class=\"footnote\" title=\"https:\/\/thingsboard.io\/docs\/samples\/\" id=\"return-footnote-28-1\" href=\"#footnote-28-1\" aria-label=\"Footnote 1\"><sup class=\"footnote\">[1]<\/sup><\/a><\/span><\/span>. A detailed description of these protocols is beyond the scope of this chapter.<\/p>\n<p style=\"text-align: justify\">As mentioned in the previous chapter, displaying telemetry data on the dashboard involves four basic steps: 1) adding the device, 2) adding the dashboard, 3) adding the alias, and 4) adding the widget. Once these settings are configured, TB is ready to establish communication with the device to receive the telemetry data. In this chapter, we will explore how to use Python programming to publish data to TB. Before delving into the Python code, let&#8217;s take a brief overview of the MQTT protocol.<\/p>\n<h1 style=\"text-align: justify\"><strong><span style=\"color: #3366ff\">3.1\u00a0 MQTT Protocol<\/span><\/strong><\/h1>\n<p style=\"text-align: justify\">The MQTT protocol is widely used messaging protocol in the Internet of Things (IoT) domain. It defines a set of rules that govern the exchange of data between IoT devices over the Internet. MQTT is commonly used for both consumer and industrial IoT (IIoT) applications. The protocol follows an event-based communication model, employing the concepts of publishers and subscribers to establish a Pub\/Sub (Publish\/Subscribe) pattern for device connectivity <span style=\"color: #3366ff\"><span style=\"color: #000000\"><a class=\"footnote\" title=\"https:\/\/thingsboard.io\/docs\/samples\/\" id=\"return-footnote-28-2\" href=\"#footnote-28-2\" aria-label=\"Footnote 2\"><sup class=\"footnote\">[2]<\/sup><\/a> <\/span><\/span><span style=\"color: #3366ff\"><span style=\"color: #000000\"><a class=\"footnote\" title=\"https:\/\/thingsboard.io\/docs\/samples\/esp8266\/\" id=\"return-footnote-28-3\" href=\"#footnote-28-3\" aria-label=\"Footnote 3\"><sup class=\"footnote\">[3]<\/sup><\/a> <a class=\"footnote\" title=\"https:\/\/thingsboard.io\/docs\/user-guide\/integrations\/send-data-external-mqtt-brokers\/\" id=\"return-footnote-28-4\" href=\"#footnote-28-4\" aria-label=\"Footnote 4\"><sup class=\"footnote\">[4]<\/sup><\/a> .<\/span><\/span><\/p>\n<p style=\"text-align: justify\">Publishing refers to the process where a device sends data to a server, also known as a broker. On the other hand, subscription refers to the process of receiving data from the broker. MQTT client libraries can be found on the Internet, catering to various types of publishers and subscribers. data is differentiated from one another using &#8220;topics&#8221;, which serve as identifiers within the MQTT ecosystem. The broker acts as an intermediary, managing the communication between IoT devices. It functions as a server, while the connected devices operate as clients. The MQTT broker ensures that they are delivered correctly to the subscribed clients.<\/p>\n<h2 style=\"text-align: justify\"><span style=\"color: #000000\"><strong>1.\u00a0\u00a0\u00a0 MQTT Client<\/strong><\/span><\/h2>\n<p style=\"text-align: justify\">In the MQTT protocol, both publishers and subscribers are referred to as clients. The distinction between a publisher and a subscriber lies in their respective roles in the message exchange process. Publishers are clients that send messages or data to the broker server, while subscribers are clients that have subscribed to the broker server to receive messages.<\/p>\n<p style=\"text-align: justify\">The MQTT client can encompass a wide range of devices, from small microcontrollers to large servers capable of handling the TCP\/IP stack required for network communication with the MQTT server. For instance, an MQTT client could be a resource-constrained device connected via a wireless network, running minimal code. Since MQTT is built on top of TCP\/IP, both the client and the broker must support this network stack.<\/p>\n<p style=\"text-align: justify\">Implementing the MQTT protocol in a client is relatively straightforward and efficient compared to other protocols like HTTP, requiring minimal resources. Therefore, MQTT is particularly suitable for IoT solutions with limited resources. Numerous programming languages, such as Python, Java, JavaScript, and C\/C++, have developed libraries to support MQTT. The MQTT wiki <span style=\"color: #3366ff\"><span style=\"color: #000000\"><a class=\"footnote\" title=\"https:\/\/thingsboard.io\/docs\/iot-gateway\/config\/mqtt\/\" id=\"return-footnote-28-5\" href=\"#footnote-28-5\" aria-label=\"Footnote 5\"><sup class=\"footnote\">[5]<\/sup><\/a><\/span><\/span> provides a comprehensive list of these libraries, enabling developers to leverage MQTT in their preferred programming languages.<\/p>\n<h2 style=\"text-align: justify\"><span style=\"color: #000000\"><strong>2.\u00a0\u00a0\u00a0 MQTT Broker<\/strong><\/span><\/h2>\n<p style=\"text-align: justify\">In the MQTT protocol, the broker plays a central role in facilitating publish\/subscribe communication. A single broker has the capability to handle a large number of connected MQTT clients simultaneously. In the MQTT architecture, clients do not directly communicate with each other but instead communicate through the broker. The broker performs several essential functions, including:<\/p>\n<ul>\n<li style=\"text-align: justify\">Message Acquisition: The broker receives and collects all published messages from the publishing clients.<\/li>\n<li style=\"text-align: justify\">Session Management: The broker manages session data for each connected client, maintaining information about client subscriptions and message delivery.<\/li>\n<li style=\"text-align: justify\">Message Filtering: The broker filters incoming messages based on their topic and determines the subscribers for each message.<\/li>\n<li style=\"text-align: justify\">Message Distribution: The broker distributes the published messages to the appropriate subscribing clients.<\/li>\n<li style=\"text-align: justify\">Authentication and Authorization: The broker is responsible for authenticating and authorizing clients. This is particularly critical in MQTT since data is transmitted over public networks like the Internet. Each client is assigned an access token to ensure secure communication with the broker.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">The broker can be implemented as a server hosted in a cloud platform, providing scalable and reliable MQTT communication infrastructure. Alternatively, the broker can also function as an intermediate gateway that facilitates communication between different devices, allowing for seamless connectivity across diverse IoT devices and networks.<\/p>\n<figure id=\"attachment_491\" aria-describedby=\"caption-attachment-491\" style=\"width: 939px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-491\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/2-2.gif\" alt=\"\" width=\"939\" height=\"464\" \/><figcaption id=\"caption-attachment-491\" class=\"wp-caption-text\"><span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 13. Different types of MQTT brokers<\/em><\/span><\/figcaption><\/figure>\n<h1><\/h1>\n<h1><strong><span style=\"color: #3366ff\">3.2\u00a0 Telemetry Data<\/span><\/strong><\/h1>\n<p style=\"text-align: justify\">In the TB platform, a comprehensive range of features is available to handle telemetry data published from devices with various types and attributes. Telemetry data in TB is represented in JavaScript Object Notation (JSON) format and can be published or subscribed to by clients. A typical example of telemetry data in TB would be a list of key-value pairs, structured as follows:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-205 alignleft\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u0635\u0648\u062a\u06cc-\u0646\u0641\u06cc\u0633-1.gif\" alt=\"\" width=\"345\" height=\"180\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The JSON format allows for organizing and structuring the data in a readable and standardized manner. Each data point can be associated with a timestamp, capturing the time of its occurrence or recording. This timestamp provides valuable temporal information and enhances the context and usefulness of the telemetry data. Whether it is the value of a variable, the status of a switch, or any other relevant information, the inclusion of timestamps enables efficient time-series analysis and facilitates tracking data trends and patterns over time. Throughout the rest of the chapter, we will utilize this JSON formatting in Python to transmit telemetry data to TB, enabling seamless integration and communication between the Python code and the TB platform.<\/p>\n<h1 style=\"text-align: justify\"><strong><span style=\"color: #3366ff\">3.3\u00a0 \u00a0The Python Code<\/span><\/strong><\/h1>\n<p style=\"text-align: justify\">To send telemetry data to TB using Python programming, you need to publish the data in JSON format.<\/p>\n<p><strong>Opening the MQTT port<\/strong><\/p>\n<p>Before starting the coding process, it is important to check the client port used for communication with TB over MQTT. The default port for MQTT communication is 1883. To check if this port is open, you can follow these steps:<\/p>\n<p>1- Open the Command Prompt (cmd) on your Windows PC or laptop with administrative privileges.<\/p>\n<p>2- Enter the command &#8220;netstat -a&#8221; and press Enter.<\/p>\n<p>3- Look for the printed results to find the open ports.<\/p>\n<p>4- Check if port 1883 is listed among the open ports. If it is listed, it means that the port is open and available for MQTT communication with TB.<\/p>\n<figure id=\"attachment_207\" aria-describedby=\"caption-attachment-207\" style=\"width: 906px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-207\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644.jpg\" alt=\"\" width=\"906\" height=\"279\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644.jpg 906w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644-300x92.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644-768x237.jpg 768w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644-65x20.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644-225x69.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/\u067e\u0627\u0631\u0633-\u0641\u0627\u0646\u0627\u0644-350x108.jpg 350w\" sizes=\"auto, (max-width: 906px) 100vw, 906px\" \/><figcaption id=\"caption-attachment-207\" class=\"wp-caption-text\"><em><span style=\"color: #3366ff\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Figure 14. List of open ports<\/span><\/em><\/figcaption><\/figure>\n<p style=\"text-align: justify\">5- If you come across a closed port that isn&#8217;t included in the list, it&#8217;s necessary to open it. To do so, follow these steps: Go to &#8220;Windows Defender Firewall&#8221; by entering the &#8220;firewall.cpl&#8221; command in the Run command window. In the opened window, click on &#8220;Advanced settings&#8221; on the left side. From the options on the left, select &#8220;Inbound Rules&#8221;. On the right side, choose &#8220;New Rule&#8221;. Finally, proceed with the remaining steps in order to successfully open the port.<\/p>\n<p>After opening the &#8220;New Inbound Rule&#8221; window, follow these steps:<\/p>\n<ul>\n<li>Choose &#8220;Port&#8221; and click &#8220;Next&#8221;.<\/li>\n<li>Select &#8220;TCP&#8221; and enter &#8220;1883&#8221; in the &#8220;Specific local ports&#8221; field. Click &#8220;Next&#8221;.<\/li>\n<li>Allow the connection by selecting &#8220;Allow the connection&#8221; and click &#8220;Next&#8221;.<\/li>\n<li>Check all the boxes for &#8220;Domain&#8221;, &#8220;Private&#8221;, and &#8220;Public&#8221; profiles, and click &#8220;Next&#8221;.<\/li>\n<li>In the &#8220;Name&#8221; field, enter &#8220;MQTT Service Port&#8221; and click &#8220;Finish&#8221;.<\/li>\n<li>Once completed, you will find port 1883 listed in the main ports as described in Step 4. You can refer to figure below for a visual representation of this step.<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-258 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13.jpg\" alt=\"\" width=\"504\" height=\"295\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13.jpg 504w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13-300x176.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13-65x38.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13-225x132.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-13-350x205.jpg 350w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-261 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12.jpg\" alt=\"\" width=\"638\" height=\"299\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12.jpg 638w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12-300x141.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12-65x30.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12-225x105.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mx-12-350x164.jpg 350w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-238 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2.jpg\" alt=\"\" width=\"939\" height=\"405\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2.jpg 939w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2-300x129.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2-768x331.jpg 768w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2-65x28.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2-225x97.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-2-350x151.jpg 350w\" sizes=\"auto, (max-width: 939px) 100vw, 939px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-251 size-full aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11.jpg\" alt=\"\" width=\"614\" height=\"448\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11.jpg 614w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11-300x219.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11-65x47.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11-225x164.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-11-350x255.jpg 350w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-249 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9.jpg\" alt=\"\" width=\"573\" height=\"449\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9.jpg 573w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9-300x235.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9-65x51.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9-225x176.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-9-350x274.jpg 350w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-250 aligncenter\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10.jpg\" alt=\"\" width=\"592\" height=\"447\" srcset=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10.jpg 592w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10-300x227.jpg 300w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10-65x49.jpg 65w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10-225x170.jpg 225w, https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/77ui_2_2156-10-350x264.jpg 350w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p><span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 15. Opening the MQTT client port in Windows<\/em><\/span><\/p>\n<p><strong>Publishing Data to TB<\/strong><\/p>\n<p style=\"text-align: justify\">1- To install the Paho MQTT library in Python, assuming you have Python already installed on your client machine, open a command prompt or terminal window and execute the command &#8220;pip install paho-mqtt&#8221;. This will download and install the Paho MQTT library along with its dependencies. Once the installation is complete, you&#8217;ll be able to utilize the library in your Python projects.<\/p>\n<p>pip install paho_mqtt<\/p>\n<p style=\"text-align: left\">2- Import the following required libraries into your code:<\/p>\n<div>\n<div>import paho.mqtt.client as paho \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #mqtt library<\/div>\n<div>import os<\/div>\n<div>import json<\/div>\n<div>import time<\/div>\n<div>from datetime import datetime<\/div>\n<\/div>\n<p style=\"text-align: justify\">3- To define the \u201c<strong>ACCESS_TOKEN<\/strong><strong>\u201d<\/strong> variable along with the address of the broker and the client port, you should include the following lines in your code:<\/p>\n<div>\n<div>ACCESS_TOKEN=&#8217;my18rTF09PA4WgKIfAtf&#8217; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #Token of your device<\/div>\n<div>broker=&#8221;thingsboard.cloud&#8221; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#host name<\/div>\n<div>port=1883 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #data listening port<\/div>\n<\/div>\n<p style=\"text-align: justify\">Replace &#8220;<strong>your_access_token<\/strong>&#8221; with the actual value of the access token derived from the TB platform, as it was shown in Figure 6. Similarly, replace &#8220;<strong>broker_address<\/strong>&#8221; with the address of your MQTT broker which in our case is \u201c<strong>thingsboard.cloud<\/strong>\u201d. Assign the appropriate port number (i.e., 1883) to &#8220;<strong>broker_port<\/strong>&#8220;. By defining these variables, you will have the necessary information to establish a connection between your code and the MQTT broker.<\/p>\n<p style=\"text-align: justify\">4- To define the callback method for publishing data and handle the response when telemetry data is successfully published to ThingsBoard (TB), you can include the following code snippet:<\/p>\n<div>\n<div>def on_publish(client,userdata,result): \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #create function for callback<\/div>\n<div>\u00a0 \u00a0 print(&#8220;data published to thingsboard \\n&#8221;)<\/div>\n<div>\u00a0 \u00a0 pass<\/div>\n<\/div>\n<p style=\"text-align: justify\">5- Define a client object in your code and configure its parameters:<\/p>\n<div>client1= paho.Client(&#8220;control1&#8221;) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#create client object<\/div>\n<div>client1.on_publish = on_publish \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #assign function to callback<\/div>\n<div>client1.username_pw_set(ACCESS_TOKEN) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #access token from thingsboard device<\/div>\n<div>client1.connect(broker,port,keepalive=60) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #establish connection<\/div>\n<p style=\"text-align: justify\">6- Publish sample telemetry data that is written in JSON format in an infinite loop:<\/p>\n<div>\n<div>while True:<\/div>\n<div>\u00a0 \u00a0 # {&#8216;Humidity&#8221;: + &#8220;60&#8221;; \u00a0&#8220;Temperature&#8221;: &#8220;10&#8221;}<\/div>\n<div>\u00a0 \u00a0payload=&#8221;{&#8220;<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;\\&#8221;Humidity\\&#8221;:&#8221; + str(hmnt) + &#8220;,&#8221;<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;\\&#8221;temperature\\&#8221;:&#8221; + str(temp)<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;}&#8221;<\/div>\n<div>\u00a0 \u00a0ret= client1.publish(&#8220;v1\/devices\/me\/telemetry&#8221;,payload) #topic-v1\/devices\/me\/telemetry<\/div>\n<div>\u00a0 \u00a0print(&#8220;Please check LATEST TELEMETRY field of your device&#8221;)<\/div>\n<div>\u00a0 \u00a0print(payload);<\/div>\n<div>\u00a0 \u00a0time.sleep(5)<\/div>\n<\/div>\n<p>In this step, you can put your telemetry data, i.e., <strong><span style=\"color: #008080\">Humidity <span style=\"color: #000000\">=<\/span> 55 <\/span><\/strong>(%) and <span style=\"color: #008080\"><strong>Temperature<\/strong><\/span> = <span style=\"color: #008080\"><strong>24<\/strong><\/span> (0C), in a variable named payload, publishing it to the following topic: \u201c<span style=\"color: #008080\"><strong>v1\/devices\/me\/telemetry<\/strong><\/span>\u201d.<\/p>\n<p style=\"text-align: justify\">7- Aggregating the aforementioned Python scripts, the final code will be as follow. Running this code will publish the telemetry data every five seconds.<\/p>\n<div>\n<div>import paho.mqtt.client as paho \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #mqtt library<\/div>\n<div>import os<\/div>\n<div>import json<\/div>\n<div>import time<\/div>\n<div>from datetime import datetime<\/div>\n<div>ACCESS_TOKEN=&#8217;my18rTF09PA4WgKIfAtf&#8217; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #Token of your device<\/div>\n<div>broker=&#8221;thingsboard.cloud&#8221; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#host name<\/div>\n<div>port=1883 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #data listening port<\/div>\n<div>def on_publish(client,userdata,result): \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #create function for callback<\/div>\n<div>\u00a0 \u00a0 print(&#8220;data published to thingsboard \\n&#8221;)<\/div>\n<div>\u00a0 \u00a0 pass<\/div>\n<div>client1= paho.Client(&#8220;control1&#8221;) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0#create client object<\/div>\n<div>client1.on_publish = on_publish \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #assign function to callback<\/div>\n<div>client1.username_pw_set(ACCESS_TOKEN) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #access token from thingsboard device<\/div>\n<div>client1.connect(broker,port,keepalive=60) \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 #establish connection<\/div>\n<div>temp = 86<\/div>\n<div>hmnt = 40<\/div>\n<div>while True:<\/div>\n<div>\u00a0 \u00a0 # {&#8216;Humidity&#8221;: + &#8220;60&#8221;; \u00a0&#8220;Temperature&#8221;: &#8220;10&#8221;}<\/div>\n<div>\u00a0 \u00a0payload=&#8221;{&#8220;<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;\\&#8221;Humidity\\&#8221;:&#8221; + str(hmnt) + &#8220;,&#8221;<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;\\&#8221;temperature\\&#8221;:&#8221; + str(temp)<\/div>\n<div>\u00a0 \u00a0payload+=&#8221;}&#8221;<\/div>\n<div>\u00a0 \u00a0ret= client1.publish(&#8220;v1\/devices\/me\/telemetry&#8221;,payload) #topic-v1\/devices\/me\/telemetry<\/div>\n<div>\u00a0 \u00a0print(&#8220;Please check LATEST TELEMETRY field of your device&#8221;)<\/div>\n<div>\u00a0 \u00a0print(payload);<\/div>\n<div>\u00a0 \u00a0time.sleep(5)<\/div>\n<div><\/div>\n<\/div>\n<p style=\"text-align: justify\">8- To view the latest telemetry data of defined devices in your TB account, navigate to the &#8220;Latest Telemetry&#8221; section. This section provides you with the most recent telemetry data sent by your devices, allowing you to monitor the values of attributes or parameters being tracked.<\/p>\n<figure id=\"attachment_231\" aria-describedby=\"caption-attachment-231\" style=\"width: 821px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-231 size-full\" src=\"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-content\/uploads\/sites\/1962\/2023\/06\/432mi2_1818.gif\" alt=\"\" width=\"821\" height=\"475\" \/><figcaption id=\"caption-attachment-231\" class=\"wp-caption-text\"><span style=\"color: #3366ff\"><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Figure 16. Latest telemetry data<\/em><\/span><\/figcaption><\/figure>\n<hr class=\"before-footnotes clear\" \/><div class=\"footnotes\"><ol><li id=\"footnote-28-1\">https:\/\/thingsboard.io\/docs\/samples\/ <a href=\"#return-footnote-28-1\" class=\"return-footnote\" aria-label=\"Return to footnote 1\">&crarr;<\/a><\/li><li id=\"footnote-28-2\">https:\/\/thingsboard.io\/docs\/samples\/ <a href=\"#return-footnote-28-2\" class=\"return-footnote\" aria-label=\"Return to footnote 2\">&crarr;<\/a><\/li><li id=\"footnote-28-3\">https:\/\/thingsboard.io\/docs\/samples\/esp8266\/ <a href=\"#return-footnote-28-3\" class=\"return-footnote\" aria-label=\"Return to footnote 3\">&crarr;<\/a><\/li><li id=\"footnote-28-4\">https:\/\/thingsboard.io\/docs\/user-guide\/integrations\/send-data-external-mqtt-brokers\/ <a href=\"#return-footnote-28-4\" class=\"return-footnote\" aria-label=\"Return to footnote 4\">&crarr;<\/a><\/li><li id=\"footnote-28-5\">https:\/\/thingsboard.io\/docs\/iot-gateway\/config\/mqtt\/ <a href=\"#return-footnote-28-5\" class=\"return-footnote\" aria-label=\"Return to footnote 5\">&crarr;<\/a><\/li><\/ol><\/div>","protected":false},"author":1053,"menu_order":3,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-28","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapters\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/wp\/v2\/users\/1053"}],"version-history":[{"count":26,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapters\/28\/revisions"}],"predecessor-version":[{"id":517,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapters\/28\/revisions\/517"}],"part":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapters\/28\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/pressbooks\/v2\/chapter-type?post=28"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/wp\/v2\/contributor?post=28"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.bccampus.ca\/iotplatform\/wp-json\/wp\/v2\/license?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}