Receiving information from OpenRemote

Hello everyone,

Let me give you a bit of an explanation of what I have managed to have so far.

I have an Python code that is able to send information to OpenRemote (both numerical or JSON type). My question is whether it is possible to receive information from OpenRemote to the Python code.

One of the reasons that I use OpenRemote is that I was hoping that I could share information/data through it.
My idea is that a Python code would send data to OpenRemote. Once uploaded, another Python code would receive this data (or part of it). this way I can use OpenRemote as a data sharing platform but I cannot find anything online that allows to do this… do you know perhaps?

Thanks!

Hi @Casentive
have you checked this?

Hi welcome to the forum,

glad to hear you came so far.

Ofc it’s possible, i guess you use the paho lib? so you can subscribe to any topic.

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()

I didn’t have the opportunity to test paho yet, but from the PDF on Don’s message here

Seems that you need to work with the on_message function. From there you can probably save/export/do whatever on the message you receive

1 Like

Hi,

to subscribe to a attribute or value you have to set the correct topic format.
In your case you have to subscribe to:

clientMQTT.subscribe(f"master/{clientID}/attributevalue/{attributeSub}/{assetIDSub}")

notice the diffrence:

publish: writeattributevalue
subscribe: attributevalue for the payload and attribute for the json

1 Like