37
I/O Programming with Java on the Raspberry Pi with Pi4J JavaOne 2013 - CON7968 Robert Savage, Software Architect, AMX LLC. Let’s Get Physical:

Con7968 let's get physical - io programming using pi4 j

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Con7968   let's get physical - io programming using pi4 j

I/O Programming with Java on the Raspberry Pi with Pi4J

JavaOne 2013 - CON7968

Robert Savage, Software Architect, AMX LLC.

Let’s Get Physical:

Page 2: Con7968   let's get physical - io programming using pi4 j

Additional ResourcesBlog Articles on Project:

http://www.savagehomeautomation.com/j1pi4j

Source Code For Demoshttps://github.com/savagehomeautomation/pi4j-javaone-demos

Page 3: Con7968   let's get physical - io programming using pi4 j

Disclaimer!

UAYOR! / YMMV!(use at your own risk / your mileage may vary)

The projects and sample code provided in this demonstration are intended as examples and may not cover all practical use cases, local building codes, fire and/or security regulations depending on your implementation and

location.

THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

DISCLAIMER:Views expressed are solely the personal views and opinions of the presenter and do not represent the views of Oracle Corp., AMX, LLC or any other company.

Page 4: Con7968   let's get physical - io programming using pi4 j

Agenda / Topics Raspberry Pi Overview Pi4J Overview Basic “Getting Started”

Examples Simple GPIO Example/Demo GPIO Triggers Example/Demo RS232 Serial Communication

Example / Demo

Components Expansion Putting It All Together Roadmap Q&A

Page 5: Con7968   let's get physical - io programming using pi4 j

Raspberry PiSingle Board ComputerLinux Operating systemARM ArchitectureLow CostLow Power

Page 6: Con7968   let's get physical - io programming using pi4 j

Java on the Raspberry PiOpenJDK

Installation: http://www.savagehomeautomation.com/raspi-openjdk

Oracle Hotspot JVM for ARM/Linux (preferred) JDK 7 Installation: http://www.savagehomeautomation.com/raspi-jdk7 JDK 8 Installation: http://www.savagehomeautomation.com/raspi-jdk8

Soft-float ABIHard-float ABI

Page 7: Con7968   let's get physical - io programming using pi4 j

What is Pi4J?Open Source ProjectLow Level I/OObject-oriented APIEvent BasedRaspberry Pi Platform (Linux/ARM)Java / C (JNI + Native)

Page 8: Con7968   let's get physical - io programming using pi4 j

Pi4J : Supported I/OGPIO General Purpose I/O

UART Serial / RS232

SPI Serial Peripheral Interface

I2C Inter-Integrated Circuit

PWM Pulse-Width-Modulation

Page 9: Con7968   let's get physical - io programming using pi4 j

Pi4J : GPIO DemoBasic program using a simple

momentary button (input) and LED (output).

GOAL:

Illuminate LED when the buttonis pressed. Deactivate LED whenbutton is released.

Page 10: Con7968   let's get physical - io programming using pi4 j

GPIO

Dem

o : W

iring

Dia

gram

Page 11: Con7968   let's get physical - io programming using pi4 j

GPIO

Dem

o : P

rogr

am L

ogic

Page 12: Con7968   let's get physical - io programming using pi4 j

// create GPIO controllerfinal GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; input pinfinal GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP);

// led; output pinfinal GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW);

// create event listener for button input pinbuttonPin.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {

if(event.getState().isHigh()){ // turn off LED pin ledPin.setState(PinState.LOW); } else{ // turn on LED pin ledPin.setState(PinState.HIGH); }}});

GPIO

Dem

o : S

ampl

e Co

de

Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-1-gpio

Page 13: Con7968   let's get physical - io programming using pi4 j

GPIO

Dem

o : I

n Ac

tion

Pi4J : GPIO Demo

Demo Time!I’m not here for pretty pictures ..

Lets see some *working* code!

Page 14: Con7968   let's get physical - io programming using pi4 j

Pi4J : GPIO Trigger DemoPi4J provides simple

automation triggers for commonGPIO event interactions.

( == less code)

GOAL:

Toggle LED state when the buttonis pressed.

Page 15: Con7968   let's get physical - io programming using pi4 j

GPIO

Trig

ger D

emo

: Wiri

ng D

iagr

am

Page 16: Con7968   let's get physical - io programming using pi4 j

GPIO

Trig

ger D

emo

: Pro

gram

Log

ic

Page 17: Con7968   let's get physical - io programming using pi4 j

// create GPIO controllerfinal GpioController gpio = GpioFactory.getInstance();

// momentary push-button switch; activates when button is pushedfinal GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP);

// led; illuminates when GPIO is HIfinal GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW);

// create button event listenerbuttonPin.addTrigger(new GpioToggleStateTrigger(PinState.LOW, ledPin));

GPIO

Trig

ger D

emo

: Sam

ple

Code

Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-2-gpio-trigger

Page 18: Con7968   let's get physical - io programming using pi4 j

GPIO

Trig

ger D

emo

: In

Actio

n Pi4J : GPIO Trigger Demo

Demo Time!This isn’t rocket science!

Page 19: Con7968   let's get physical - io programming using pi4 j

Pi4J : RS232 DemoBasic program using RS232

serial communication.

GOAL:

Display “Hello World” onLED message reader.

Page 20: Con7968   let's get physical - io programming using pi4 j

RS23

2 De

mo

: Wiri

ng D

iagr

am

Page 21: Con7968   let's get physical - io programming using pi4 j

RS23

2 De

mo

: Pro

gram

Log

ic

Page 22: Con7968   let's get physical - io programming using pi4 j

RS23

2 De

mo

: Sam

ple

Code

// create an instance of the serial communications classfinal Serial serial = SerialFactory.createInstance();

// open the default serial port provided on the P1 header// (this is where our LED reader is connected)serial.open(Serial.DEFAULT_COM_PORT, 2400); // 2400 BAUD, N, 8, 1

// create and register the serial data listenerserial.addListener(new SerialDataListener() { @Override public void dataReceived(SerialDataEvent event) { // print out the data received to the console System.out.println(event.getData()); }});

// send "Hello World" message to signserial.writeln("<ID01><PA><CL>Hello World! -- ");Thread.sleep(500);serial.writeln("<ID01><RPA>");

Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-3-rs232

Page 23: Con7968   let's get physical - io programming using pi4 j

RS23

2 De

mo

: In

Actio

n

Pi4J : RS232 Demo

Demo Time!

Page 24: Con7968   let's get physical - io programming using pi4 j

Pi4J : Component API The component APIs provides an abstraction layer from the

hardware I/O layer. This allows hardware design/circuitry to change with *less*

impact to your implementation code. For example, a RELAY could be controlled from GPIO, RS232,

SPI, or I2C. You program defines the RELAY impl up front based on the hardware interface, but the rest of your program logic works against the RELAY component interface and not the direct hardware /communication IO interfaces.

Page 25: Con7968   let's get physical - io programming using pi4 j

Pi4J : Component APIKeypadLight / LEDDimmable LightLCDPower ControllerRelay

Momentary SwitchToggle SwitchAnalog SensorDistance SensorMotion SensorTemperature Sensor

Page 26: Con7968   let's get physical - io programming using pi4 j

Com

pone

nt D

emo

: Wiri

ng D

iagr

am

Page 27: Con7968   let's get physical - io programming using pi4 j

Com

pone

nt D

emo

: Pro

gram

Log

ic

Page 28: Con7968   let's get physical - io programming using pi4 j

Com

pone

nt D

emo

: Sam

ple

Code

// create GPIO controllerfinal GpioController gpio = GpioFactory.getInstance();

// momentary push-button switch; activates when button is pushedfinal MomentarySwitch momentarySwitch = new GpioMomentarySwitchComponent( gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP), PinState.HIGH, // "OFF" PIN STATE PinState.LOW); // "ON" PIN STATE

// led; illuminates when momentary switch is pushedfinal LED led = new GpioLEDComponent( gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW));

// create momentary switch event listenermomentarySwitch.addListener(new SwitchListener() { @Override public void onStateChange(SwitchStateChangeEvent event) { if(event.getNewState() == SwitchState.ON){ led.on(); // turn ON LED } else{ led.off(); // turn OFF LED } }});

Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-4-components

Page 29: Con7968   let's get physical - io programming using pi4 j

Com

pone

nt D

emo

: In

Actio

n Pi4J : Component Demo

Demo Time!

Page 30: Con7968   let's get physical - io programming using pi4 j

Pi4J : I/O ExpansionGPIO General Purpose I/OPWM Pulse-Width ModulationADC Analog-to-Digital ConverterDAC Digital-to-Analog Converter

Page 31: Con7968   let's get physical - io programming using pi4 j

Pi4J : Putting It All Together

Time to jump in to the deep end of the pool.

Page 32: Con7968   let's get physical - io programming using pi4 j

Pi4J : Access Control DemoRFID AccessKeypad AccessOverride (“Exit”) SwitchDoorbellDoor SensorsKeypad Tamper Switch

Electromechanical Lock (Solenoid)

Security AlarmNotificationsAccess LogRemote Access

Page 33: Con7968   let's get physical - io programming using pi4 j

Acce

ss C

ontro

l Dem

o : W

iring

Dia

gram

Page 34: Con7968   let's get physical - io programming using pi4 j

Acce

ss C

ontro

l Dem

o : I

n Ac

tion Pi4J : Access Control Demo

Code & Demo Time!

Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-5-access-control

Page 35: Con7968   let's get physical - io programming using pi4 j

Pi4J : RoadmapVersion 1.0 Release – Q1 2014Additional Component Interfaces + ImplsPlatform AbstractionTesting HarnessLibrary Refactoring1-Wire / Infrared / Bluetooth / USB HID / RTCWeb Service/Remote API

Page 36: Con7968   let's get physical - io programming using pi4 j

Questions?