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

38
Introduction to Java ME for the Raspberry Pi [JavaOne 2016 CON3189] Kevin Hooke Julio Palma Vazquez

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

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

Introduction to Java MEfor the Raspberry Pi [JavaOne 2016 CON3189]

Kevin HookeJulio Palma Vazquez

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

Who Are We?

Copyright © 2016 Accenture All rights reserved.

Kevin HookeTechnology ArchitectAccenture Java Community Champion@kevinhooke

Julio PalmaTechnology ArchitectAccenture Java Community Champion@restalion

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

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/8Kevin HookeJulio Palma

Jorge HidalgoJulio Palma

Jorge HidalgoMariano Rodriguez

Copyright © 2016 Accenture All rights reserved.

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

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

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

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:

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

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.

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

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.

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

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.

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

NetBeans setup (3)

• ME SDK Welcome displayed on restart:

Copyright © 2016 Accenture All rights reserved.

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

Creating a new ME Project

• File / New Project / Java ME Embedded

Copyright © 2016 Accenture All rights reserved.

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

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.

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

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.

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

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.

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

Publish project to Pi

• Change Project Properties on Project• Select ‘Platform’• Change ‘Device’ to newly added Raspberry Pi

Copyright © 2016 Accenture All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

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.

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

Java ME Preconfigured Pin example

From: http://docs.oracle.com/javame/8.3/get-started-rpi/device-i-o-preconfigured-list.htm#MEEPG148

Device IdPin name

Copyright © 2016 Accenture All rights reserved.

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

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.

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

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.

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

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.

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

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.

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

Raspberry Pi Emulated Device (3)

Copyright © 2016 Accenture All rights reserved.

In Custom Device Editor, Edit – add required libraries for your projecte.g. Device I/O Api – Access and Control (DeviceManager api) and GPIO (for GPIO pins api)

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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:

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

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.

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

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=8385509

+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!

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

Demos!

Copyright © 2016 Accenture All rights reserved.

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

Demo Source

https://github.com/kevinhooke/JavaMESimpleLEDTest

https://github.com/kevinhooke/RaspPiLEDsAndButtonsGame

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

Downloads & Useful Links

https://www.raspberrypi.org

https://netbeans.org/

http://www.oracle.com/technetwork/java/embedded/javame/javame-sdk/downloads/javamesdkdownloads-2166598.html

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

Copyright © 2014 Accenture. All rights reserved.

Q&A

• Any questions?