Pi4J - Connecting Java to the Raspberry Pi
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
Source Repository
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
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.
- Control GPIO
- Listen for GPIO Events
- Shutdown GPIO
- Trigger GPIO on Events
- Serial Communication
- System/Network Information
Simple Usage Example
Below is a simple example demonstrating controlling a single GPIO pin state.
(Please visit this page for a more in-depth view of this example: Control GPIO Example)
public static void main(String[] args) throws InterruptedException { System.out.println("<--Pi4J--> GPIO Control Example ... started."); // create gpio controller Gpio gpio = GpioFactory.createInstance(); // provision gpio pin #01 as an output pin and turn on GpioPin pin = gpio.provisionOuputPin(Pin.GPIO_01, "MyLED",
PinState.HIGH); System.out.println("--> GPIO state should be: ON"); Thread.sleep(5000); // turn off gpio pin #01 pin.low(); System.out.println("--> GPIO state should be: OFF");Thread.sleep(5000); // toggle the current state of gpio pin #01 (should turn on) pin.toggle(); System.out.println("--> GPIO state should be: ON"); Thread.sleep(5000); // toggle the current state of gpio pin #01 (should turn off) pin.toggle(); System.out.println("--> GPIO state should be: OFF"); Thread.sleep(5000); // turn on gpio pin #01 for 1 second and then off System.out.println("--> GPIO state should be: ON for only 1 second"); pin.pulse(1000); }
Stay tuned ... more articles to come on using the Pi4J library.
Reader Comments (4)
Thanks for all the information ,it was very helpful i really like that you are providing information on core and advance java ,being enrolled in
advance and core java http://www.wiziq.com/course/1779-core-and-advance-java-concepts i was looking for such information on advance and core java and your information helped me a lot. I really like that you are providing such information. Thanks.
Hi,
I hope this is the right place to raise this. I'm hoping to use pi4j in a home automation project.
I've a lot of Java experience but little Linux. Running a test Java program as root on the pi with the 2.0.0.SNAPSHOT jar in the classpath, I am getting an unsatisfied link error from NativeLibraryLoader line 178, /tmp/libpi4j.so no such file or directory.
Sure enough, /tmp/libpi4j.so doesn't exist (although it is in the jar).
If I manually copy the .so file into /tmp, I get a message that the program is attempting to delete it, which it does. Then it complains that the file doesn't exist, which it now doesn't.
The operating system is Debian Wheezy. Do I need to compile with javac, javah and gcc from source to make a new .so file, or is there some Linuxy thing that I'm just missing through ignorance?
Regards,
Chris
@Chris,
Are you using the OpenJDK? There have been a couple of users that have experienced this same issue while running under the OpenJDK. I need to and will setup and test this under the OpenJDK to try and get to the bottom of this issue. In the meantime, you can try it using the Oracle JDK running on the Debian Wheezy image. Below is a link on how to install the Oracle JDK on your Raspberry Pi.
http://www.savagehomeautomation.com/projects/raspberry-pi-installing-oracle-java-development-kit-jdk-170u.html
Hi,
Hopefully you don't mind me posting this here. pi4j looks great and I'm trying to use it for a little project, I can control the outputs fine but I can't get any examples of the inputs working. Both the control and trigger examples don't produce any results.
I'm certain it's not fault of pi4j, I don't have the exact switch used in the examples which is the most likely culprit or there is a problem with the pi. I'm a complete novice with electronics and components, do you have any advice or suggestions for how I can produce an input without that exact switch. I have a friend who knows a load about electronics who can help me and he doesn't get why the switch isn't working.
Any advice would be great.