Manual how to get started with Pi4j 2.0

756 views
Skip to first unread message

Daniel Mårtensson

unread,
Jul 7, 2020, 6:09:34 PM7/7/20
to Pi4J
Hi!

I'm trying to use Pi4J 2.0 with Raspberry Pi 4B and I have some issues because I cannot see any documentation about Pi4J 2.0.
I have included Pi4J Core 2.0 jar file into my project and this code won't work for me. Have I missed something?


Robert Savage

unread,
Jul 7, 2020, 6:53:40 PM7/7/20
to Pi4J
Pi4J v2 is still a work in progress ... including documentation!
What is available is currently here:


In Pi4J V2, the core library is purely API and framework but not actual I/O implementation.  
You will need to also include an I/O provider plugin and platform plugin as well.

At the moment, the only I/O plugin we have implemented is the PiGpio plugin.  
Also include the RaspberryPi plugin to provide the "Platform" needed for RaspberryPi devices.



<!-- include Pi4J Plugins (Platforms and I/O Providers) --> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-raspberrypi</artifactId> <version>${pi4j.version}</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-pigpio</artifactId> <version>${pi4j.version}</version> </dependency>

The PiGpio plugin uses the PiGpio Native Library available here:  http://abyz.me.uk/rpi/pigpio/

You will have to install the latest PiGpio library on your Pi.  Pi4J does not provide this.  We only provide a JNI wrapper JAR for PiGpio and a PiGpio Provider Plugin JAR.

If you look at the Maven POM in the example projects, that should help you see what dependencies are needed.

So in total, you will need the following JARs to get started.
  • pi4j-core (Core API and Framework)
  • pi4j-plugin-raspberrypi (Platform Plugin)
  • pi4j-plugin-pigpio (I/O Provider Plugin)
  • slf4j-api (Logging Framework API)
  • slf4j-simple (Simple console output logger). [or any other SLF4J implementation]
  • pi4j-library-pigpio (JNI wrapper for native lib: http://abyz.me.uk/rpi/pigpio/ )

Also note when Pi4J V2 is released, we will be moving over to this forum site and discontinuing activity on this Google Groups:

Thanks, Robert

Jim Darby

unread,
Jul 7, 2020, 7:08:37 PM7/7/20
to pi...@googlegroups.com

Hi Daniel,

I've been using pi4j with NetBeans (don't shout at me!) natively on a Raspberry Pi (3 now 4) for some time now. I've found it the easiest environment for a newbie (at least to Java originally).

I'm still learning Java and Pi4J V2 uses a lot of new feature of Java 11 that I wasn't aware of before. Good and interesting times.

I've found several issues but they are being worked on and the development team are very helpful.

What are you using as a development environment?

Regards,

Jim.

--
You received this message because you are subscribed to the Google Groups "Pi4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pi4j+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pi4j/84cda415-f5ef-4b04-a09f-e18255452f13o%40googlegroups.com.

Daniel Mårtensson

unread,
Jul 8, 2020, 10:25:25 AM7/8/20
to Pi4J
>> What are you using as a development environment?

I'm using Eclipse. I used NetBeans before, but the most used Java IDE is Eclipse. That's why I'm using Eclipse. Boring, but robust and stable.
To unsubscribe from this group and stop receiving emails from it, send an email to pi...@googlegroups.com.

Daniel Mårtensson

unread,
Jul 8, 2020, 1:38:28 PM7/8/20
to Pi4J
Thank you!

I'm building a logger software with Vaadin + Pi4J + MySQL and it going to be a quite large project. Going to upload it on GitHub later. I saw that the examples/manuals are not done yet for the PWM and I2C.

Here is I have done with Pi4J. I'm afraid I cannot continue to work with my project until manuals/documentation is made.

    // This will read the pulse input and the stop signal
    private DigitalInputConfigBuilder createDigitalInput(int pinInput, Context pi4j) {
        return DigitalInput.newConfigBuilder(pi4j)
                .id("Digial input")
                .name("Digital input")
                .address(pinInput)
                .pull(PullResistance.PULL_DOWN)
                .debounce(3000L)
                .provider("pigpio-digital-input");
    }

    // This will turn on/off the FQP30N06L MOSFET
    private PwmConfigBuilder createDigitalPWMOutput(int pinOutput, Context pi4j) {
        return Pwm.newConfigBuilder(pi4j)
                .id("Digital output")
                .name("PWM output")
                .address(pinOutput)
                .pwmType(PwmType.HARDWARE)
                .frequency(pwmFrequency)
                .dutyCycle(100);
    }

    // This will read the ADS1115 16-bit ADC
    private I2CConfigBuilder createI2C(Context pi4j, int address) {
        return I2C.newConfigBuilder(pi4j)
                .bus(0)
                .description("I2C connection")
                .name("I2C connection")
                .id("I2C connection").device(address);
    }

Frank Delporte

unread,
Jul 8, 2020, 2:18:16 PM7/8/20
to Pi4J
Hello Daniel

If you want to start already, you can indeed make a copy of the minimal example to have a starter Maven project with the minimum dependencies.

And then you can extend this with info from the an extra V2 examples project which includes the different I/O. This project will be used to further extend the documentation site, so you can already take a look directly in the code of these examples:


Frank

Daniel Mårtensson

unread,
Jul 8, 2020, 4:08:33 PM7/8/20
to Pi4J
Thank you! That's helped me a lot. I'm using ADS1115 ADC and will Pi4J 2.0 support that code in the future? I have C code for ADS1115 but it seems be lots of trouble when converting that C code to Java, because C have enums and unsinged variables that Java does not have.

'

Daniel Mårtensson

unread,
Jul 9, 2020, 12:46:42 PM7/9/20
to Pi4J
Is it better that you implement ADS1115 for Pi4J 2.0, rather than I do it? The code is avaiable for Pi4J 1.2 and that will be implemented into 2.0 ?

I'm not so confertable with OOP Java and I always prefere structural programming with Java.

Frank Delporte

unread,
Jul 9, 2020, 2:26:07 PM7/9/20
to Pi4J
Hello Daniel, we don't plan to integrate specific device support into the core Pi4J V2 library. This breaks indeed with the V1 but will allow us to focus on the core functionality and have that very stable and tested.

Based on available time and/or contributions by others, we can decide later to provide device specific separate projects as part of https://github.com/Pi4J but this is not the focus at this moment.

Frank

Daniel Mårtensson

unread,
Jul 9, 2020, 3:12:22 PM7/9/20
to Pi4J
Thanks for the reply Frank.

Well, I guess it's up to me then. Hmm... I can do that, but Java data types are that preventing me to create a communication with the devices from e.g i2c.

I have issues to convert byte to uint8 and short to uint16 in Java. Do you know a walk around for this? Only C and C++ have the unsigned data types.

Robert Savage

unread,
Jul 9, 2020, 3:30:23 PM7/9/20
to Pi4J

To add to Franks comment ...

Pi4J V2 will not provide the higher level device and component library (sensors, switches, lights, relays, motors, etc) anymore.
Pi4J V2 will support I/O provider plugins to support GPIO Expanders, ADCs, DACs, PWM Expanders, etc.  Basically anything that exposes the low-level I/O interfaces.  
These will be supported as plugins so that users are free to create their own and maintain them in independent repositories separate from the Pi4J core.

With that said ... I'm sure we will provide a few reference examples, but that effort has not yet been scoped nor which expansion hardware devices we will model.  
So there is a chance that we will create one for ADS1115, but no guarantees.


> I have issues to convert byte to uint8 and short to uint16 in Java. Do you know a walk around for this? Only C and C++ have the unsigned data types

Yes, in Java you have to upcast them.  No unsigned types in Java.  At least not yet.

Frank Delporte

unread,
Jul 9, 2020, 3:44:38 PM7/9/20
to Pi4J

On Wednesday, July 8, 2020 at 12:09:34 AM UTC+2, Daniel Mårtensson wrote:

Daniel Mårtensson

unread,
Jul 9, 2020, 5:04:29 PM7/9/20
to Pi4J
I understand! Better to build a strong Pi4J instead of supporting other peripherals.

I think it should be better for Pi4J to sending ints, instead of bytes when it comes to i2c/spi/can(?). Because ints can have between 0 and 255 and also cover the range uint16. Correct me if I'm wrong about using ints instead of bytes(singed bytes).

Do you mean like this? Look at row 339. Upcasting?

If that's works fine, I can use all my STM32 C-code libraries in Pi4J and I got a lot of them. Just replace all uint8,16,32 with int and change the STM32 HAL commands with Pi4J HAL commands.
Reply all
Reply to author
Forward
0 new messages