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

Pi-Face + Java + Pi4J

Mon Feb 25, 2013 1:38 pm

I just got my hands on a Pi-Face expansion board for my Raspberry Pi. My first objective was to get this board integrated into the Pi4J project and fully working with Java on my Raspberry Pi. The following article (in the link below) covers installing the Pi-Face board, setting up the SPI communication driver, and demonstrates programming the Pi-Face in Java. With the new Pi-Face Java API interface now available in the Pi4J libraries this makes it a breeze to program and work with in your own Java program.

FULL ARTICLE: http://www.savagehomeautomation.com/piface
DEMO VIDEO: http://youtu.be/Q-bihGjJ1lA

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

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

Re: Pi-Face + Java + Pi4J

Tue Mar 05, 2013 10:19 am

Hi Robert,
congratulation for your project.
I'm waiting my piface board to crete a plugin for our domotic framework.
I have a question: in this topic http://www.raspberrypi.org/phpBB3/viewt ... 3&start=25 a user asked me to control his piggy-back board with our software freedomotic.
I must study better your library, but is it possible to use it with that board?
Thanks in advance
Mauro
Freedomotic Open IoT Framework
http://freedomotic.com

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

Re: Pi-Face + Java + Pi4J

Tue Mar 05, 2013 11:02 am

Hi Mauro,

Unfortunately Pi4J does not currently support 1-wire, IR transmitter, IR receiver, CAN interface, Bluetooth, etc. This is a very interesting add-on board and I think Pi4J would be a good fit for these type of communication I/Os, but its just not currently in the project.
http://shop.basis.biz/shop/images/manuf ... n_Rev2.pdf

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

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

Re: Pi-Face + Java + Pi4J

Tue Mar 05, 2013 12:49 pm

Thanks for your reply.
We'll adopt another solution based on restapi or xml file.
Regards
Mauro
Freedomotic Open IoT Framework
http://freedomotic.com

cyberde
Posts: 4
Joined: Sun Mar 17, 2013 3:45 pm

Re: Pi-Face + Java + Pi4J

Sun Mar 17, 2013 3:55 pm

I am having some diffuculties with Pi4J and the PiFace, I've setup a listener on a switch which is working perfectly. But it seems like it's not always registering if the switchstate changed.
For example when I press the switch I see that the state changes to LOW, which is perfect, but when I let go of the switch my listener sometimes doesn't get's notified that the state has changed to HIGH again (unless I press the button a couple of more times).

See here:

Code: Select all

RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
Is there any way I can increase the poll rate or something else to let it respond adequately?

The above is solved, I had multiple classes for different buttons with their own listeners. The only problem was that each class created a PiFace instance. Now that I'm giving the same PiFace instance to all classes in the constructor the problem has been solved.

Also another weird thing is that after the power has been off it doesn't even recognize the PiFace buttons untill i start the piface-emulator perl app or do a pfio.init() in Python.

Is there some sort of way I need to initialize the piface board with java as well?

The above is still unsolved, after powering on the PiFace does not get initialized by Pi4J so I first need to specifically piface/python/demos/toggle.py (or something that does something with the switches) and then my java app works.

If I run the code below after powering on my Pi, then my java app works. Without it, the app never responds to the switches & digital input

Code: Select all

#!/usr/bin/env python

import piface.pfio
import time

piface.pfio.init()

switch = [ piface.pfio.Switch(i) for i in range(0, 4) ]

for i in range(0, 4):
    print switch[i].value

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

Re: Pi-Face + Java + Pi4J

Fri Mar 22, 2013 2:56 pm

There are several lines of initialization code that configure the MCP23S17 chip for use with Pi4J. These all happen in the class constructor. See this code:

https://github.com/Pi4J/pi4j/blob/maste ... .java#L114

If you notice the pin interrupt commands, I suspect these are why you don't see any button changes. We use the interrupt behavior of the chip to determine when pin state changes occur rather than a brute force polling logic.

You could modify this code and create an "initialize()" function that you could call externally as well as gets called by the class constructor and move all this init code there. This would be a good test to see if that fixes the issue.

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

cyberde
Posts: 4
Joined: Sun Mar 17, 2013 3:45 pm

Re: Pi-Face + Java + Pi4J

Sun Mar 24, 2013 3:09 pm

I see, but it should work anyway right? I'm using the folowing code (very simple and removed a lot of stuff), basically it's like this:

Code: Select all

        private final PiFaceSwitch SWITCH = PiFaceSwitch.S1;

        PiFace piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);

        piface.getSwitch(SWITCH).addListener(new SwitchListener()
        {
            @Override
            public void onStateChange(SwitchStateChangeEvent event)
            {
                if (event.getNewState() == SwitchState.ON)
                {
                    System.out.println("Button changed to ON");
                }
            }
        });
It only prints the text when I first run the init script/sample in Perl (which I posted above). The same applies to the piface example: https://github.com/Pi4J/pi4j/blob/maste ... ample.java. It won't just work by running the java code.

The issue I was having with with not registering the button-press sometimes has been solved by not creating more than one PiFaceDevice instance.

Is there some other way to automatically initialize the buttons on the board? All the other things work, like the relays and the output pins/leds.

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

Re: Pi-Face + Java + Pi4J

Mon Mar 25, 2013 7:50 pm

It should initialize on the constructor of 'PiFaceDevice'.
When I was working with mine, I never loaded any Python or Perl code, only Java.
I can retest it on my system in a few days.

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

cyberde
Posts: 4
Joined: Sun Mar 17, 2013 3:45 pm

Re: Pi-Face + Java + Pi4J

Tue Mar 26, 2013 6:27 pm

savageautomate wrote:It should initialize on the constructor of 'PiFaceDevice'.
When I was working with mine, I never loaded any Python or Perl code, only Java.
I can retest it on my system in a few days.

-Robert
That would be awesome. By the way, everything else works, leds, output pins, relays just not the buttons.
I own a PiFace Digital 2.1 (element14).

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

Re: Pi-Face + Java + Pi4J

Tue Mar 26, 2013 8:15 pm

Just for clarification ... is it just the button events that are not firing?

One thing you could try in the meantime is running these commands to make sure your system is up to date. I think there was an issue with the GPIO eventing is a earlier builds.

sudo apt-get update
sudo apt-get upgrade


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

cyberde
Posts: 4
Joined: Sun Mar 17, 2013 3:45 pm

Re: Pi-Face + Java + Pi4J

Wed Mar 27, 2013 7:14 am

Correct, just the button events are not firing (as well as the wire-connectors that go with them). The rest works perfectly.

I've also done the apt-get update/upgrade last week, tonight when I get home I will do it again. But last time it didn't help. I also tried the firmware-update script available, also no avail.

eickler
Posts: 1
Joined: Wed May 15, 2013 3:15 pm

Re: Pi-Face + Java + Pi4J

Wed May 15, 2013 3:33 pm

Hi there,

I was just wondering if there was any followup on the events not firing. Today I also struggled a while with this and after finding this thread, I resolved it through starting the piface-emulator. It happens as well with the examples provided along with the pi4j installation.

Cheers,
André

deciojunior
Posts: 1
Joined: Tue May 28, 2013 5:53 pm

Re: Pi-Face + Java + Pi4J

Tue May 28, 2013 6:00 pm

Hello,

I have the same problem I can not receive events from INPUTS Pi Face with pi4j, the rest works.

Someone found the solution to this problem?
I appreciate a lot if you guys could help me.

Asher Waldfogel
Posts: 5
Joined: Sun Jul 21, 2013 7:08 pm

Re: Pi-Face + Java + Pi4J

Sun Jul 21, 2013 7:33 pm

Same issue. Inputs work in PiFaceExample.java after I run the Python example, but not before.

BTW, pi4j is the most professionally written library I've seen in this ecosystem. Nice work!

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

Re: Pi-Face + Java + Pi4J

Mon Jul 22, 2013 11:38 am

Hi Guys,

There is some discussion on this over here on the Pi4J groups thread.
User "Pat" was able to get button events working with his Pi4J and PiFace.

https://groups.google.com/forum/#!topic ... lKYpbUXuTI

Thanks, Robert
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: Pi-Face + Java + Pi4J

Mon Jul 22, 2013 11:39 am

Asher Waldfogel wrote:BTW, pi4j is the most professionally written library I've seen in this ecosystem. Nice work!
Thanks for the compliment! :ugeek:
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

Asher Waldfogel
Posts: 5
Joined: Sun Jul 21, 2013 7:08 pm

Re: Pi-Face + Java + Pi4J

Mon Jul 22, 2013 8:41 pm

I found a workaround. Read the INTCAPB register (address 0x11) just once at initialization, then all the examples work correctly until the Pi is powered down and powered back on. I'm guessing the Python library reads the INTCAPB register for some reason and explains why pi4j works after running the Python modules.

I found this by adding code to the PiFaceExample.java to read out the chip registers (and see if anything changed before/after running the Python code). After I read the entire bank 0x00 to 0x15 everything worked correctly - so I changed to code to read out a register range. All it takes is to read register 0x11 once after initialization, and the input triggers work normally.

I'm running OS 3.6.11+ (Jun 17 2013 20:49:11) I'm running the soft float ABI to work with Oracle Java 1.7.0_21

Everything else is stock - Pi is ModelB_Rev2 board, PiFace Digital 2.1.

davidi74
Posts: 1
Joined: Sun Jul 28, 2013 8:46 am

Re: Pi-Face + Java + Pi4J

Sun Jul 28, 2013 8:55 am

Could you please post an example of how you did this? I was not able to get the inputs working either.

Thank you.
Asher Waldfogel wrote:I found a workaround. Read the INTCAPB register (address 0x11) just once at initialization, then all the examples work correctly until the Pi is powered down and powered back on. I'm guessing the Python library reads the INTCAPB register for some reason and explains why pi4j works after running the Python modules.

I found this by adding code to the PiFaceExample.java to read out the chip registers (and see if anything changed before/after running the Python code). After I read the entire bank 0x00 to 0x15 everything worked correctly - so I changed to code to read out a register range. All it takes is to read register 0x11 once after initialization, and the input triggers work normally.

I'm running OS 3.6.11+ (Jun 17 2013 20:49:11) I'm running the soft float ABI to work with Oracle Java 1.7.0_21

Everything else is stock - Pi is ModelB_Rev2 board, PiFace Digital 2.1.

Asher Waldfogel
Posts: 5
Joined: Sun Jul 21, 2013 7:08 pm

Re: Pi-Face + Java + Pi4J

Sun Jul 28, 2013 7:58 pm

Little bit of a hack, but try adding these lines of code to PiFaceExample.java just before the first button listener:

byte packet[] = {0x41, 0x13, 0x00};
int result = Spi.wiringPiSPIDataRW(0, packet, 3);

This code reads the GPIOB control register. After you read it once, the original PiFaceExample.java code works on subsequent runs until you power cycle the board.

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

Re: Pi-Face + Java + Pi4J

Thu Oct 03, 2013 5:43 am

Asher,

Thanks for the info you provided. I was able to use that and try to debug the problem.

I have posted a brand new 1.0-SNAPSHOT build that should include this button feedback fix.
https://code.google.com/p/pi4j/downloads/list

Once it's independently confirmed to be working, I'll write more on the details of the underlying issue.

Defect Ticket : https://github.com/Pi4J/pi4j/issues/53

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

suipaste
Posts: 17
Joined: Tue Jul 17, 2012 6:50 pm

Re: Pi-Face + Java + Pi4J

Tue Jan 07, 2014 9:10 pm

Hi Robert, this thread looks pretty dead but I can independently confirm that your latest snapshot fixes the problem.

It was a long trail to find the fix from your original tutorial here: http://www.savagehomeautomation.com/piface to the posts in this google group https://groups.google.com/forum/#!topic ... lKYpbUXuTI so it probably wouldn't be a bad idea to update that tutorial to include this new fix.

Also maybe change the stuff about SPI drivers to include the alternate option of enabling them through the configuration menu you get when you boot the pi for the first time.

As for me I'm just chuffed that I've finally got the relays working using java code!

gaetancollaud
Posts: 2
Joined: Tue May 27, 2014 7:49 pm

Re: Pi-Face + Java + Pi4J

Tue May 27, 2014 7:51 pm

Hi,

The 1.0.0-SNAPSHOT fixed the problem for me too.

Thank you very much for this lib by the way. ;) And thank you for the maven repository, it's very useful !

Gaétan

Return to “Java”