raspberry pi and pi4j

Post on 12-Apr-2017

176 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Pi4J and Pi

Narendran Solai Sridharan

What is Raspberry Pi?

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

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

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

“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

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

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.

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

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.

GPIO

Dem

o : W

iring

Dia

gram

Gnd

GPIO1

GPIO26

Gnd

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

GPIO

Dem

o : I

n Ac

tion

Pi4J : GPIO Demo

Demo Time!!!

Pi4J : GPIO Trigger Demo

• Pi4J 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

Gnd

GPIO18

GPIO12

Gnd

No Change

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

GPIO

Trig

ger D

emo

: In

Actio

n

Pi4J : GPIO Trigger 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 API

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

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

Com

pone

nt D

emo

: Wiri

ng D

iagr

am

Gnd

GPIO1

GPIO26

Gnd

No Change

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

Com

pone

nt D

emo

: In

Actio

n

Pi4J : Component Demo

Demo Time!

More Components

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

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

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

top related