Last active
May 9, 2024 09:15
-
-
Save shiyazt/ea9942d9705948df19d7d13aafab4f04 to your computer and use it in GitHub Desktop.
This Section illustrates the Client Side and Server side RPC (Remote Procedure Call) in ThingsBoard IoT Platform. Client_Side_RPC.py : This program will illustrates the Client side, RPC Server_Side_RPC.py : This Program will illustrates the Server side RPC and Temperature_Controller_Server_Side_RPC.py : This program illustrates Server side RPC u…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Program illustrates the Client Side RPC on ThingsBoard IoT Platform | |
# Paste your ThingsBoard IoT Platform IP and Device access token | |
# Client_Side_RPC.py : This program will illustrates the Client side | |
import os | |
import time | |
import sys | |
import json | |
import random | |
import paho.mqtt.client as mqtt | |
# Thingsboard platform credentials | |
THINGSBOARD_HOST = 'Paste your ThingsBoard IP' | |
ACCESS_TOKEN = 'Paste your Device Access Token Here ' | |
request = {"method": "getemail", "params": ""} | |
# MQTT on_connect callback function | |
def on_connect(client, userdata, flags, rc): | |
print("rc code:", rc) | |
client.subscribe('v1/devices/me/rpc/response/+') | |
client.publish('v1/devices/me/rpc/request/1',json.dumps(request), 1) | |
# MQTT on_message caallback function | |
def on_message(client, userdata, msg): | |
print('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload)) | |
# start the client instance | |
client = mqtt.Client() | |
# registering the callbacks | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.username_pw_set(ACCESS_TOKEN) | |
client.connect(THINGSBOARD_HOST, 1883, 60) | |
try: | |
client.loop_forever() | |
except KeyboardInterrupt: | |
client.disconnect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Program illustrates the Simple Server Side RPC on ThingsBoard IoT Platform | |
# Paste your ThingsBoard IoT Platform IP and Device access token | |
# RPC Server_Side_RPC.py : This Program will illustrates the Server side RPC | |
import os | |
import time | |
import sys | |
import json | |
import random | |
import paho.mqtt.client as mqtt | |
# Thingsboard platform credentials | |
THINGSBOARD_HOST = 'Paste your ThingsBoard IP' | |
ACCESS_TOKEN = 'Paste your Device Access Token Here' | |
button_state = {"enabled": False} | |
def setValue (params): | |
button_state['enabled'] = params | |
print("Rx setValue is : ",button_state) | |
# MQTT on_connect callback function | |
def on_connect(client, userdata, flags, rc): | |
print("rc code:",rc) | |
client.subscribe('v1/devices/me/rpc/request/+') | |
# MQTT on_message caallback function | |
def on_message(client, userdata, msg): | |
print('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload)) | |
if msg.topic.startswith('v1/devices/me/rpc/request/'): | |
requestId = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)] | |
print("requestId : ",requestId) | |
data = json.loads(msg.payload) | |
if data['method'] == 'getValue': | |
print("getvalue request\n") | |
print("sent getValue : ",button_state) | |
client.publish('v1/devices/me/rpc/response/'+requestId, json.dumps(button_state), 1) | |
if data['method'] == 'setValue': | |
print("setvalue request\n") | |
params = data['params'] | |
setValue(params) | |
client.publish('v1/devices/me/attributes', json.dumps(button_state), 1) | |
# start the client instance | |
client = mqtt.Client() | |
# registering the callbacks | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.username_pw_set(ACCESS_TOKEN) | |
client.connect(THINGSBOARD_HOST,1883,60) | |
try: | |
client.loop_forever() | |
except KeyboardInterrupt: | |
client.disconnect() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Program illustrates the Server Side RPC on ThingsBoard IoT Platform | |
# Paste your ThingsBoard IoT Platform IP and Device access token | |
# Temperature_Controller_Server_Side_RPC.py : This program illustrates Server side RPC using a Simulated Temperature Controller | |
import os | |
import time | |
import sys | |
import json | |
import random | |
import paho.mqtt.client as mqtt | |
from threading import Thread | |
# Thingsboard platform credentials | |
THINGSBOARD_HOST = 'Paste your ThingsBoard IP' #Change IP Address | |
ACCESS_TOKEN = 'Paste your ThingsBoard Device Access Token' | |
sensor_data = {'temperature': 25} | |
def publishValue(client): | |
INTERVAL = 2 | |
print("Thread Started") | |
next_reading = time.time() | |
while True: | |
client.publish('v1/devices/me/telemetry', json.dumps(sensor_data),1) | |
next_reading += INTERVAL | |
sleep_time = next_reading - time.time() | |
if sleep_time > 0: | |
time.sleep(sleep_time) | |
def read_temperature(): | |
temp = sensor_data['temperature'] | |
return temp | |
# Function will set the temperature value in device | |
def setValue (params): | |
sensor_data['temperature'] = params | |
#print("Rx setValue is : ",sensor_data) | |
print("Temperature Set : ",params,"C") | |
# MQTT on_connect callback function | |
def on_connect(client, userdata, flags, rc): | |
#print("rc code:", rc) | |
client.subscribe('v1/devices/me/rpc/request/+') | |
# MQTT on_message callback function | |
def on_message(client, userdata, msg): | |
#print('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload)) | |
if msg.topic.startswith('v1/devices/me/rpc/request/'): | |
requestId = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)] | |
#print("requestId : ", requestId) | |
data = json.loads(msg.payload) | |
if data['method'] == 'getValue': | |
#print("getvalue request\n") | |
#print("sent getValue : ", sensor_data) | |
client.publish('v1/devices/me/rpc/response/' + requestId, json.dumps(sensor_data['temperature']), 1) | |
if data['method'] == 'setValue': | |
#print("setvalue request\n") | |
params = data['params'] | |
setValue(params) | |
# create a client instance | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.username_pw_set(ACCESS_TOKEN) | |
client.connect(THINGSBOARD_HOST,1883,60) | |
t = Thread(target=publishValue, args=(client,)) | |
try: | |
client.loop_start() | |
t.start() | |
while True: | |
pass | |
except KeyboardInterrupt: | |
client.disconnect() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am trying to acheive a simple task, however i am confused because its not working. After watching your tutorial, I am trying the code below but i am not getting desired results. I want to turn the knob (Knob widget in Thingsboard) and plot the value set by knob. For instance, rotate the knob and show the value of knob on chart/card etc. The script is below, can you help me to identify where i am doing wrong?
#!/usr/bin/env python
import os
import time
import smbus
import sys
import paho.mqtt.client as mqtt
import json
THINGSBOARD_HOST = 'ourdevice.tk'
ACCESS_TOKEN = 'iJqpMTIULqzlMgU2QpAw'
def on_connect(client, userdata, rc):
client.subscribe("v1/devices/me/rpc/request/+")
def on_message(client, userdata, msg):
data = json.loads(msg.payload)
data = int (data)
client.publish('v1/devices/me/telemetry', data)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(ACCESS_TOKEN)
client.connect(THINGSBOARD_HOST, 1883, 60)
try:
client.loop_forever()
except KeyboardInterrupt:
sys.exit()