Skip to content

Commit 6e2ddbb

Browse files
authored
Added more examples for rpc's usage (#4)
* add usage example for attribute requests * add usage example for attribute update * remove extra client initialization from rpc example * fix ruff formatting * fix formatting * fix formatting * select better variable name for MQTT loop timeout
1 parent 47d6f2c commit 6e2ddbb

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

examples/attribute_requests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
# Sanity check: Wi-Fi must be connected before MQTT
13+
print("WiFi connected:", wifi.radio.connected)
14+
print("IP:", wifi.radio.ipv4_address)
15+
16+
# ThingsBoard connection settings
17+
HOST = "YOUR_HOST" # e.g. "thingsboard.cloud" or "192.168.1.10"
18+
PORT = 1883 # standard MQTT port (non-TLS)
19+
TOKEN = "YOUR_ACCESS_TOKEN" # device access token from ThingsBoard
20+
TIMEOUT = 20 # how long we keep pumping MQTT loop (seconds)
21+
22+
# Create and connect MQTT client
23+
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
24+
client.connect() # establishes MQTT session + subscriptions inside your SDK (if implemented)
25+
26+
27+
def on_attributes_change(result, exception=None):
28+
# Callback is called when the attributes response arrives
29+
if exception is not None:
30+
print("Exception:", exception)
31+
else:
32+
print("Attributes response:", result)
33+
34+
35+
# Request client/shared attributes by keys (your SDK forms attributes/request/<id>)
36+
client.request_attributes(client_keys=["atr1", "atr2"], callback=on_attributes_change)
37+
38+
# IMPORTANT: CircuitPython needs a loop to receive/process MQTT packets
39+
deadline = time.monotonic() + TIMEOUT
40+
while time.monotonic() < deadline:
41+
client.check_for_msg() # wraps MiniMQTT.loop() -> triggers callbacks
42+
time.sleep(0.05) # small sleep to reduce CPU usage

examples/attribute_update.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
# Sanity check: Wi-Fi must be connected before MQTT
13+
print("Connected:", wifi.radio.connected)
14+
print("IP:", wifi.radio.ipv4_address)
15+
16+
# ThingsBoard connection settings
17+
HOST = "YOUR_HOST" # e.g. "thingsboard.cloud" or "192.168.1.10"
18+
PORT = 1883 # standard MQTT port (non-TLS)
19+
TOKEN = "YOUR_ACCESS_TOKEN" # device access token from ThingsBoard
20+
TIMEOUT = 20 # how long we keep pumping MQTT loop (seconds)
21+
22+
# Create and connect MQTT client
23+
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
24+
client.connect()
25+
26+
27+
def callback(result, *args): # noqa: F841
28+
# Called when subscribed attribute update arrives
29+
# (extra args may contain metadata depending on your SDK design)
30+
print("Received data:", result)
31+
32+
33+
# Subscribe to updates of a single attribute key (e.g. shared attribute "frequency")
34+
sub_id = client.subscribe_to_attribute("frequency", callback) # returns subscription id (optional)
35+
36+
# IMPORTANT: keep looping so incoming MQTT messages are processed
37+
deadline = time.monotonic() + TIMEOUT
38+
while time.monotonic() < deadline:
39+
client.check_for_msg() # wraps MiniMQTT.loop() -> triggers callbacks
40+
time.sleep(0.05) # small sleep to reduce CPU usage

examples/rpc_example.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
RPC_METHODS = ("Pwd", "Ls")
2222

2323

24-
# Create MQTT client instance
25-
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
26-
27-
2824
def on_server_side_rpc_request(request_id, request_body):
2925
# request_id: numeric id from the MQTT topic
3026
# request_body: decoded JSON dict, typically {"method": "...", "params": ...}
@@ -65,6 +61,7 @@ def on_server_side_rpc_request(request_id, request_body):
6561
client.send_rpc_reply(request_id, reply)
6662

6763

64+
# Create MQTT client instance
6865
client = TBDeviceMqttClient(HOST, port=PORT, access_token=TOKEN)
6966
# Register the server-side RPC callback before the main loop
7067
client.set_server_side_rpc_request_handler(on_server_side_rpc_request)

0 commit comments

Comments
 (0)