Skip to content

Commit bdfcee0

Browse files
authored
Merge pull request #95 from samson0v/develop-2.0
Added provisioning and firmware update examples
2 parents f2d977e + f1209fe commit bdfcee0

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
# Example script to device provisioning using the DeviceClient
16+
17+
import asyncio
18+
from random import randint
19+
20+
from tb_mqtt_client.entities.data.provisioning_request import AccessTokenProvisioningCredentials, ProvisioningRequest
21+
from tb_mqtt_client.entities.data.timeseries_entry import TimeseriesEntry
22+
from tb_mqtt_client.service.device.client import DeviceClient
23+
24+
25+
async def main():
26+
provisioning_credentials = AccessTokenProvisioningCredentials(
27+
provision_device_key='YOUR_PROVISION_DEVICE_KEY',
28+
provision_device_secret='YOUR_PROVISION_DEVICE_SECRET',
29+
)
30+
provisioning_request = ProvisioningRequest('localhost', credentials=provisioning_credentials)
31+
provisioning_response = await DeviceClient.provision(provisioning_request)
32+
33+
if provisioning_response.error is not None:
34+
print(f"Provisioning failed: {provisioning_response.error}")
35+
return
36+
37+
print('Provisined device config: ', provisioning_response)
38+
39+
# Create a DeviceClient instance with the provisioned device config
40+
client = DeviceClient(provisioning_response.result)
41+
await client.connect()
42+
43+
# Send single telemetry entry to provisioned device
44+
await client.send_telemetry(TimeseriesEntry("batteryLevel", randint(0, 100)))
45+
46+
await client.stop()
47+
48+
49+
if __name__ == "__main__":
50+
asyncio.run(main())

examples/device/firmware_update.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
# Example script to update firmware using the DeviceClient
16+
17+
import asyncio
18+
from time import monotonic
19+
20+
from tb_mqtt_client.common.config_loader import DeviceConfig
21+
from tb_mqtt_client.service.device.client import DeviceClient
22+
23+
24+
firmware_received = asyncio.Event()
25+
firmware_update_timeout = 30
26+
27+
28+
async def firmware_update_callback(_, payload):
29+
print(f"Firmware update payload received: {payload}")
30+
firmware_received.set()
31+
32+
33+
async def main():
34+
config = DeviceConfig()
35+
config.host = "localhost"
36+
config.access_token = "YOUR_ACCESS_TOKEN"
37+
38+
client = DeviceClient(config)
39+
await client.connect()
40+
41+
await client.update_firmware(on_received_callback=firmware_update_callback)
42+
43+
update_started = monotonic()
44+
while not firmware_received.is_set() and monotonic() - update_started < firmware_update_timeout:
45+
await asyncio.sleep(1)
46+
47+
await client.stop()
48+
49+
50+
if __name__ == "__main__":
51+
asyncio.run(main())

0 commit comments

Comments
 (0)