Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/attribute_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2026 ThingsBoard Inc.
#
# SPDX-License-Identifier: Unlicense

import time

import wifi # CircuitPython Wi-Fi module

from tb_device_mqtt import TBDeviceMqttClient # ThingsBoard MQTT client wrapper (your SDK)

# Sanity check: Wi-Fi must be connected before MQTT
print("WiFi connected:", wifi.radio.connected)
print("IP:", wifi.radio.ipv4_address)

# ThingsBoard connection settings
HOST = "YOUR_HOST" # e.g. "thingsboard.cloud" or "192.168.1.10"
PORT = 1883 # standard MQTT port (non-TLS)
TOKEN = "YOUR_ACCESS_TOKEN" # device access token from ThingsBoard
TIMEOUT = 20 # how long we keep pumping MQTT loop (seconds)

# Create and connect MQTT client
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
client.connect() # establishes MQTT session + subscriptions inside your SDK (if implemented)


def on_attributes_change(result, exception=None):
# Callback is called when the attributes response arrives
if exception is not None:
print("Exception:", exception)
else:
print("Attributes response:", result)


# Request client/shared attributes by keys (your SDK forms attributes/request/<id>)
client.request_attributes(client_keys=["atr1", "atr2"], callback=on_attributes_change)

# IMPORTANT: CircuitPython needs a loop to receive/process MQTT packets
deadline = time.monotonic() + TIMEOUT
while time.monotonic() < deadline:
client.check_for_msg() # wraps MiniMQTT.loop() -> triggers callbacks
time.sleep(0.05) # small sleep to reduce CPU usage
40 changes: 40 additions & 0 deletions examples/attribute_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2026 ThingsBoard Inc.
#
# SPDX-License-Identifier: Unlicense

import time

import wifi # CircuitPython Wi-Fi module

from tb_device_mqtt import TBDeviceMqttClient # ThingsBoard MQTT client wrapper (your SDK)

# Sanity check: Wi-Fi must be connected before MQTT
print("Connected:", wifi.radio.connected)
print("IP:", wifi.radio.ipv4_address)

# ThingsBoard connection settings
HOST = "YOUR_HOST" # e.g. "thingsboard.cloud" or "192.168.1.10"
PORT = 1883 # standard MQTT port (non-TLS)
TOKEN = "YOUR_ACCESS_TOKEN" # device access token from ThingsBoard
TIMEOUT = 20 # how long we keep pumping MQTT loop (seconds)

# Create and connect MQTT client
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)
client.connect()


def callback(result, *args): # noqa: F841
# Called when subscribed attribute update arrives
# (extra args may contain metadata depending on your SDK design)
print("Received data:", result)


# Subscribe to updates of a single attribute key (e.g. shared attribute "frequency")
sub_id = client.subscribe_to_attribute("frequency", callback) # returns subscription id (optional)

# IMPORTANT: keep looping so incoming MQTT messages are processed
deadline = time.monotonic() + TIMEOUT
while time.monotonic() < deadline:
client.check_for_msg() # wraps MiniMQTT.loop() -> triggers callbacks
time.sleep(0.05) # small sleep to reduce CPU usage
5 changes: 1 addition & 4 deletions examples/rpc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
RPC_METHODS = ("Pwd", "Ls")


# Create MQTT client instance
client = TBDeviceMqttClient(host=HOST, port=PORT, access_token=TOKEN)


def on_server_side_rpc_request(request_id, request_body):
# request_id: numeric id from the MQTT topic
# request_body: decoded JSON dict, typically {"method": "...", "params": ...}
Expand Down Expand Up @@ -65,6 +61,7 @@ def on_server_side_rpc_request(request_id, request_body):
client.send_rpc_reply(request_id, reply)


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