pi4j | Java I/O library for Raspberry Pi
kandi X-RAY | pi4j Summary
kandi X-RAY | pi4j Summary
Java I/O Library for Raspberry Pi. [Maven Central] ![Snyk Security Badge] Project website: Pi4J Community Forum (new): Version 2.0 Project Discussions (new): Project issues list: Release builds are available from: * [Maven Central] * [Downloads] Snapshot builds are available from: * [Sonatype OSS] * [Downloads] Copyright 2012-2021 Pi4J. Please see the [BUILD.md] BUILD.md) instructions for details on how to compile/build this project.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Loads a library .
- Invoked when the pin state is pressed
- Resize the buffer
- The main entry point .
- Executes an ioctl command .
- Shuts down the GPIO pins .
- Returns a SerialConfigConfig for the given arguments .
- Get the default port for a given system type .
- Handle a pin event
- Helper method to create a cleanup task that can be used to cleanup any pending outputs .
pi4j Key Features
pi4j Examples and Code Snippets
Community Discussions
Trending Discussions on pi4j
QUESTION
I am honestly about to just give up, i've tried so many different possibilities, for multiple weeks now, almost a month, of multiple problems.
I am a new-ish programmer, especially with java, but i have a good understanding about java
I am able to create a maven project no problem, i have no problems with the structure of java itself, but i don't fully understand the pom.xml.
The file compiles just fine, but when i go to start it with java -jar (filename)
, i get the following output;
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/Pi4J at com.pi.rasberri.Main.main(Main.java:16) Caused by: java.lang.ClassNotFoundException: com.pi4j.Pi4J at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ... 1 more
Heres my pom that i have figuratively almost dismembered;
...
ANSWER
Answered 2021-Dec-18 at 16:39Thanks tgdavies, MadProgrammer, and khmarbaise for the answers, the fix was to create a fat jar, which is basically a jar file that contains all the dependencies in one file, example can be found in the original question/comments
Now, as to the other problem, the issue is with this bit of code;
QUESTION
I have GrovePi Zero(GrovePi0) from GrovePi Zero Base Kit and Grove - PH Sensor Kit (E-201C-Blue) I am using Java (I can use any version of JDK 8...17) on a Raspberry Pi Zerro 2. with GrovePi-pi4j with Pi4j version 1.4 (can use any version)
my class GrovePHSensor below represents the PH Sensor.
...ANSWER
Answered 2022-Jan-24 at 11:40@GroveAnalogPin
public class GrovePHSensor extends GroveAnalogInputDevice {
private static final Logger l =
LogManager.getLogger(GrovePHSensor.class.getName());
/*** pH values range */
public static final double PH_RANGE = 14.0;
/***
number of possible samples with 10 bit analog to digital converter
*/
public static final int A2D_RANGE = 1023;
public GrovePHSensor(GrovePi grovePi, int pin) throws IOException {
super(grovePi.getAnalogIn(pin, 4));
}
@Override
public Double get(byte[] data) {
// the extraction of the value is taken from the sample of the
// RotarySensor3Led
// https://github.com/DexterInd/GrovePi/blob/master/Software/Java8/GrovePi-spec/src/main/java/org/iot/raspberry/grovepi/devices/GroveRotarySensor.java
// this is how its is done there
int[] v = GroveUtil.unsign(data);
double sensorValue = (v[1] * 256) + v[2];
// the Analog to Digital is 10 bit -> 1023 intervals that cover the range of pH of 0 to 14
// hence the pH value is the sensor value that is 0 to 1024
// times the pH per interval of the A2D 14/1023
double ph = sensorValue * (PH_RANGE / (double) A2D_RANGE);
l.trace("sensorValue = {} ph={}", sensorValue, ph);
return ph;
}
}
QUESTION
I'm in the process of writing a java program for a Raspberry Pi where I want to access the GPIO. For this I use the Pi4J-Library Version 2 (https://pi4j.com). As IDE I use Visual Studio Code - as they suggest.
In my VSC workspace are included:
- the Pi4J library project
- a minimal example application (https://pi4j.com/getting-started/minimal-example-application)
- my newly created project (created with
mvn archetype:generate -DarchetypeCatalog=local
)
I can compile and package the minimal example application and my own project using the appropriate maven commands.
Then I have a java library for desktop applications I wrote myself. I added this library and a project, that uses the library, to the workspace. I can run this desktop application from VSC. The library and the application where originally written using Eclipse. Maven is not involved.
Now I'm trying to use a class from the desktop-library in my Pi4J-project: new MyLibClass
VSC displays "MyLibClass cannot be resolved to a type". I have imported the class: import package.name.MyLibClass
. Obviously the Pi4J-project doesn't know where to find the class. But when I CTRL-click the class name, it opens the corresponding file.
I think I need to add a dependency to the pom.xml of my Pi4J-project.
But I have absolutely no idea what to specify for groupId
, artifactId
and version
. The desktop-library is not a maven project.
Thanks a lot for your help in advance!
...ANSWER
Answered 2021-Nov-17 at 21:14Maven supports three types of repository: local, central and remote. Normally, dependencies you add to your pom.xml
file are pulled from the central repository. What you can do is compile your library to a jar, and drop that in your local repository, which can be found in one of the following locations depending on your OS:
- Windows: C:\Users\\.m2
- Linux: /home//.m2
- Mac: /Users//.m2
You can install your jar in your local repository as follows:
QUESTION
I have a strip of WS2811 adressable RGB leds and I am trying to control them using my raspberry pi. I am programming in java. To access the GPIO I am using a java library called Pi4j (version 1.2; java 8). This version has built-in PWM control.
My question is the following: How can I send the data to the leds trough a GPIO pin?
(I am still a beginner when it comes to physical computing, so please be gentle)
...ANSWER
Answered 2021-Nov-16 at 21:12I haven't used NeoPixels directly on a Pi before (though I have many times used FadeCandy which is a USB LED controller that also does some nice dithering/frame blending).
Driving NeoPixels isn't as straight forward as plain PWM on individual LEDs.
There's a good Adafruit RaspberryPi NeoPixel wiring/coding tutorial. The notes on wiring/power are super useful. Even though the code is in Python, it's still useful to test/run.
Having a quick look at the pi4j documentation, while it has all the "ingredients", it doesn't have built-in support for NeoPixels. Doing a quick search I spotted the diozero library which seems to have support for NeoPixels.
I haven't tested diozero
, but it's worth giving it a go.
QUESTION
Eclipse 2021-03 (4.19.0) on Windows 10 Pro (20H2) Raspberry Pi 4 running Raspbian 10 Java 11 (since it is the latest I've seen on the Raspberry Pi.
I've recently encountered a problem while exporting runnable jar files. On new and older programs in the workspace, when they are exported as runnable jar files on the Windows 10 Pro and moved to the Raspberry Pi, I get the message "Error: An unexpected error occurred while trying to open file test.jar"
Having written a "Hello World" program and exported it, it ran just fine. This leads me to believe it is something with the project name, the Maven repositories, some quirk on the Raspberry Pi, or some other unknown.
The project names are now "ButtonMediaPlayerV24" where 24 indicates 2.4 (to keep previous versions, the period was removed in hopes it would fix the issue)
The Maven Repositories are VLCj and pi4j.
Have anything I should check or try?
Tried: creating a new project, with the name "ButtonMediaPlayer" with the same result.
UPDATE 1: After gutting the program and putting it into the test project, I found GPIO 30 and 31 had "PinPullResistance.PULL_DOWN" in the initiation, which was given as an error message after getting rid of the additional classes.
UPDATE 2: The Maven repositories are not the problem, and the project name isn't an issue.
Further updates if I find more.
...ANSWER
Answered 2021-Mar-25 at 18:31The error appears to be the way I was trying to create local version control, which involved using Windows Explorer to create a new copy, and rename it. It seems to be possible to do this, but Eclipse must rename the file to update not obvious locations where the file name is. Without that, it seems to break every time.
QUESTION
I would like to execute a piece of code every 10 seconds. I have found an example on this forum, but have some issues with my implementation.
...ANSWER
Answered 2021-Jan-03 at 18:04You need a class that extends TimerTask and override the public void run() method, which will be executed everytime you pass an instance of that class to timer.schedule() method.
QUESTION
I am trying to run a simple test on my Raspberry Pi4 using the pi4j java library v1.2. Somehow it's not doing anything. I also don't see any errors on console. It just finishes after 2 seconds. I'm expecting the LED light to turn on and turn off after 2 seconds.
I also updated my gpio binary to v2.52 as per this https://stackoverflow.com/a/63433316/607637 but it's still the same. Also, the command gpio write 19 1
is not doing anything.
GPIO
...ANSWER
Answered 2020-Oct-29 at 04:01It appears its answered here:
- https://raspberrypi.stackexchange.com/questions/113511
- https://raspberrypi.stackexchange.com/questions/111679
It looks Pi4j is using a different pin numbering scheme. "Under the hood", WiringPi is used to control the GPIOs.
So, I needed to tell it to use Broadcom pin numbering scheme using:
GpioFactory.setDefaultProvider(new RaspiGpioProvider(RaspiPinNumberingScheme.BROADCOM_PIN_NUMBERING));
Now its working:
QUESTION
ANSWER
Answered 2020-Jan-25 at 11:18Okay, apparently it was just me being stupid.
The mistake is that I was using the class GpioPinDigitalOutput instead of GpioPinDigitalInput.
I changed it and also modified this line
QUESTION
A simple beginner project using an external library, that I can't get to build due to something basic I'm missing here. Thanks for your help.
build.gradle
...ANSWER
Answered 2020-Jan-12 at 00:22The com.pi4j:pi4j-parent
dependency you declared is not a typical dependency you'd expect. It is the parent Maven POM for the Pi4J Project.
If you view the contents of this dependency, you can see there are no published *.jar
artifacts: https://repo1.maven.org/maven2/com/pi4j/pi4j-parent/1.2/
Since there are no published JAR artifacts, you won't have anything on your classpath. What you need to do is declare the dependencies you need:
Based on your imports, you'll want to pi4j-core
package. So declare it as a dependency:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install pi4j
You can use pi4j like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the pi4j component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page