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;
}
}
}
This question is old but here is how I got my pins to work using the WiringPi library.
Referring to this question.
I have this code below which works when running in a SSH shell but not after the device reboots and runs as a cronjob after reboot.
public static String cmdCommands()
{
try
{
//blink LED; works in shell only
Runtime r = Runtime.getRuntime();
r.exec("gpio mode 4 out");
r.exec("gpio write 4 1");
Thread.sleep(500);
r.exec("gpio write 4 0");
Thread.sleep(500);
return "done";
}
catch(Exception e)
{
return "Exception: " + e.getMessage();
}
}
To fix this basically I had to use String[]
and build the command. Very easy after struggling to find a fix
Working Code example here:
public static String cmdCommands()
{
try
{
//blink LED
Runtime r = Runtime.getRuntime();
r.exec(new String[] {"sudo", "gpio", "mode", "4", "out"});
r.exec(new String[] {"sudo", "gpio", "write", "4", "1"});
Thread.sleep(500);
r.exec(new String[] {"sudo", "gpio", "write", "4", "0"});
return "done";
}
catch(Exception e)
{
return "Exception: " + e.getMessage();
}
}
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.