Introduction to Java ME
for the Raspberry Pi
[JavaOne 2016 CON3189]
Kevin Hooke
Julio Palma Vazquez
Who Are We?
Copyright © 2016 Accenture All rights reserved.
Kevin Hooke
Technology Architect
Accenture Java Community Champion
@kevinhooke
Julio Palma
Technology Architect
Accenture Java Community Champion
@restalion
Related Sessions JavaOne 2016
Monday, Sep 19 Tuesday, Sep 20 Wednesday, Sep 21
Introduction to Java ME 8
[CON3189]
Java ME and Single-Board
Computers for Creating
Industrial
Middleware [CON3187]
All Your Faces Belong to Us:
Building an Open Face
Recognition Platform
[CON6217]
12:30 p.m. - 1:30 p.m 2:30 p.m. - 3:30 p.m 3:00 p.m. - 4:00 p.m
Hilton - Golden Gate 6/7/8 Hilton - Golden Gate 6/7/8 Hilton - Golden Gate 6/7/8
Kevin Hooke
Julio Palma
Jorge Hidalgo
Julio Palma
Jorge Hidalgo
Mariano Rodriguez
Copyright © 2016 Accenture All rights reserved.
Session Goals
Copyright © 2016 Accenture All rights reserved.
Java ME SDK + IDE Plugins+
+
+
By Afrank99 - Own work, CC BY-SA 2.0,
https://commons.wikimedia.org/w/index.php?curid=227264
By Inductiveload - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=8385509
Session Goals
Copyright © 2016 Accenture All rights reserved.
Java ME SDK + IDE Plugins+
+
+
By Afrank99 - Own work, CC BY-SA 2.0,
https://commons.wikimedia.org/w/index.php?curid=227264
By Inductiveload - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=8385509
Build one of these:
Getting Started: Required Setup
Dev tools:
• Java ME SDK 8.3 (for Window or Linux)
• NetBeans 8.1 (Or Eclipse)
• NetBeans Java ME SDK 8.3 plugins (Or Eclipse)
Raspberry Pi runtime:
• Raspberry Pi (although can develop with emulator in ME SDK)
• Raspbian
• Java ME Embedded 8.3 for Raspberry Pi
Copyright © 2016 Accenture All rights reserved.
NetBeans setup
• Tools / Plugins /
Settings tab, Add new
‘Update Center’
• Point to unzipped
Java ME SDK plugins
in a dir using file://
On Windows:
• Use file:///
• path to folder with no
spaces in name works
best)
Copyright © 2016 Accenture All rights reserved.
NetBeans setup (2)
• ‘Available plugins’
tab
• Select:
• Java ME SDK
Tools
• Java ME SDK
Demos
• Press Install
• Ok and restart
NetBeans when
prompted
Copyright © 2016 Accenture All rights reserved.
NetBeans setup (3)
• ME SDK Welcome
displayed on restart:
Copyright © 2016 Accenture All rights reserved.
Creating a new ME Project
• File / New Project / Java
ME Embedded
Copyright © 2016 Accenture All rights reserved.
Creating a new ME project (2)
• Defaults will use an emulated
Embedded Device
• Emulated devices and connections to
real hardware provided by Java ME
SDK Device Manager (Windows and
Linux)
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi setup
• SCP/SFTP Java ME JDK zip for Raspberry Pi to your Pi
• Unzip, cd to bin
• ./fix_permissions.sh
• Run with: sudo ./usertest.sh
Copyright © 2016 Accenture All rights reserved.
Connect Device Manager to Java ME on Pi
• Press ‘+’ in Device Connection Manager
• If connected over network, give IP of the
remote Pi (might auto detect)
• Should see connection status listed as
connected (below):
Copyright © 2016 Accenture All rights reserved.
Publish project to Pi
• Change Project Properties on Project
• Select ‘Platform’
• Change ‘Device’ to newly added Raspberry Pi
Copyright © 2016 Accenture All rights reserved.
Java ME Hello World!
• Instead of a simple (typical)
System.out.println()
• Internet of Things (IoT) and
embedded devices are all about
hardware
• Let’s say ‘Hello World’ with a
flashing LED!
• First need to understand
available GPIO pins on the Pi
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi GPIO pins
• Pins for Input and Output – 3.3v
• For interacting with external devices:
switches, LEDs, sensors etc
• Building blocks for building IoT type
solutions!
From: Herbfargus - Own work, CC BY-SA 4.0,
https://commons.wikimedia.org/w/index.php?curid=47497384
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi GPIO pins(2)
• What pin does what? (not all are GPIO)
• Raspberry Pi docs site shows which pins are power/ground, and GPIO
• Interactive online diagrams, like https://pinout.xyz/
From: https://www.raspberrypi.org/documentation/usage/gpio-plus-and-
raspi2/README.md
Copyright © 2016 Accenture All rights reserved.
Java ME and GPIO Pins
DeviceManager.open(int)
• refers to GPIO pin by Device Id (NOT GPIO pin number)
• e.g. 1 == GPIO4
• See mappings in docs here:
http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured-
list.htm#MEEPG148
Copyright © 2016 Accenture All rights reserved.
Java ME and GPIO Pins
DeviceManager.open(int)
• refers to GPIO pin by Device Id (NOT GPIO pin number)
• e.g. 1 == GPIO4
• See mappings in docs here:
http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured-
list.htm#MEEPG148
Less confusing – open by “pin name”, e.g. “GPIO4”
DeviceManager.open(“GPIO4”, GPIOPin.class)
• Refers to pin by configured name, e.g. “GPIO4”.
• Mappings defined in same doc above
Copyright © 2016 Accenture All rights reserved.
Java ME Preconfigured Pin example
From: http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured-
list.htm#MEEPG148
Device Id
Pin name
Copyright © 2016 Accenture All rights reserved.
Java ME Hello World – Flashing LED!
GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class);
boolean currentValue = false;
while(true){
currentValue = !currentValue;
pin18.setValue(currentValue);
System.out.println("Changing pin value to: " + currentValue);
Thread.sleep(2000);
}
Copyright © 2016 Accenture All rights reserved.
Progress so far
By Afrank99 - Own work, CC BY-SA 2.0,
https://commons.wikimedia.org/w/index.php?curid=227264
GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class);
Pin18.setValue(true);
+
We can now turn on/off LEDs!
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi Emulator Device
• Allows for local testing
• Use a preconfigured emulator device that represents pins on actual device:
https://github.com/SubOptimal/RaspberryPI_EmulatorDevice
• Download zip
• From Java ME Tools in NetBeans, select “Custom Device Emulator”
• Click Import, point to downloaded zip
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi Emulated Device (2)
• Now see
RaspberryPi_EmulatorDevice
in Device Selector list
• By default – does not include
libraries, need to select in
config too
Copyright © 2016 Accenture All rights reserved.
Raspberry Pi Emulated Device (3)
Copyright © 2016 Accenture All rights reserved.
In Custom Device Editor, Edit – add required libraries for your project
e.g. Device I/O Api – Access and Control (DeviceManager api) and GPIO (for GPIO pins
api)
Java ME API Security Permissions
• Deploying first app, you’ll likely see this error (if you’re not following a tutorial):
Copyright © 2016 Accenture All rights reserved.
Java ME API Security Permissions (2)
• Select View / Device Log: “Permission check failed”
This indicates API
permissions are missing
You need to set Permissions
to access resources on the
device
From Project Properties /
Application Descriptor:
Copyright © 2016 Accenture All rights reserved.
Java ME API Security Permissions (3)
• Click Add – add open permission for each GPIO pin you need to control (or wildcard *:*):
Copyright © 2016 Accenture All rights reserved.
ME device id vs GPIO pin number error
Error:
jdk.dio.DeviceNotFoundException: Device 14 not found
- jdk/dio/DeviceManager.open(), bci=87
Check:
Are you trying to use GPIO pins ids, and not Device Ids?
e.g.
DeviceManager.open(14)
(there is no DeviceId == 14)
Copyright © 2016 Accenture All rights reserved.
Configuring GPIO pins & creating Event Listeners
• To handle button clicks (button click = event)
• Configure pin for input, and register a listener:
GPIOPin gpio4 = DeviceManager.open(new GPIOPinConfig.Builder()
.setPinNumber(GPIO4_pin)
.setDirection(GPIOPinConfig.DIR_INPUT_ONLY)
.setTrigger(GPIOPinConfig.TRIGGER_FALLING_EDGE)
.build());
gpio4.setInputListener((PinListener) this);
Copyright © 2016 Accenture All rights reserved.
Event Listeners (2)
• Implement the PinListener interface:
public class JavaMETestButton extends MIDlet implements PinListener {
// …
public void valueChanged(PinEvent event) {
//do something here!
System.out.println("gpio4 event/button press!");
}
Copyright © 2016 Accenture All rights reserved.
Default Java ME defined pin mappings
DIR_INPUT_ONLY
• GPIO4
• GPIO17
• GPIO22
• GPIO27
DIR_OUTPUT_ONLY
• GPIO7
• GPIO18
• GPIO23
• GPIO24
• GPIO25
Copyright © 2016 Accenture All rights reserved.
Pre-configured / default
pins:
Progress so far (2)
• You can now:
–Write code to interact with the physical world!
–Turn on/off pins - turn on LEDs/Lights, trigger relays
–Detect high/low on a pin - react to switch/button/sensor
Copyright © 2016 Accenture All rights reserved.
Progress so far (3)
By Afrank99 - Own work, CC BY-SA 2.0,
https://commons.wikimedia.org/w/index.php?curid
=227264
GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class);
pin18.setValue(true);
+
Copyright © 2016 Accenture All rights reserved.
By Inductiveload - Own work, Public Domain,
https://commons.wikimedia.org/w/index.php?curid=8
385509
+
GPIOPin gpio4 = DeviceManager.open(new GPIOPinConfig.Builder()
.setPinNumber(GPIO4_pin)
.setDirection(GPIOPinConfig.DIR_INPUT_ONLY)
.setTrigger(GPIOPinConfig.TRIGGER_FALLING_EDGE)
.build());
gpio4.setInputListener((PinListener) this);
Basic building blocks for any embedded project!
Demos!
Copyright © 2016 Accenture All rights reserved.
Demo Source
https://github.com/kevinhooke/JavaMESimpleLEDTest
https://github.com/kevinhooke/RaspPiLEDsAndButtonsGame
Downloads & Useful Links
https://www.raspberrypi.org
https://netbeans.org/
http://www.oracle.com/technetwork/java/embedded/javame/javame-
sdk/downloads/javamesdkdownloads-2166598.html
Copyright © 2014 Accenture. All rights reserved.
Q&A
• Any questions?

[CON3189] JavaOne 2016 - Introduction to Java ME development for the Raspberry Pi

  • 1.
    Introduction to JavaME for the Raspberry Pi [JavaOne 2016 CON3189] Kevin Hooke Julio Palma Vazquez
  • 2.
    Who Are We? Copyright© 2016 Accenture All rights reserved. Kevin Hooke Technology Architect Accenture Java Community Champion @kevinhooke Julio Palma Technology Architect Accenture Java Community Champion @restalion
  • 3.
    Related Sessions JavaOne2016 Monday, Sep 19 Tuesday, Sep 20 Wednesday, Sep 21 Introduction to Java ME 8 [CON3189] Java ME and Single-Board Computers for Creating Industrial Middleware [CON3187] All Your Faces Belong to Us: Building an Open Face Recognition Platform [CON6217] 12:30 p.m. - 1:30 p.m 2:30 p.m. - 3:30 p.m 3:00 p.m. - 4:00 p.m Hilton - Golden Gate 6/7/8 Hilton - Golden Gate 6/7/8 Hilton - Golden Gate 6/7/8 Kevin Hooke Julio Palma Jorge Hidalgo Julio Palma Jorge Hidalgo Mariano Rodriguez Copyright © 2016 Accenture All rights reserved.
  • 4.
    Session Goals Copyright ©2016 Accenture All rights reserved. Java ME SDK + IDE Plugins+ + + By Afrank99 - Own work, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=227264 By Inductiveload - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=8385509
  • 5.
    Session Goals Copyright ©2016 Accenture All rights reserved. Java ME SDK + IDE Plugins+ + + By Afrank99 - Own work, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=227264 By Inductiveload - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=8385509 Build one of these:
  • 6.
    Getting Started: RequiredSetup Dev tools: • Java ME SDK 8.3 (for Window or Linux) • NetBeans 8.1 (Or Eclipse) • NetBeans Java ME SDK 8.3 plugins (Or Eclipse) Raspberry Pi runtime: • Raspberry Pi (although can develop with emulator in ME SDK) • Raspbian • Java ME Embedded 8.3 for Raspberry Pi Copyright © 2016 Accenture All rights reserved.
  • 7.
    NetBeans setup • Tools/ Plugins / Settings tab, Add new ‘Update Center’ • Point to unzipped Java ME SDK plugins in a dir using file:// On Windows: • Use file:/// • path to folder with no spaces in name works best) Copyright © 2016 Accenture All rights reserved.
  • 8.
    NetBeans setup (2) •‘Available plugins’ tab • Select: • Java ME SDK Tools • Java ME SDK Demos • Press Install • Ok and restart NetBeans when prompted Copyright © 2016 Accenture All rights reserved.
  • 9.
    NetBeans setup (3) •ME SDK Welcome displayed on restart: Copyright © 2016 Accenture All rights reserved.
  • 10.
    Creating a newME Project • File / New Project / Java ME Embedded Copyright © 2016 Accenture All rights reserved.
  • 11.
    Creating a newME project (2) • Defaults will use an emulated Embedded Device • Emulated devices and connections to real hardware provided by Java ME SDK Device Manager (Windows and Linux) Copyright © 2016 Accenture All rights reserved.
  • 12.
    Raspberry Pi setup •SCP/SFTP Java ME JDK zip for Raspberry Pi to your Pi • Unzip, cd to bin • ./fix_permissions.sh • Run with: sudo ./usertest.sh Copyright © 2016 Accenture All rights reserved.
  • 13.
    Connect Device Managerto Java ME on Pi • Press ‘+’ in Device Connection Manager • If connected over network, give IP of the remote Pi (might auto detect) • Should see connection status listed as connected (below): Copyright © 2016 Accenture All rights reserved.
  • 14.
    Publish project toPi • Change Project Properties on Project • Select ‘Platform’ • Change ‘Device’ to newly added Raspberry Pi Copyright © 2016 Accenture All rights reserved.
  • 15.
    Java ME HelloWorld! • Instead of a simple (typical) System.out.println() • Internet of Things (IoT) and embedded devices are all about hardware • Let’s say ‘Hello World’ with a flashing LED! • First need to understand available GPIO pins on the Pi Copyright © 2016 Accenture All rights reserved.
  • 16.
    Raspberry Pi GPIOpins • Pins for Input and Output – 3.3v • For interacting with external devices: switches, LEDs, sensors etc • Building blocks for building IoT type solutions! From: Herbfargus - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=47497384 Copyright © 2016 Accenture All rights reserved.
  • 17.
    Raspberry Pi GPIOpins(2) • What pin does what? (not all are GPIO) • Raspberry Pi docs site shows which pins are power/ground, and GPIO • Interactive online diagrams, like https://pinout.xyz/ From: https://www.raspberrypi.org/documentation/usage/gpio-plus-and- raspi2/README.md Copyright © 2016 Accenture All rights reserved.
  • 18.
    Java ME andGPIO Pins DeviceManager.open(int) • refers to GPIO pin by Device Id (NOT GPIO pin number) • e.g. 1 == GPIO4 • See mappings in docs here: http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured- list.htm#MEEPG148 Copyright © 2016 Accenture All rights reserved.
  • 19.
    Java ME andGPIO Pins DeviceManager.open(int) • refers to GPIO pin by Device Id (NOT GPIO pin number) • e.g. 1 == GPIO4 • See mappings in docs here: http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured- list.htm#MEEPG148 Less confusing – open by “pin name”, e.g. “GPIO4” DeviceManager.open(“GPIO4”, GPIOPin.class) • Refers to pin by configured name, e.g. “GPIO4”. • Mappings defined in same doc above Copyright © 2016 Accenture All rights reserved.
  • 20.
    Java ME PreconfiguredPin example From: http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured- list.htm#MEEPG148 Device Id Pin name Copyright © 2016 Accenture All rights reserved.
  • 21.
    Java ME HelloWorld – Flashing LED! GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class); boolean currentValue = false; while(true){ currentValue = !currentValue; pin18.setValue(currentValue); System.out.println("Changing pin value to: " + currentValue); Thread.sleep(2000); } Copyright © 2016 Accenture All rights reserved.
  • 22.
    Progress so far ByAfrank99 - Own work, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid=227264 GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class); Pin18.setValue(true); + We can now turn on/off LEDs! Copyright © 2016 Accenture All rights reserved.
  • 23.
    Raspberry Pi EmulatorDevice • Allows for local testing • Use a preconfigured emulator device that represents pins on actual device: https://github.com/SubOptimal/RaspberryPI_EmulatorDevice • Download zip • From Java ME Tools in NetBeans, select “Custom Device Emulator” • Click Import, point to downloaded zip Copyright © 2016 Accenture All rights reserved.
  • 24.
    Raspberry Pi EmulatedDevice (2) • Now see RaspberryPi_EmulatorDevice in Device Selector list • By default – does not include libraries, need to select in config too Copyright © 2016 Accenture All rights reserved.
  • 25.
    Raspberry Pi EmulatedDevice (3) Copyright © 2016 Accenture All rights reserved. In Custom Device Editor, Edit – add required libraries for your project e.g. Device I/O Api – Access and Control (DeviceManager api) and GPIO (for GPIO pins api)
  • 26.
    Java ME APISecurity Permissions • Deploying first app, you’ll likely see this error (if you’re not following a tutorial): Copyright © 2016 Accenture All rights reserved.
  • 27.
    Java ME APISecurity Permissions (2) • Select View / Device Log: “Permission check failed” This indicates API permissions are missing You need to set Permissions to access resources on the device From Project Properties / Application Descriptor: Copyright © 2016 Accenture All rights reserved.
  • 28.
    Java ME APISecurity Permissions (3) • Click Add – add open permission for each GPIO pin you need to control (or wildcard *:*): Copyright © 2016 Accenture All rights reserved.
  • 29.
    ME device idvs GPIO pin number error Error: jdk.dio.DeviceNotFoundException: Device 14 not found - jdk/dio/DeviceManager.open(), bci=87 Check: Are you trying to use GPIO pins ids, and not Device Ids? e.g. DeviceManager.open(14) (there is no DeviceId == 14) Copyright © 2016 Accenture All rights reserved.
  • 30.
    Configuring GPIO pins& creating Event Listeners • To handle button clicks (button click = event) • Configure pin for input, and register a listener: GPIOPin gpio4 = DeviceManager.open(new GPIOPinConfig.Builder() .setPinNumber(GPIO4_pin) .setDirection(GPIOPinConfig.DIR_INPUT_ONLY) .setTrigger(GPIOPinConfig.TRIGGER_FALLING_EDGE) .build()); gpio4.setInputListener((PinListener) this); Copyright © 2016 Accenture All rights reserved.
  • 31.
    Event Listeners (2) •Implement the PinListener interface: public class JavaMETestButton extends MIDlet implements PinListener { // … public void valueChanged(PinEvent event) { //do something here! System.out.println("gpio4 event/button press!"); } Copyright © 2016 Accenture All rights reserved.
  • 32.
    Default Java MEdefined pin mappings DIR_INPUT_ONLY • GPIO4 • GPIO17 • GPIO22 • GPIO27 DIR_OUTPUT_ONLY • GPIO7 • GPIO18 • GPIO23 • GPIO24 • GPIO25 Copyright © 2016 Accenture All rights reserved. Pre-configured / default pins:
  • 33.
    Progress so far(2) • You can now: –Write code to interact with the physical world! –Turn on/off pins - turn on LEDs/Lights, trigger relays –Detect high/low on a pin - react to switch/button/sensor Copyright © 2016 Accenture All rights reserved.
  • 34.
    Progress so far(3) By Afrank99 - Own work, CC BY-SA 2.0, https://commons.wikimedia.org/w/index.php?curid =227264 GPIOPin pin18 = DeviceManager.open(“GPIO18”, GPIOPin.class); pin18.setValue(true); + Copyright © 2016 Accenture All rights reserved. By Inductiveload - Own work, Public Domain, https://commons.wikimedia.org/w/index.php?curid=8 385509 + GPIOPin gpio4 = DeviceManager.open(new GPIOPinConfig.Builder() .setPinNumber(GPIO4_pin) .setDirection(GPIOPinConfig.DIR_INPUT_ONLY) .setTrigger(GPIOPinConfig.TRIGGER_FALLING_EDGE) .build()); gpio4.setInputListener((PinListener) this); Basic building blocks for any embedded project!
  • 35.
    Demos! Copyright © 2016Accenture All rights reserved.
  • 36.
  • 37.
    Downloads & UsefulLinks https://www.raspberrypi.org https://netbeans.org/ http://www.oracle.com/technetwork/java/embedded/javame/javame- sdk/downloads/javamesdkdownloads-2166598.html
  • 38.
    Copyright © 2014Accenture. All rights reserved. Q&A • Any questions?