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

Post on 30-Aug-2014

2.725 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

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

JavaOne 2013 - CON7968

Robert Savage, Software Architect, AMX LLC.

Let’s Get Physical:

Additional ResourcesBlog Articles on Project:

http://www.savagehomeautomation.com/j1pi4j

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

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.

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

Raspberry PiSingle Board ComputerLinux Operating systemARM ArchitectureLow CostLow Power

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

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

Pi4J : Supported I/OGPIO General Purpose I/O

UART Serial / RS232

SPI Serial Peripheral Interface

I2C Inter-Integrated Circuit

PWM Pulse-Width-Modulation

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.

GPIO

Dem

o : W

iring

Dia

gram

GPIO

Dem

o : P

rogr

am L

ogic

// 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

GPIO

Dem

o : I

n Ac

tion

Pi4J : GPIO Demo

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

Lets see some *working* code!

Pi4J : GPIO Trigger DemoPi4J provides simple

automation triggers for commonGPIO event interactions.

( == less code)

GOAL:

Toggle LED state when the buttonis pressed.

GPIO

Trig

ger D

emo

: Wiri

ng D

iagr

am

GPIO

Trig

ger D

emo

: Pro

gram

Log

ic

// 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

GPIO

Trig

ger D

emo

: In

Actio

n Pi4J : GPIO Trigger Demo

Demo Time!This isn’t rocket science!

Pi4J : RS232 DemoBasic program using RS232

serial communication.

GOAL:

Display “Hello World” onLED message reader.

RS23

2 De

mo

: Wiri

ng D

iagr

am

RS23

2 De

mo

: Pro

gram

Log

ic

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

RS23

2 De

mo

: In

Actio

n

Pi4J : RS232 Demo

Demo Time!

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.

Pi4J : Component APIKeypadLight / LEDDimmable LightLCDPower ControllerRelay

Momentary SwitchToggle SwitchAnalog SensorDistance SensorMotion SensorTemperature Sensor

Com

pone

nt D

emo

: Wiri

ng D

iagr

am

Com

pone

nt D

emo

: Pro

gram

Log

ic

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

Com

pone

nt D

emo

: In

Actio

n Pi4J : Component Demo

Demo Time!

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

Pi4J : Putting It All Together

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

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

Electromechanical Lock (Solenoid)

Security AlarmNotificationsAccess LogRemote Access

Acce

ss C

ontro

l Dem

o : W

iring

Dia

gram

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

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

Questions?

Contact / Links / Shameless PlugsWebsite pi4j.com

Source https://github.com/Pi4J/pi4j/Forum https://groups.google.com/forum/#!forum/pi4j

Blog savagehomeautomation.com

Twitter @pi4j | @savageautomate

Email robert@pi4j.com

top related