spring-bootkotlinraspberry-pi3servopi4j

Controlling a servo at Raspberry Pi with Springboot and ServoBlaster


I am trying to control a servo motor from a web interface. I am using SpringBoot 2.0, ServoBlaster and pi4j.

In order to start the application I am running as root ./gradlew bootrun --no-daemon. It has to be root in order to handle the GPIOs and I don't have any security worries about the device.

In simplified (a class with just the main function) Java/Kotlin I achieved to control the servo by any of the following ways:

  1. RPIServoBlasterProvider

    val servoProvider = RPIServoBlasterProvider() val servo0 = servoProvider.getServoDriver(servoProvider.definedServoPins[5]) println("Go to 150") //middle servo0.servoPulseWidth = 150 println("Went to ${servo0.servoPulseWidth}") Thread.sleep(1550)

  2. Write to /dev/servoblaster

    val out = PrintWriter(FileOutputStream("/dev/servoblaster"), true) println("Go to 65 again") out.println("5=65") out.flush() out.close

  3. Call a secondary script which writes to /dev/servoblaster

    val servoId = 5 val script = "/home/pi/ServoHardwareSteering.sh" val cmdMinPosition = "$script $servoId 65" val cmdMidPosition = "$script $servoId 150" val cmdMaxPosition = "$script $servoId 235" val runtime = Runtime.getRuntime() println(cmdMidPosition) runtime.exec(cmdMidPosition)//.waitFor() Thread.sleep(1550)

  4. Write the value to a file and have a secondary execute reading this file and applying this value to the servo

I have tried all of the above in Springboot but without success.

So the question is, could somebody tell me how could I:

  1. use the RPIServoBlasterProvider class from Springboot? OR
  2. write to /dev/servoblaster? OR
  3. execute any terminal script? OR
  4. where to save the script in order to be able to call it OR
  5. write to a simple file (ex. afile.txt)? OR
  6. solve the issue in a better way that I did not think about already.

Solutions at any of the above questions could help me solve my problem.

PS: Is there anything wrong with the blockquote for the source code in stackoverflow? I could not format it as a block and I used the line code formatting!


Solution

  • The whole problem was with the servod of the ServoBlaster. It was accidentally killed and I had to run it once again! I followed the No 5 solution.