Objective: The Objective of this exercise is to integrate Spring Boot and Pi4J to create a simple Web application to make the LED toggle (on or off) based on the REST URL hit on the browser.
Task details:
The task that we are going to achieve is to make the LED toggle (on or off) based on the REST URL hit/refresh on the browser.
Every time once the URL http://<raspberryIp>:9999/light is hit (or refreshed) then the LED on the Raspberry Pi will toggle (on or off).
YouTube Video:
The YouTube Video for this tutorial is available at this link and also can be found at the end of this tutorial.
GitHub Source:
https://github.com/agilerules/IOT/tree/master/iotled
YouTube Video:
The YouTube Video for this tutorial is available at this link and also can be found at the end of this tutorial.
GitHub Source:
https://github.com/agilerules/IOT/tree/master/iotled
Dependencies:
Software:
Spring Boot with Tomcat Embedded
Pi4j
Java
Maven
Hardware:
Raspberry Pi (installed with Java, Maven)
3 Jumper wires
LED
220 Ohm Resistor
Breadboard
GPIO Ribbon Cable
Breadboard
GPIO Ribbon Cable
Steps:
1. Complete the following Wiring process of Raspberry Pi, LED, Resistor and with few jumper wires as shown in the below diagram.
Wiring Diagram:
Bread Board Wiring |
Bread Board Wiring |
- A wire from Pin 6 to Ground ( - on Breadboard)
- A wire from Pint 12 (GPIO_1) to + line on Breadboard
- Fix the LED and the long end of LED Anode (+) to be connected to Resistor (Yellow side on top of Resistor)
- Other end of Resistor (i.e the red colour on top) to the + line of Breadboard
- LED Cathode (-) to – line of Breadboard
2. Now connect to Raspberry Pi using Putty with your Raspberry Pi Credentials and by-default, you will in the path /home/pi.
3. For proper organization purpose, I created folders (using mkdir command) \projects\spring_pi4j. But this is not mandatory. You can even skip this step and move to next step.
4. From the \spring_pi4j folder, execute the following command to Git clone the code to your local raspberry Pi.
git clone https://github.com/agilerules/IOT.git
5. Once the git clone is complete, you will see IOT folder created in \spring_pi4j folder. Now go to IOT folder (using the command cd IOT) and you will see the folder iotled inside this folder IOT.
6. Move to this iotled folder (using the command cd iotled)
7. Now execute the following command to start the maven build. (Incase if mvn command is not recognized then it means that maven is not installed on your Raspberry Pi. Please follow my tutorial on how to install Maven on Raspberry Pi.)
10. Now come back to previous folder (i.e to iotled) and run the following command and you should see the Spring in your screen as shown in the below screen shot, which indicates that Spring Boot is working fine.
11. You should finally see the message saying “Started Application...”. Also you can see that the REST end points / and /light are loaded and Tomcat Started on port 9999.
12. Now go to browser and hit the below URL where replace <raspberryIp> with your Raspberry pi IP address.
http://<raspberryIp>:9999/
13. Similarly once you hit the below URL, the LED connected to Raspberry Pi should toggle (On or Off).
a) Pom.xml:
The below are two library dependencies that we need for this application: spring-boot-starter-web - This is for Spring boot to enable the Web application with embedded Tomcat. This would need the following parent Spring Boot starter package to be defined in< parent> node. <artifactId>spring-boot-starter-parent</artifactId>
b) Application.java:
This is simple Spring Boot code with @SpringBootApplication annotation and SpringApplication.run() method with the arguments (class, args) inside main() method.
@SpringBootApplication:
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.
@SpringBootApplication is a convenience annotation that adds all of the following:
@Configuration tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviours such as setting up a DispatcherServlet.
@ComponentScan tells Spring to look for other components, configurations, and services in the the hello package, allowing it to find the HelloController.
Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
The run() method returns an ApplicationContext and this application then retrieves all the beans that were created either by your app or were automatically added thanks to Spring Boot. It sorts them and prints them out.
c) LEDController.java:
@RestController:
Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.
@RequestMapping maps / to the index() method and returns “Hello World!!”
@RequestMapping maps /light to the light() method which uses the Pi4j API’s to toggle LED on or off and returns the response “Response from light!!”
public static GpioPinDigitalOutput pin;
GpioController gpio = GpioFactory.getInstance();
The above line will create GPIOController instance using GPIOFactory’s getInstnace() method.
pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01,"My LED",PinState.LOW);
The above line will provision the pin GPIO_01 as the Ouput pin and default the Pin state to LOW (which means, it will be in off state by-defualt) and “My LED” is the user defined name for the LED.
pin.toggle();
The above line will make the LED toggle – on and off.
application.properties: (in src\main\resources)
server.port = 9999
The above entry indicate the embedded Tomcat in Spring Boot to use the port 9999 (rather than the default 8080 port).
That’s it. Hope this tutorial is useful for you.
That’s it. Hope this tutorial is useful for you.
No comments:
Post a Comment