Using Python to communicate with OpenRemote using MQTT (paho-MQTT)

Hi all,

I want to share a short guide a student has made while she was working on a project using OpenRemote. All credits go to Nina Taken and her team for creating a wonderful project (video will come soon) and this guide.

MQTT-OpenRemote.pdf (488.3 KB)

They connected an ESP32 with sensors to monitor soil and air conditions for plants to OpenRemote. She used paho-MQTT as MQTT client in Python to connect to our broker. I hope it will be helpful for some of you!

If you have any questions I will direct them to her, or ask her to join our forum.

Don

2 Likes

Hey Don,

I guess there is missing some Code for the ESP.

ESP32 can micropython, but it is not that usual, and the example in the doc don‘t provide micropython for an ESP.

Hi Denis,

Thanks for letting me know. I adjusted the title a little bit to focus more on the Python side of it.

Don

1 Like

Used this example, and I had problem with clientMQTT.tls_set(certify.where()) ()
CERTIFICATE_VERIFY_FAILED

May be it was because I use virtualbox+ubuntuserver with OR and trying to connect from PC+windows over local net.
So, if you have same problems, you can try this code (visual studio code + python3.10):

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 = ‘UwNbCqV_YOUR_SECRET_DYDvzP5Z’
host = ‘salmon.local’
port = 8883
clientID = ‘WQTTwrt’
assetID = ‘7PaWocH_YOUR_THING_pq93h’
attributeWr = ‘writeAttribute’
attribute_value = 10

attributeRd = ‘subscribeAttribute’

clientMQTT = mqtt.Client(clientID)
clientMQTT.username_pw_set(username, password = secret)

//# the key steps here
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
//# if you do not want to check the cert hostname, skip it
//# 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)

while attribute_value <=42 :
clientMQTT.publish(f"master/{clientID}/writeattributevalue/{attributeWr}/{assetID}“, attribute_value)
clientMQTT.subscribe(f"master/{clientID}/attribute/{attributeRd}/{assetID}”)

time.sleep(5)
attribute_value += 1

clientMQTT.disconnect()
clientMQTT.loop_stop()

Here:
//# the key steps here
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
//# if you do not want to check the cert hostname, skip it
//# context.check_hostname = False

you need to remove //

1 Like