B4J Question Raspberry Pi GPIO Error Pin already in use

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

developed a simple B4J server application (using websockets) to switch, via Browser, a LED connected to the GPIO PIN 1 On/Off. Information in the Browser is updated while blinking...
When opening in the Browser, the app is running OK, but when the Browser is refreshed the app crashes:
B4X:
java.lang.RuntimeException: com.pi4j.io.gpio.exception.GpioPinExistsException: This GPIO pin already exists: GPIO 1

Searched several threads and it seems the Pin has to be released by using API method (included in pi4j-core.jar)
B4X:
gpio.shutdown();
gpio.unprovisionPin(pin);

This could be accomplished in Websockets_Disconnect.

Currently the jPi4J library does not contain such a method.

Question: Is there a workaround avialable or would it be possible to include the method unprovisionPin in jPi4J libary?

Appreciated
 

Attachments

  • blinkingled.zip
    10.9 KB · Views: 230

rwblinn

Well-Known Member
Licensed User
Longtime User
GPIO.shutdown = yes have tried - same error.

Init the controller during server start = yes have been considering that (init the controller and the pins in Main - this is working as have tried), but then have not found a solution (yet) how update the websocket jqueryelements (defined in the websocket class) from Main when the user connects to the server.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Make sure to go over the threads section in this tutorial: https://www.b4x.com/android/forum/t...ads-sessions-and-server-events.39969/#content

The best solution is to use a code module to handle the controller. Use CallSubDelayed to call subs in this module. The subs will run on the main thread. This way you can write code that can handle multiple clients correctly.

Use CallSubDelayed again from the code module to the websocket handler when there a response is available.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Erel,

thanks for your steer - have rewritten this Raspberry Pi GPIO Experiment "The blinking LED" accordingly. Very Good learning about thread handling.
The app is working fine. For those interested, the code attached.
This solution is the basis for several Raspberry Pi GPIO Experiments - all controlled via Browser. The next in progress is an Obstacle Detector.

More to follow - look here.

Again amaizing what can be done with B4J.
 

Attachments

  • blinkingled.zip
    12.4 KB · Views: 267
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…