Working with GPIO by Using Java ME Embedded and a Raspberry Pi
Overview
Purpose
This tutorial covers how to create Java Embedded applications
that read and write to general-purpose input/output (GPIO) pins on
a Raspberry PI by using Oracle Java ME Embedded 8.
Time to Complete
Approximately 1 hour
Introduction
Intelligent devices are becoming an ever more important and
ubiquitous part of our every day lives. Mobile phones represented
the first wave of smaller personal computers. And now, as the
price of electronics and processing power continues to fall, there
is an intersection between sensors and other electromechanical
devices and computers that live on the edge of the Internet: close
to the source of the data, processing the data locally and sending
just what is required to other computers to consume. This wave of
machine-to-machine (M2M) technology, or more broadly, the Internet
of Things (IoT), is rapidly shaping the future of computing.
Oracle Java Platform, Micro Edition (Java ME) provides Java
developers with a direct path to this new market space by using
their existing knowledge and skills.
Scenario
In this tutorial, you create a Java ME Embedded 8 application by
using a desktop development environment. Using a breadboard, you
wire a simple circuit to connect a push button switch to a GPIO
pin (input) and you wire a light-emitting diode (LED) to an output
pin. The application changes the state of the LED (on or off)
every time you press the button. This circuit illustrates the use
of a pull-down resistor and a modification to the trigger type on
the input pin.
Hardware
The following is a list of hardware requirements:
Raspberry Pi Model B Revision 2.0 (512 Mb)
Raspberry Pi Cobbler Kit or equivalent GPIO header to
breadboard connector (For this tutorial, we used the Adafruit
Raspberry Pi starter kit, which contains all parts that
are needed.)
Breadboard
Breadboard wires (solid core copper wires with stripped ends
recommended)
Have purchased the hardware required for the tutorial.
Creating a Java ME Embedded Project That Uses GPIO
In this section, you create a project by using NetBeans and you
test it locally by using the Windows emulator. You also add the
necessary permissions to access GPIO pins.
Modify the class to implement PinListener and
AutoCloseable. Implementing AutoCloseable
allows instances of this class to be used in a
try-with-resources clause, and requires that the class
implement a close method.
// Emulator Port names privatestaticfinalintLED1_ID=1; privatestaticfinalintButton_Pin=0; privateGPIOPinled1; privateGPIOPinbutton1;
The start method of the class attempts to
initialize the LED and button components. Copy this method
into the class. Note the two different approaches used to
open a pin. The LED is opened as a output pin using the
integer id of the pin. This pin is an output pin by
default. See the Resources section for more information on
which pins are input and output by default.
The button is opened as an input pin using a GPIOConfig
object to define the pin port number, pin number,
direction, mode, trigger and initial value. In general, it
is a good practice to use a config object to open pins.
publicvoidstart() throwsIOException { // Open the LED pin (Output) led1=DeviceManager.open(LED1_ID);
// Config file for the button - trigger on a rising edge (from low to high) GPIOPinConfigconfig1=newGPIOPinConfig(DeviceConfig.DEFAULT, Button_Pin, GPIOPinConfig.DIR_INPUT_ONLY, DeviceConfig.DEFAULT, GPIOPinConfig.TRIGGER_RISING_EDGE, false);
// Open the BUTTON pin (Input) button1=DeviceManager.open(config1);
// Add this class as a pin listener to the buttons button1.setInputListener(this);
// Turn the LED on, then off - this tests the LED led1.setValue(true); try { Thread.sleep(1000); } catch (InterruptedExceptionex) { }
// Start with the LED off (false) led1.setValue(false); }
Notice that the button is configured to trigger on the
rising edge.
The close method is called to close the LED
and button resources. Copy this method into the class.
The valueChanged method is invoked when the
state of the input pin changes. Because of the button
configuration, this method is invoked when the button
changes state from low to high (rising trigger). Copy this
method into the class.
// Simple one button = one LED try { System.out.println("setting led1"); led1.setValue(!led1.getValue()); // Toggle the value of the led } catch (IOExceptionex) { System.out.println("IOException: "+ex); } }
Press Ctrl + Shift + I to add the
required import statements.
Implementing the GPIOIMlet Class
Add the following class fields to the GPIOIMlet
class.
The startApp method is invoked when the
IMlet changes state from paused to running. Copy this code
over the startApp method that is generated
by NetBeans.
The destroyApp method is invoked when the
IMlet changes state from active state to destroyed. In
this method, all resources should be released, and all
threads should be stopped. Copy this code over the destroyApp
method that is generated by NetBeans.
The Button state changes to Low, yet the LED remains High,
because the trigger for the event in the code is only on the
rising edge of transition (that is, only when the button state
is changing from Low to High).
A breadboard is a convenient way to prototype a
circuit. A breadboard is "split" into two halves by a
slot running vertically down the center of it. Each row
of five holes on each half is connected electrically and
isolated from any other row. The columns on the left and
right edges are "rails." Each rail is electrically
connected for the entire length of the board (unless
indicated otherwise).
Orient the Raspberry Pi vertically. Pin 1 of the GPIO
header is labeled in the image.
The pins are numbered from 1 through 26, with the
odd-numbered pins on the left and the even-numbered pins
on the right. The pins with white labels should not be
connected and are marked "DNC" (do not connect) on many
headers.
Checking the Hardware Version of Your Raspberry Pi
Important: Be sure that you have a Raspberry
Pi Model B Revision 2.0 board before wiring your Raspberry Pi.
Start a PuTTY connection with your Raspberry Pi and enter
cat /proc/cpuinfo.
If the displayed revision value is 000d, 000e,
or 000f, you have a Raspberry Pi Model B,
512 Mb, Version 2. For more information about revision
codes, see the Resources section.
Wiring the Circuit on the Breadboard
Place the 26-pin header connector into the breadboard so
that:
The connector straddles the center.
Pin 1 (labeled "3V3") is in the first column at the
top left.
The "key" (the square cutout in the center of the
connector) is on the left side.
Perform the following tasks, using the row numbers shown
in the image as a reference:
Insert a push button switch starting at row 20 on the
breadboard. The switch should straddle the center of the
board.
Insert an LED. The long lead should go into row 18 on
the right side of the board, and the short lead should
go into the right negative (blue) rail.
Insert a 10 K Ohm resistor (brown, black, orange)
between row 20 on the right side and the negative (blue)
rail on the right. This is one side of the switch. Note:
It does not matter which way the resistor is facing
(left or right).
Insert a 560 Ohm resistor (green, blue, brown) into
row 18, with one lead on either side of the center of
the board.
Insert a red wire from pin 1 to the positive (red)
rail on the left.
Insert a red wire between the positive (red) rail and
row 22 on the left (this is the other side of the
switch).
Insert a white wire between GPIO pin 17 (row 6 on the
right side of the board) and row 20 on the right side of
the board.
Insert a blue wire between GPIO pin 23 (row 8 on the
right side of the board) and row 18 on the left side of
the board.
Insert a green wire between row 3 on the right and the
negative (blue) rail.
Caution: Double-check that pin 1, marked
3V3 (on the left), is connected to the positive rail, and
pin 6, the third one from the top on the right, is
connected to the negative rail. If you connect to the 5V
pin by accident, you could damage your Raspberry Pi.
The 10 K resistor attached to the ground pulls the value of
the input pin (#17) low until the switch is momentarily
pressed. When the value of the input pin goes high, it
signals an event to the PinListener. When the output pin
(#23) is set high, the LED illuminates. The 560 Ohm resistor
lowers the current that reaches the LED.
Connecting the Ribbon Cable Between the Raspberry Pi and the
Header
Power off your Raspberry Pi by unplugging the power cable.
Caution: Before handling your Raspberry
Pi, take care to touch a metal object in order to
discharge static electricity.
Connect the ribbon cable to the GPIO header on the
Raspberry Pi so that the white stripe (pin 1) is on the
right.
Caution: Make sure that the cable is not
offset before pushing it down onto the header. All pins
should be covered by the cable header.
Connect the other end of the ribbon cable to the
connector attached to the breadboard. The ribbon cable
header can be inserted only one way into the connector on
the breadboard.
Modifying the NetBeans Project to Use the
EmbeddedExternalDevice1 (Raspberry Pi)
In GPIOPinTest.java, comment out the class
fields that define the LED and button pin information for
the emulator and replace them with the class fields for
the Raspberry Pi.
You may notice that occasionally, the LED flickers, or
does not turn off. This is a result of switch "bounce".
Because the switch is a mechanical device, there can be
more than one rising edge event as the switch mechanism is
pressed or released. There are hardware and software
solutions for switch bounce.
Press the Stop button in the emulator
to quit the IMlet.
Modify the valueChanged method to start an
anonymous thread that sets a boolean variable and then
sleeps to allow the switch to bounce. Subsequent pin
events will be thrown away while the thread is sleeping.