raspbbery pi pi4j could not work by v4l2(camera driver)

189 views Asked by At

in the a project, a NOIR pi camera and work it by java and eclipse true. and need to turn on IR-LED when camera is start to preview. so use pi4j in a new class for turn on and turn off LED. but when call it the pi4j class in source of camera panel, then camera not started. what is the problem

pi4j Class:

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");
                        }
                }
        }

error:

wiringPiSetup: Must be root. (Did you forget sudo?)
1

There are 1 answers

0
behnam heidary On BEST ANSWER

The problem was solved with python source:

changed java class to:

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();
        }
}

and 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()

so now problem is about java class return reader.readline() null. and led power on and off not worked, but python /home/pi/noirLedControl.py currently run in terminal. my java lib version is "1.8.0_122-ea", so i run my project by two java SE 1.8 and 1.7 and problem not solved. and os is debian 8 every one know what is problem?