Download presentation
Presentation is loading. Please wait.
Published byRamón Lara Cabrera Modified over 6 years ago
1
Java Programming on the Raspberry Pi with Pi4J
Rob Ratcliff
2
What is a Raspberry Pi? Single Board Computer – Pi 3 Model B+
Full-size HDMI ARMv8 64-bit with 1.4 GHz 1 GB RAM 4 USB ports 40 I/O Pins for GPIO, UART, Serial and I2C Ethernet, Wi-Fi and Bluetooth networking CSI camera port for connecting a Raspberry Pi camera Linux Operating System – Raspbian ARM Hardware Architecture Low Cost - $10 (Pi Zero) to $35 (Pi 3 Model B+) for Card Low Power
3
Finding Compatible Hardware
Zillions of devices available Search Amazon, Adafruit for Raspberry Pi devices 3.3V or 5V, I2C, GPIO, Serial “Hats” can be used to expand capability by stacking boards
4
History of Pi4J Open source library to interact with low-level I/O capabilities on the Raspberry Pi First commit on github on 9/8/2012 by Robert Savage (Savage Home Automation) 82 outstanding issues, but stable Runs on Java 8 Based on WiringPi C Library and
5
Setup of the Raspberry Pi
SDCard Setup Format Fat32 (exFat does not work) Up to 64 GB supported, but has to be initially partitioned into 32 GB parts. Noobs will extend the 32 GB partition automatically. Raspbian can expand as well. Noobs bootstrapper – Unzip and copy contents to SD card’s root directory Plug in monitor with HDMI cable and USB mouse and keyboard Power on with microUSB cable and configure Raspbian Reboot to Raspbian Update OS – sudo apt-get update; sudo apt-get upgrade; sudo reboot Add Hostname using sudo raspi-config Install Bonjour/Zeroconf - Allows the PI to be accessed by hostname.local from your PC without DNS or knowing the DHCP IP address sudo apt-get install libnss-mdns On Windows, install Bonjour Print Service from Apple: Setup account and SSH
6
Useful Tools socat – convert serial communications to Ethernet
i2c-tools - scan for and interact with I2C devices from the command line Win32 Disk Imager – dump an image of your SD card to a file for backup or copying tomcat8 – Java web server VNC
7
IDE Setup sudo update-alternatives –display java
8
NetBeans Automated Steps to Run Application on Pi
Compile code Build a JAR file SCP JAR file Run application using SSH
9
Pi4J Capabilities Export & unexport GPIO pins
Configure GPIO pin direction Configure GPIO pin edge detection – detect falling, rising edge for debounce Control/write GPIO pin states Pulse GPIO pin state - PWM Read GPIO pin states Listen for GPIO pin state changes (interrupt-based; not polling) Automatically set GPIO states on program termination (GPIO shutdown) Triggers for automation based on pin state changes Send & receive data via RS232 serial communication I²C Communication SPI Communication Extensible GPIO Provider interface to add GPIO capacity via expansion boards Access system information and network information from the Raspberry Pi Wrapper classes for direct access to WiringPi Library from Java
10
Protocols GPIO – General Purpose I/O
I²C – (I Squared C) – two wire communication using a signal and timer wire Serial USB TCP/IP UDP Bluetooth
11
GPIO Input or Output Pins High (3.3 VDC) or Low (0 VDC) States
Outputs used to control things like relays, lights, motors, bells, etc. Pullup and Pulldown resistors
12
GPIO Programming with Pi4J
Hardware Pins to Motorola Broadcom chipset numbering Physical pin locations can change between board revisions Pi4J can use Broadcom or WiringPi numbering schemes
13
Pi4J/WiringPi Board-Agnostic Numbering Scheme
14
GPIO Output Circuit GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalOutput output = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_12, PinState.LOW); output.high(); // set to high state output.low(); // set to low state output.toggle(); // switch to other state output.pulse(1000); // set state for a duration Credit: Robert Savage
15
GPIO Input Circuit GpioController gpio = GpioFactory.getInstance();
GpioPinDigitalInput input = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, PinPullResistance.PULL_DOWN); input.addListener( new GpioPinListenerDigital() { public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { System.out.println( "State = " + event.getState() + ", Edge = " + event.getEdge() + ", Pin = " + event.getPin() + ", Pressed = " + input.isHigh() ); } // Lambdas work as well input.addListener((GpioPinListenerDigital) (GpioPinDigitalStateChangeEvent event) -> {System.out.println(event) } );
16
GPIO Input Reference GPIO inputs require a reference voltage to prevent the pin from floating or picking up extraneous voltages A “Pull Up” or “Pull Down” resistor setting can be specified on the Raspberry Pi using Pi4J
17
I²C Originally developed by Phillips in 1982
Multi-Master Multi-Slave Communication model Two wires – clock driven by Master and Data from Slave or Master Speeds: 100 kbs (standard), 400 kbs (full speed), 1 mbs (fast) and 3.2mbs Device addressed by unique ID, 7 bit address
18
I²C Programming with Pi4J
I²C pins must be enabled using the raspi-config Multiple I²C devices can be connected to the RaspberryPi Use “sudo i2cdetect -y 1” to see registered I2C addresses for bus 1 Access data a byte at a time Most significant byte (MSB) is first read Vendor documentation can be confusing. Use open source code to gain clarification.
19
Angle Sensor Example ADC121C021
Potentiometer with SeedStudio Grove Socket
20
Remote Control of Devices
Any network protocol that Java 8 supports MQTT – Message Queuing Telemetry Transport Asynchronous messaging Pub/Sub architecture DIRMI – RMI replacement that supports bi-directional communication over a single socket and more QoS attributes in a Java-centric architecture – client/server model Web sockets, REST, etc. Can use socat to convert serial and other protocols to UDP or TCP/IP for easy access
21
Resources Raspberry Pi with Programming the Internet of Things by Stephen Chin and James Weaver - source code for pi4j - source code for book Extensive support for many devices for many languages – C library that is the foundation of pi4j A Few Hardware Vendors SeeedStudio Grove Adafruit – Provides Python code for most of their hardware Texas Instruments (sold by Mouser, DigiKey, etc.) Pi Starter and Learning Kit Vendors – Lots of others on Amazon Adeept - Adeept New Ultimate Starter Learning Kit for Raspberry Pi 3 2 Model B/B+ Python ADXL345 GPIO Cable DC Motor CanaKit - CanaKit Raspberry Pi 3 Complete Starter Kit - Includes 32 GB Samsung EVO+CanaKit Raspberry Pi 3 Complete Starter Kit - Includes 32 GB Samsung EVO+
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.