0

I am trying to use the GPIO pins on the Raspberry Pi. Currently I am using ProcessBuilder to execute the commands. However, whenever I run it it always says:

Exception in thread "main" java.io.IOException: Cannot run program "echo 18 > /sys/class/gpio/export": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
        at Testing.initiate(Testing.java:32)
        at Testing.main(Testing.java:8)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
        at java.lang.ProcessImpl.start(ProcessImpl.java:130)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
        ... 2 more

Here is the code I am using:

    ProcessBuilder pb = new ProcessBuilder("echo " + gpiopin + " > /sys/class/gpio/export");
    ProcessBuilder pb1 = new ProcessBuilder("echo out > /sys/class/gpio/gpio" + gpiopin + "/direction");
    ProcessBuilder pb2 = new ProcessBuilder("echo 1 > /sys/class/gpio/gpio" + gpiopin + "/value");
    pb.redirectErrorStream(true);
    pb1.redirectErrorStream(true);
    pb2.redirectErrorStream(true);
    Process ps = pb.start();
    Process ps1 = pb1.start();
    Process ps2 = pb2.start();

Why is this happening? Also, if there is a better way of doing this how?

Thanks

Edit: Seems like there are no more errors, but the commands are not running. Could someone please look at my code to make sure i'm not being stupid?

import java.util.Scanner;


public class Testing {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        initiate(18);

        while(true) {
            String input = scan.nextLine();

            if(input.equals("on")) {
                System.out.println("on");
                bellOn();
            }else if(input.equals("off")) {
                System.out.println("off");
                bellOff();
            }
        }
    }

    static int gpio = 18;
    static String gp = "" + gpio;
    static boolean initiated = false;

    public static boolean initiate(int gpiopin) throws Exception {
        gpio = gpiopin;
        ProcessBuilder pb = new ProcessBuilder("echo", gp, ">", "/sys/class/gpio/export");
        ProcessBuilder pb1 = new ProcessBuilder("echo", "out", ">", "/sys/class/gpio/gpio" + gp + "/direction");
        //ProcessBuilder pb2 = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gpiopin + "/value");
        pb.redirectErrorStream(true);
        pb1.redirectErrorStream(true);
        //pb2.redirectErrorStream(true);
        Process ps = pb.start();
        Process ps1 = pb1.start();
        //Process ps2 = pb2.start();
        initiated = true;
        return true;
    }

    public static boolean bellOn() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "0", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }

    public static boolean bellOff() throws Exception {
        if(initiated != true) {
            return false;
        }else{
            ProcessBuilder pb = new ProcessBuilder("echo", "1", ">", "/sys/class/gpio/gpio" + gp + "/value");
            pb.redirectErrorStream(true);
            Process ps = pb.start();
            return true;
        }
    }
}
Tore Olsen
  • 1,983
  • 2
  • 20
  • 32
cheese5505
  • 922
  • 5
  • 13
  • 28
  • Found something here, maybe it could help you out? http://stackoverflow.com/questions/10735415/process-builder-gives-a-no-such-file-or-directory-on-mac-while-runtime-exec – ihsan Sep 05 '14 at 10:18
  • Seems there are no more errors, but I think I have done somethign wrong in the command. – cheese5505 Sep 05 '14 at 10:25
  • You should do something like `... new ProcessBuilder("echo " + gpiopin, ">", "/sys/class/...");`, I suppose. – ihsan Sep 05 '14 at 10:29
  • I just added more code to my main post - please havfe a look at it – cheese5505 Sep 05 '14 at 10:32
  • 2
    I do this in C++, but I just open /sys/class/gpio as a file. Can't you do this in Java? That would save all the messing around with ProcessBuilder.. – Greycon Sep 05 '14 at 11:42
  • Have you considered Pi4J? http://pi4j.com/ – Dropout Sep 05 '14 at 12:29
  • @Greycon I will try that, thanks. – cheese5505 Sep 05 '14 at 19:56
  • @Dropout I will use that as a back up plan as I don't really want to mess around with external libraries and would rather keep it in one piece of code. – cheese5505 Sep 05 '14 at 19:56

1 Answers1

-1

Here is how i do this in C++, I'm pretty sure you can translate to Java...

void ResetCommand::execute() {
    // Perform a hardware reset on the Arduino using GPIO pins
    ofstream fd;
    char buf[255];

    cout << "Exporting GPIO Pin 8" << endl;
    fd.open("/sys/class/gpio/export", ios::out);
    fd << "8";
    fd.close();

    // Set out direction
    cout << "Setting as output" << endl;
    fd.open("/sys/class/gpio/gpio8/direction", ios::out);
    fd << "out";
    fd.close();

    // Blip the reset pin
    cout << "Resetting... " << endl;
    fd.open("/sys/class/gpio/gpio8/value", ios::out);
    fd << 0;
    usleep(1);
    fd << 1;
    fd.close();

    // Finished, so free up the pin
    cout << "Releasing " << endl;
    fd.open("/sys/class/gpio/unexport", ios::out);
    fd << "8";
    fd.close();
}
Greycon
  • 749
  • 6
  • 19