Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Raspberry Pi 2 Mod B - GPIO How

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;
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);
}
public void destroyApp(boolean unconditional) {
System.out.println("******************************************");
System.out.println("* GPIO Sample destroyed *");
System.out.println("******************************************");
notifyDestroyed();
}
}
Best 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
-
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
-
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);
}
}public void destroyApp(boolean unconditional) {
System.out.println("******************************************");
System.out.println("* GPIO Sample destroyed *");
System.out.println("******************************************");
notifyDestroyed();
}
}
-
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.
-
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
2 GPIO7 GPIO 7 controllerNumber = 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;
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);
}
public void destroyApp(boolean unconditional) {
System.out.println("******************************************");
System.out.println("* GPIO Sample destroyed *");
System.out.println("******************************************");
notifyDestroyed();
}
}