""" This script initializes pin 4 on the raspberry pi
    as an input.

    After that, it sets up a UDP socket and starts an
    endless loop.

    In the loop, it reads the status of pin 4 every
    second and sends a packet with the state to the
    server.

    V0.01  28-AUG-2014 Te
"""

import RPi.GPIO as GPIO
import socket
import time

pin = 4

GPIO.setmode( GPIO.BCM )
GPIO.setup( pin, GPIO.IN )

ip   = "192.168.1.203"
port = 54345
sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )

while True:
    if GPIO.input(pin):
        sock.sendto( "on", (ip,port) )
    else:
        sock.sendto( "off", (ip,port) )

    time.sleep( 1 )
