0

Is it possible to access Raspberry Pi from a Spring Boot Application (possibly running on docker container on that RPi), for example using Pi4J or do I need to create an itermediary app that would communicate with the spring boot app?

For context - I need to create a web application that would allow remote control of several DC Engines.

Fryzjerro
  • 15
  • 3
  • Define What you mean by access? Give an example/use-case. – Yogesh_D Aug 16 '22 at 14:18
  • you could create a local web service on the pi and only allow connections from localhost. Call the local web services to control the pins from the spring boot application via REST – codebrane Aug 16 '22 at 14:32
  • @Yogesh_D I have given the context - I need to control DC engines via REST API – Fryzjerro Aug 16 '22 at 19:08
  • @codebrane so it is possible to control the pins from within the spring boot app, right? – Fryzjerro Aug 16 '22 at 19:08
  • Please check this example which is a combination of heading + spring + pi4j https://foojay.io/today/blink-a-led-on-raspberry-pi-with-vaadin/ that shows you can indeed use spring with pi4j on the raspberry pi – Frank Aug 17 '22 at 06:37

2 Answers2

2

What I understand you want to do is:

Have a Web/Rest Application running on a Pi that allows you to control things like GPIO, PWN, I2C functionality on the Pi. This is possible using pi4j. You can find the details at https://pi4j.com/.

This library will allow you to control things like GPIO, PWM, Servos.

Do note that I am not sure of the intricacies of running this pi4j based application inside a docker container as it requires access to native system which will be abstracted out in the docker env.

I would suggest building a regular app running natively and then work on moving it into a docker image.

Note: By DC Engine I am assuming you mean DC Motors.

Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
1

Using Pi4J you could access the GPIO pins directly, example here. You could build Pi4J in a docker container and put the Spring boot app in the same container and allow the container to access the GPIO pins.

That would be the backend implementation. For the the client facing implementation you could create a REST API in the Spring boot app, e.g.:

GET https://your.rpi.com/engine/{name}/

which returns JSON status of engine e.g.

https://your.rpi.com/engine/airconditioning/

returns:

{ "status": "running" }

then:

PUT https://your.rpi.com/engine/airconditioning/start
PUT https://your.rpi.com/engine/airconditioning/stop

although the above are not strictly REST. They are RPC.

Or PUT to a control endpoint:

PUT https://your.rpi.com/engine/airconditioning/

with the content:

{
  "command": "start|stop"
}

PUT would be a better choice as POST is used in REST to create a new resource. You would be changing the status of an existing resource, i.e. an engine.

The code that implements the endpoints e.g.

https://your.rpi.com/engine/{name}/

would be where the Pi4J integration was implemented.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
codebrane
  • 4,290
  • 2
  • 18
  • 27