Welcome everyone!
Today I will be walking through how to host a Spring Boot server on a Raspberry Pi to control an LED using an web UI.
You can find a link to my first LED test project here: https://github.com/ArcSoftware/PiLedTest
First we will fork this repository. Then we will clone the new repo by the following git command:
/git clone [your new repo URL.git]
$ sudo yum install git-all
Install HomeBrew, then Git:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew install git
Install here: http://msysgit.github.io
Open new project with all default settings, adding only "Auto-Import".
Create a Java Class named "PiCourseController".
Copy the following code into your controller class:
@Controller
public class PiCourseController {
private RaspberryPiManager piManager;
public PiCourseController() {
piManager = new RaspberryPiManager();
@RequestMapping(path = "/", method = RequestMethod.GET)
public String home(Model model, String led) {
if (led != null) { piManager.toggleLED(led);}
return "index";
}
}
Now create a Java Class named "PiManager".
Copy the following code into your new manager class:
public class PiManager {
private final GpioController gpio;
private GpioPinDigitalOutput testLED;
public PiManager() {
gpio = GpioFactory.getInstance();
testLED = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "testLED", PinState.LOW);
}
public void toggleLED(String led) {
if (led.equalsIgnoreCase("testled")) {
testLED.toggle();
}
}
Open the HTML file named "index.html" that I have pre created for you and paste the following code into the body:
Toggle Light: <a href="/?led=testled"><button>LED</button></a>
Perform the following to get your code updated on your GitHub account:
$ git add .
$ git commit -m "adding changes"
$ git push
Now SSH into your Pi using terminal: (PuTTY for Windows)
$ ssh [email protected].[subnet].[ip]
password: raspberry
Perform the following once connected:
$ sudo apt-get install git
$ curl -s get.pi4j.com | sudo bash
Change to a good directory to clone your repo as you did before:
$ git clone [repo.git]
Change to the jar directory:
cd PiCourse\builds\libs
ls
Start your spring server!
sudo java -jar [jar file name]
192.168.[subnet].[ip]:8080