Page 1 of 1

Installing pi4j on Rasp pi 3

Posted: Wed Jun 29, 2016 2:24 pm
by fkaspero
Hi,

There's a new launch of pi4j that works for the pi3 (it can be seen here http://pi4j.com/download.html it's the 1.1 Snapshot version), but I'm having a hard time installing it.

It doesn't seem to be possible to download this version directly through the terminal, so I downloaded the debian installer package and, after executing it, it just went on... forever. Ok, I moved on and tried the zip archive and when I tried to use Xarchiver to unzip it in the correct file (/opt/pi4j) the raspberry pi didn't let me.

Finally, the doubt: is there any command line to unzip the file in the correct place, even though raspberry pi doesn't seem to let me change the /opt file?

Re: Installing pi4j on Rasp pi 3

Posted: Wed Jun 29, 2016 8:21 pm
by SonOfAMotherlessGoat
How did you execute the .deb? Did you use the `dpkg` command?

If you are having troubles with the debian package, I would suggest not trying to unzip directly into the /opt directory, you may require elevated privileges to unpack into that directory, and you lose the benefits of using a package manager to do the installation.

Please update us with the exact commands you used to try to install the debian package, and we can help you with getting that installed.

Re: Installing pi4j on Rasp pi 3

Posted: Thu Jun 30, 2016 7:33 am
by fkaspero
I had completely forgotten about the dpkg command. Used it and worked, pi4j installed in the correct directory. Thanks!

Re: Installing pi4j on Rasp pi 3

Posted: Thu Jun 30, 2016 9:42 am
by fkaspero
Actually, way too soon to say it worked. I used the command

Code: Select all

sudo dpkg -i /home/pi/Downloads/pi4j-1.1-SNAPSHOT.deb
The terminal showed:

Code: Select all

Preparing to unpack .../pi4j-1.1-SNAPSHOT.deb ...
Unpacking pi4j (1.1~SNAPSHOT) over (1.1~SNAPSHOT) ...
upgrade
Setting up pi4j (1.1~SNAPSHOT)
And there was created a file pi4j in /opt, as it should. In /opt/pi4j/lib I have 4 .jar files (pi4j-core, pi4j-device, pi4j-gpio-extension, pi4j-service).
I did a small test program using the libraries in Eclipse in my PC (I referenced them in the project), compiled it (no errors) and passed the .class file generated to the Raspberry Pi. The file is Interface.class and the package is called testeEnvio. I tried to execute it with the following command:

Code: Select all

java Interface
And I got:

Code: Select all

Error: could not find or load main class Interface
I figured it was because I forgot the pachage, so I tried:

Code: Select all

java testeEnvio.Interface
Apparently, it could not get the pi4j library:

Code: Select all

Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
	at testeEnvio.Interface.<init>(Interface.java:14)
	at testeEnvio.Interface.main(Interface.java:30)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 2 more
I copied the code to Geany in the pi using the pi4j library. The libraries are imported like that:

Code: Select all

import com.pi4j.io.gpio.GpioController
And I got errors because it says the packages I'm trying to import do not exist. Any idea?

[EDIT]: To be more specific, this is my code

Code: Select all

package testeEnvio;

import java.awt.*;
import java.awt.event.*;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalMultipurpose;
import com.pi4j.io.gpio.PinMode;
import com.pi4j.io.gpio.RaspiPin;

public class Interface extends Frame implements WindowListener 
{
	final GpioController gpio = GpioFactory.getInstance();
	private Button btnRead, btnOn, btnOff;
    public TextField dataRead;
    final GpioPinDigitalMultipurpose mypin = gpio.provisionDigitalMultipurposePin(RaspiPin.GPIO_01, PinMode.DIGITAL_OUTPUT);

    public Interface() 
    {
    	setLayout(new FlowLayout());
        add(new Label("Test"));
        addWindowListener(this);
        
        dataRead = new TextField("0", 3);
        dataRead.setEditable(false);
        add(dataRead);
        
        btnRead = new Button("Read");
        add(btnRead);
        
        btnOff = new Button("Off");
        add(btnOff);
        
        btnOn = new Button("On");
        add(btnOn);
        
        setTitle("Test");
        setSize(400, 100);
        setVisible(true);
        final GpioController gpio = GpioFactory.getInstance();        	
    }
    
    private class BtnListener implements ActionListener
    {
         @Override
         public void actionPerformed(ActionEvent evt)
         {
             Button source = (Button)evt.getSource();
             
             if (source == btnOn){
            	mypin.high();
                }
             else if (source == btnOff){
                mypin.low();
                }
             else {
                 mypin.setMode(PinMode.DIGITAL_INPUT);
                 
                 if (mypin.getState().isHigh()){
                	 dataRead.setText("1");
                 }
                 else if (mypin.getState().isLow()){
                	 dataRead.setText("00");
                 }

                 mypin.setMode(PinMode.DIGITAL_OUTPUT);
            	 
             }
         }
    }
    
    public static void main(String[] args) 
    {
        new Interface();
    }
    
    @Override
    public void windowClosing(WindowEvent evt) {
       System.exit(0);  
    }
  
    @Override public void windowOpened(WindowEvent evt) { }
    @Override public void windowClosed(WindowEvent evt) { }
    @Override public void windowIconified(WindowEvent evt) { }
    @Override public void windowDeiconified(WindowEvent evt) { }
    @Override public void windowActivated(WindowEvent evt) { }
    @Override public void windowDeactivated(WindowEvent evt) { }
}
Btw, I tried removing the nested class, it didn't change a thing :/

Re: Installing pi4j on Rasp pi 3

Posted: Wed Jul 27, 2016 7:34 pm
by savageautomate
If the Pi4J JAR's are not already included in your Java programs classpath you can optionally include a "-classpath" argument to the "java" command line to tell Java to include the classpath for the Pi4J classes.

Code: Select all

sudo java -classpath .:classes:/opt/pi4j/lib/'*' testeEnvio.Interface
Or you could copy the Pi4J JARs to your local program's path. Hint: typically you only need the pi4j-core.jar file if you are just using basic GPIO, SPI, I2C, SERIAL functionality.

Thanks, Robert