Calling a method when GPIO pin changes state

48 views
Skip to first unread message

albe

unread,
Aug 11, 2015, 12:59:46 PM8/11/15
to libbulldog
Hi,

I am attempting to monitor a GPIO using libbulldog. Here is the scenario.

GPIO_xx is set via a MQTT message from MQTT_publisher (app) via OpenHab. When testing using an LED on GPIO pin and sending a MQTT message to set to 1 - LED illuminates - Send message set to 0 - LED goes off.

I would like to see the GPIO state change using libbulldog. I need this information to send the command to a wireless relay to turn on/off a device.

For the LED to work correctly the GPIO pin is set to an Openhab switch which is output. I do not see in the libbulldog DigitalOutput class a call back for GPIO state change. Also, if GPIO is set to output from openhab an DigitalInput in libbulldog I get an error access denied when attempting to cycle pin state. I tried OpenHab contact which is input but will not cycle led and I get nothing using the buttonListener. The message changing the GPIO state within OpenHab is visible and the Pin state changes.

It appears that button.addListener(new ButtonListener()) does not spawn a listener thread. We have other threads running listening to radios for inbound messages etc. We need the listener to be threaded. Is this possible?

I am a recently back using Java so not an expert and may not be using terms correctly with respect to threads. The behavior we are looking for is like the xbee java library
// Register a listener to handle the samples received by the local device.
//            MyIIOSampleReceiveListener IIOListener = new MyIIOSampleReceiveListener();
//            IIOListener.setGPIOMap(hashGPIO1);
//            localDevice.addIOSampleListener(IIOListener);

The above spawns a thread and allows the application to proceed. Any ideas how to accomplish this with libbulldog?

Thank you..

Datenheld

unread,
Aug 11, 2015, 1:10:23 PM8/11/15
to libbulldog
You won't be able to easily see this with libbulldog currently if the pin is driven by another application.

However, i can suggest a workaround. Setup a digital input with libbulldog and attach an interrupt listener. Wire your output also to this input. Problem solved.

albe

unread,
Aug 11, 2015, 6:18:52 PM8/11/15
to libbulldog
Hi Thank you.. Is there an example of how to create an interrupt listener and one that executes in a thread? I looked at source for button and RotaryEncoder but it is not obvious to me how to accomplish the work around.. again thanks for your assistance..

Datenheld

unread,
Aug 11, 2015, 6:26:16 PM8/11/15
to libbu...@googlegroups.com
You can take a look how the Button class id implemented: https://github.com/Datenheld/Bulldog/blob/master/bulldog.devices/src/main/java/org/bulldog/devices/switches/Button.java

Basically you need a DigitalInput and attach an InterruptListener to it, which will run in a thread. You can create a DigitalInput like this:

board.getPin(PIN_NAME).as(DigitalInput.class)

Reply all
Reply to author
Forward
0 new messages