Skip to end of banner
Go to start of banner

MQTT - Beispiel Anbindungen

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Integration in FEHM

Zunächst wird ein Gerät vom Typ MQTT2_CLIENT (https://fhem.de/commandref.html#MQTT_CLIENT), mit Port 61894 in FHEM angelegt um sich mit der NWPM Touch zu verbinden.

defmod nwpm_mqtt_client MQTT2_CLIENT 192.168.178.38:61894
attr nwpm_mqtt_client clientId nwpm_mqtt_client
attr nwpm_mqtt_client mqttVersion 3.1.1
attr nwpm_mqtt_client subscriptions gateway/iothub/twin_reported_state extern/nwpm_mqtt_client/#
attr nwpm_mqtt_client username mqtt
attr nwpm_mqtt_client verbose 5

Anschließend wird ein Gerät vom Typ MQTT2_DEVICE angelegt, dass die Wärmepumpe darstellt.

defmod nwpm_mqtt_device MQTT2_DEVICE
attr nwpm_mqtt_device IODev nwpm_mqtt_client
attr nwpm_mqtt_device getList modbus_get modbus_get_result gateway/modbus/get_value/$EVTPART1 {"name":"$EVTPART1","mqtt_msg_properties":{"response_topic":"extern/nwpm_mqtt_client/get_value_reply/$EVTPART1","correlation_data": 0}}
attr nwpm_mqtt_device readingList nwpm_mqtt_client:gateway/iothub/twin_reported_state:.* twin_reported_state\ nwpm_mqtt_client:gateway/iothub/twin_reported_state:.* { json2nameValue($EVENT) }\ nwpm_mqtt_client:extern/nwpm_mqtt_client/get_value_reply/.*:.* modbus_get_result
attr nwpm_mqtt_device setList modbus_set gateway/modbus/set_value/$EVTPART1 {"name":"$EVTPART1","value":["$EVTPART2"],"mqtt_msg_properties":{"response_topic":"","correlation_data": 0}}\ modbus_set_multiple gateway/modbus/set_value/$EVTPART1 {"name":"$EVTPART1","value":$EVTPART2,"mqtt_msg_properties":{"response_topic":"","correlation_data": 0}}

Integration in openHAB

Voraussetzung

  • Installation von MQTT Bindig

  • Installation von JSONPath Transformation

Beispiel Konfiguration

heatpumpMqtt.things file:

Bridge mqtt:broker:heatpump [ host="192.168.178.38", secure=false , port=61894, username="mqtt", password="80B56598 ",reconnectTime=30000, clientId="openhabClientHeatpump" ]{
  Thing topic values {
  Channels:
    Type number : outsideTemp "Outside temperature " [ stateTopic="gateway/iothub/twin_reported_state", transformationPattern="JSONPATH:$.telemetry.1301a" ]
    Type number : opMode_manual "Manual operation mode" [ stateTopic="gateway/iothub/twin_reported_state", transformationPattern="JSONPATH:$.telemetry.714i", commandTopic="gateway/modbus/set_value/714i", formatBeforePublish="{\"name\": \"714i\",\"value\":[\"%d\"],\"mqtt_msg_properties\": {\"correlation_data\": 0, \"response_topic\": \"extern/openhabClientHeatpump/response\"}}"]
    }
}
heatpumpMqtt.items file:
Number Heatpump_OT "Outside temperature [%.1f °C]" {channel="mqtt:topic:heatpump:values:outsideTemp"}
Number Heatpump_opMode "Manual operation mode %s" {channel="mqtt:topic: heatpump:values:opMode"}

Integration in ioBroker

Voraussetzung

  • Installation MQTT Broker/Client Adapter mit der Client-Modbus Konfiguration auf Port 61894

  • Installation Engine Adapter um eigene Scripte ausführen zu können

  • Installation vis Adapter um eine Web-GUI erstellen zu können

Beispiel Konfiguration

on({id: "mqtt.0.gateway.iothub.twin_reported_state", change: "any"}, function (obj) { ParseTwinReportedState(obj); });
CreateOrSetState('mqtt.0.input.modbus_set_state', 'modbus_set_state', '', true);
on({id: "javascript.0.mqtt.0.input.modbus_set_state", change: "any"}, function (obj) { HandleModbusSetStateInput(obj); });
CreateOrSetState('mqtt.0.input.modbus_get_state', 'modbus_get_state', '', true);
on({id: "javascript.0.mqtt.0.input.modbus_get_state", change: "any"}, funktion (obj) { HandleModbusGetStateInput(obj); });

function ParseTwinReportedState(obj) {
  try {
    var rootJson = JSON.parse(obj.state.val);
    var jsonTable = [];
    Object.keys(rootJson).forEach(function(outerKey) {
      var innerJson = rootJson[outerKey];
      Object.keys(innerJson).forEach(function(innerKey) {
        CreateOrSetState('mqtt.0.parsed.twin_reported_state.' + outerKey + '.' + innerKey, innerKey, innerJson[innerKey]);
        jsonTable.push({Name: innerKey, Value: innerJson[innerKey]});
      });
    });
    CreateOrSetState('mqtt.0.parsed.twin_reported_state_table', 'twin_reported_state_table', JSON.stringify(jsonTable));
  } catch (e) {console.error("Error in ParseTwinReportedState(): " + e);}
}

function HandleModbusSetStateInput(obj) {
  try{
    var inputSplit = obj.state.val.split(" ");
    if(inputSplit.length < 2) return;
    var modbusIdx = inputSplit[0];
    var valArray = [];
    for(var i=1; i<inputSplit.length; i++){valArray.push(inputSplit[i]);}
    var modbusSetJSON = {
      name: modbusIdx,
      value: valArray,
      mqtt_msg_properties: {response_topic: "extern/iobroker/set_value_reply", correlation_data: 0}
    }
    sendTo('mqtt.0', 'sendMessage2Client', {topic: 'gateway/modbus/set_value/' + modbusIdx, message: JSON.stringify(modbusSetJSON)});
  }
  catch (e) {console.error("Error in ParseModbusSetState(): " + e);}
}

function HandleModbusGetStateInput(obj) {
  try{
    var modbusIdx = obj.state.val;
    if(modbusIdx.length < 1) return;
    var modbusGetJSON = {
      name: modbusIdx,
      mqtt_msg_properties: {response_topic: "extern/iobroker/get_value_reply", correlation_data: 0}
    }
    sendTo('mqtt.0', 'sendMessage2Client', {topic: 'gateway/modbus/get_value/' + modbusIdx, message: JSON.stringify(modbusGetJSON)});
  } catch (e) {console.error("Error in ParseModbusGetState(): " + e);}
}

function CreateOrSetState(objectId, objectName, objectValue, onlyCreate = false){
  if(!existsState(objectId)) createState(objectId, objectValue,{name: objectName, type: 'string', role: 'value'}, function () {});
  else if(!onlyCreate) setState(objectId, objectValue);
}  
  • Ein JSON-Table Widget, das mit der

    "javascript.0.mqtt.0.parsed.twin_reported_state_table"

    Object-ID verbunden ist, um den kompletten TwinReportedState anzuzeigen

  • Input Widgets zum Setzen

    "javascript.0.mqtt.0.input.modbus_set_state"

    und Abrufen

    "javascript.0.mqtt.0.input.modbus_get_state"

    einzelner beliebiger Modbuswerte.

  • String Widgets zur Anzeige einzelner Werte des TwinReportedState, z.B.

    "javascript.0.mqtt.0.parsed.twin_reported_state.telemetry.714i"

    oder Ergebnisse von set

    "mqtt.0.extern.iobroker.set_value_reply"

    und get

    "mqtt.0.extern.iobroker.get_value_reply"

    Operationen.

  • No labels