Skip to content

Pi4J/pi4j-springboot

Repository files navigation

Pi4J Spring Boot Starter

GitHub Actions build state License

Chat on Slack Site Twitter Follow


This project provides a Spring Boot Starter to use the Pi4J V2 library in your Spring Boot project.

About this 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:

Running the sample application on a Raspberry Pi

  • 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
$ mvn 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/19.0.2-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.1.0)

2023-05-26T15:11:12.614+02:00  INFO 2148 --- [           main] c.p.spring.boot.sample.app.Application   : Starting Application v0.0.1.ea using Java 17.0.6 with PID 2148 (/home/pi/pi4j-springboot/pi4j-spring-boot-starter-sample-app/target/pi4j-spring-boot-starter-sample-app-0.0.1.ea.jar started by root in /home/pi/pi4j-springboot)
2023-05-26T15:11:12.630+02:00  INFO 2148 --- [           main] c.p.spring.boot.sample.app.Application   : No active profile set, falling back to 1 default profile: "default"
2023-05-26T15:11:20.607+02:00  INFO 2148 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-26T15:11:20.665+02:00  INFO 2148 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-26T15:11:20.668+02:00  INFO 2148 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-05-26T15:11:21.078+02:00  INFO 2148 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-26T15:11:21.090+02:00  INFO 2148 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 7946 ms
2023-05-26T15:11:23.861+02:00  INFO 2148 --- [           main] com.pi4j.Pi4J                            : New auto context
2023-05-26T15:11:23.862+02:00  INFO 2148 --- [           main] com.pi4j.Pi4J                            : New context builder
2023-05-26T15:11:24.486+02:00  INFO 2148 --- [           main] c.p.p.impl.DefaultRuntimePlatforms       : adding platform to managed platform map [id=raspberrypi; name=RaspberryPi Platform; priority=5; class=com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform]
2023-05-26T15:11:26.134+02:00  INFO 2148 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 14 endpoint(s) beneath base path '/actuator'
2023-05-26T15:11:26.723+02:00  INFO 2148 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-26T15:11:26.940+02:00  INFO 2148 --- [           main] c.p.spring.boot.sample.app.Application   : Started Application in 16.876 seconds (process running for 19.337)
2023-05-26T15:11:39.543+02:00  INFO 2148 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-26T15:11:39.544+02:00  INFO 2148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-26T15:11:39.550+02:00  INFO 2148 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
  • Open browser with http://{RASPBERRY_PI_IP_ADDRESS}:8080/actuator/info
    • Output on a PC: pins-section):
      {
          "os.name":"Mac OS X",
          "os.architecture":"aarch64",
          "os.version":"14.5",
          "board.name":"UNKNOWN",
          "board.description":"Unknown",
          "board.model.label":"Unknown",
          "board.cpu.label":"Unknown",
          "board.soc":"UNKNOWN",
          "java.version":"22",
          "java.runtime":"22+36",
          "java.vendor":"Azul Systems, Inc.",
          "java.vendor.version":"Zulu22.28+91-CA",
          "reading.volt.value":0.0,
          "reading.temperature.celsius":0.0,
          "reading.temperature.fahrenheit":32.0,
          "reading.uptime":"16:26  up 13 days,  3:35, 1 user, load averages: 2.63 3.45 3.32",
          "pi4jRegistry":{}
      }
    • Output on a Raspberry Pi 4 (but truncated the pins-section):
      TODO

Notes

  • All info about the Pi4J project can be found on pi4j.com.
  • TODO: describe this Spring Boot Starter on the Pi4J website.

Thanks to

This project was started by Daniel Frey and got further improved by DaShaun Carter during a few live-coding sessions:

  1. TODO link to first Twitch
  2. Working on Pi4J Spring Boot
  3. Spring Boot Pi4J Starter :: Testing my PR