Neuigkeiten:

Am Sonntag den 8.12.2024 kann es ab ca. 8:00 Uhr zu kurzzeitigen Einschränkungen / Ausfällen bei den Diensten des FHEM Vereines kommen.
Die Server müssen mal gewartet und dabei neu gestartet werden ;)

Hauptmenü

MQTT-GPIO Skript

Begonnen von mtk, 25 April 2020, 16:36:25

Vorheriges Thema - Nächstes Thema

mtk

Hallo,

ich wollte nicht auf jedem meiner Raspis ein FHEM installieren um über die GPIO an die Ein- und Ausgänge zu kommen.
Deshalb habe ein kleines Python Skript geschrieben, das man auf einem RASPI (als Service) starten kann und sich als MQTT Device aus FHEM anprechen lässt. Um es in FHEM einfach zu gestalten (vor allem für Anfänger wie mich), erzeugt das Skript auch eine FHEM Konfiguration, die sich per include in FHEM sehr einfach einbinden lässt.

Für eigene Gehversuche habe ich die print Befehle nur auskommentiert und drin gelassen.
Das Skript setzt voraus, das auf dem Raspi bereits Python, MQTT usw. installiert sind, siehe Header im Skript.
Ist es in FHEM eingebunden erzeugt es im Raum PiNode MQTT ein paar Elemente, siehe Anhang.
Die Devices sind vom Typ Proxy realisiert, womit diese alle wichtigen Grundfunktionen erben.
Zu jedem GPIO werden auch automatisch ein paar sinnvolle notify funktionen erzeugt, mit denen man auf die Zustände reagieren kann.
Weiterhin habe ich eine Funktion PIR als separaten thread realisiert, der auf GPIO 24 einen PIR Sensor erwartet und für eine einstellbare Zeit eine Bewegung meldet. Im zugehörigen Notify kann man dann eine passende Reaktion auslösen.
Hier könnt Ihr natürlich selber auch weitere Funktionen einbauen, wie z.B. per 1-wire angeschlossene Sensoren o.ä. realisieren.
Ich möchte noch Temperatur, Feuchtigkeit usw. einbauen. Passende Code Beispiele in Python finden sich genug.

Bei Interesse kann ich das ganze gerne etwas besser und weiter dokumentieren.

Grüße Michael


#!/usr/bin/python -u
############################################################
## Description
## GPIO-MQTT gateway
## with export of FHEM config file: fhem.inc_HOSTNAME.cfg
## usage: python GPIO_MQTT_PIR.py
## prerequisites: python, PIP
## pip install rpi.gpio
## pip install paho-mqtt
##
############################################################
## Author: Michael T. Klein
## Copyright: MTK 2020
## Version: 1.3.stable
## Maintainer:
## Email:
## Status: tested
############################################################
# comment your config here:
#
# +-----+---------+------+---+ Connector+---+------+---------+-----+
# | BCM |   Name  | Mode |   | Physical |   | Mode | Name    | BCM |
# +-----+---------+------+---+----++----+---+------+---------+-----+
# |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |
# |   2 |   SDA.1 |      |   |  3 || 4  |   |      | 5v      |     |
# |   3 |   SCL.1 |      |   |  5 || 6  |   |      | 0v      |     |
# |   4 | GPIO. 7 |      |   |  7 || 8  |   | ALT0 | TxD     | 14  |
# |     |      0v |      |   |  9 || 10 |   | ALT0 | RxD     | 15  |
# |  17 | GPIO. 0 |   IN |   | 11 || 12 |   | IN   | GPIO. 1 | 18  |
# |  27 | GPIO. 2 |   IN |   | 13 || 14 |   |      | 0v      |     |
# |  22 | GPIO. 3 |   IN |   | 15 || 16 |   | IN   | GPIO. 4 | 23  |
# |     |    3.3v |      |   | 17 || 18 |   | IN   | GPIO. 5 | 24  |
# |  10 |    MOSI |      |   | 19 || 20 |   |      | 0v      |     |
# |   9 |    MISO |      |   | 21 || 22 |   | IN   | GPIO. 6 | 25  |
# |  11 |    SCLK |      |   | 23 || 24 |   |      | CE0     | 8   |
# |     |      0v |      |   | 25 || 26 |   |      | CE1     | 7   |
# |   0 |   SDA.0 |      |   | 27 || 28 |   |      | SCL.0   | 1   |
# |   5 | GPIO.21 |   IN |   | 29 || 30 |   |      | 0v      |     |
# |   6 | GPIO.22 |   IN |   | 31 || 32 |   | IN   | GPIO.26 | 12  |
# |  13 | GPIO.23 |   IN |   | 33 || 34 |   |      | 0v      |     |
# |  19 | GPIO.24 |   IN |   | 35 || 36 |   | IN   | GPIO.27 | 16  |
# |  26 | GPIO.25 |  OUT |   | 37 || 38 |   | IN   | GPIO.28 | 20  |
# |     |      0v |      |   | 39 || 40 |   | IN   | GPIO.29 | 21  |
# +-----+---------+------+---+----++----+---+------+---------+-----+
# | BCM |   Name  | Mode |   | Physical |   | Mode | Name    | BCM |
# +-----+---------+------+---+----------+---+------+---------+-----+

import os
import socket
import paho.mqtt.client as mqtt
from time import sleep
import time
import datetime
import RPi.GPIO as GPIO
import paho.mqtt.publish as publish
from threading import Thread

#Configuration MQTT Topics
MQTT_SERVER             = "localhost"
#Output
MQTT_PATH_STATE         = "gpio/Output/Pin_"
MQTT_PATH_CONFIG = "gpio/Configuration"
#Input
MQTT_PATH_STATE_INPUT   = "gpio/Input/Pin_"

#PIR
MQTT_PATH_STATE_PIR     = "PIR/state"
MQTT_PATH_TIMESTAMP_PIR = "PIR/timestamp"
MQTT_PATH_DEBUG_PIR     = "PIR/text"
SENSOR_PIN_PIR = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN_PIR, GPIO.IN)

#init PIR states
PIR_active = 0
publish.single(MQTT_PATH_DEBUG_PIR, "Scanne ...", hostname=MQTT_SERVER)
publish.single(MQTT_PATH_STATE_PIR, "0", hostname=MQTT_SERVER)


SleepTimeL = 0.2
# |---- gpio 0-6 ---------|---- gpio 21-29 -------|
# BCM  Pins GPIO.0-6 GPIO.21-29 [17,18,27,22,23,24,25,5, 6 ,13,19,26,12,16,20,21]
# Phys Pins GPIO.0-6 GPIO.21-29 [11,12,13,15,16,18,22,29,31,33,35,37,32,36,38,40]
#
# Define configuration of input and output pins in BCM format und Pin number on expansion connector
#
OutputPinListBCM =  [17,18,27,22,23,24,25,05,06,13,19,26,12,16,20,21]
OutputPinListPhys = [11,12,13,15,16,18,22,29,31,33,35,37,32,36,38,40]
InputEnable  =      [ 0, 0, 0, 0, 0, 1, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0]

# Make you choice:
# 0=output   1=input   2=input with pullup    3=input with pulldown


# publish configuration of enabled outputs to MQTT_PATH_CONFIG_Pin
stringliste = ""
for i in OutputPinListBCM:
    stringliste = stringliste+str(i) + ":" + str(InputEnable[OutputPinListBCM.index(i)]) + " | "
#print stringliste
publish.single(MQTT_PATH_CONFIG, stringliste, hostname=MQTT_SERVER)

# INIT GPIO PORTS
# setup all gpio register if enabled to GPIO.OUT, else to GPIO.IN
# ===============================================================
#print "INIT GPIO PORTS\n"

for i in OutputPinListBCM:
   if InputEnable[OutputPinListBCM.index(i)] == 0:
        GPIO.setup(i, GPIO.OUT)
        # uncomment to setup initial states for all output to HIGH or LOW
        # GPIO.output(i, GPIO.HIGH)
        #print "Setting output for Pin_", i
        publish.single(MQTT_PATH_STATE + str(i), GPIO.input(i), hostname=MQTT_SERVER)
        publish.single(MQTT_PATH_STATE + str(i) + "/Info", "Configured as Output on Raspi-Connector Pin: " \
        + str(OutputPinListPhys[OutputPinListBCM.index(i)]) , hostname=MQTT_SERVER)
   if InputEnable[OutputPinListBCM.index(i)] == 1:
        GPIO.setup(i, GPIO.IN)
        #print "Setting input for Pin_", i
        publish.single(MQTT_PATH_STATE_INPUT + str(i), GPIO.input(i), hostname=MQTT_SERVER)
        publish.single(MQTT_PATH_STATE_INPUT + str(i) + "/Info", "Configured as Input on Raspi-Connector Pin: " \
        + str(OutputPinListPhys[OutputPinListBCM.index(i)]) , hostname=MQTT_SERVER)
   if InputEnable[OutputPinListBCM.index(i)] == 2:
        GPIO.setup(i, GPIO.IN, pull_up_down = GPIO.PUD_UP)
        #print "Setting input pull up for Pin_", i
        publish.single(MQTT_PATH_STATE_INPUT + str(i), GPIO.input(i), hostname=MQTT_SERVER)
        publish.single(MQTT_PATH_STATE_INPUT + str(i) + "/Info", "Configured as Input (pull up) on Raspi-Connector Pin: " \
        + str(OutputPinListPhys[OutputPinListBCM.index(i)]) , hostname=MQTT_SERVER)
   if InputEnable[OutputPinListBCM.index(i)] == 3:
        GPIO.setup(i, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
        #print "Setting input pull down for Pin_", i
        publish.single(MQTT_PATH_STATE_INPUT + str(i), GPIO.input(i), hostname=MQTT_SERVER)
        publish.single(MQTT_PATH_STATE_INPUT + str(i) + "/Info", "Configured as Input (pull down) on Raspi-Connector Pin:" \
        + str(OutputPinListPhys[OutputPinListBCM.index(i)]) , hostname=MQTT_SERVER)


# Define yor callback functions here, will be started as threads
#===============================================================
# must be called from def callback_input(channel), see below

#    if channel == SENSOR_PIN_PIR:
# if GPIO.input(SENSOR_PIN_PIR):     # if SENSOR_PIN_PIR == 1
#     t = Thread(target=callback_pir, args=(channel,)) # start extra thread for PIR detection
#        t.start()

def callback_pir(channel):
    global PIR_active
    now = datetime.datetime.now()
    if GPIO.input(SENSOR_PIN_PIR) and PIR_active == 0:     # if SENSOR_PIN == 1
       PIR_active = 1
       #print('Es gab eine Bewegung!')
       publish.single(MQTT_PATH_STATE_PIR, "1", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_DEBUG_PIR, "Bewegung erkannt!", hostname=MQTT_SERVER)
       #print now.strftime('%Y-%m-%d %H:%M:%S')
       publish.single(MQTT_PATH_TIMESTAMP_PIR, now.strftime('%Y-%m-%d %H:%M:%S'), hostname=MQTT_SERVER)
       # Variante nach Zeit (30s) bis wieder Bewegung erkannt werden soll:
       time.sleep(30)
       #print('Sleep vorbei. Scanne wieder ...')
       PIR_active = 0
       publish.single(MQTT_PATH_DEBUG_PIR, "Scanne ...", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_STATE_PIR, "0", hostname=MQTT_SERVER)

# The callback Function for reacting on signal changes on all inputs
#===================================================================

def callback_input(channel):
now = datetime.datetime.now()
#print ("callback_input" + str(channel))
#if OutputEnable[OutputPinListBCM.index(channel)] == 0 :
if InputEnable[OutputPinListBCM.index(channel)] in (1,2,3): # it is output

    if GPIO.input(channel): # if SENSOR_PIN of channel == 1 or high
       #print now.strftime('%Y-%m-%d %H:%M:%S') + " : " + (MQTT_PATH_STATE_INPUT + str(channel) + ': ON')
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/state" , "1", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/stateText" , "ON", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/timestamp_ON", now.strftime('%Y-%m-%d %H:%M:%S'), hostname=MQTT_SERVER)
       time.sleep(SleepTimeL);
    else: # if SENSOR_PIN of channel != 1 or low
       #print now.strftime('%Y-%m-%d %H:%M:%S') + " : " + (MQTT_PATH_STATE_INPUT + str(channel) + ': OFF')
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/stateText", "OFF", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/state", "0", hostname=MQTT_SERVER)
       publish.single(MQTT_PATH_STATE_INPUT + str(channel) + "/timestamp_OFF", now.strftime('%Y-%m-%d %H:%M:%S'), hostname=MQTT_SERVER)
       time.sleep(SleepTimeL);

    if channel == SENSOR_PIN_PIR:
if GPIO.input(SENSOR_PIN_PIR):     # if SENSOR_PIN_PIR == 1
    t = Thread(target=callback_pir, args=(channel,)) # start extra thread for PIR detection
        t.start()

# callback main, connect MQTT server. If message on gpio/Output/Pin_XX set output with value of state
# ===================================================================================================

def main():
  def on_connect(client, userdata, flags, rc):
client.subscribe("gpio/Output/#")
#print(" ... MQTT Server connected with result code " + str(rc)) + "\n ...  gpio/Output/# subscribed"

  def on_message(client, userdata, msg):
#print("Topic: " + msg.topic + " with Payload: " + str(msg.payload.decode("utf-8"))) + "!"
        mymsg = str(msg.payload.decode("utf-8"))
mytopic = str(msg.topic)
#print "Topic: " + mytopic + " with Payload: " + mymsg + "!"
now = datetime.datetime.now()

# Search for topic in List of available output pins (BCM names) and set gpio to state LOW or HIGH
        for i in OutputPinListBCM:
      #print ("Output/Pin_" + str(i))
        if mytopic == "gpio/Output/Pin_" + str(i) :
                if mymsg == "ON" or mymsg == "1" or mymsg == "on" :
                        GPIO.output(i, GPIO.LOW)
                        time.sleep(SleepTimeL);
                        #print "Pin_" + str(i) + " on"
        publish.single(MQTT_PATH_STATE + str(i) + "/stateText", "on", hostname=MQTT_SERVER)
                        publish.single(MQTT_PATH_STATE + str(i) + "/state", "1"     , hostname=MQTT_SERVER)
                        publish.single(MQTT_PATH_STATE + str(i) + "/timestamp_ON" , now.strftime('%Y-%m-%d %H:%M:%S'), hostname=MQTT_SERVER)
#print (MQTT_PATH_STATE + str(i) + "/|-- Timestamp ON:" +  now.strftime('%Y-%m-%d %H:%M:%S'))

                if mymsg == "OFF" or mymsg == "0" or mymsg == "off":
                        GPIO.output(i, GPIO.HIGH)
                        time.sleep(SleepTimeL);
                        #print "Pin_" + str(i) + " off"
  publish.single(MQTT_PATH_STATE + str(i) + "/stateText", "off", hostname=MQTT_SERVER)
                        publish.single(MQTT_PATH_STATE + str(i) + "/state", "0"      , hostname=MQTT_SERVER)
                      publish.single(MQTT_PATH_STATE + str(i) + "/timestamp_OFF"  , now.strftime('%Y-%m-%d %H:%M:%S'), hostname=MQTT_SERVER)
#print (MQTT_PATH_STATE + str(i) + "/|-- Timestamp OFF:" +  now.strftime('%Y-%m-%d %H:%M:%S'))

  client = mqtt.Client()
  client.on_connect = on_connect
  client.on_message = on_message
  client.connect("localhost", 1883, 60)
  client.loop_forever()



# create FHEM include file
# ========================
IPAdress = os.popen("hostname -I | cut -d ' ' -f 1").readlines()
MQTT_IP = " ".join(IPAdress)
MQTT_IP = MQTT_IP[:-1]
#print ("IP used for FHEM MQTT client: " + MQTT_IP)
MQTT_HOSTNAME = socket.gethostname()
#print ("HOSTNAME used for FHEM cfg: " + MQTT_HOSTNAME)
filename = "fhem.inc_" + socket.gethostname() + ".cfg"
#print (" ... exported FHEM cfg: " + filename)

f = file(filename,'w')

# define MQTT for devices
f.writelines('# -------------------------------------------------------------------------------\n')
f.writelines('# Version 1.0 18.4.2020 \n')
f.writelines('# -------- '+ MQTT_HOSTNAME + ' MQTT DEVICE ' + MQTT_IP + ' on port 1883 \n')
f.writelines('# -------------------------------------------------------------------------------\n')

f.writelines('define '+ socket.gethostname()+ 'MQTT MQTT ' + MQTT_IP + ':1883\n')
f.writelines('attr '+ MQTT_HOSTNAME + 'MQTT devStateIcon opened:remotecontrol/black_btn_GREEN disconnected:remotecontrol/black_btn_RED\n')
f.writelines('attr '+ MQTT_HOSTNAME + 'MQTT group ' + MQTT_HOSTNAME + ' MQTT,' + MQTT_HOSTNAME + ' DEVICE,' + MQTT_HOSTNAME + ' INPUT\n')
f.writelines('attr '+ MQTT_HOSTNAME + 'MQTT room PiNode MQTT\n')
f.writelines('attr '+ MQTT_HOSTNAME + 'MQTT alias  - MQTT host: ' + MQTT_HOSTNAME + '\n')


# PIR
f.writelines('\n# -------- '+ MQTT_HOSTNAME + ' PIR MQTT DEVICE -----------\n')
f.writelines('define '+ MQTT_HOSTNAME + '_PIR MQTT_DEVICE\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR IODev '+ MQTT_HOSTNAME + 'MQTT\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR autoSubscribeReadings PIR/+\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR devStateIcon 1:IR@red 0:IR@blue\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR group ' + MQTT_HOSTNAME + ' INPUT\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR icon mqtt\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR room PiNode MQTT\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR stateFormat state\n')
f.writelines('attr '+ MQTT_HOSTNAME + '_PIR alias PIR Sensor\n')

# NOTIFY to switch any FHEM Device
f.writelines('\n# NOTIFY ---------\n')
f.writelines('define '+ MQTT_HOSTNAME + '_PIR_n1 notify ' + MQTT_HOSTNAME + '_PIR:(0|1) { if (Value(' + MQTT_HOSTNAME + ') eq "1") { fhem ("set pinode1_GPIO_OUT_XX ON") } else { fhem ("set pinode1_GPIO_OUT_XX OFF") } } \n')
f.writelines('attr '  + MQTT_HOSTNAME + '_PIR_n1 group ' + MQTT_HOSTNAME + ' INPUT\n')
f.writelines('attr '  + MQTT_HOSTNAME + '_PIR_n1 disable 1\n')
f.writelines('attr '  + MQTT_HOSTNAME + '_PIR_n1 room PiNode MQTT\n')
f.writelines('attr '  + MQTT_HOSTNAME + '_PIR_n1 icon rc_SETUP\n')
f.writelines('attr '  + MQTT_HOSTNAME + '_PIR_n1 alias PIR Sensor ---> FHEM command\n')

f.writelines('\n# -------- '+ MQTT_HOSTNAME + ' GPIO MQTT DEVICES -----------\n')

for i in OutputPinListBCM:
# INPUT
if InputEnable[OutputPinListBCM.index(i)] in (1,2,3):
  f.writelines('\n# -------------------------------------------------------------------------------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' MQTT_DEVICE\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' IODev '+ MQTT_HOSTNAME + 'MQTT\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' devStateIcon 1:remotecontrol/black_btn_RED 0:remotecontrol/black_btn_BLUE\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' group ' + MQTT_HOSTNAME + ' INPUT\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' icon remotecontrol/black_btn_MENU2\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' room PiNode MQTT\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' alias GPIO IN ' + str(i) + '\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ' autoSubscribeReadings gpio/Input/Pin_' + str(i) + '/+\n')

# NOTIFY INPUT template e.g. switch off some time later
  f.writelines('\n# NOTIFY ---------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 notify ' + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + ':(0|1) { if (Value("' + MQTT_HOSTNAME +'_GPIO_IN_' + str(i) + '") eq "1") { fhem ("set pinode1_GPIO_OUT_XX_proxy ON") } else { fhem ("set pinode1_GPIO_OUT_XX_proxy OFF") } } \n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 group ' + MQTT_HOSTNAME + ' INPUT\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 disable 1\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 room PiNode MQTT\n')
  #f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 icon rc_SETUP\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_IN_' + str(i) + '_n1 alias GPIO IN ' + str(i) + ' ---> FHEM command \n')



# OUTPUT
if InputEnable[OutputPinListBCM.index(i)] == 0:
  #print " creating output for : " + str(i)
  f.writelines('\n# DEVICE -------------------------------------------------------------------------------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' MQTT_DEVICE\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' IODev '+ MQTT_HOSTNAME + 'MQTT\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' devStateIcon 1:rc_1:0 0:rc_0:1 on:rc_1:off off:rc_0:on\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' group ' + MQTT_HOSTNAME + ' MQTT\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' icon mqtt\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' room PiNode MQTT\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' autoSubscribeReadings gpio/Output/Pin_' + str(i) + '/+\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' publishSet on off gpio/Output/Pin_' + str(i) + '\n')
  f.writelines('attr   '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' alias ' + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' Device MQTT\n')

  # PROXY OUTPUT to enable FHEM commands like on-for-timer, blink a.s.o.
  f.writelines('\n# PROXY --------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy readingsProxy ' + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) +':state\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy devStateIcon on:remotecontrol/black_btn_RED:off off:remotecontrol/black_btn_BLUE:on\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy group ' + MQTT_HOSTNAME + ' DEVICE\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy icon remotecontrol/black_btn_MENU2\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy room PiNode MQTT\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy setFn {($CMD eq "on")?"on":"off"}\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy setList on off\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy valueFn {($VALUE == 0)?"off":"on"}\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy webCmd blink 10 1 : off-for-timer 1 : on-for-timer 1\n')
  f.writelines('attr '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_proxy alias GPIO OUT ' + str(i) + '\n')

  # NOTIFY OUTPUT template e.g. switch off some time later, assign to groups for display in DEVICE (below button) or separate list FUNCTIONS
  f.writelines('\n# NOTIFY ---------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 notify ' + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ':1 defmod meinTimer at +00:00:02 set ' + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ' 0\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 group ' + MQTT_HOSTNAME + ' DEVICE\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 disable 1\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 room PiNode MQTT\n')
  #f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 icon rc_SETUP\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n1 alias GPIO OUT ' + str(i) + ' Timer\n')

  # NOTIFY OUTPUT template e.g. switch FHEM Device
  f.writelines('\n# NOTIFY ---------\n')
  f.writelines('define '+ MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 notify ' + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + ':(0|1) { if (Value("' + MQTT_HOSTNAME +'_GPIO_IN_' + str(i) + '") eq "1") { fhem ("set pinode1_GPIO_OUT_XX ON") } else { fhem ("set pinode1_GPIO_OUT_XX OFF") } } \n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 group ' + MQTT_HOSTNAME + ' FUNCTIONS\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 disable 1\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 room PiNode MQTT\n')
  #f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 icon rc_SETUP\n')
  f.writelines('attr '  + MQTT_HOSTNAME + '_GPIO_OUT_' + str(i) + '_n2 alias GPIO OUT ' + str(i) + ' ---> FHEM command\n')
  f.flush()

  # service pir restart ; scp fhem.inc_pinode1.cfg aio:/opt/fhem/FHEM
  # ssh aio 'chown fhem:dialout  /opt/fhem/FHEM/fhem.inc_pinode1.cfg'


if __name__ == '__main__':
try:
#callbacks  for all input signals
#print ("main started")
for i in OutputPinListBCM:
# if output not enabled  it is input, then create call back for this Pin
    # if OutputEnable[OutputPinListBCM.index(i)] == 0:
    if InputEnable[OutputPinListBCM.index(i)] in (1,2,3) : # it is input
GPIO.add_event_detect(i , GPIO.BOTH, callback=callback_input)
# print ("input" + str(i))
  main()
except KeyboardInterrupt:
GPIO.cleanup()

aktuelles FHEM auf XEN Server, Debian VM,  usb-redirector (XEN host -> VM):  (868 MHz) CUL V3 + (868 MHz) CUL V3

Nodes: RPi with shairport-sync, cLirc, mjpg-streamer, mpd, hostapd, hifiberry, TTS (picowave), NOOBS Multiboot (raspian + openelec)

87insane

Danke! Finde ich nicht schlecht!

Gesendet von meinem LM-G810 mit Tapatalk