Open main menu

DAVE Developer's Wiki β

Changes

Enabling the connection to ThingsBoard IoT platform
=== Enabling the connection to ThingsBoard IoT platform ===
In order to upload collected data to ThingsBoard platform, a simple North plugin was developed. To this end, the Microsoft Azure plugin was used as reference. The following box shows the code. The ThingsBoard South plugin is minimalist and unfit for a real-world production environment. Nevertheless, it was enough to perform the basic tests described in this TN. Before testing this new plugin, the mosquitto-clients package was installed as the plugin invokes the <code>mosquitto_pub</code> command line tool.  
<pre class="board-terminal">
root@sbcspg:/usr/local/fledge/python/fledge/plugins/north# apt install mosquitto mosquitto-clients
Reading package lists... Done
Created symlink /etc/systemd/system/multi-user.target.wants/mosquitto.service → /lib/systemd/system/mosquitto.service.
Processing triggers for libc-bin (2.31-13+deb11u5) ...
</pre>
 
The following box shows the code of the ThingsBoard plugin.
 
<syntaxhighlight lang="python" class"mw-collapsible mw-collapsed">
# -*- coding: utf-8 -*-
 
""" ThingsBoard North plugin"""
import asyncio
import time
import json
import sys
import os
 
 
 
from fledge.common import logger
from fledge.plugins.north.common.common import *
 
__author__ = "Andrea Marson"
__copyright__ = "Copyright (c) 2023 DAVE Srl"
__license__ = "Apache 2.0"
__version__ = "${VERSION}"
 
_LOGGER = logger.setup(__name__)
 
_CONFIG_CATEGORY_NAME = "THINGSBOARD"
_CONFIG_CATEGORY_DESCRIPTION = "ThingsBoard Python North Plugin"
 
_DEFAULT_CONFIG = {
'plugin': {
'description': 'ThingsBoard North Plugin',
'type': 'string',
'default': 'thingsboard',
'readonly': 'true'
},
"tb_iot_platform_host": {
"description": "ThingsBoard IoT platform host.",
"type": "string",
"default": "put the public name of your host here",
"order": "2",
"displayName": "ThingsBoard IoT platform host"
},
"tb_mqtt_topic": {
"description": "MQTT topic",
"type": "string",
"default": "v1/devices/me/telemetry",
"order": "3",
"displayName": "MQTT topic",
'readonly': 'true'
},
"tb_device_access_token": {
"description": "Device's access token.",
"type": "string",
"default": "put your device's token here",
"order": "4",
"displayName": "Device's access token"
},
"source": {
"description": "Source of data to be sent on the stream.",
"type": "enumeration",
"default": "readings",
"options": ["readings"],
"order": "5",
"displayName": "Source",
'readonly': 'true'
}
}
 
def plugin_info():
return {
'name': 'thingsboard',
'version': '2.1.0',
'type': 'north',
'interface': '1.0',
'config': _DEFAULT_CONFIG
}
 
def plugin_init(data):
_LOGGER.info('Initializing ThingsBoard North Python Plugin')
global thingsboard_north, config
thingsboard_north = ThingsBoardNorthPlugin()
config = data
_LOGGER.info(f'config = {config}')
return config
 
"""
Example of payload
 
[{'reading': {'base_joint_temperature_deg_C': 34, 'base_joint_current_mA': 2042}, 'asset_code': 'Modbus TCP', 'id': 35613, 'ts': '2023-02-22 16:33:49.961000+00:00', 'user_ts': '2023-02-22 16:33:47.126849+00:00'}, {'reading': {'base_joint_temperature_deg_C': 34, 'base_joint_current_mA': 2045}, 'asset_code': 'Modbus TCP', 'id': 35614, 'ts': '2023-02-22 16:33:49.961000+00:00', 'user_ts': '2023-02-22 16:33:48.126817+00:00'}, {'reading': {'base_joint_temperature_deg_C': 34, 'base_joint_current_mA': 2060}, 'asset_code': 'Modbus TCP', 'id': 35615, 'ts': '2023-02-22 16:33:49.961000+00:00', 'user_ts': '2023-02-22 16:33:49.126827+00:00'}]
 
Example of upload
 
mosquitto_pub -d -q 1 -h "$THINGSBOARD_HOST_NAME" -t "v1/devices/me/telemetry" -u "$ACCESS_TOKEN" -m '{"base_joint_temperature_deg_C":33}'
"""
async def plugin_send(data, payload, stream_id):
try:
_LOGGER.info(f'ThingsBoard North Python - plugin_send: {stream_id}')
_LOGGER.info(f'data = {data}')
_LOGGER.info(f'payload = {payload}')
_LOGGER.info(f'host = {data["tb_iot_platform_host"]["value"]}')
for reading in payload:
_LOGGER.info(f'reading = {reading}')
_LOGGER.info(f'reading[reading] = {reading["reading"]}')
s = 'mosquitto_pub -d -q 1 -h "' + data["tb_iot_platform_host"]["value"] + '" -t "' + data["tb_mqtt_topic"]["value"] + '" -u "' + data["tb_device_access_token"]["value"] + '" -m \'' + str(reading["reading"]) + "\'"
_LOGGER.info(f's = {s}')
os.system(s)
except asyncio.CancelledError as ex:
_LOGGER.exception(f'Exception occurred in plugin_send: {ex}')
else:
_LOGGER.info('payload sent successfully')
return True, payload[len(payload-1)]["id"], len(payload)
 
def plugin_shutdown(data):
pass
 
# TODO: North plugin can not be reconfigured? (per callback mechanism)
def plugin_reconfigure():
pass
 
class ThingsBoardNorthPlugin(object):
""" North ThingsBoard Plugin """
</syntaxhighlight>
4,650
edits