import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalInput; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.PinPullResistance; import com.pi4j.io.gpio.RaspiPin; import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; import com.pi4j.io.gpio.event.GpioPinListenerDigital; import java.io.*; import java.net.*; public class ButtonLedNetwork { private final static int PORT = 12345; private final static String HOST = "raspberrypi.local"; public static void main(String[] args) throws InterruptedException, IOException, UnknownHostException { Socket socket = new Socket(HOST, PORT); final GpioController gpioCtrl = GpioFactory.getInstance(); final GpioPinDigitalInput button = gpioCtrl.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_UP); final GpioPinDigitalOutput pin = gpioCtrl.provisionDigitalOutputPin(RaspiPin.GPIO_01, "Red led", PinState.LOW); button.setShutdownOptions(true); pin.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF); System.out.println("Ready to take off!"); button.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { try { if (event.getState() == PinState.LOW) { System.out.println("Bouton enfoncé"); pin.high(); socket.getOutputStream().write("run".getBytes("UTF-8")); } else if (event.getState() == PinState.HIGH) { System.out.println("Bouton relaché"); pin.low(); socket.getOutputStream().write("stop".getBytes("UTF-8")); } } catch (IOException e) { System.out.println("Error send command: " + e.getMessage()); } } }); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { socket.close(); gpioCtrl.shutdown(); } catch (IOException e) { System.out.println("Error close socket: " + e.getMessage()); } } }); while (true) { Thread.sleep(500); } //clientSocket.close(); //gpioCtrl.shutdown(); } }