User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Pi4J - A Java library to interface with the Raspberry Pi

Fri Sep 21, 2012 1:37 pm

Hear Ye all fellow Java programmers ...

Image

Announcing the Pi4J project!

This project is intended to provide a bridge between the native hardware and Java for full access to the Raspberry Pi in with a Java-friendly object-oriented approach. Pi4J is an open source project developed by professional software engineers. In addition to the basic raw hardware access functionality, this project also attempts to provide a set of advanced features that make working with the Raspberry Pi an easy to implement and more convenient experience for Java developers.

Project Website: http://www.pi4j.com
Source Repository: https://github.com/Pi4J/pi4j

Basic Features
  • Export & unexport GPIO pins
    Configure GPIO pin direction
    Configure GPIO pin edge detection
    Control/write/set GPIO pin states
    Read GPIO pin states
    Send & receive data via RS232 serial communication
Advanced Features
  • Pulse GPIO pin states
    Listen for GPIO pin state changes (interrupt-based events; not polling)
    Automatically set GPIO states on program termination (GPIO shutdown)
    Triggers for automation based on pin state changes
    Access system information and network information from the Raspberry Pi
    Wrapper classes for direct access to WiringPi Library from Java
Getting Started

To get started using the Pi4J library, please see the Usage page and review each of the examples below to explore the functionality provided by the Pi4j library.

This project uses Gordon's Wiring Pi native library under the hood. Pi4J also exposed a direct JNI wrapper of static methods around the Wiring Pi functions if you prefer to go the low-level route.

Please let us know what you think about the project! Let us know if there is anything missing or that can be improved on.

Thanks!
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Fri Sep 21, 2012 2:09 pm

Thank you! This project looks great for all Raspberry Pi Java IO needs!
There have been a lot of request to access I2C from Java on the forum, this looks like a perfect project to implement it!

Cheers and have a great day! :)
Xerxes
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Fri Sep 21, 2012 2:43 pm

xranby wrote:Thank you! This project looks great for all Raspberry Pi Java IO needs!
There have been a lot of request to access I2C from Java on the forum, this looks like a perfect project to implement it!

Cheers and have a great day! :)
Xerxes
Thanks for checking out the project! Pi4J certainly wraps all of the Wiring Pi exposed functionality (http://pi4j.com/apidocs/index.html?com/ ... mmary.html). Most of the time thus far has been concentrated on trying to provide a more OO style interface around GPIO access. But moving forward, we certainly want to provide additional Java-friendly interfaces for the other hardware capabilities as well.
Thanks again!
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Fri Sep 21, 2012 2:56 pm

FYI,

I will be posting a series of articles on my blog site that will help further demonstrate how to use the Pi4J library.

http://www.savagehomeautomation.com/pro ... ry-pi.html

... stay tuned for more ...
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 11:06 am

I'm looking forward to trying this out. Currently I use framboos' library which if I remember correctly also uses wiring PI but lacks interrupts and closing states both of which would be very useful to me.

I'll let you know how I get on trying it.

User avatar
freedomotic
Posts: 154
Joined: Sat Apr 21, 2012 3:59 pm
Location: Italy
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 12:18 pm

Hi,
very interesting library. We'll try it for a new plugin for Freedomotic project.
Regards
Freedomotic Open IoT Framework
http://freedomotic.com

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 3:54 pm

Just a quick note to say I've now implemented this in my already working boiler control project to replace the Framboos wiringpi wrapper.

So far so good, its fast enough to support a 16x2 lcd which rpi-gpio-java wasn't (but framboos was). I like the ability to set a pin shutdown state - Framboos left pins in a bit of a random state.

Do I need any special kernels to support gpio interrupts as I'd like to setup these next to replace my input button monitoring loops.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 6:46 pm

fruitloaf wrote: Do I need any special kernels to support gpio interrupts as I'd like to setup these next to replace my input button monitoring loops.
With the latest SFP image: 2012-08-08-wheezy-armel.zip available on the http://www.raspberrypi.org/downloads page, no special kernel changes are needed :-) It just works!

How are you finding the API? Is it reasonably intuitive?

I am refactoring a few things today (to be included in 0.0.2-SNAPSHOT) in the Gpio interface to permit functional direct access to the methods that use Pin property instead of using GpioPin. Currently all Gpio methods require pins to be provisioned first, but if for software that may be simply an external observer or peer, I wanted to provide a means to access GPIO without having to first provision pins. (edge case)

I also added an OSGi manifest for any OSGi consumers and have reduced the compiler to Java 1.6 to relax the JVM requirement slightly.

Thanks
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

fruitloaf
Posts: 84
Joined: Sun Feb 05, 2012 11:41 am

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 8:53 pm

savageautomate wrote:
fruitloaf wrote: How are you finding the API? Is it reasonably intuitive?
The API is fine though it took me a bit to find out which class to do certain actions on but just finishing the documentation would help a lot. Your library does more than the other ones I've used so having the docs is more important. For instance I was trying to work out how to close a pin and I think some of the setShutdownBehaviour methods were missing from the online docs but were in the javadocs so I didn't realise I could just close a pin from that. Also unexport wasn't documented and I hadn't downloaded the source so I couldn't see if that was just doing the same as setshutdownbehaviour.

I'll report back more when I've done some more work with it as I just spent half an hour integrating it with my existing project doing simple set and gets on the pins.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Sep 23, 2012 11:19 pm

fruitloaf wrote: The API is fine though it took me a bit to find out which class to do certain actions on but just finishing the documentation would help a lot.
Documentation is definitely still a work in progress.

I have posted an updated version, please note the most notable change to the API is that I changed the Gpio interface to GpioController and I changed the GpioFactory method name from createInstance to getInstance.

Code: Select all

// create gpio controller instance
GpioController gpio = GpioFactory.getInstance();
There were two Gpio class names in the project, so I renamed one of them to avoid any confusion.
I renamed the createInstance method in GpioFactory because I modified the implementation to always return the static instance rather than throwing an exception if the instance had been previously created.
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Wed Sep 26, 2012 5:26 pm

A special "Thank You" to Adafruit for the mention of the Pi4J project on their blog site:
http://www.adafruit.com/blog/2012/09/21 ... pberry_pi/

Thank You
Image
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Wed Oct 03, 2012 8:46 pm

Peter "rotok" have built a working #raspi #java #linux #i2c binding using #JNA.
http://www.raspberrypi.org/phpBB3/viewt ... 73#p187073
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

bumann
Posts: 8
Joined: Sun Oct 07, 2012 3:39 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 07, 2012 3:50 pm

Hello,
i 've tried your ListenGPIOExample successfully with one button.

No I try to listen to two buttons. Without success.

Here is my code:

Code: Select all

// START SNIPPET: listen-gpio-snippet
package com.pi4j.example;

/*
 * #%L
 * **********************************************************************
 * ORGANIZATION  :  Pi4J
 * PROJECT       :  Pi4J :: Java Examples
 * FILENAME      :  ListenGpioExample.java  
 * 
 * This file is part of the Pi4J project. More information about 
 * this project can be found here:  http://www.pi4j.com/
 * **********************************************************************
 * %%
 * Copyright (C) 2012 Pi4J
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPin;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinEdge;
import com.pi4j.io.gpio.PinResistor;
import com.pi4j.io.gpio.event.GpioListener;
import com.pi4j.io.gpio.event.GpioPinStateChangeEvent;

/**
 * This example code demonstrates how to setup a listener
 * for GPIO pin state changes on the Raspberry Pi.  
 * 
 * @author Robert Savage
 */
public class ListenGpioExample
{
    public static void main(String args[]) throws InterruptedException
    {
        

	// create gpio controller instance
	GpioController gpio = GpioFactory.getInstance();

	GpioPin myButton = gpio.provisionInputPin(Pin.GPIO_02,            // PIN NUMBER
                                                  "MyButton",             // PIN FRIENDLY NAME
                                                  PinEdge.BOTH,           // PIN EDGE DETECTION
                                                  PinResistor.PULL_DOWN); // PIN RESISTANCE

	GpioPin myButton1 = gpio.provisionInputPin(Pin.GPIO_03,            // PIN NUMBER
                                                  "MyButton1",             // PIN FRIENDLY NAME
                                                  PinEdge.BOTH,           // PIN EDGE DETECTION
                                                  PinResistor.PULL_DOWN); // PIN RESISTANCE
	


	myButton.addListener(new GpioExampleListener());
	myButton1.addListener(new GpioExampleListener());



	
        for (;;)
        {
            Thread.sleep(500);
        }
    }
}

/**
 * This class implements the GPIO listener interface
 * with the callback method for event notifications
 * when GPIO pin states change.
 * 
 * @see GpioListener
 * @author Robert Savage
 */


class GpioExampleListener implements GpioListener
{
    public void pinStateChanged(GpioPinStateChangeEvent event)
    {
        System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = "
                + event.getState());
    }
}


// END SNIPPET: listen-gpio-snippet
When i push gpio2 button, the scrren shows:

--> GPIO PIN STATE CHANGE: "MyButton" <GPIO 2 (PCM_DOUT)> = HIGH
--> GPIO PIN STATE CHANGE: "MyButton1" <GPIO 3> = HIGH
--> GPIO PIN STATE CHANGE: "MyButton" <GPIO 2 (PCM_DOUT)> = LOW
--> GPIO PIN STATE CHANGE: "MyButton1" <GPIO 3> = LOW

Do you have an idea to fix the problem. Thanks

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 07, 2012 6:03 pm

bumann wrote:Hello,
i 've tried your ListenGPIOExample successfully with one button.
No I try to listen to two buttons. Without success.
...
Do you have an idea to fix the problem. Thanks

Hi bumann,

It smells like a defect. Looking into the issue now. Stay tuned....
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 07, 2012 6:47 pm

bumann wrote:Hello,
i 've tried your ListenGPIOExample successfully with one button.
No I try to listen to two buttons. Without success.
Do you have an idea to fix the problem. Thanks
Hi bumann,

I have recorded the defect here:
https://github.com/Pi4J/pi4j/issues/2

A new 0.0.2-SNAPSHOT build is now available with this issue fixed. Please download the latest version of the JAR and let me know if this does not correct the issue for you.

Thank You,
Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

bumann
Posts: 8
Joined: Sun Oct 07, 2012 3:39 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 07, 2012 8:08 pm

Thanks for the qick fix. It works like a charme :-)
Greetings from Germany!

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Mon Oct 08, 2012 1:06 am

Just a heads up,

Several users have sent emails with issues related to the error message "unsatisfied link error /tmp/libpi4j.so". I was finally able to sit down today and work out this issue. It turned out to be an incompatibility between soft-float ABI and hard-float ABI. Basically the native library that I had embedded in the JAR was compiled on Debian Wheezy soft-float ABI and thus would not work on Raspbian or any other hard-float distribution.

I have modified the build to now include both a hard-float and soft-float compiled version of the "libpi4j.so" native library. I also added the logic automatically load the library for soft-float or hard-float distributions automatically (using fail-over logic at this time).

The issue details are listed here.
https://github.com/Pi4J/pi4j/issues/1

You can download the latest 0.0.2-SNAPSHOT to get this fix.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

bumann
Posts: 8
Joined: Sun Oct 07, 2012 3:39 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Tue Oct 09, 2012 5:36 am

Thanks for the nice pi lib. A i2c bus implementaition into the lib would be very nice, so we can use more than 8 gpio ports.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Tue Oct 09, 2012 5:58 am

bumann wrote:Thanks for the nice pi lib. A i2c bus implementaition into the lib would be very nice, so we can use more than 8 gpio ports.
It's definitely on the TODO list.
Trying to get all GPIO issues resolved and documented first.

I would like the see the library offer support for the following IO:
- GPIO
- SERIAL
- I2C
- SPI

All in due time :-)
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Tue Oct 09, 2012 7:14 am

A new 0.0.2-SNAPSHOT build has been deployed.

A new defect was found in the last build. The latest wiringPi sources now include both a static and shared library in the build. The libpi4j native lib was originally getting linked against the static wiringPi lib but now that both static and dynamic libs are available at linking time, the GCC linker opted to link against the dynamic lib since it did not have any explicit library linking instructions defined in the libpi4j make file. Modified the libpi4j make file to explicitly link against the static wiringPi library.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

bumann
Posts: 8
Joined: Sun Oct 07, 2012 3:39 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 14, 2012 4:13 pm

Hi, I have a little problem with GpioPinExistsException.

here`s my code:

Code: Select all

 public static void initializeGpio()
	    {			    
       	 // create gpio controller
      		 GpioController gpio = GpioFactory.getInstance();
        
       	 // provision gpio pin #07 as an output pin and turn off
		

       	 GpioPin pin = gpio.provisionOuputPin(Pin.GPIO_07, "MyLED", PinState.LOW);
			pin=gpio.provisionInputPin(Pin.GPIO_07, "myButton4", PinEdge.BOTH, PinResistor.PULL_DOWN);
            }


When I run it, a GpioPinExistsException is thrown. I understand this, but how can I change the pin from output to input or vice versa in my application?

In general, how can I "close" a pin in the code, so that it doesn`t exist anylonger?

Thanks

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sun Oct 14, 2012 5:18 pm

bumann wrote: I understand this, but how can I change the pin from output to input or vice versa in my application?
In general, how can I "close" a pin in the code, so that it doesn`t exist any longer?
Use the pin.setMode() (and in versions 0.0.2-SNAPSHOT and earlier pin.setDirection()) to change an input to an output pin and vice-versa.

I'll add support for "unprovision" in version 0.0.3 to really get rid of a pin if needed.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

bumann
Posts: 8
Joined: Sun Oct 07, 2012 3:39 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Tue Oct 16, 2012 8:22 am

Thanks, works fine.

chisleu
Posts: 14
Joined: Sat Oct 27, 2012 5:35 pm

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sat Oct 27, 2012 5:40 pm

I followed the instructions in attempting to run a test app to be sure I have everything working for a java devel environment... I'm used to eclipse now, so using vim/ssh and openjdk is hard for me (until I finish setting things up! :) )

What am I doing wrong? I followed the install instructions and installed that wiringpi library. This seems to be saying it can't find the gpio stuff from pi4j.

Code: Select all

[[email protected] src]# java -classpath .:pi4j-core-0.0.3-SNAPSHOT.jar com.pi4j.example.ControlGpioExample
<--Pi4J--> GPIO Control Example ... started.
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/io/gpio/GpioFactory
        at com.pi4j.example.ControlGpioExample.main(ControlGpioExample.java:50)
Caused by: java.lang.ClassNotFoundException: com.pi4j.io.gpio.GpioFactory
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        ... 1 more
I'm very excited to start playing with the gpio stuff soon! :)

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: Pi4J - A Java library to interface with the Raspberry Pi

Sat Oct 27, 2012 9:03 pm

chisleu wrote:...
What am I doing wrong? I followed the install instructions and installed that wiringpi library. This seems to be saying it can't find the gpio stuff from pi4j.
Hi chisleu,

There may have been some problem/defect with the previously deployed artifacts. There have been quite a bit of changes made to 0.0.3-SNAPSHOT trying to broaden the support base for GPIO needs going forward. This is why I have been quiet on the project for the past couple of weeks ... trying to get to a stable point.

I have rebuilt and redeployed all the project artifacts today and tested the ControlGpioExample.java code on both hard-float (using OpenJDK) and soft-float (using Oracle JDK) distributions. It all seems to be working now. Please give it another go. Make sure to download and replace the current JARs and source files with the latest uploaded copies.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

Return to “Other programming languages”