Connecting Raspberry Pi with OpenRemote (localhost)

Hi, I have two devices: a Raspberry Pi (IP: 192.168.0.108) and a MacBook (192.168.0.52, localhost). On my MacBook there runs OpenRemote on localhost and on my Raspberry Pi I have a DHT22 sensor connected, which measures temperature and humidity. Now I want to publish these values to my OR plattform on the MacBook. My python-script looks like the following:

import paho.mqtt.client as mqtt
import Adafruit_DHT
import time


# Set up DHT22 sensor
dht_sensor = Adafruit_DHT.DHT22
dht_pin = 4  # Replace with the GPIO pin number that the sensor is connected to


username = 'ewg:mqttuser2'
secret = 'I8KY0e339HTejA3V611PRTA69zfi4mOC'
host = '192.168.0.52'
port = 8883
clientID = 'client456'
assetID = '5WMeImEqR3CDpR90Ltd2Qv'
attribute = 'humidity'


def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to broker")
    else:
        print("Connection failed")


def on_publish(client, userdata, result):
    print("Data published \n")
    print(humidity)  # test if humidity is correct
    pass


clientMQTT = mqtt.Client(clientID)
clientMQTT.username_pw_set(username, password=secret)
clientMQTT.on_connect = on_connect
clientMQTT.on_publish = on_publish
clientMQTT.connect(host, port=port)

# Continuously read and send temperature values
while True:
    humidity, temperature = Adafruit_DHT.read_retry(dht_sensor, dht_pin)
    if humidity is not None and temperature is not None:
        humidity = int(humidity)
        clientMQTT.publish(f"ewg/{clientID}/writeattributevalue/{attribute}/{assetID}", humidity)
    else:
        print("Failed to read temperature sensor data")
    time.sleep(5)

clientMQTT.disconnect()

The script is running, so no error message appears, but no values are arriving at my MacBook. Does someone has an idea what could be the problem? I have a service user created and also an asset with the attributes ‘temperature’ and ‘humidity’ (both of kind Number).

Hi,
try it #disable ssl verification ,

import paho.mqtt.client as mqttclient
import time
import ssl
import json

def on_connect(client, userdata, flags, rc):
if rc == 0:
print(“Connected to the broker”)
global Connected
Connected = True
else:
print(f"Connection failed with code {rc}")

def on_publish(client, userdata, mid):
print(f"Data published with MID: {mid}")

Connected = False
username = “conscient:mqtt”
secret = “code”
host = “192.168.29.47”
port = 8883
clientId = “MQTTpub”
assetId = ‘5P0NXgqghEM6zO5eQvW45b’

clientMQTT = mqttclient.Client(clientId)
clientMQTT.username_pw_set(username, password=secret)

Manually create an SSL context and disable certificate verification

context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
clientMQTT.tls_set_context(context)

clientMQTT.on_connect = on_connect
clientMQTT.on_publish = on_publish

clientMQTT.connect(host, port=port)
clientMQTT.loop_start()

while Connected != True:
time.sleep(0.1)

Publish to individual topics

clientMQTT.publish(f"conscient/{clientId}/writeattributevalue/humidity/{assetId}“, str(data[“HUMIDITY”]))
clientMQTT.publish(f"conscient/{clientId}/writeattributevalue/temp/{assetId}”, str(data[“TEMPERATURE”]))
clientMQTT.publish(f"conscient/{clientId}/writeattributevalue/RSSI/{assetId}", str(data[“RSSI”]))

time.sleep(4)

clientMQTT.disconnect()
clientMQTT.loop_stop()

print(“Disconnected.”)

Hi, Did you try sending a message to OR with the MQTTX application?

Good morning,

After looking at your code, I can see that it should work; what I am suspecting is that your script is having issues reaching the OpenRemote MQTT broker. There might also be issues with if you are using the proxied version of OpenRemote. I would first try to use a desktop MQTT client to test out if you can reach the MQTT broker from localhost; then, I would try to check the docker containers and see what ports are being forwarded out of them. From what I am guessing, it is probably a networking issue and not a scripting issue.

For your reference, here is my own script for testing some OpenRemote features:

import paho.mqtt.client as mqtt
import time
import ssl
import logging
from geojson import Point

realm = "smartcity"
username = 'smartcity:mqtttest'
secret = 'Jl3zdQPdTNK2kxImVrG9hB0qLJCSI4fV'
host = 'localhost'
port = 8883
clientID = 'pythonScriptForMockData'
assetID = '3L5nfQx5m6i45ppNeFxiGB'

locations = [...] # fill in

points = [Point(location) for location in locations]

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    logging.info("Connected with result code "+str(rc))


def on_publish(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    logging.info(msg.topic+" "+str(msg.payload))


def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected disconnection.")
        logging.info(f"Unexpected disconnection. Reason: {mqtt.connack_string(rc)}")


def on_log(client, userdata, level, buf):
    print("log: ", buf)
    logging.info("log: ", buf)


mqtt_client = mqtt.Client(clientID)
mqtt_client.username_pw_set(username, password=secret)
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
mqtt_client.tls_set_context(context)
mqtt_client.on_connect = on_connect
mqtt_client.on_disconnect = on_disconnect
mqtt_client.on_log = on_log
mqtt_client.connect(host, port=port, keepalive=930)
mqtt_client.loop_start()

# Continuously read and send temperature values
while True:

    for point in points:
        value = mqtt_client.publish(f"{realm}/{clientID}/writeattributevalue/location/{assetID}", str(point), 2)
        # print(str(point))
        time.sleep(5)
    


# Disconnect MQTT client
mqtt_client.disconnect()
mqtt_client.loop_stop()