Amazon Product List

If you are planning on purchasing any of the items listed here from Amazon, please use the links below. Using these links will provide a small contribution to maintain this site at no additional cost to you. Thank You for your support.

  • Diversitech® WS-1 - Wet Switch Flood Detector
    Diversitech® WS-1 - Wet Switch Flood Detector
  • Twine Portable Wi-Fi Sensor + Full Sensor Package
    Twine Portable Wi-Fi Sensor + Full Sensor Package
  • Getting Started with Raspberry Pi
    Getting Started with Raspberry Pi
  • Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
    Edimax EW-7811Un 150 Mbps Wireless 11n Nano Size USB Adapter with EZmax Setup Wizard
  • Airlink Fully compatible Wireless N 150 Ultra Mini-USB Adapter (AWLL5099)
    Airlink Fully compatible Wireless N 150 Ultra Mini-USB Adapter (AWLL5099)
  • SB Clear Raspberry Pi Enclosure
    SB Clear Raspberry Pi Enclosure
  • Raspberry Pi User Guide
    Raspberry Pi User Guide
  • Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
    Onsite Pro FS1NPTW Whole Home Wireless FloodStop with 1 Inch Valve
  • Floodstop Washing Machine Valve Shutoff Kit
    Floodstop Washing Machine Valve Shutoff Kit
  • Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
    Onsite Pro FS3/4H Washing Machine FloodStop with Straight Valves
  • Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
    Aqua Managers - FS 1 1/4-NPT - Floodstop for Water Heaters Water Leak Detection System - White - 1.25 in. pipe
  • Floodstop Individual Water Appliance Additional Water Sensor XS-01
    Floodstop Individual Water Appliance Additional Water Sensor XS-01
  • Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-20 Power Relay Accessory, 20 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
    Furman MP-15 Power Relay Accessory, 15 Amp, Two Outlets, Remote Turn-on from Momentary or Maintained Contact Switches
  • Watts 500800 Premier Hot Water Recirculation Pump, Blue
    Watts 500800 Premier Hot Water Recirculation Pump, Blue
  • Watt'S Sensor Valve Kit for Hot Water Recirculating Pump (0955801)
    Watt'S Sensor Valve Kit for Hot Water Recirculating Pump (0955801)
  • Kicker 10cvt8-2 Shallow 8
    Kicker 10cvt8-2 Shallow 8" Subwoofer 2-ohm 400 Watts
  • Kenwood Kfc-P709Ps 6.5-Inch Performance Series Component Speaker System
    Kenwood Kfc-P709Ps 6.5-Inch Performance Series Component Speaker System
  • Metra 70-2002 Radio Wiring Harness for Saturn 00-05
    Metra 70-2002 Radio Wiring Harness for Saturn 00-05
  • Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio
    Metra Reverse Wiring Harness 71-2002 for Select 2000-2005 Saturn Vehicles OEM Radio

Contributions

Contributions are very much appreciated and are used to host this website and continue to fund new DIY home automation projects. Thank You for your support.

Friday
Sep212012

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


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.


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.

PrintView Printer Friendly Version

EmailEmail Article to Friend

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.

September 24, 2012 | Unregistered Commentersarah

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

October 1, 2012 | Unregistered CommenterChris

@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

October 1, 2012 | Registered CommenterRobert Savage

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.

December 5, 2012 | Unregistered Commentermarkc

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« Raspberry Pi - Installing the Edimax EW-7811Un USB WiFi Adapter (WiFiPi) | Main | Raspberry Pi - Power Up using the 5V Header Pin »