Home > Software engineering >  Access Raspberry Pi modules from Spring Boot application
Access Raspberry Pi modules from Spring Boot application

Time:08-18

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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related