Getting funky with Pi Push a button and an LED lights up1 and sends a signal to another computer that plays a song.

First project on the Raspberry pi, really simple but definitely fun and educational to implement. Also, second time using python and sockets so I'd especially appreciate input on anything I do inefficiently.2


Mostly feedback to ensure that the program is running/button is active.
I know it's straightforward, but this site has shown me that even the simplest things leave room to be executed much more elegantly. 


SongServer.java:

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.awt.Desktop;

public class SongServer {
    final static int PORT = 9001;

    public static void main(String[] args) {
        try(ServerSocket server = new ServerSocket(PORT)) {
            System.out.println(new Date() + "\nServer online.");

            while(true) {
                new Thread(new ClientHandler(server.accept())).start();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private static class ClientHandler implements Runnable {
        private Socket socket;
        private BufferedReader in;
        private PrintWriter out;

        ClientHandler(Socket socket) {
            this.socket = socket;
            System.out.println("Client connected!");
        }

        @Override
        public void run() {
            try {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(), true);

                out.println("Connection success");
                while (true) {
                    String input = in.readLine();
                    if (input == null || input.trim().isEmpty()) {
                        continue;
                    }
                    System.out.println(input);

                    if (input.startsWith("play")) {
                        Desktop.getDesktop().open(new File("uptown_funk.mp3"));
                    }
                }
            } catch (Exception e) {
                System.out.println("Client disconnected!");
            }
        }
    }
}


funk_button.py:

import RPi.GPIO as GPIO
import socket

funk_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
funk_socket.connect(("10.0.0.18", 9001))
sent = False

button = 17
led = 22

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.setup(button, GPIO.IN)

while True:
    if GPIO.input(button):
        GPIO.output(led, GPIO.LOW)
    else:
        GPIO.output(led, GPIO.HIGH)
        if not sent:
            funk_socket.send(bytearray("play\r\n", "utf-8"))
            sent = True
Looks good as a proof of concept. I recommend to modularize python code, that is


Make socket interface into a function
Make GPIO interface into a function
Have a

if __name__ == '__main__'


clause to call them (directly or via a main) function.


I would also handle failures to connect (retry maybe?); a total fun is to implement service discovery via pybonjour or zeroconf.