Kopplung der MQTT Broker CCU-Jack und Mosquitto - gelöst

diverse Zusatzsoftware

Moderator: Co-Administratoren

Antworten
HomeManiac
Beiträge: 73
Registriert: 24.12.2015, 13:13
Hat sich bedankt: 2 Mal

Kopplung der MQTT Broker CCU-Jack und Mosquitto - gelöst

Beitrag von HomeManiac » 29.12.2022, 18:22

Hallo,

leider habe ich es nicht geschafft, eine funktionierende Kommunikation zw. den beiden o.g. MQTT-Brokern (Bridge) herzustellen.
Nach etlichen Fehlversuchen, habe ich mir kurzhand dieses kleine Python Programm geschrieben:

Code: Alles auswählen

#!/usr/bin/python3 -u
import time
import json
import datetime
import sys
import paho.mqtt.client as mqtt
import queue

## topic part, to be used in local MQTT Server
CONST_HM_LOCAL_NAME = "hm2/"

## MQTT Server Parameter for the CCU3 Server
CONST_CCU3_SERVER = "192.168.2.120"
CONST_CCU3_PORT   = 1883

## MQTT Server Parameter for the Local Server
CONST_LOCAL_SERVER = "192.168.2.160"
CONST_LOCAL_PORT   = 1883
CONST_LOCAL_RETAIN = True

#message queue received by the CCU mqtt server
qCCU3 = queue.Queue()

# message queue received by the LOCAL(?) mqtt server
qLOCAL = queue.Queue()


## Error Log, if needed ##
def on_log(client, userdata, level, buf):
        print("log: ",buf)

def on_connectCCU3(client, userdata, flags, rc):
        print("Connected with result code <"+str(rc)+"> to CCU3")
        clientCCU3.subscribe("device/status/#", qos=0)

def on_messageCCU3(client, userdata, msg):
        global qCCU3
        ## rewrite topic to be used in the local server
        newTopic=msg.topic.replace("device/", CONST_HM_LOCAL_NAME)

        #print("rewrite topic: " + msg.topic+" => "+newTopic)

        ## put values in the CCU3 send queue
        qCCU3.put((newTopic, msg.payload))


def on_connectLOCAL(client, userdata, flags, rc):
        print("Connected with result code <"+str(rc)+"> to LOCAL")
        clientLOCAL.subscribe("hm2/set/#", qos=0)

def on_messageLOCAL(client, userdata, msg):
        global qLOCAL
        ## rewrite topic to be used for the CCU Jack on teh CCU
        newTopic=msg.topic.replace(CONST_HM_LOCAL_NAME, "device/")

        #print("rewrite topic: " + msg.topic+" => "+newTopic)

        ## put values in the LOCAL send queue
        qLOCAL.put((newTopic, msg.payload))

clientCCU3 = mqtt.Client(client_id="bridgeToLocal")
clientCCU3.on_connect = on_connectCCU3
clientCCU3.on_message = on_messageCCU3
#clientCCU3.on_log = on_log
clientCCU3.connect(CONST_CCU3_SERVER, CONST_CCU3_PORT, 60)
clientCCU3.loop_start()


clientLOCAL = mqtt.Client(client_id="bridgeToCCU3")
clientLOCAL.on_connect = on_connectLOCAL
clientLOCAL.on_message = on_messageLOCAL
#clientLOCAL.on_log = on_log
clientLOCAL.connect(CONST_LOCAL_SERVER, CONST_LOCAL_PORT, 60)
clientLOCAL.loop_start()

while(1):
        time.sleep(.05)   # wait 50ms


        if not qCCU3.empty():
                ## yes, something to send
                topic, msg = qCCU3.get()
                clientLOCAL.publish(topic, msg, qos=0, retain=CONST_LOCAL_RETAIN)


        if not qLOCAL.empty():
                ## yes, something to send
                topic, msg = qLOCAL.get()
                clientCCU3.publish(topic, msg, qos=0, retain=False)



        if qLOCAL.qsize() > 2000:
                print("QueueLocal to large, aborting")
                exit(1)

        if qCCU3.qsize() > 2000:
                print("QueueCCU3 to large, aborting")
                exit(1)


Es übersetzt "Befehle" an ein HM-Gerät auf dem lokalen Broker direkt in die entsprechende Anweisung für den CCU-Jack Broker. Zusätzlich wird der Status der HM-Geräte auf den lokalen Broker übertragen.
Beispiel - Befehl an Aktor REQ0577529:
Aus Topic: "hm2/set/REQ0577529/1/STATE" wird als "device/set/REQ0577529/1/STATE" an CCU-Jack gesendet und schaltet damit das Device REQ0577529.

Beispiel - Status von Aktor
Aus Topic: "device/status/REQ0577529/1/STATE" aus der CCU-Jack Umgebung wird "hm2/status/REQ0577529/1/STATE" auf dem lokalen Broker

Falls jemand ähnliche Problme hat, ggf hilft es ja ...

VG

Antworten

Zurück zu „Sonstige Addons“