Hello @pcr and @Denis, thanks for the answers.
So I followed the manual that pcr just linked and I manage to send info into my OpenRemote locally hosted session.
What I have a difficulty implementing is subscribing to a topic. Just as in the manual I have created the subscribe attribute but I cannot turn on and off through Python but only using MQTT explorer using the code line clientMQTT.subscribe(f"master/{clientID}/writeattributevalue/{attributeSub}/{assetIDSub}")
What I understood so far about the subscribe function is that if my agent is subscribe to a topic it will receive the data depending on my choice (i.e. update every time it changes or every 15 min).
I am implementing the paho lib like you suggest but I am quite lost with the documentation.
It walks you through activating and deactivation a boolean value of the subscribe attribute but I don’t understand how I can implement that into my code such that if subscribed to a topic my Python variable changes…
I would imagine that I need to create a variable in my code that can register the data but I can’t find anything on how to do so.
I have put the Python code that I am using here below, hopefully this helps clearing up my doubts:
(it is basically the same proposed by napster7c6 in this post: Using Python to communicate with OpenRemote using MQTT (paho-MQTT) - #3 by Don)
import paho.mqtt.client as mqtt
import time
import certifi
import ssl
import json
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected #Use global variable
Connected = True #Signal connection
else:
print("Connection failed")
def on_publish(client, userdata, result):
print(f'Data published. Data: {attribute_value}')
pass
def on_message(client, userdata, message):
msg = json.loads(message.payload.decode('utf-8'))
id = msg['attributeState']['ref']['id']
att_name = msg["attributeState"]['ref']['name']
att_value = msg["attributeState"]["value"]
print(f'Message received. Asset ID: {id}. Attribute name: {att_name}. Attribute value: {att_value}')
Connected = False
username = 'master:mqttuser'
secret = 'CMHdaTXQwzaO9NBIZGw37VE81IYW2Th3'
host = 'localhost'
port = 8883
clientID = 'client123'
assetIDwr = '4g3pGJzQXgsEdeo2htWw5Q'
assetIDSub = '4g3pGJzQXgsEdeo2htWw5Q'
attributeWr = 'Baseline'
attributeSub = 'SubscribeAttribute'
attribute_value = 12
test = 0
clientMQTT = mqtt.Client(clientID)
clientMQTT.username_pw_set(username, password = secret)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.check_hostname = False
clientMQTT.tls_set_context(context)
clientMQTT.on_connect = on_connect
clientMQTT.on_publish = on_publish
clientMQTT.on_message = on_message
clientMQTT.connect(host, port=port)
clientMQTT.loop_start()
while Connected != True:
time.sleep(0.1)
clientMQTT.publish(f"master/{clientID}/writeattributevalue/{attributeWr}/{assetIDwr}", attribute_value)
clientMQTT.subscribe(f"master/{clientID}/writeattributevalue/{attributeSub}/{assetIDSub}")
clientMQTT.disconnect()
clientMQTT.loop_stop()