-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrequest_attributes.py
More file actions
65 lines (49 loc) · 1.94 KB
/
request_attributes.py
File metadata and controls
65 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
This sketch demonstrates connecting and retrieving attributes using ThingsBoard SDK
"""
import time
import network
from thingsboard_sdk.tb_device_mqtt import TBDeviceMqttClient
WIFI_SSID = "YOUR_SSID"
WIFI_PASSWORD = "YOUR_PASSWORD"
# Thingsboard we want to establish a connection to
THINGSBOARD_HOST = "thingsboard.cloud"
# MQTT port used to communicate with the server, 1883 is the default unencrypted MQTT port,
# whereas 8883 would be the default encrypted SSL MQTT port
THINGSBOARD_PORT = 1883
# See https://thingsboard.io/docs/getting-started-guides/helloworld/
# to understand how to obtain an access token
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
# Enabling WLAN interface
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Establishing connection to the Wi-Fi
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
while not wlan.isconnected():
pass
print('Connected! Network config:', wlan.ifconfig())
# Flag to indicate that we received the attributes from the server, so we can exit the loop and disconnect
IS_ATTR_RECEIVED = False
def on_attributes_change(result, exception=None):
# This is a callback function that will be called when we receive the response from the server
global IS_ATTR_RECEIVED
if exception is not None:
print("Exception: " + str(exception))
else:
print(result)
IS_ATTR_RECEIVED = True
# Initialising client to communicate with ThingsBoard
client = TBDeviceMqttClient(host=THINGSBOARD_HOST, port=THINGSBOARD_PORT, access_token=ACCESS_TOKEN)
# Connect to ThingsBoard
client.connect()
# Sending data to retrieve it later
client.send_attributes({"atr1": "value1", "atr2": "value2"})
# Requesting attributes
client.request_attributes(["atr1", "atr2"], callback=on_attributes_change)
# Wait until we receive the attributes from the server
while not IS_ATTR_RECEIVED:
time.sleep(1)
# Disconnect from ThingsBoard
client.disconnect()