Read status GPIO pins raspberry pi

Einklappen
X
 
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge
  • K.Clemens
    Smart Home'r
    • 28.08.2015
    • 92

    #1

    Read status GPIO pins raspberry pi

    I tried searching the German section of this forum, but without any luck. (Bad is an understatement of my knowledge of German!)

    Has anybody already tried to read out the status of the GPIO pins of an raspberry pi into Loxone?
    I want to use a raspberry pi in my cabin in my garden to arrange the lights and music. I want to control the lights via a physical button on the pi, but I also want to put the lights to off with the all off button in my house.

    Anybody any idea what would be a good start?
  • Jan W.
    Lox Guru
    • 30.08.2015
    • 1363

    #2
    Yes, I'm using this to forward information from 1-wire sensors that are directly connected to the raspberry pi to the MS. On the raspberry you need something like (example with python code):

    Code:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import socket
    import RPi.GPIO as GPIO
    
    # use RPi.GPIO Layout  (same as pin numbers)
    GPIO.setmode(GPIO.BOARD)
    
    # use pin 29 (BCM_GPIO 5) as output
    GPIO.setup(29, GPIO.OUT)
    
    # use pin 37 (BCM_GPIO 26) as input
    GPIO.setup(37, GPIO.IN)
    
    # IP address and UDP port of Loxone Miniserver
    MINISERVER_IP = "192.168.1.10"
    UDP_PORT = 7700
    
    # create UDP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    ... other init stuff
    
    while True:
       # main loop
       ...
       # the following command sends out the variable temp_c that contains a temperature value to the MS
       sock.sendto( "<ID=Sensor1 Value=" + str( temp_c ) + " >", ( MINISERVER_IP, UDP_PORT ) )
    
       # turn out a LED (connected to GPIO pin on the raspi)
       GPIO.output(29, GPIO.LOW)
    
       if GPIO.input(37) == GPIO.HIGH:
          sock.sendto( "<ID=Reed1 Value=1 >", ( MINISERVER_IP, UDP_PORT ) )
       else:
          sock.sendto( "<ID=Reed1 Value=0 >", ( MINISERVER_IP, UDP_PORT ) )
    
       time.sleep(0.1)
    On the MS you need a virtual UDP input with the same port as on the pi side and underneath a virtual UDP input command with e.g. <ID=Sensor1 Value=\v
    or <ID=Reed1 Value=\v

    Even if you only need digital inputs e.g. 0 and 1, it might be better to configure the virtual UDP input as analog and get the value with \v than to use a digital input. In this case the virtual input stays on "1" until a "0" is received.

    Digital inputs would only trigger a short impulse if a string like <ID=button1> is received. They don't have a value that you can/need to read. The virtual UDP input command that is used on the MS as a virtual input does not stay on a value like 1. In case of a button that is pushed on the pi it may work, of course.

    This is just an example that I've put together from two python scripts I am using (hopefully without a syntax error). So far I only need to send information to the MS, but not the other way around. There are a couple of other threads in this forum (try google translate for e.g. https://www.loxforum.com/forum/faqs-...nden#post43610 . Even if the translation might not be that good, the code example and screenshots should help.

    Cheers,
    Jan

    Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
    Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
    Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
    Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
    Node-RED: IKEA Tradfri

    Kommentar

    • K.Clemens
      Smart Home'r
      • 28.08.2015
      • 92

      #3
      Thank you for your reply! It is really helpfull.

      However, when I use the code below I get following error:
      Code:
       python relay_control_via_loxone.py
      commando1
      UDP target IP: 192.168.0.xx
      
      [B]Traceback (most recent call last):
        File "relay_control_via_loxone.py", line 28, in <module>
          message = RecvUdp (1235)
        File "relay_control_via_loxone.py", line 20, in RecvUdp
          sock.bind((UDP_IP2, UDP_PORT))
        File "/usr/lib/python2.7/socket.py", line 224, in meth
          return getattr(self._sock,name)(*args)
      socket.error: [Errno 22] Invalid argument[/B]
      Code:
      #!/usr/bin/env python
      
      import RPi.GPIO as GPIO
      import time
      import socket
      
      GPIO.setwarnings(False)
      
      UDP_IP = "192.168.0.xx"
      UDP_IP2 = "192.168.0.xxx"
      
      sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
      def SendUdp( UDP_PORT, sent_data ):
      
         sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
         sock.sendto(sent_data, (UDP_IP, UDP_PORT))
      
      def RecvUdp( UDP_PORT ):
      
         sock.bind((UDP_IP2, UDP_PORT))
         recv_data , addr = sock.recvfrom(1235)
         return recv_data
      
      #Verstuur udp pakket naar Loxone poort 1234
      #SendUdp(1234,"Test123")
      
      while True:
          message = RecvUdp (1235)
          print message
      
          print "UDP target IP:", UDP_IP
          time.sleep(1)

      Kommentar

      • Jan W.
        Lox Guru
        • 30.08.2015
        • 1363

        #4
        Well, you should do some (online) python training to get more familiar with that programming language and the socket interface. Im not the expert on this matter, but if I read the explanation of ".bind" (see https://docs.python.org/2/library/so....socket.bind):
        Bind the socket to address. The socket must not already be bound...

        In your code the ".bind" is in the RecvUdp function that is called multiple times - that is not allowed, because the socket is already bound to the IP address. You should move the sock.bind command out of the RecvUdp function and move it to the line after the socket.socket command.
        Miniserver v14.5.12.7, 2x Ext., 2x Relay Ext., 2x Dimmer Ext., DMX Ext., 1-Wire Ext., Gira KNX Tastsensor 3 Komfort, Gira KNX Präsenzmelder, Fenster- und Türkontakte, Loxone Regen- und Windsensor, Gira Dual Q Rauchmelder vernetzt, 1x Relais-Modul
        Loxberry: SmartMeter, MS Backup, CamConnect, Weather4Lox
        Lüftung: Helios KWL EC 370W ET mit Modbus TCP - via Pico-C
        Heizung: Stiebel Eltron WPF 5 cool (Sole-Wasser WP) mit ISG, FB-Heizung mit 18 Kreisen, Erdsonde - via modbus/TCP
        Node-RED: IKEA Tradfri

        Kommentar

        • K.Clemens
          Smart Home'r
          • 28.08.2015
          • 92

          #5
          Thanks for the reply. I searched the internet for this error, but you solved it Thank you!

          Kommentar

          Lädt...