MQTT - Integration 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"}, function (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
einzelner beliebiger Modbuswerte.
String Widgets zur Anzeige einzelner Werte des TwinReportedState, z.B.
oder Ergebnisse von set
und get
Operationen.