在一个项目中,一个NOIR pi相机并通过java和eclipse真实地工作。当相机开始预览时需要打开IR-LED。所以在新课程中使用pi4j打开并关闭LED。但是当它在相机面板的源头中称为pi4j类时,则相机无法启动。有什么问题
pi4j班级:
import com.pi4j.io.gpio.*;
public class gpio_prg {
private static GpioPinDigitalOutput pin;
private GpioController gpio;
public void out(int bcmn, boolean state){
System.out.println("gpio controler");
gpio = GpioFactory.getInstance();
if(bcmn == 29){
if(state){
System.out.println("gpio pin");
pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_29, "MyLED", PinState.HIGH);
pin.setShutdownOptions(true, PinState.LOW);
System.out.println("--> GPIO NOIR LED state should be: ON");
}else{
pin.low();
System.out.println("--> GPIO NOIR LED state should be: OFF");
}
}
}
错误:
wiringPiSetup: Must be root. (Did you forget sudo?)
答案 0 :(得分:0)
使用python源解决了这个问题:
将java类更改为:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class gpio_prg {
public void out() throws IOException, InterruptedException{
String command = "python /home/pi/noirLedControl.py";
Process proc = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
System.out.print("led status = ");
System.out.println(reader.readLine());
proc.waitFor();
}
}
和noirLedControl.py
:
import RPi.GPIO as GPIO
f = open('ledstatus', 'r')
s = 3
s = f.read()
f.close()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
if(s == '0'):
GPIO.output(21, GPIO.LOW)
print "off"
s = 1
else:
GPIO.output(21, GPIO.HIGH)
print "on"
s = 0
f = open('ledstatus', 'w')
f.write(str(s))
f.close()
所以现在问题是java类返回reader.readline()
null。并且电源开启和关闭都没有工作,但python /home/pi/noirLedControl.py
目前在终端运行。
我的java lib版本是" 1.8.0_122-ea",所以我用两个java SE 1.8和1.7运行我的项目,问题没有解决。和os是debian 8
每个人都知道什么是问题?