Run Java application for MCP23017 without root permissions? Yes, it is possible!

234 views
Skip to first unread message

Alex Konshin

unread,
Aug 23, 2013, 9:27:57 PM8/23/13
to pi...@googlegroups.com
I wondered from the very beginning why Pi4J requires root privileges for i2c when they are not actually required in common use cases.
I found out that it is possible to work around and it is easy to do.

      final MCP23017GpioProvider gpioProvider = new MCP23017GpioProvider( I2CBus.BUS_1, 0x21 );
      GpioFactory.setDefaultProvider( gpioProvider );
     
      // create gpio controller
      gpio = GpioFactory.getInstance();

So you just need to create the provider and call GpioFactory.setDefaultProvider before GpioFactory.getInstance.

But you still need to call the following commands before starting you Java application:
sudo modprobe i2c-dev
sudo chmod o+rw /dev/i2c*

You can put them into /etc/rc.local so they will be executed on Raspberry Pi’s startup.


Explanation:

Basically you just should not call com.pi4j.wiringpi.Gpio.wiringPiSetup*. Method com.pi4j.wiringpi.Gpio.wiringPiSetup() is called from constructor of RaspiGpioProvider. This is the default provider and this constructor is called if it was not set explicitly by GpioFactory.setDefaultProvider.

Alex

Pi4J

unread,
Aug 24, 2013, 9:35:26 AM8/24/13
to pi...@googlegroups.com
Thank's for sharing that.  If someone has asked that question, I'm not sure I would have readily identified such an easy workaround. 

Thanks, 
Robert
Reply all
Reply to author
Forward
0 new messages