In this tutorial, we use Spring Boot, Spring MVC and Java to interface the GPIO pin array using Pi4J!

seen from United States
seen from United Kingdom
seen from Bangladesh

seen from Canada

seen from Canada
seen from Netherlands
seen from Türkiye
seen from United Arab Emirates

seen from Malaysia
seen from Russia
seen from United States

seen from Canada

seen from United States

seen from Malaysia

seen from Canada
seen from Canada
seen from Germany
seen from Türkiye

seen from United States
seen from United States
In this tutorial, we use Spring Boot, Spring MVC and Java to interface the GPIO pin array using Pi4J!
Pebble Controlled Raspberry PiBot
The Pebble 2 way Communication SDK for Andoid and iOS has just been released, so what better way to try it out than by adding Pebble controls to my Raspberry PiBot Controller.
Pressing up on the Pebble steers left, select is a toggle between forwards or stop and down steers right. The web service on the Pi returns a Json status message which is displayed on the Tablet Console. The tablet app also features voice commands via Android Google's android services.
This is also repeated onto the Pebble when a command is sent from either device.
The code for the Pebble app can be found at https://bitbucket.org/chughes42/pebble-pibot-app and is based on the examples included with the Pebble SDK.
Pi Bot Eclipse Project Repos
I am hosting the code for my Pi Bot at the below links. Android Pi Controller Java Servlet (PiBot control code) I recently gave a chat/demo at #IWDEV 9 using the below slides. #IWDEV 9 slides
First run for Raspberry Pi Bot
First test run for my Raspberry Pi powered bot inspired by cymplecy's blog (http://cymplecy.wordpress.com/). I found the magician chassis on a hobby site and was then directed to the blog by a friend. cymplecy used Scratch to program the Pi, I have once again gone for the Pi4J library to control the GPIO port from Java code, running inside a Jetty servlet container.
Apart from it's going backwards, not to bad for a first run.
Screenshot from the Android application running on my Nexus 7. The forward and turning speeds can be adjusted and a status is displayed in the lower centre of the screen when a new instruction is sent to the Pi.
Controlling the Berry Clip/Raspberry Pi from a Java Web Service/Servlet
I wanted to see if I could control the GPIO port on the Raspberry Pi from a Web Service, since the Berry Clip is pretty much a solid piece of kit I and i can not connect it up wrong I decided that getting this working was a good first step and I could interface my own circuits after I had proved my concept. I decide to use the Jetty Servlet engine. This is a light weight web server and would take less resources than using an instance of Apache Tomcat. This was downloaded from http://www.eclipse.org/jetty/ and installed onto the Pi .Once installed jetty can be started going to the installation directory and issuing the command:
sudo java -jar start.jar
Jetty on it's own does not require sudo access but to access the GPIO port on the pi requires root. This is not a good idea for any production environments or internet facing services.
I then created my servlet code and configuration in Eclipse on my desktop pc. The code was exported from eclipse as a war file and then copied into the webapps directory of jetty on the pi. If jetty is running it will scan this directory and automatically install the servlet. I found if updating the war file it was a good idea to restart jetty or strange errors seem to creep in. I added a basic html form to allow the number of the LED to be activated to be entered by the user and posted to the servlet.
The form allowing users to select an LED
This is accessible at :8080/BerryPi_Servlet/
The code is once again built using the Pi4J library from the Pi4J Project (http://pi4j.com/).
The Eclipse project including the exported WAR file can be found on bitbucket: https://bitbucket.org/chughes42/berryclipws/src The bulk of the java code is shown below BerryPiWS.java
package com.qubecad.pi.berrypiws; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; /** * Servlet class BerryPiWS to Control the Berry Clip for the raspberry Pi * available from http://www.raspberrypi-spy.co.uk/ */ public class BerryPiWS extends HttpServlet { private static final long serialVersionUID = 1L; GpioController gpio; GpioPinDigitalOutput pina; GpioPinDigitalOutput pinb; GpioPinDigitalOutput pinc; GpioPinDigitalOutput pind; GpioPinDigitalOutput pine; GpioPinDigitalOutput pinf; /** * Default constructor. */ public BerryPiWS() { GpioController gpio = GpioFactory.getInstance(); // Set up the pins and set low to start System.out.println("Setting up GPIO Pins for output"); pina = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_07, "Pin A", PinState.LOW); pinb = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00, "Pin B", PinState.LOW); pinc = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_03, "Pin C", PinState.LOW); pind = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_12, "Pin D", PinState.LOW); pine = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_13, "Pin E", PinState.LOW); pinf = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_14, "Pin F", PinState.LOW); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // get the number of the LED to turn on and print to console String action = request.getParameter("action"); System.out.println("action=" + request.getParameter("action")); try { // activate the request light, pause and reset. if (action.equals("1")) { System.out.println("A high"); pina.high(); Thread.sleep(1000); pina.low(); } else if (action.equals("2")) { System.out.println("B high"); pinb.high(); Thread.sleep(1000); pinb.low(); } else if (action.equals("3")) { System.out.println("C high"); pinc.high(); Thread.sleep(1000); pinc.low(); } else if (action.equals("4")) { System.out.println("D high"); pind.high(); Thread.sleep(1000); pind.low(); } else if (action.equals("5")) { System.out.println("E high"); pine.high(); Thread.sleep(1000); pine.low(); } else if (action.equals("6")) { System.out.println("F high"); pinf.high(); Thread.sleep(1000); pinf.low(); } } catch (InterruptedException e) { System.out.print("Exception "); } // display the LED select form on the return page PrintWriter out = response.getWriter(); String pagehtml = ""; // if there was a valid LED number display a message if ("123456".contains(action)) { pagehtml = pagehtml + " LED number " + action + " selected "; } String pagehtmlform = "
"; out.println(pagehtml + pagehtmlform); } } Links: The Pi4J Project http://pi4j.com/ Raspberry Pi Spy http://www.raspberrypi-spy.co.uk/berryclip-6-led-add-on-board/ Jetty Web Server http://www.eclipse.org/jetty/
Using an Ultrasonic Range Finder with the Raspberry Pi from Java.
A little while ago I bought an ultrasonic range finder from ebay to see if I could get it working with my Raspberry Pi. I got it working from Python and then demonstrated it to #IWDEV a local group of developers who meet to chat about developing on the Isle of Wight, as part of an open chat about the Raspberry Pi. It seemed the logical progression was to try and get it working from Java. The resulting code is shown below. I first tried to use an event listener to capture the results from the senser but failed to get it working (wondering it if it is a timing/sensitivity issue) so I have resulted to using a loop which checks the pin state. As per my earlier post about using a stepper motor I have used the Pi4J library to access the Pi's GPIO port. There is a guide to connecting the sensor to the Pi by +RaspberryPi Spy at http://www.raspberrypi-spy.co.uk/ if you are interested in giving it ago. RangeFinder.Java:
package com.qubecad.pi.PiBot; import com.pi4j.io.gpio.GpioPinDigitalInput; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent; import com.pi4j.io.gpio.event.GpioPinListenerDigital; public class RangeFinder { double result = 0; GpioPinDigitalOutput firepulse; GpioPinDigitalInput result_pin; RangeFinder(GpioPinDigitalOutput trigger, GpioPinDigitalInput result_pin) { this.firepulse = trigger; this.result_pin = result_pin; } /** * * Trigger the Range Finder and return the result * * @return */ public double getRange() { System.out.println("Range Finder Triggered"); try { // fire the trigger pulse firepulse.high(); Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); System.out.println("Exception triggering range finder"); } firepulse.low(); // wait for the result double startTime = System.currentTimeMillis(); double stopTime = 0; do { stopTime = System.currentTimeMillis(); if ((System.currentTimeMillis() - startTime) >= 40) { break; } } while (result_pin.getState() != PinState.HIGH); // calculate the range. If the loop stopped after 38 ms set the result // to -1 to show it timed out. if ((stopTime - startTime) <= 38) { result = (stopTime - startTime) * 165.7; } else { System.out.println("Timed out"); result = -1; } return result; } }
PiBot.java
package com.qubecad.pi.PiBot; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalInput; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinPullResistance; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; public class PiBot { /** * @param args */ public static void main(String[] args) { // Setup GPIO Pins GpioController gpio = GpioFactory.getInstance(); //range finder pins GpioPinDigitalOutput rangefindertrigger = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_00, "Range Finder Trigger", PinState.LOW); GpioPinDigitalInput rangefinderresult = gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, "Range Pulse Result", PinPullResistance.PULL_DOWN); // Create the range finder RangeFinder rangefinder = new RangeFinder(rangefindertrigger,rangefinderresult); do { // Get the range double distance=rangefinder.getRange(); System.out.println("RangeFinder result ="+distance +"mm"); } while (false!=true); } }
Links: The Pi4J Project (http://pi4j.com/) #IWDEV on facebook (https://www.facebook.com/groups/iwdev/) RaspberryPi-spy.co.uk (http://www.raspberrypi-spy.co.uk)
Raspberry Pi Stepper Motor Control using Java.
I came across the Pi4J Project today. The project is working on a library to allow Pi programming from Java. It is not at version 1 yet so still quiet new but the library lets you access the GPIO port on your Pi and join in some of the fun I thought you needed to use python for. My Pi is currently connected to a stepper motor so I thought I would have ago at getting it to run from java,the below code is the result. Once I had the pi setup and proved the java setup was working correctly I used Eclipse on my Windows PC to write my code/compile and WinSCP to transfer it to the Pi. The Pi4J project have a number of examples on their site and their control example (http://pi4j.com/example/control.html) was the starting point for my code. I am using a 28BYJ-48 stepper motor and ULN 2003 driver board which I picked up from ebay awhile ago. Update: The latest version of Pi4J now includes a dedicated stepper motor class and example code. The eclipse project can be download from: http://github.com/qubecad/hmm-pi.git
package com.qubecad.pi.stepper; import java.util.HashMap; import com.pi4j.io.gpio.GpioController; import com.pi4j.io.gpio.GpioFactory; import com.pi4j.io.gpio.GpioPinDigitalOutput; import com.pi4j.io.gpio.PinState; import com.pi4j.io.gpio.RaspiPin; public class PiStepper { /** * * Driving a Stepper Motor From Java * * @param args * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // create gpio controller GpioController gpio = GpioFactory.getInstance(); // Set up the pins and set low to start System.out.print("Setting up GPIO Pins for ouput"); GpioPinDigitalOutput pina = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_05, "Pin A", PinState.LOW); GpioPinDigitalOutput pinb = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_06, "Pin B", PinState.LOW); GpioPinDigitalOutput pinc = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_10, "Pin C", PinState.LOW); GpioPinDigitalOutput pind = gpio.provisionDigitalOuputPin(RaspiPin.GPIO_11, "Pin D", PinState.LOW); HashMap driveLogic=new HashMap(); driveLogic.put(0, "1001"); driveLogic.put(1, "0001"); driveLogic.put(2, "0011"); driveLogic.put(3, "0010"); driveLogic.put(4, "0110"); driveLogic.put(5, "0100"); driveLogic.put(6, "1100"); driveLogic.put(7, "1000"); System.out.print("Driving motor"); while (true) { for (int i = 1; i < driveLogic.size(); i++) { String grayCode = (String) driveLogic.get(i); setPin(pina,grayCode.charAt(0)); setPin(pinb,grayCode.charAt(1)); setPin(pinc,grayCode.charAt(2)); setPin(pind,grayCode.charAt(3)); Thread.sleep(10); } } } /** * * Sets the passed pin Low or High depending on the passed value. * * @param pin * @param value */ private static void setPin(GpioPinDigitalOutput pin,char value){ if (value == '0') { pin.low(); } else { pin.high(); } } }