#Controller device client = TBDeviceMqttClient(THINGSBOARD_HOST, ACCESS_TOKEN) #Call back def on_attribute_change(client, result, exception): print(client) # get client key & values and print it # junction1_result will be the value clientObj = result["client"] junction1_result = clientObj["junction1"] print(junction1_result) if exception is not None: print("Exception: " + str(exception)) else: print(result) client.connect() #client.request_attributes(["junction1", "junction2"], callback=on_attribute_change) #Poll for changes while True: client.request_attributes([ "Edge1_Len", "Edge1_Straight", "Edge1_Right", "Edge2_Len", "Edge2_Straight", "Edge2_Right" ], callback=on_attribute_change) time.sleep(5)
def main(): client = TBDeviceMqttClient("127.0.0.1", "A2_TEST_TOKEN") client.connect() client.request_attributes(["atr1", "atr2"], callback=on_attributes_change) while not client.stopped: time.sleep(1)
import logging import time from tb_device_mqtt import TBDeviceMqttClient logging.basicConfig(level=logging.DEBUG) def on_attributes_change(result, exception): if exception is not None: print("Exception: " + str(exception)) else: print(result) client = TBDeviceMqttClient("127.0.0.1", "A2_TEST_TOKEN") client.connect() client.request_attributes(["atr1", "atr2"], callback=on_attributes_change) while True: time.sleep(1)
# dependently of request method we send different data back def on_server_side_rpc_request(client, request_id, request_body): print(client, request_id, request_body) if request_body["method"] == "getCPULoad": client.send_rpc_reply(request_id, {"CPU percent": psutil.cpu_percent()}) elif request_body["method"] == "getMemoryUsage": client.send_rpc_reply(request_id, {"Memory": psutil.virtual_memory().percent}) client = TBDeviceMqttClient("127.0.0.1", "A2_TEST_TOKEN") client.set_server_side_rpc_request_handler(on_server_side_rpc_request) client.connect() # to fetch the latest setting for upload frequency configured on the server client.request_attributes(shared_keys=["uploadFrequency"], callback=on_upload_frequency_change) # to subscribe to future changes of upload frequency client.subscribe_to_attribute(key="uploadFrequency", callback=on_upload_frequency_change) def main(): while True: client.send_telemetry({ "cpu": psutil.cpu_percent(), "memory": psutil.virtual_memory().percent }) print("Sleeping for " + str(uploadFrequency)) time.sleep(uploadFrequency)