raspberry pi and pi4j

28
Pi4J and Pi Narendran Solai Sridharan

Upload: narendran-solai-sridharan

Post on 12-Apr-2017

176 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Raspberry pi and pi4j

Pi4J and Pi

Narendran Solai Sridharan

Page 2: Raspberry pi and pi4j

What is Raspberry Pi?

• Single Board Computer• Linux Operating system• ARM Architecture• Low Cost• Low Power

Page 3: Raspberry pi and pi4j

What is Pi4J?• Open Source Project• Raspberry Pi Platform (Linux/ARM)• Abstraction over Low Level I/O• Supports I/O Programming

• Object-oriented API• Event Based• Java / C (JNI + Native)

• Provides Listeners , Triggers & Component API

Page 4: Raspberry pi and pi4j

Acknowledgment to Pi4J Creator

Robert SavageSoftware Architect

savage.home.automation

Examples for this session has been taken from demos created by Robert Savage for JavaOne 2013 Conference.

https://github.com/savagehomeautomation/pi4j-javaone-demos

Page 5: Raspberry pi and pi4j

“We perform I/O Programming on Raspberry Pi to

realize IoT”

Input / Output“We are working on hardware I/O”

We withdraw Physical Output

Or Provide Physical

Input

Page 6: Raspberry pi and pi4j

GPIO – Main Programmable I/O Interface

It acts as General Purpose I/O – GPIO

Apart from that it also supports via Configuration

UART - Serial / RS232SPI - Serial Peripheral InterfaceI2C - Inter-Integrated CircuitPWM - Pulse-Width-Modulation

Pi4J provides Java Classes supporting all of these I/O Operations

Page 7: Raspberry pi and pi4j

Pull up and Pull DownIn these circuits

The pull-down resistor pulls the voltage down to zero. If the pull-up switch is pressed, it pulls the voltage up to whatever the + supply is.

The pull-up resistor pulls the voltage up to whatever the + supply is. If the pull-down switch is pressed, it pulls the voltage down to zero.

Page 8: Raspberry pi and pi4j

3 Main Designs Classes

• Listeners• Triggers• Component

Other Components

• Pull up and Pull down Resistors• Toggle output and handy methods to access

pins and change their states

Page 9: Raspberry pi and pi4j

Pi4J : GPIO Demo

• Basic program using a simplemomentary button (input) and LED (output).

• GOAL:

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

Page 10: Raspberry pi and pi4j

GPIO

Dem

o : W

iring

Dia

gram

Gnd

GPIO1

GPIO26

Gnd

Page 11: Raspberry pi and pi4j

GPIO

Dem

o : P

rogr

am L

ogic

Page 12: Raspberry pi and pi4j

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

// led; output pinfinal GpioPinDigitalOutput ledPin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_26, 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

Page 13: Raspberry pi and pi4j

GPIO

Dem

o : I

n Ac

tion

Pi4J : GPIO Demo

Demo Time!!!

Page 14: Raspberry pi and pi4j

Pi4J : GPIO Trigger Demo

• Pi4J provides simple automation triggers for commonGPIO event interactions.

( == less code)

• GOAL:

Toggle LED state when the buttonis pressed.

Page 15: Raspberry pi and pi4j

GPIO

Trig

ger D

emo

: Wiri

ng D

iagr

am

Gnd

GPIO18

GPIO12

Gnd

No Change

Page 16: Raspberry pi and pi4j

GPIO

Trig

ger D

emo

: Pro

gram

Log

ic

Page 17: Raspberry pi and pi4j

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

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

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

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

GPIO

Trig

ger D

emo

: Sam

ple

Code

Page 18: Raspberry pi and pi4j

GPIO

Trig

ger D

emo

: In

Actio

n

Pi4J : GPIO Trigger Demo

Demo Time!!!

Page 19: Raspberry pi and pi4j

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 20: Raspberry pi and pi4j

Pi4J : Component API

• Keypad• Light / LED• Dimmable Light• LCD• Power Controller• Relay• Momentary Switch• Toggle Switch

• Analog Sensor• Distance Sensor• Motion Sensor• Temperature Sensor

Page 21: Raspberry pi and pi4j

Com

pone

nt D

emo

: Wiri

ng D

iagr

am

Gnd

GPIO1

GPIO26

Gnd

No Change

Page 22: Raspberry pi and pi4j

Com

pone

nt D

emo

: Pro

gram

Log

ic

Page 23: Raspberry pi and pi4j

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 } }});

Page 24: Raspberry pi and pi4j

Com

pone

nt D

emo

: In

Actio

n

Pi4J : Component Demo

Demo Time!

Page 25: Raspberry pi and pi4j

More Components

Page 26: Raspberry pi and pi4j

Pi4J : I/O Expansion

• GPIO General Purpose I/O• PWM Pulse-Width Modulation• ADC Analog-to-Digital Converter• DAC Digital-to-Analog Converter

https://github.com/Pi4J/pi4j/tree/master/pi4j-gpio-extension

Page 27: Raspberry pi and pi4j

Pi4J : Components & Devices Abstraction

Components

• LCD• Buzzer• Light• Motor• Potentiometer• Motion Sensor

& Other Sensors

• Power & Relay

https://github.com/Pi4J/pi4j/tree/master/pi4j-device/

Devices

• Fireplace• Garage• Gate• Pibrella• Sprinklers• Access

Page 28: Raspberry pi and pi4j

References

• Pi4J - http://pi4j.com/• Fritgzing - http://fritzing.org/home/• Wiring Pi - http://wiringpi.com/• More Examples - https://

github.com/Pi4J/pi4j/tree/master/pi4j-example

• Watch Interview of Robert Savage - https://www.youtube.com/watch?v=Z_eI7DfDMjI