I am working on Raspberry pi and java to make the LED to blink by using pi4j , everything is cleared and worked fine , the LED is blinking as per the code but when i run the second time it causes the following error, i have searche lot there is lot of same question without the clear answer how to solve it ,any help would be appreciated
final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(
RaspiPin.GPIO_01, "PinLED", PinState.HIGH);
System.out.println("light is: ON");
Thread.sleep(2000);
pin.low();
System.out.println("light is: OFF");
Thread.sleep(1000);
System.out.println("light is: ON for 1 second");
pin.pulse(1000, true);
pin.setShutdownOptions(true, PinState.LOW, PinPullResistance.OFF);
gpio.shutdown();
This is the Complete error i am getting
com.pi4j.io.gpio.exception.GpioPinExistsException: This GPIO pin already exists: GPIO 1
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionPin(GpioControllerImpl.java:507)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:645)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:672)
com.pi4j.io.gpio.impl.GpioControllerImpl.provisionDigitalOutputPin(GpioControllerImpl.java:684)
com.restFulService.Controller.LedControl.ledTestFun(LedControl.java:52)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:483)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:202)
net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:180)
org.apache.catalina.filters.CorsFilter.handleNonCORS(CorsFilter.java:439)
org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:178)
Easy way to add lots of GPIO is by using the I2C bus with a MCP23017 chip. You can add up to eight of those on the bus and each has 16 GPIOs - so that's 128 with zero effort. (Theoretically you can add more by using an I2C extender.) The model B has two I2c buses = 256 ports.
For example, to make pin 18 drive high, first input raspi-gpio set 18 op to make pin 18 an output, then raspi-gpio set 18 dh to set it to high. You can also combine commands, such as raspi-gpio set 18 op dh to set pin 18 to an output and drive it high in the same line.
You need to unProvision your pin.
After u call .shutdown() you need to do the following
...
gpio.shutdown();
...
gpio.unProvisionPin(pin);
...
This should release pi4j's internal reference and allow you to reprovision it later
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With