Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
I/O Programming with Java on the
Raspberry Pi with Pi4J
JavaOne 2013 - CON7968
Robert Savage, Software Architect, AMX LLC....
Additional Resources
Blog Articles on Project:
http://www.savagehomeautomation.com/j1pi4j
Source Code For Demos
https://gi...
Disclaimer!
UAYOR! / YMMV!
(use at your own risk / your mileage may vary)
The projects and sample code provided in this de...
Agenda / Topics
 Raspberry Pi Overview
 Pi4J Overview
 Basic “Getting Started” Examples
 Simple GPIO Example/Demo
 GP...
Raspberry Pi
 Single Board Computer
 Linux Operating system
 ARM Architecture
 Low Cost
 Low Power
Java on the Raspberry Pi
 OpenJDK
 Installation: http://www.savagehomeautomation.com/raspi-openjdk
 Oracle Hotspot JVM ...
What is Pi4J?
 Open Source Project
 Low Level I/O
 Object-oriented API
 Event Based
 Raspberry Pi Platform (Linux/ARM...
Pi4J : Supported I/O
 GPIO General Purpose I/O
 UART Serial / RS232
 SPI Serial Peripheral Interface
 I2C Inter-Integr...
Pi4J : GPIO Demo
 Basic program using a simple
momentary button (input)
and LED (output).
 GOAL:
Illuminate LED when the...
GPIODemo:WiringDiagram
GPIODemo:ProgramLogic
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary push-button switch; input pi...
GPIODemo:InAction
Pi4J : GPIO Demo
Demo Time!
I’m not here for pretty pictures ..
Lets see some *working* code!
Pi4J : GPIO Trigger Demo
 Pi4J provides simple
automation triggers for common
GPIO event interactions.
( == less code)
 ...
GPIOTriggerDemo:WiringDiagram
GPIOTriggerDemo:ProgramLogic
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary push-button switch; activate...
GPIOTriggerDemo:InAction
Pi4J : GPIO Trigger Demo
Demo Time!
This isn’t rocket science!
Pi4J : RS232 Demo
 Basic program using RS232
serial communication.
 GOAL:
Display “Hello World” on
LED message reader.
RS232Demo:WiringDiagram
RS232Demo:ProgramLogic
RS232Demo:SampleCode
// create an instance of the serial communications class
final Serial serial = SerialFactory.createIn...
RS232Demo:InAction
Pi4J : RS232 Demo
Demo Time!
Pi4J : Component API
 The component APIs provides an abstraction layer from the
hardware I/O layer.
 This allows hardwar...
Pi4J : Component API
 Keypad
 Light / LED
 Dimmable Light
 LCD
 Power Controller
 Relay
 Momentary Switch
 Toggle ...
ComponentDemo:WiringDiagram
ComponentDemo:ProgramLogic
ComponentDemo:SampleCode
// create GPIO controller
final GpioController gpio = GpioFactory.getInstance();
// momentary pus...
ComponentDemo:InAction
Pi4J : Component Demo
Demo Time!
Pi4J : I/O Expansion
 GPIO General Purpose I/O
 PWM Pulse-Width Modulation
 ADC Analog-to-Digital Converter
 DAC Digit...
Pi4J : Putting It All Together
Time to jump in to the
deep end of the pool.
Pi4J : Access Control Demo
 RFID Access
 Keypad Access
 Override (“Exit”) Switch
 Doorbell
 Door Sensors
 Keypad Tam...
AccessControlDemo:WiringDiagram
AccessControlDemo:InAction
Pi4J : Access Control Demo
Code & Demo Time!
Full source available at: https://github.com/savag...
Pi4J : Roadmap
 Version 1.0 Release – Q1 2014
 Additional Component Interfaces + Impls
 Platform Abstraction
 Testing ...
Questions?
Contact / Links / Shameless Plugs
 Website pi4j.com
 Source https://github.com/Pi4J/pi4j/
 Forum https://groups.google....
Upcoming SlideShare
Loading in …5
×

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

6,911 views

Published on

Published in: Technology
  • Be the first to comment

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

  1. 1. I/O Programming with Java on the Raspberry Pi with Pi4J JavaOne 2013 - CON7968 Robert Savage, Software Architect, AMX LLC. Let’s Get Physical:
  2. 2. Additional Resources Blog Articles on Project: http://www.savagehomeautomation.com/j1pi4j Source Code For Demos https://github.com/savagehomeautomation/pi4j-javaone-demos
  3. 3. 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.
  4. 4. 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
  5. 5. Raspberry Pi  Single Board Computer  Linux Operating system  ARM Architecture  Low Cost  Low Power
  6. 6. Java on the Raspberry Pi  OpenJDK  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 ABI  Hard-float ABI
  7. 7. What is Pi4J?  Open Source Project  Low Level I/O  Object-oriented API  Event Based  Raspberry Pi Platform (Linux/ARM)  Java / C (JNI + Native)
  8. 8. Pi4J : Supported I/O  GPIO General Purpose I/O  UART Serial / RS232  SPI Serial Peripheral Interface  I2C Inter-Integrated Circuit  PWM Pulse-Width-Modulation
  9. 9. Pi4J : GPIO Demo  Basic program using a simple momentary button (input) and LED (output).  GOAL: Illuminate LED when the button is pressed. Deactivate LED when button is released.
  10. 10. GPIODemo:WiringDiagram
  11. 11. GPIODemo:ProgramLogic
  12. 12. // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; input pin final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP); // led; output pin final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW); // create event listener for button input pin buttonPin.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); }} }); GPIODemo:SampleCode Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-1-gpio
  13. 13. GPIODemo:InAction Pi4J : GPIO Demo Demo Time! I’m not here for pretty pictures .. Lets see some *working* code!
  14. 14. Pi4J : GPIO Trigger Demo  Pi4J provides simple automation triggers for common GPIO event interactions. ( == less code)  GOAL: Toggle LED state when the button is pressed.
  15. 15. GPIOTriggerDemo:WiringDiagram
  16. 16. GPIOTriggerDemo:ProgramLogic
  17. 17. // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; activates when button is pushed final GpioPinDigitalInput buttonPin = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_UP); // led; illuminates when GPIO is HI final GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW); // create button event listener buttonPin.addTrigger(new GpioToggleStateTrigger(PinState.LOW, ledPin)); GPIOTriggerDemo:SampleCode Full source available at: https://github.com/savagehomeautomation/pi4j-javaone-demos/tree/master/pi4j-demo-2-gpio-trigger
  18. 18. GPIOTriggerDemo:InAction Pi4J : GPIO Trigger Demo Demo Time! This isn’t rocket science!
  19. 19. Pi4J : RS232 Demo  Basic program using RS232 serial communication.  GOAL: Display “Hello World” on LED message reader.
  20. 20. RS232Demo:WiringDiagram
  21. 21. RS232Demo:ProgramLogic
  22. 22. RS232Demo:SampleCode // create an instance of the serial communications class final 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 listener serial.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 sign serial.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
  23. 23. RS232Demo:InAction Pi4J : RS232 Demo Demo Time!
  24. 24. 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.
  25. 25. Pi4J : Component API  Keypad  Light / LED  Dimmable Light  LCD  Power Controller  Relay  Momentary Switch  Toggle Switch  Analog Sensor  Distance Sensor  Motion Sensor  Temperature Sensor
  26. 26. ComponentDemo:WiringDiagram
  27. 27. ComponentDemo:ProgramLogic
  28. 28. ComponentDemo:SampleCode // create GPIO controller final GpioController gpio = GpioFactory.getInstance(); // momentary push-button switch; activates when button is pushed final 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 pushed final LED led = new GpioLEDComponent( gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, PinState.LOW)); // create momentary switch event listener momentarySwitch.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
  29. 29. ComponentDemo:InAction Pi4J : Component Demo Demo Time!
  30. 30. Pi4J : I/O Expansion  GPIO General Purpose I/O  PWM Pulse-Width Modulation  ADC Analog-to-Digital Converter  DAC Digital-to-Analog Converter
  31. 31. Pi4J : Putting It All Together Time to jump in to the deep end of the pool.
  32. 32. Pi4J : Access Control Demo  RFID Access  Keypad Access  Override (“Exit”) Switch  Doorbell  Door Sensors  Keypad Tamper Switch  Electromechanical Lock (Solenoid)  Security Alarm  Notifications  Access Log  Remote Access
  33. 33. AccessControlDemo:WiringDiagram
  34. 34. AccessControlDemo:InAction 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
  35. 35. Pi4J : Roadmap  Version 1.0 Release – Q1 2014  Additional Component Interfaces + Impls  Platform Abstraction  Testing Harness  Library Refactoring  1-Wire / Infrared / Bluetooth / USB HID / RTC  Web Service/Remote API
  36. 36. Questions?
  37. 37. Contact / Links / Shameless Plugs  Website 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

×