1

I recently tried following the instructions listed here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RaspberryPi_GPIO/RaspberryPi_GPIO.html#section1

I was able to do the extremely simple stuff like output hello to my PI. However, I could not get changes written to the GPIO pins to take effect, using either the method described in the default code, or creating a DeviceConfig.

I used the WiringPi utility to test if maybe my wiring was bad, but I was able to blink the LED from WiringPi. Something interesting, is that using gpio readall shown the changes that the imlet wrote to the pin (setting 1 to turn on an LED), but there was not actual change on the LED. This worked the same for trying to turn and LED on and off.

I have the permissions configured correctly (I think...).Is there anyone who has got this working, that could perhaps send me their project?

  • It may be something simple like a mismatch between the gpio numbering you think you are using and the gpio numbering being used by Java. There are at least 3 different gpio numbering schemes. – joan Nov 27 '14 at 20:53
  • @joan I already tried the wiringpi notation, the BCM notation, and physical pin notation. I know that I need to be using the BCM notation, because that is what the example says, and I can observe a change in the correct pin when I call "gpio readall". The problem is that nothing happens in the physical circuit. – Daniel D'Souza Nov 27 '14 at 21:39
  • Oh well. Best to get the obvious possibilities out of the way. Does the LED light if you carefully move the wire from the gpio to the 3V3 pin? – joan Nov 27 '14 at 21:45
  • @joan yes, moving the wire from GPIO23 to 3v3 lights the led. I can also use the "gpio write" command to write to GPIO23, and the led lights up – Daniel D'Souza Nov 27 '14 at 22:09
  • 1
    Pity. That seems to rule out everything but the Java software. – joan Nov 27 '14 at 22:17
0

If you update Raspbian to the latest version, then the regular Java 8 SE will be installed. No need anymore to fiddle with Java ME.

With Java 8, and the Pi4J library (http://pi4j.com) it is simple to do GPIO. A very simple example is here:

/*
 * Simple LED Flashing
 */

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

public class Polizeilicht {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("<--Pi4J--> Polizeilicht");

        // GPIO Controller
        final GpioController gpio = GpioFactory.getInstance();

        // PIN 1 is red, Pin 2 blue, both initially off
        final GpioPinDigitalOutput pinRed = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "Red", PinState.LOW);
        final GpioPinDigitalOutput pinBlue = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "Blue", PinState.LOW);

        // Button to stop, wired to ground, so we need pull-up
        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, PinPullResistance.PULL_UP);

        do {
            // flicker blue
            pinBlue.high();
            Thread.sleep(50);
            pinBlue.low();
            Thread.sleep(50);
            pinBlue.high();
            Thread.sleep(50);;
            pinBlue.low();
            Thread.sleep(100);
            // flicker red
            pinRed.high();
            Thread.sleep(50);
            pinRed.low();
            Thread.sleep(50);
            pinRed.high();
            Thread.sleep(50);;
            pinRed.low();
            Thread.sleep(100);
        } while (myButton.isHigh());
        gpio.shutdown();
    }
}

If it's wired up correctly, you will see this: http://youtu.be/kC99wIjlY3I

| improve this answer | |
  • Thanks for the code, I ended up deciding to move to normal Java. I did find the error with Java ME, though, the permissions for the VM were not set correctly. The new version 8.1 fixed that. – Daniel D'Souza Jan 8 '15 at 20:01
  • You are right. It is easier to make things with PI4J but it is very specific to Raspberry pi. The ones, who would like to learn embedded Java, I would not suggest the use of PI4J. – Ad Infinitum Jan 23 '17 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.