Pregunta

I have a Raspberry Pi rev. 2 (512mb ram version). I have some LEDs that I am controlling over Pi4J. Every LED works except the ones that i attach/connect to pin 21/27. I have changed the LEDs to see if one is broken but it was not broken. I did some research on google and found this. Unfortunately I could not find a way to change Pin21 to 27 because Pi4j uses another numbering scheme. (For numbering see here)

I couldn't find an error in my code but it looks like this:

package gpio;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

public class GPIOController {

    final long second = 1000L;

    final GpioController gpio = GpioFactory.getInstance();

    final GpioPinDigitalOutput red_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_03);
    final GpioPinDigitalOutput red_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_02);

    final GpioPinDigitalOutput yellow_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_00);
    final GpioPinDigitalOutput yellow_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_07);

    final GpioPinDigitalOutput green_1 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_06);
    final GpioPinDigitalOutput green_2 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_05);
    final GpioPinDigitalOutput green_3 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_04);
    final GpioPinDigitalOutput green_4 = gpio
        .provisionDigitalOutputPin(RaspiPin.GPIO_01);

    public GPIOController() {
        gpio.shutdown();
    }

    // level 1 = red_1, level 8 = green_4
    public void pulse(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.pulse(second);
            break;
        case 2:
            red_2.pulse(second);
            break;
        case 3:
            yellow_1.pulse(second);
            break;
        case 4:
            yellow_2.pulse(second);
            break;
        case 5:
            green_1.pulse(second);
            break;
        case 6:
            green_2.pulse(second);
            break;
        case 7:
            green_3.pulse(second);
            break;
        case 8:
            green_4.pulse(second);
            break;
        }
    }

    public void high(int level) {
        allLow();
        switch(level) {
        case 1:
            red_1.high();
            break;
        case 2:
            red_2.high();
            break;
        case 3:
            yellow_1.high();
            break;
        case 4:
            yellow_2.high();
            break;
        case 5:
            green_1.high();
            break;
        case 6:
            green_2.high();
            break;
        case 7:
            green_3.high();
            break;
        case 8:
            green_4.high();
            break;
        }
    }

    public void allLow() {
        gpio.shutdown();
    }

}

I call the methods through this class:

package connection;

import gpio.GPIOController;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Receiver {

    private ServerSocket server;
    private Socket client;
    private DataInputStream clientIn;
    private DataOutputStream clientOut;

    private GPIOController gpioController = new GPIOController();

    public Receiver(int port) throws IOException {
        server = new ServerSocket(port);
        listenForClient();
    }

    // client listener in a new thread
    public void listenForClient() {
        Runnable listener = new Runnable() {
            public void run() {
                while (true)
                    try {
                        // only the most recently connected client
                        // can control the LEDs
                        client = server.accept();
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }

            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Client Listener");
        listenerThread.start();
    }

    // command listener in a new thread
    public void listenForCommands() {
        final Runnable listener = new Runnable() {
            public void run() {
                while (true) {
                    try {
                        if (client != null) {
                            DataInputStream clientIn = new DataInputStream(
                                client.getInputStream());
                            String msg = clientIn.readUTF();
                            handleMessage(msg);
                        }
                    } catch (IOException e) {
                        e.printStackTrace(System.out);
                    }
                }
            }
        };
        Thread listenerThread = new Thread(listener);
        listenerThread.setName("Command Listener");
        listenerThread.start();
    }

    public void handleMessage(String msg) {
        String[] command = msg.split(" ");
        String commandType = command[0]; // "pulse" or "permanent"
        for (int i = 1; i < command.length; i++) {
            try {
                if (commandType.equals("pulse"))
                    gpioController.pulse(Integer.parseInt(command[i]));
                else
                    gpioController.high(Integer.parseInt(command[i]));
            } catch (Exception e) {
                try {
                    clientOut = new DataOutputStream(client.getOutputStream());
                    clientOut.writeUTF(e.getMessage());
                } catch (IOException ioe) {
                    ioe.printStackTrace(System.out);
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            new Receiver(8374).listenForCommands();
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
    }

}

Is there anything I can do to get that Pin working (including quick & dirty solutions)? By the way; I do not want to use the P5 Pins because I use a PiCobbler. I also want to use Java for this. Thanks in advance.

Selim

¿Fue útil?

Solución

So, It was a bug related to the distro I guess. sudo apt-get dist-upgrade fixed it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top