Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
manuals:symcon:mqtt-integration [2016/11/30 21:35] – [Symcon] paulg4h | manuals:symcon:mqtt-integration [2018/09/23 19:15] (aktuell) – paulg4h | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
====== MQTT in Symcon Einbinden ====== | ====== MQTT in Symcon Einbinden ====== | ||
+ | |||
+ | <WRAP center round info 60%> | ||
+ | Stand September 2018, IPS 4.4 | ||
+ | </ | ||
+ | |||
Diese Anleitung zeigt wie man auf einem Raspberry PI 3 das MQTT Protokoll über Mosquitto und mqttwarn in Symcon schreibend und per Modul auch Beschreibbar ist. Also in beide Richtungen Ereigniss Basierend kommuniziert so wie ich es schon seit einem halben Jahr Produktiv mit diversen Arduinos und ESP8266 (PubSub Library) einsetze. | Diese Anleitung zeigt wie man auf einem Raspberry PI 3 das MQTT Protokoll über Mosquitto und mqttwarn in Symcon schreibend und per Modul auch Beschreibbar ist. Also in beide Richtungen Ereigniss Basierend kommuniziert so wie ich es schon seit einem halben Jahr Produktiv mit diversen Arduinos und ESP8266 (PubSub Library) einsetze. | ||
Zeile 8: | Zeile 13: | ||
**Auch müssen alle Werte in < | **Auch müssen alle Werte in < | ||
</ | </ | ||
+ | |||
===== Grund Installation am PI Einrichten ===== | ===== Grund Installation am PI Einrichten ===== | ||
Zeile 35: | Zeile 41: | ||
===== Pakete Installieren ===== | ===== Pakete Installieren ===== | ||
+ | Einen MQTT Broker z.B. das Paket mosquitto Installieren | ||
+ | aptitude install mosquitto mosquitto_pub mosquitto_sub | ||
+ | | ||
+ | ===== Symcon Vorbereiten ===== | ||
+ | |||
+ | ==== MQTT Client zum Senden in Symcon Installieren ==== | ||
+ | Zum Senden von Daten per MQTT_Publish aus jedem script: | ||
+ | https:// | ||
+ | |||
+ | |||
+ | ==== Mosquitto an Symcon Anbinden ==== | ||
+ | |||
+ | Eine Kategorie anlegen | ||
+ | Datenpunkte/ | ||
+ | | ||
+ | In diesem Knoten werden dann alle Geräte und Datenpunkte Automatisch erstellt / Aktualisiert, | ||
+ | Datenpunkte/ | ||
+ | und erstellen das Script " | ||
+ | |||
+ | Die Datei: | ||
+ | Datenpunkte/ | ||
+ | |||
+ | mit folgendem Inhalt: | ||
+ | <code php><? | ||
+ | include_once(IPS_GetScript(42011 )[" | ||
+ | |||
+ | $d = true; $msg = ""; | ||
+ | $value = ""; | ||
+ | |||
+ | if ($d){ | ||
+ | $msg .= " | ||
+ | $msg .= print_r($_IPS, | ||
+ | } | ||
+ | |||
+ | if ($_IPS[' | ||
+ | //On Manual Execute check child events and set Event Trigger to "On Change" | ||
+ | foreach (IPS_GetChildrenIDs($_IPS[' | ||
+ | if (IPS_GetObject($value)[' | ||
+ | IPS_SetEventTrigger($value, | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | if (($_IPS[' | ||
+ | if ($_IPS[' | ||
+ | // | ||
+ | $value = ($_IPS[' | ||
+ | } | ||
+ | if (($_IPS[' | ||
+ | $value = ($_IPS[' | ||
+ | } | ||
+ | |||
+ | if (($value == (GetValueBoolean($_IPS[' | ||
+ | if($d){ $msg .= " | ||
+ | $run = false; | ||
+ | } | ||
+ | |||
+ | if ($d){ | ||
+ | $msg .= " | ||
+ | } | ||
+ | |||
+ | $topic = explode(";", | ||
+ | |||
+ | if((strlen($topic) > 3) and ($run)){ | ||
+ | if ($d){ | ||
+ | $msg .= " | ||
+ | $msg .= " | ||
+ | $msg .= " | ||
+ | $msg .= " | ||
+ | } | ||
+ | MQTT_Publish($idMQTTBroker , $topic ,$value ,1 , true); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | ?></ | ||
+ | |||
+ | |||
+ | Das Eigentliche Script welces die Daten per RPC vom mqttwarn Daemon entgegennimmt und ablegt, in dem nicht Vergessen die ID's in den ersten Zeilen durch die Eigenen ersetzen: | ||
+ | |||
+ | <code php> <? | ||
+ | $d = true; $msg = ""; | ||
+ | //sample value: devices/ | ||
+ | // mqtt topic to symcon variable id mapping | ||
+ | $idParent = IPS_GetParent($_IPS[' | ||
+ | $archiveId = 32289; | ||
+ | $relaisScriptId = 17676; //Relais Script ID | ||
+ | |||
+ | // get value from mqtt variable | ||
+ | if( array_key_exists(" | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | if ($d){ $msg .= " | ||
+ | |||
+ | // Datentyp der payload herausfinden und ins Format für IPS Übersetzen | ||
+ | | ||
+ | if (is_float($payload+0)){ | ||
+ | // | ||
+ | | ||
+ | } | ||
+ | if (($contType == 99) and (is_numeric($payload))){ | ||
+ | // | ||
+ | | ||
+ | } | ||
+ | if (($contType == 99) and (is_string($payload))){ | ||
+ | if ((strtolower($payload) == " | ||
+ | //Boolean | ||
+ | $contType = 0; | ||
+ | } else { | ||
+ | //String | ||
+ | $contType = 3; | ||
+ | } | ||
+ | } | ||
+ | $idf = $idParent; | ||
+ | // | ||
+ | | ||
+ | if ($i == (count($topics) -1)){ | ||
+ | $type = 2; | ||
+ | } else { | ||
+ | $type = 0; | ||
+ | } | ||
+ | $idf = checkIfElementExists($idf, | ||
+ | } | ||
+ | |||
+ | | ||
+ | $msg .= " | ||
+ | $msg .= "Set Itend to: " | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | // | ||
+ | | ||
+ | } | ||
+ | if ((strlen($msg) > 0) and ($d)){ | ||
+ | | ||
+ | print $msg; | ||
+ | } | ||
+ | IPS_LogMessage(" | ||
+ | } | ||
+ | |||
+ | function isTrueFloat($val){ | ||
+ | $pattern = '/ | ||
+ | |||
+ | return (!is_bool($val) && (is_float($val) || preg_match($pattern, | ||
+ | } | ||
+ | |||
+ | function mqset($id, $value){ | ||
+ | Global $msg, $d; | ||
+ | if(GetValue($id) != $value){ | ||
+ | switch(IPS_GetVariable($id)[" | ||
+ | case 0: // boolean | ||
+ | $val = filter_var($value, | ||
+ | if($d){ $msg .= "val: " | ||
+ | SetValue($id, | ||
+ | break; | ||
+ | case 1: // integer | ||
+ | $val = intval($value); | ||
+ | SetValue($id, | ||
+ | break; | ||
+ | case 2: // float | ||
+ | $val = floatval($value); | ||
+ | SetValue($id, | ||
+ | break; | ||
+ | case 3: // string | ||
+ | SetValue($id, | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | function checkIfElementExists($varid, | ||
+ | Global $archiveId, $topic, $relaisScriptId; | ||
+ | $exists = false; | ||
+ | if (IPS_HasChildren($varid)){ | ||
+ | // Wenn Childs da sind alle durchlaufen | ||
+ | foreach (IPS_GetChildrenIDs($varid) as $key){ | ||
+ | if (IPS_GetObject($key)[" | ||
+ | // Prüfen ob schon ein Objekt mit genau dem Namen existiert | ||
+ | if (IPS_GetObject($key)[" | ||
+ | // Dann nicht erstellen und die id merken | ||
+ | $exists = true; | ||
+ | $id = $key; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if (!$exists){ | ||
+ | | ||
+ | //Wenn es eine Variable sein soll | ||
+ | $id = IPS_CreateVariable($contType); | ||
+ | } else { | ||
+ | //Sonst ist es eine Kategorie | ||
+ | $id = IPS_CreateCategory(); | ||
+ | } | ||
+ | IPS_SetName($id, | ||
+ | IPS_SetParent($id, | ||
+ | if($type == 2){ // Variable | ||
+ | if ($contType == 0){ // Boolean | ||
+ | if (strpos(" | ||
+ | // | ||
+ | | ||
+ | | ||
+ | } elseif(strpos(" | ||
+ | | ||
+ | } elseif ($name == " | ||
+ | | ||
+ | } else { | ||
+ | // | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | if (strpos(" | ||
+ | IPS_SetVariableCustomProfile($id, | ||
+ | } | ||
+ | if (strpos(" | ||
+ | IPS_SetVariableCustomProfile($id, | ||
+ | } | ||
+ | if (strpos(" | ||
+ | IPS_SetVariableCustomProfile($id, | ||
+ | } | ||
+ | IPS_SetInfo($id, | ||
+ | // | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | return $id; | ||
+ | } | ||
+ | |||
+ | function checkCreateEvent($varid , $scriptId){ | ||
+ | $create = true; | ||
+ | if (IPS_HasChildren($varid)){ | ||
+ | // Wenn Childs da sind alle durchlaufen | ||
+ | | ||
+ | // Wenn diese Events sind | ||
+ | if (IPS_GetObject($key)[" | ||
+ | // Prüfen ob schon ein Event mit genau dem script existiert | ||
+ | if (IPS_GetEvent($key)[" | ||
+ | // Sonst erstellen | ||
+ | $create = false; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | // Sonst erstellen | ||
+ | | ||
+ | } | ||
+ | if ($create){ | ||
+ | //Event Anlegen | ||
+ | $eid = IPS_CreateEvent(0); | ||
+ | IPS_SetEventTrigger($eid, | ||
+ | IPS_SetParent($eid, | ||
+ | IPS_SetEventActive($eid, | ||
+ | } | ||
+ | } | ||
+ | ?></ | ||
- | ==== Mosquitto (MQTT Broker) ==== | + | ==== MQTT Broker |
- | aptitude install mosquitto -y | + | aptitude install mosquitto |
| | ||
==== mqttwarn (mqtt daemon der alle Ereignisse zu Symcon weiterleitet) ==== | ==== mqttwarn (mqtt daemon der alle Ereignisse zu Symcon weiterleitet) ==== | ||
[[https:// | [[https:// | ||
Abhängige Pakete Installieren: | Abhängige Pakete Installieren: | ||
- | pip install paho-mqtt | + | pip install paho-mqtt |
Verzeichniss erstellen | Verzeichniss erstellen | ||
cd /opt/ | cd /opt/ | ||
Zeile 49: | Zeile 321: | ||
cd mqttwarn | cd mqttwarn | ||
chmod +x mqttwarn.py | chmod +x mqttwarn.py | ||
- | cp qttwarn.ini.sample mqttwarn.ini | + | cp mqttwarn.ini.sample mqttwarn.ini |
Derinhalt der mqttwarn.ini sollte so aussehen: | Derinhalt der mqttwarn.ini sollte so aussehen: | ||
< | < | ||
Zeile 113: | Zeile 385: | ||
[devices/#] | [devices/#] | ||
- | targets = http: | + | targets = log: |
template = ipsrpc.json | template = ipsrpc.json | ||
</ | </ | ||
Zeile 123: | Zeile 395: | ||
dann noch das template | dann noch das template | ||
- | cd / | + | cd / |
in form der Datei | in form der Datei | ||
vi ipsrpc.json | vi ipsrpc.json | ||
mit folgendem Inhalt erstellen: | mit folgendem Inhalt erstellen: | ||
- | < | + | < |
- | | + | ' |
- | ' | + | ' |
- | ' | + | ' |
- | ' | + | ' |
- | ' | + | } |
- | } | + | |
%} | %} | ||
{{ data | jsonify }}</ | {{ data | jsonify }}</ | ||
Zeile 163: | Zeile 434: | ||
2016-11-30 21: | 2016-11-30 21: | ||
- | ===== Symcon ===== | ||
- | Nach der Installation von IPS dort in einer Kategorie z.b. Datenpunlte/ | ||
- | <code php><? | ||
- | $d = false; $msg = ""; | ||
- | //sample value: devices/ | ||
- | // mqtt topic to symcon variable id mapping | ||
- | $idParent = IPS_GetParent($_IPS[' | ||
- | $archiveId = 32289 / | ||
- | $relaisScriptId = 17676 / | ||
- | |||
- | |||
- | // get value from mqtt variable | ||
- | $var = $_IPS[' | ||
- | $varsplit = explode(";", | ||
- | |||
- | if(count($varsplit)> | ||
- | $topics = explode("/", | ||
- | $topic=$varsplit[0]; | ||
- | $payload=$varsplit[1]; | ||
- | $payload = str_replace(array(" | ||
- | |||
- | if ($d){ $msg .= " | ||
- | |||
- | // Datentyp der payload herausfinden und ins Format für IPS Übersetzen | ||
- | $contType = 99; | ||
- | if (is_float($payload+0)){ | ||
- | // | ||
- | $contType = 2; | ||
- | } | ||
- | if (($contType == 99) and (is_numeric($payload))){ | ||
- | // | ||
- | $contType = 1; | ||
- | } | ||
- | if (($contType == 99) and (is_string($payload))){ | ||
- | if ((strtolower($payload) == " | ||
- | // | ||
- | $contType = 0; | ||
- | } else { | ||
- | // | ||
- | $contType = 3; | ||
- | } | ||
- | } | ||
- | $idf = $idParent; | ||
- | //Pfad durchlaufen und Prüfen ob die Kategorien / Variablen vorhanden sind und bei bedarf Anlegen | ||
- | for($i = 0; $i < count($topics); | ||
- | if ($i == (count($topics) -1)){ | ||
- | $type = 2; | ||
- | } else { | ||
- | $type = 0; | ||
- | } | ||
- | $idf = checkIfElementExists($idf, | ||
- | } | ||
- | |||
- | if($d){ | ||
- | $msg .= " | ||
- | $msg .= "Set Itend to: " | ||
- | } | ||
- | |||
- | // | ||
- | IPS_SetIdent($idf, | ||
- | |||
- | //if (GetValue($idf) != $payload){ | ||
- | //Wert Setzen | ||
- | mqset($idf, | ||
- | //} else { | ||
- | // $msg .= "Wert " | ||
- | //} | ||
- | } | ||
- | if (strlen($msg) > 0){ | ||
- | | ||
- | print $msg; | ||
- | } else { | ||
- | IPS_LogMessage(" | ||
- | } | ||
- | } | ||
- | |||
- | function isTrueFloat($val){ | ||
- | $pattern = '/ | ||
- | |||
- | return (!is_bool($val) && (is_float($val) || preg_match($pattern, | ||
- | } | ||
- | |||
- | function mqset($id, $value){ | ||
- | switch(IPS_GetVariable($id)[" | ||
- | case 0: // boolean | ||
- | $val = filter_var($value, | ||
- | SetValue($id, | ||
- | break; | ||
- | case 1: // integer | ||
- | $val = intval($value); | ||
- | SetValue($id, | ||
- | break; | ||
- | case 2: // float | ||
- | $val = floatval($value); | ||
- | SetValue($id, | ||
- | break; | ||
- | case 3: // string | ||
- | SetValue($id, | ||
- | break; | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | function checkIfElementExists($varid, | ||
- | Global $archiveId, $topic, $relaisScriptId; | ||
- | $exists = false; | ||
- | if (IPS_HasChildren($varid)){ | ||
- | // Wenn Childs da sind alle durchlaufen | ||
- | foreach (IPS_GetChildrenIDs($varid) as $key){ | ||
- | if (IPS_GetObject($key)[" | ||
- | // Prüfen ob schon ein Objekt mit genau dem Namen existiert | ||
- | if (IPS_GetObject($key)[" | ||
- | // Dann nicht erstellen und die id merken | ||
- | $exists = true; | ||
- | $id = $key; | ||
- | } | ||
- | } | ||
- | } | ||
- | } | ||
- | if (!$exists){ | ||
- | | ||
- | //Wenn es eine Variable sein soll | ||
- | $id = IPS_CreateVariable($contType); | ||
- | } else { | ||
- | //Sonst ist es eine Kategorie | ||
- | $id = IPS_CreateCategory(); | ||
- | } | ||
- | IPS_SetName($id, | ||
- | | ||
- | | ||
- | if ($contType == 0){ // Boolean | ||
- | if (strpos(" | ||
- | //Wenn der Variablen Name das wort " | ||
- | IPS_SetVariableCustomProfile($id, | ||
- | IPS_SetVariableCustomAction($id, | ||
- | } elseif(strpos(" | ||
- | | ||
- | } elseif ($name == " | ||
- | | ||
- | } else { | ||
- | //Wenn es eine Boolean Variable ist das Profile ~Switch zuweisen | ||
- | | ||
- | | ||
- | checkCreateEvent($id, | ||
- | } | ||
- | } | ||
- | if (strpos(" | ||
- | IPS_SetVariableCustomProfile($id, | ||
- | } | ||
- | if (strpos(" | ||
- | IPS_SetVariableCustomProfile($id, | ||
- | } | ||
- | if (strpos(" | ||
- | IPS_SetVariableCustomProfile($id, | ||
- | } | ||
- | IPS_SetInfo($id, | ||
- | // | ||
- | AC_SetLoggingStatus($archiveId, | ||
- | | ||
- | } | ||
- | } | ||
- | return $id; | ||
- | } | ||
- | |||
- | function checkCreateEvent($varid , $scriptId){ | ||
- | $create = true; | ||
- | if (IPS_HasChildren($varid)){ | ||
- | // Wenn Childs da sind alle durchlaufen | ||
- | | ||
- | // Wenn diese Events sind | ||
- | if (IPS_GetObject($key)[" | ||
- | // Prüfen ob schon ein Event mit genau dem script existiert | ||
- | if (IPS_GetEvent($key)[" | ||
- | // Sonst erstellen | ||
- | $create = false; | ||
- | } | ||
- | } | ||
- | } | ||
- | } else { | ||
- | // Sonst erstellen | ||
- | | ||
- | } | ||
- | if ($create){ | ||
- | // | ||
- | $eid = IPS_CreateEvent(0); | ||
- | IPS_SetEventTrigger($eid, | ||
- | IPS_SetParent($eid, | ||
- | IPS_SetEventActive($eid, | ||
- | } | ||
- | } | ||
- | ?></ | ||