|
| 1 | +# Copyright 2025 ThingsBoard |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# This example demonstrates how to connect to ThingsBoard over SSL using the GatewayClient, |
| 16 | +# connect a device, and send telemetry data securely. |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import logging |
| 20 | +import random |
| 21 | + |
| 22 | +from tb_mqtt_client.common.config_loader import GatewayConfig |
| 23 | +from tb_mqtt_client.common.logging_utils import configure_logging, get_logger |
| 24 | +from tb_mqtt_client.entities.data.timeseries_entry import TimeseriesEntry |
| 25 | +from tb_mqtt_client.service.gateway.client import GatewayClient |
| 26 | + |
| 27 | +configure_logging() |
| 28 | +logger = get_logger(__name__) |
| 29 | +logger.setLevel(logging.INFO) |
| 30 | +logging.getLogger("tb_mqtt_client").setLevel(logging.INFO) |
| 31 | + |
| 32 | + |
| 33 | +PLATFORM_HOST = 'localhost' # Update with your ThingsBoard host |
| 34 | +PLATFORM_PORT = 8883 # Default port for MQTT over SSL |
| 35 | + |
| 36 | + |
| 37 | +# Update with your CA certificate, client certificate, and client key paths. There are no default files generated. |
| 38 | +# You can generate them using the following guides: |
| 39 | +# Certificates for server - https://thingsboard.io/docs/user-guide/mqtt-over-ssl/ |
| 40 | +# Certificates for client - https://thingsboard.io/docs/user-guide/certificates/?ubuntuThingsboardX509=X509Leaf |
| 41 | +CA_CERT_PATH = "mqttserver.pem" # Update with your CA certificate path (Default - mqttserver.pem in the examples directory) |
| 42 | +CLIENT_CERT_PATH = "cert.pem" # Update with your client certificate path (Default - cert.pem in the examples directory) |
| 43 | +CLIENT_KEY_PATH = "key.pem" # Update with your client key path (Default - key.pem in the examples directory) |
| 44 | + |
| 45 | + |
| 46 | +async def main(): |
| 47 | + config = GatewayConfig() |
| 48 | + |
| 49 | + config.host = PLATFORM_HOST |
| 50 | + config.port = PLATFORM_PORT |
| 51 | + |
| 52 | + config.ca_cert = CA_CERT_PATH |
| 53 | + config.client_cert = CLIENT_CERT_PATH |
| 54 | + config.private_key = CLIENT_KEY_PATH |
| 55 | + |
| 56 | + client = GatewayClient(config) |
| 57 | + await client.connect() |
| 58 | + |
| 59 | + device_name = "Test Device B1" |
| 60 | + device_profile = "Test devices" |
| 61 | + logger.info("Connecting device: %s", device_name) |
| 62 | + device_session, publish_results = await client.connect_device(device_name, device_profile, wait_for_publish=True) |
| 63 | + |
| 64 | + # Sending telemetry data to the connected device |
| 65 | + list_timeseries = [ |
| 66 | + TimeseriesEntry(key="temperature", value=random.randint(20, 35)), |
| 67 | + TimeseriesEntry(key="humidity", value=random.randint(40, 80)) |
| 68 | + ] |
| 69 | + logger.info("Sending list of timeseries: %s", list_timeseries) |
| 70 | + await client.send_device_timeseries(device_session=device_session, data=list_timeseries, wait_for_publish=True) |
| 71 | + logger.info("List of timeseries sent successfully.") |
| 72 | + |
| 73 | + await client.stop() |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + asyncio.run(main()) |
0 commit comments