6

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)
Vicky
  • 1,412
  • 4
  • 18
  • 30

1 Answers1

14

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

user3663725
  • 188
  • 1
  • 6