Skip to content

Commit 301f8dc

Browse files
authored
Added device claiming example (#6)
* add device claiming example * update device claiming example
1 parent 357e33e commit 301f8dc

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2026 ThingsBoard Inc.
3+
#
4+
# SPDX-License-Identifier: Unlicense
5+
6+
import time
7+
8+
import wifi # CircuitPython Wi-Fi module
9+
10+
from tb_device_mqtt import TBDeviceMqttClient # ThingsBoard MQTT client wrapper (your SDK)
11+
12+
# Quick sanity-check that Wi-Fi is up before using MQTT
13+
print("WiFi connected:", wifi.radio.connected)
14+
print("IP:", wifi.radio.ipv4_address)
15+
16+
# ThingsBoard connection settings
17+
HOST = "thingsboard.cloud" # or your local TB host/IP
18+
PORT = 1883 # standard MQTT port (non-TLS)
19+
TOKEN = "YOUR_ACCESS_TOKEN" # device access token from ThingsBoard
20+
21+
# Claiming settings
22+
SECRET_KEY = "DEVICE_SECRET_KEY" # key configured in the claiming widget
23+
DURATION_MS = 30000 # how long the claim request is valid (ms)
24+
25+
client = None
26+
27+
try:
28+
# Create ThingsBoard MQTT client
29+
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
30+
31+
print("Connecting to ThingsBoard...")
32+
client.connect()
33+
print("Connected to ThingsBoard")
34+
35+
# Send claiming request
36+
print("Sending claiming request...")
37+
client.claim_device(secret_key=SECRET_KEY, duration_ms=DURATION_MS)
38+
print("Claiming request was sent")
39+
40+
# Keep the script alive for the claiming window
41+
# (so the device stays online while the claim is performed)
42+
time.sleep(DURATION_MS)
43+
44+
except Exception as e:
45+
print("Failed to execute device claiming:", e)
46+
47+
finally:
48+
# Disconnect cleanly
49+
if client is not None:
50+
try:
51+
client.disconnect()
52+
except Exception as e:
53+
print("Disconnect failed:", e)
54+
print("Connection closed")

0 commit comments

Comments
 (0)