Contributions

Contributions are appreciated and used to host this site. Thank You for your support.

Feature Products
  • Raspberry Pi with Java: Programming the Internet of Things (IoT)
    Raspberry Pi with Java: Programming the Internet of Things (IoT)
  • Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
    Raspberry Pi 2 Model B Project Board - 1GB RAM - 900 MHz Quad-Core CPU
  • Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
    Premium Clear Case for Raspberry Pi 2 Model B Quad Core and Raspberry Pi Model B+ (B PLUS)
  • 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
  • Getting Started with Raspberry Pi
    Getting Started with Raspberry Pi
  • Raspberry Pi User Guide
    Raspberry Pi User Guide
  • PiBrella LED Add On Board
    PiBrella LED Add On Board
  • Diversitech® WS-1 - Wet Switch Flood Detector
    Diversitech® WS-1 - Wet Switch Flood Detector
  • 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
  • Floodstop Individual Water Appliance Additional Water Sensor XS-01
    Floodstop Individual Water Appliance Additional Water Sensor XS-01
  • 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
  • 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
  • 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
Monday
Dec142015

Pi4J Now Supports Non-Privileged Access (no more root/sudo)

Non-Privileged Access

The Pi4J Project now supports non-privileged access to the basic GPIO input and output functions of the Raspberry Pi as well as serial (UART), I2C and SPI communication.  What this means is that launching your Java application with "sudo" or running as a user account with "root" permissions is no longer required!  

How Does it Work?

The latest Raspbian images now includes kernel support for a user accessible mapped memory access to the GPIO hardware via the "/dev/gpiomem" interface.  The latest images also include the predefined UDEV rules configuration allowing read/write access to the GPIO interfaces for users accounts that are members of the "gpio" group.  The UDEV rules also permit SPIDEV access to users who are members of the "spi" group and I2C access to users who are members of the "i2c" group.  Be default the "pi" user is a member of all three of these groups.

What Do I Need?

Latest Raspbian Image

You will need to be running the latest Raspbian Jesse image for the Raspberry Pi:
https://www.raspberrypi.org/downloads/raspbian/ 

Device Tree

You will need the Device Tree enabled.  Don't worry, its enabled by default in the latest images.  That is unless you disabled it :-)  You can enable it under the "Advanced" menu in the "raspi-config" utility.  More information on device tree support: https://www.raspberrypi.org/documentation/configuration/device-tree.md

 

Latest Pi4J Libraries

You will need the latest Pi4J-1.1-SNAPSHOT builds (dated 2015-12-14 or later).  
http://pi4j.com/download.html  

Non-Privileged Access for Serial (UART), SPI, I2C

For SPI and I2C communications, no further action is needed to enable non-privileged access.  For Serial (UART), if you are trying to use the default hardware UART ("/dev/ttyAMA0"), then the login shell feature must be disabled under the "Advanced" menu in the "raspi-config" utility.

Non-Privileged Access for GPIO

Non-privileged access for GPIO is not enabled by default due to the fact that some functions such as PWM are not yet supported in a non-privileged context.  So you must explicitly enable non-privileged GPIO access either via an environment variable or via a one line addition to your Java source code.  

Note: Enabling non-privileged GPIO access will prevent clock and PWM functions from working.

Enable via Java Code

The Java code is probably the easiest method so we will start with that.  Its basically just a single line of code (as shown below) that must be called before you create your GPIO controller instance.  This method will throw a RuntimeException if it is not able to enable non-privileged access.

A complete example source file is available here:
https://github.com/Pi4J/pi4j/blob/develop/pi4j-example/src/main/java/NonPrivilegedGpioExample.java#L62

Enable via Environment Variable 

An alternative to embedding this logic inside your Java code is to export the environment variable "WIRINGPI_GPIOMEM".

export WIRINGPI_GPIOMEM=1

If you want this environment variable to be persisted you may want to append it to the end of your ~/.bashrc file.  This is really an environment variable used by WiringPi directly.  More details on this environment variable can bve found here:  http://wiringpi.com/wiringpi-update-to-2-29/ 

Launching Your Non-Privileged Java Program

To launch your Java program with non-privileged GPIO access, simply omit the "sudo" instruction from your java command line:

java -classpath .:classes:'*':classes:/opt/pi4j/lib/'*' NonPrivilegedGpioExample

Or if you are using the "pi4j" script, then you don't have to do anything.  We have removed to "sudo" instruction from the Java launch commands inside the pi4j script.

pi4j NonPrivilegedGpioExample
 


Alternate Platforms:

Please note non-privileged access is only supported on the Raspberry Pi platform at this time.  The individual WiringPi library ports for BananaPi, BananaPro and Odroid would need to be updated to support this feature.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (6)

Hi Savage,

The GpioUtil.enableNonPrivilegedAccess(); is not working. I am getting Runtime exception.

I have enabled device tree as well, but no luck. I was really waiting for a non sudo java code to control GPIO pins.

Please let me know if you have any clues.

-Deepak

February 22, 2016 | Unregistered Commenterdeepak

Make sure your user is in the "gpio" group, or whoever is the owning group the gpio device files (ls -l /sys/class/gpio)

March 7, 2016 | Unregistered Commenteraxos88

That worked ...Thanks a lot

April 10, 2016 | Unregistered Commenterdeepak

I want to sharpen my skills in java programming so I like paying visit to a Java forum of my choice, so that I may learn various tricks and tactics. Now, I am going to follow this blog as well, as it will help enriching my knowledge.

August 16, 2016 | Unregistered CommenterJennifer

Apache web server is also open source platform that can be easily used with Linux and Windows. Now-a-days, it has become easy to find a htaccess generator online, Once you create htaccess file, it will become easier to run a website flawlessly, without seeing 301 redirect error.

September 7, 2016 | Unregistered CommenterJack

The link in :
"A complete example source file is available here:
https://github.com/Pi4J/pi4j/blob/develop/pi4j-example/src/main/java/NonPrivilegedGpioExample.java#L62"
is wrong. The correct one is:
https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/NonPrivilegedGpioExample.java#L62

Thanks,
Fabio

October 4, 2016 | Unregistered CommenterFabio Heday

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>
« ModMyPi PiOT Relay Board Review | Main | Raspberry Pi with Java: Programming the Internet of Things (IoT) »