Dies ist eine alte Version des Dokuments!
Inhaltsverzeichnis
MQTT in Symcon Einbinden
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.
Alle Befehle werden als root am PI ausgeführt, wenn jemand nicht weiß wie das geht sollte er es besser lassen oder sich etwas in Linux einarbeiten!
Auch müssen alle Werte in <spitzen> Klammern durch die Eigenen ersetzt werden!
Grund Installation am PI Einrichten
Zuerst muss am PI folgendes mit
raspi config
eingestellt werden:
- Expand Filesystem
- Boot Options
- B4 (Boot to CLI)
- Internationalisation Options
- Change Locale –> de_DE.utf8 und auch als Standard Einstellen
- Change Timezone –> Europe / Wo auch immer ihr Wohnt
- Change Keyboard Layout –> Other / German
- Password von user PI Ändern udn auch eines für root setzen
- in der Datei
nano /etc/ssh/sshd_config
- die Zeile
#PermitRootLogin without-password
mit dem # Vorangestellt auskommentieren damit ihr euch als root nach einem neustart des Dienstes per SSH anmelden könnt.
service ssh restart
- Einige Paket deinstallieren
aptitude purge wolfram-engine -y aptitude update && aptitude upgrade -y
- Einige Standard Tools installieren
aptitude install htop iftop iotop vim -y
Pakete Installieren
Mosquitto (MQTT Broker)
aptitude install mosquitto -y
mqttwarn (mqtt daemon der alle Ereignisse zu Symcon weiterleitet)
Installation aus der Wiki von hier Abhängige Pakete Installieren:
pip install paho-mqtt
Verzeichniss erstellen
cd /opt/
Herunterladen mit:
git clone https://github.com/jpmens/mqttwarn.git cd mqttwarn chmod +x mqttwarn.py cp qttwarn.ini.sample mqttwarn.ini
Derinhalt der mqttwarn.ini sollte so aussehen:
# -*- coding: utf-8 -*- # mqttwarn example configuration file "mqttwarn.ini" [defaults] hostname = 'localhost' ; default port = 1883 username = None password = None clientid = 'mqttwarn' lwt = 'clients/mqttwarn' skipretained = False cleansession = True protocol = 3 ; logging logformat = '%(asctime)-15s %(levelname)-5s [%(module)s] %(message)s' logfile = '/var/log/mqttwarn.log' ; one of: CRITICAL, DEBUG, ERROR, INFO, WARN loglevel = DEBUG #loglevel = ERROR ; path to file containing self-defined functions for formatmap and datamap ; omit the '.py' extension functions = 'samplefuncs' ; name the service providers you will be using. launch = file, log, http [config:file] append_newline = True targets = { 'f01' : ['/tmp/f.01'], 'log-me' : ['/tmp/log.me'], 'mqttwarn' : ['/tmp/mqttwarn.err'], } [config:log] targets = { 'debug' : [ 'debug' ], 'info' : [ 'info' ], 'warn' : [ 'warn' ], 'crit' : [ 'crit' ], 'error' : [ 'error' ] } [config:http] timeout = 60 targets = { #method #URL # query params or None # list auth 'get1' : [ "get", "http://example.org?", { 'q': '{name}', 'isod' : '{_dtiso}', 'xx': 'yy' }, ('username', 'password') ], 'post1' : [ "post", "http://example.net", { 'q': '{name}', 'isod' : '{_dtiso}', 'xx': 'yy' }, None ], 'postIPSORIGINAL' : [ "post", "http://<symcon ip>:3777/api/", None, ('username', 'passwd') ], 'postIPS' : [ "post", "http://<symcon ip>:3777/api/", None , None, True ] } ; special config for 'failover' events [failover] targets = log:error, file:mqttwarn [devices/#] targets = http:postIPS template = ipsrpc.json
der letzte Abschnitt [devices/#] bedeutet das alle daten im MQTT Broker (mosquitto) unter devices/* an IPS übergeben werden!
dann noch das template
cd /opt/mqttwarn/template
in form der Datei
vi ipsrpc.json
mit folgendem Inhalt erstellen:
{% set data = { 'method': "IPS_RunScriptEx", 'params': [<id des verarbeitungs scriptes>, { 'payload': topic +";" + payload } ], 'jsonrpc': "2.0", 'id': '0' } %} {{ data | jsonify }}
Supervisor
Damit mqttwarn als Dienst ausgeführt wird verwende ich supervisor wie hier Beschrieben.
aptitude install supervisor
in dem Ornder
cd /etc/supervisor/conf.d
die Datei mqttwarn.conf anlegen
vi mqttwarn.conf
mit folgendem Inhalt
[program:mqttwarn] directory = /opt/mqttwarn command = /opt/mqttwarn/mqttwarn.py user = root environment= MQTTWARNINI="/opt/mqttwarn/mqttwarn.ini"
Nun kann mqttwarn gestartet werden mit:
service supervisor start
und z.B. in einem zweiten Terminal Fenster die Log Datei beobachtet werden:
tail -f /var/log/mqttwarn.log
was so aussehen sollte:
2016-11-30 21:26:19,678 DEBUG [mqttwarn] Attempting connection to MQTT broker localhost:1883... 2016-11-30 21:26:19,678 DEBUG [mqttwarn] Setting LWT to clients/mqttwarn... 2016-11-30 21:26:19,682 INFO [mqttwarn] Starting 1 worker threads 2016-11-30 21:26:19,683 DEBUG [mqttwarn] Job queue has 0 items to process 2016-11-30 21:26:19,684 DEBUG [mqttwarn] Connected to MQTT broker, subscribing to topics... 2016-11-30 21:26:19,685 DEBUG [mqttwarn] Subscribing to devices/# (qos=0)