This project provides a Spring Boot Starter to use the Pi4J V2 library in your Spring Boot project.
- This starter requires Java 17 or higher.
- Multi-module project:
- pi4j-spring-boot: auto-configure module
- pi4j-spring-boot-starter: starter module
- pi4j-spring-boot-starter-sample-app: example application to demonstrate and test the starter
- To run the sample app:
- In IntelliJIDEA
- Go to com.pi4j.spring.boot.sample.app.Application
- Hit the run button
- After the application has launched, open a browser and check:
- Change the connected components:
- LED on: http://localhost:8080/api/pi4j/led/true
- LED off: http://localhost:8080/api/pi4j/led/false
- Get the button state: http://localhost:8080/api/pi4j/button
- Set text on the LCD: http://localhost:8080/api/pi4j/lcd/0/test
- Start from Raspberry Pi OS or Pi4J CrowPi OS 0.2.0.
- Check the Java version, should be 17 or newer.
$ java -version
openjdk version "17.0.4" 2022-07-19
OpenJDK Runtime Environment (build 17.0.4+8-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 17.0.4+8-Debian-1deb11u1, mixed mode, sharing)
- If Java is not installed yet, use SDKMAN.
$ sudo apt install zip
$ curl -s "https://get.sdkman.io" | bash
# Open new terminal
$ sdk install java 17.0.7-zulu
- Get this project from GitHub and package it.
$ git clone https://github.com/Pi4J/pi4j-springboot.git
$ cd pi4j-springboot
$ chmod +x mvnw
$ ./mvnw package
- Run it as sudo to make sure Pi4J starts correctly.
$ sudo java -jar pi4j-spring-boot-starter-sample-app/target/pi4j-spring-boot-starter-sample-app-0.0.1.ea.jar
# Or when installed with SDKMAN
$ sudo ~/.sdkman/candidates/java/17.0.7-zulu/bin/java -jar pi4j-spring-boot-starter-sample-app/target/pi4j-spring-boot-starter-sample-app-0.0.1.ea.jar
- Check the output of the application:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.5)
[pi4j-spring-boot-sample-app] [ main] c.p.spring.boot.sample.app.Application : Starting Application using Java 22.0.2 with PID 62220 (/Users/frank/Documents/GitHub/pi4j-springboot/pi4j-spring-boot-starter-sample-app/target/classes started by frank in /Users/frank/Documents/GitHub/pi4j-springboot)
[pi4j-spring-boot-sample-app] [ main] c.p.spring.boot.sample.app.Application : No active profile set, falling back to 1 default profile: "default"
[pi4j-spring-boot-sample-app] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
[pi4j-spring-boot-sample-app] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
[pi4j-spring-boot-sample-app] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.20]
[pi4j-spring-boot-sample-app] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
[pi4j-spring-boot-sample-app] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 375 ms
[pi4j-spring-boot-sample-app] [ main] com.pi4j.Pi4J : New auto context
[pi4j-spring-boot-sample-app] [ main] com.pi4j.Pi4J : New context builder
[pi4j-spring-boot-sample-app] [ main] c.pi4j.boardinfo.util.BoardInfoHelper : Detected OS: Name: Mac OS X, version: 15.0, architecture: aarch64
[pi4j-spring-boot-sample-app] [ main] c.pi4j.boardinfo.util.BoardInfoHelper : Detected Java: Version: 22.0.2, runtime: 22.0.2+9, vendor: Azul Systems, Inc., vendor version: Zulu22.32+15-CA
...
- Open browser with
http://{RASPBERRY_PI_IP_ADDRESS}:8080/actuator/info
- Output on a Raspberry Pi 4 (but truncated the pins-section):
{ "pi4j.status.led": "HIGH", "pi4j.status.button": "LOW", "os.name": "Linux", "os.architecture": "aarch64", "os.version": "6.6.31+rpt-rpi-2712", "board.name": "MODEL_5_B", "board.description": "Raspberry Pi 5 Model B", "board.model.label": "Model B", "board.cpu.label": "Cortex-A76", "board.soc": "BCM2712", "java.version": "17.0.11", "java.runtime": "17.0.11+9", "java.vendor": "Eclipse Adoptium", "java.vendor.version": "Temurin-17.0.11+9", "reading.volt.value": 0.72, "reading.temperature.celsius": 85.1, "reading.temperature.fahrenheit": 185.18, "reading.uptime": "18:26:04 up 33 min, 2 users, load average: 2.31, 1.54, 1.09", "platform.current": "RaspberryPi Platform", "platform.raspberrypi.name": "RaspberryPi Platform", "platform.raspberrypi.description": "Pi4J Platform for the RaspberryPi series of products.", "provider.pigpio-spi.name": "PiGpio SPI Provider", "provider.pigpio-spi.description": "com.pi4j.plugin.pigpio.provider.spi.PiGpioSpiProviderImpl", "provider.pigpio-spi.type.name": "SPI", "provider.gpiod-digital-output.name": "GpioD Digital Output (GPIO) Provider", "provider.gpiod-digital-output.description": "com.pi4j.plugin.gpiod.provider.gpio.digital.GpioDDigitalOutputProviderImpl", "provider.gpiod-digital-output.type.name": "DIGITAL_OUTPUT", "provider.pigpio-serial.name": "PiGpio Serial Provider", "provider.pigpio-serial.description": "com.pi4j.plugin.pigpio.provider.serial.PiGpioSerialProviderImpl", "provider.pigpio-serial.type.name": "SERIAL", "provider.linuxfs-pwm.name": "LinuxFS PWM Provider", "provider.linuxfs-pwm.description": "com.pi4j.plugin.linuxfs.provider.pwm.LinuxFsPwmProviderImpl", "provider.linuxfs-pwm.type.name": "PWM", "provider.gpiod-digital-input.name": "GpioD Digital Input (GPIO) Provider", "provider.gpiod-digital-input.description": "com.pi4j.plugin.gpiod.provider.gpio.digital.GpioDDigitalInputProviderImpl", "provider.gpiod-digital-input.type.name": "DIGITAL_INPUT", "provider.linuxfs-i2c.name": "LinuxFS I2C Provider", "provider.linuxfs-i2c.description": "com.pi4j.plugin.linuxfs.provider.i2c.LinuxFsI2CProviderImpl", "provider.linuxfs-i2c.type.name": "I2C", "registry.button.name": "Press button", "registry.button.type.name": "DIGITAL_INPUT", "registry.button.description": "DIN-24", "registry.dout-22.name": "DOUT-22", "registry.dout-22.type.name": "DIGITAL_OUTPUT", "registry.dout-22.description": "DOUT-22", "registry.i2c-1@39.name": "PCF8574AT backed LCD@39", "registry.i2c-1@39.type.name": "I2C", "registry.i2c-1@39.description": "I2C-1.39" }
- All info about the Pi4J project can be found on pi4j.com.
- TODO: describe this Spring Boot Starter on the Pi4J website.
This project was started by Daniel Frey and got further improved by DaShaun Carter during a few live-coding sessions: