This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,889,817 Users
  • 2,269,775 Discussions
  • 7,916,823 Comments

Discussions

Raspberry Pi 2 Mod B - GPIO How

HarrySatt
HarrySatt Member Posts: 26
edited Jan 7, 2016 7:00AM in Java ME Embedded

Hi

I am usind a Raspberry Pi 2 Mod B with NetBeans 8.1, Oracle JDK ME 8.2. I have established all connections and the sample program GPIOSample is compiling and running on th RPi but none of the pins is working. After that I removed all extra stuff and only use the code for setting the output pin 7. Added some code to set and reset the pins value. That also compiles but I don't see any pin working. From my point of view it should be RPi GPIO socket number 26. what am I douing wrong?

Regards, harald

Code should go into GPIOSample

/*

* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.

*/

package com.oracle.jmee.samples.gpio;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;

import jdk.dio.DeviceManager;

import jdk.dio.gpio.GPIOPin;

/**

* GPIOSample demonstrates basic GPIO functionality. The sample opens the

* following DIO devices:

*/

public class GPIOSample extends MIDlet {

    // GPIO pin which corresponds to a LED

    private GPIOPin led = null;

    @Override

    public void startApp() {

        System.out.println("******************************************");

        System.out.println("*         GPIO Sample started!           *");

        System.out.println("******************************************");

        try {

            int ledPinId = 7;

            System.out.println("Opening the GPIO pin with id " + ledPinId + " for the LED");

            led = DeviceManager.open(ledPinId);

            System.out.println("Success opening GPIO pin id " + ledPinId + " for the LED");

        } catch (IOException ex) {

            System.out.println("Cannot open pin");

            destroyApp(true);

        }

       

        boolean val = true;

        for( int i=0; i<20; i++)

        {

            try {

                Thread.sleep(500); // I know. Should not be done. Only for testing

            }

            catch( InterruptedException ex)

            {

                System.out.println("  !!! Interrupted exception");

            }

            try {

                val = !led.getValue();

                led.setValue(val);

            }

            catch ( IOException ex) {

                System.out.println(" caught IOException ex");

            }

            System.out.println(" ***  next state change: " + i + " is " + val);

        }

        destroyApp(true);

    }

    @Override

    public void destroyApp(boolean unconditional) {

        System.out.println("******************************************");

        System.out.println("*         GPIO Sample destroyed          *");

        System.out.println("******************************************");

        notifyDestroyed();

    }

}

HarrySatt

Best Answer

  • Adam G
    Adam G Member Posts: 4
    edited Jan 5, 2016 4:24PM Answer ✓

    Did you set the api permission?

    I just got my LED project working this afternoon, I had trouble with the code. You can try this to see if yours works. Sound like we have the same setup.

    Some quick notes. it seem you need to define the pin everytime by:

      GPIOPinConfig config1 = new GPIOPinConfig(0,22, GPIOPinConfig.DIR_OUTPUT_ONLY,DeviceConfig.UNASSIGNED, 0, false);

    Then call the pin

    GPIOPin PIN1 = DeviceManager.open(config1);

    and for a quick to test was to turn the pin off then on.

    PIN1.setValue(true);

                Thread.sleep(4000);

                PIN1.close();

    II used GPIO pin 22 which is actual pin 15 on the board.

    if you need more let me know, would be happy to help.

    I can send you my whole script if you like

    Adam

Answers

  • Adam G
    Adam G Member Posts: 4
    edited Jan 5, 2016 4:24PM Answer ✓

    Did you set the api permission?

    I just got my LED project working this afternoon, I had trouble with the code. You can try this to see if yours works. Sound like we have the same setup.

    Some quick notes. it seem you need to define the pin everytime by:

      GPIOPinConfig config1 = new GPIOPinConfig(0,22, GPIOPinConfig.DIR_OUTPUT_ONLY,DeviceConfig.UNASSIGNED, 0, false);

    Then call the pin

    GPIOPin PIN1 = DeviceManager.open(config1);

    and for a quick to test was to turn the pin off then on.

    PIN1.setValue(true);

                Thread.sleep(4000);

                PIN1.close();

    II used GPIO pin 22 which is actual pin 15 on the board.

    if you need more let me know, would be happy to help.

    I can send you my whole script if you like

    Adam

  • Adam G
    Adam G Member Posts: 4
    edited Jan 5, 2016 4:58PM

    Harry,  I changed your code a bit here:

    It did work on the rpi.  also removed a small piece for testing purpose. and right now you need to kill the process manually, but it did light up the led and with this code.

    Sorry I'm not a java guy at all, Im learning as I go,

    package oracle;

    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.microedition.midlet.MIDlet;
    import jdk.dio.DeviceConfig;
    import jdk.dio.DeviceManager;
    import jdk.dio.gpio.GPIOPin;
    import jdk.dio.gpio.GPIOPinConfig;


    /**

    * GPIOSample demonstrates basic GPIO functionality. The sample opens the

    * following DIO devices:

    */


    public class Oracle extends MIDlet {
        // GPIO pin which corresponds to a LED
        private GPIOPin led = null ;

        @Override
        public void startApp() {

            System.out.println("******************************************");
            System.out.println("*         GPIO Sample started!           *");
            System.out.println("******************************************");

            try {
                GPIOPinConfig config1 = new GPIOPinConfig(0,22, GPIOPinConfig.DIR_OUTPUT_ONLY,DeviceConfig.UNASSIGNED, 0, false);
               int ledPinId = 22;

                System.out.println("Opening the GPIO pin with id " + ledPinId + " for the LED");

                GPIOPin PIN22 = DeviceManager.open(config1);
                PIN22.setValue(true);
                Thread.sleep(4000);
                PIN22.close();

                System.out.println("Success opening GPIO pin id " + ledPinId + " for the LED");

            } catch (IOException ex) {

                System.out.println("Cannot open pin");

                destroyApp(true);

            } catch (InterruptedException ex) {
                Logger.getLogger(Oracle.class.getName()).log(Level.SEVERE, null, ex);
            }

           


        }

        @Override

        public void destroyApp(boolean unconditional) {

            System.out.println("******************************************");

            System.out.println("*         GPIO Sample destroyed          *");

            System.out.println("******************************************");

            notifyDestroyed();

        }

    }

    HarrySatt
  • HarrySatt
    HarrySatt Member Posts: 26
    edited Jan 7, 2016 5:11AM

    Hi Adam

    That solution works with Pin 7 (GPIO7) as well as Pin 15 (GPIO22). I wonder why there is a difference to all the samples I read and tried out. the original samples do not word altough I tried the Raspberry 8 (Revision 000d). Thanks a lot.

    Harald.

  • HarrySatt
    HarrySatt Member Posts: 26
    edited Jan 7, 2016 7:00AM

    Hi Adam

    I found out some issues with might be helpful for you. You can use the following document

    https://docs.oracle.com/javame/8.2/get-started-rpi/piportsapdx.htm

    2GPIO7GPIO 7controllerNumber = 0

    pinNumber = 7

    direction = GPIOPinConfig.DIR_OUTPUT_ONLY

    mode = GPIOPinConfig.MODE_OUTPUT_PUSH_PULL

    trigger = GPIOPinConfig.TRIGGER_BOTH_EDGES

    initValue = false

    which gives you information about devices, so you do not need to set the pns with GPIOPinConfig. But there is an error in the doc. Do not relate to the pinNumber in the listing use the pin as shown in the drawing underneath the list. there you have your GPIO<num> and the correct pin number. It differs from the list.

    with this information you can open device 2 f.e. and measure voltage changes on pin 26 of P8 on the RPi without using GPIOPinConfig.

    Regards, harald.

    following an example:

    /*

    * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.

    */

    package com.oracle.jmee.samples.gpio;

    import java.io.IOException;

    import javax.microedition.midlet.MIDlet;

    import jdk.dio.DeviceManager;

    import jdk.dio.gpio.GPIOPin;

    import jdk.dio.gpio.GPIOPinConfig;

    /**

    * GPIOSample demonstrates basic GPIO functionality. The sample opens the

    * following DIO devices:

    */

    public class GPIOSample extends MIDlet {

        // GPIO pin which corresponds to a LED

        private GPIOPin led = null;

        @Override

        public void startApp() {

            System.out.println("******************************************");

            System.out.println("*         GPIO Sample started!           *");

            System.out.println("******************************************");

            try {

                int ledDeviceID = 2; // in sample RPi pin 26 is used. work with RPi B and RPi2b

                System.out.println("Opening the GPIO pin with id " + ledDeviceID + " for the LED");

                led = DeviceManager.open(ledDeviceID);

                System.out.println("Success opening GPIO pin id " + ledDeviceID + " for the LED");

            } catch (IOException ex) {

                System.out.println("Cannot open pin");

                destroyApp(true);

            }

           

            boolean val = true;

            for( int i=0; i<20; i++)

            {

                try {

                    Thread.sleep(1000); // I know. Should not be done. Only for testing

                }

                catch( InterruptedException ex)

                {

                    System.out.println("  !!! Interrupted exception");

                }

                try {

                    val = !led.getValue();

                    led.setValue(val);

                }

                catch ( IOException ex) {

                    System.out.println(" caught IOException ex");

                }

                System.out.println(" ***  next state change: " + i + " is " + val);

            }

            destroyApp(true);

        }

        @Override

        public void destroyApp(boolean unconditional) {

            System.out.println("******************************************");

            System.out.println("*         GPIO Sample destroyed          *");

            System.out.println("******************************************");

            notifyDestroyed();

        }

    }

This discussion has been closed.