GPIO Plugin for Apama Community Edition on a Raspberry Pi

The Raspberry Pi is a great little computer that can now run Apama with the release of the Community Edition Core components. And one of the great things about the Raspberry Pi is the way in can interact with real world ‘things’ using it’s GPIO pins, such as LEDs, buttons, buzzers, etc. To connect Apama to the GPIO pins on the Raspberry Pi we have created the GPIO Plugin which will allow a user to easily monitor and respond to changes on the GPIO pins.

The download is a zip file that can be extracted anywhere using unzip, but we would suggest you unzip it into the APAMA_WORK directory, which by default is ‘~/softwareag/apamawork_9.10’ (version dependent).

The GPIO Plugin uses Wiring Pi (www.wiringpi.com) as a library to access the GPIO pins. Wiring Pi is installed with the Full Raspian OS Jessie. The GPIO Plugin is distributed as source code (so feel free to change and add to it – we’d love to hear about any changes you make) and can easily be built using the ‘ make ‘ command.

The Plugin comes in 2 parts, the library and the EPL helper events. The library is written in C++ and is coded as an Apama Correlator plugin. The EPL helper events wrap functionality of common GPIO attachments such as LEDs, Buttons and Buzzers and allow the user to represent these within Apama and provide common functionality, such as switching on an LED, or monitoing a Button for a push.

We have used the BCM pin numbering system which you should familiarise yourself with. Try ‘gpio readall’ on your Pi or visit – http://wiringpi.com/pins/

The Plugin also comes with tutorials based upon the PySys testing framework that comes with Apama. These use the EPL helper events and an example follows that responds to a button push by toggling between 2 connected LEDs.

using com.apamax.rpi.gpio.Setup;
using com.apamax.rpi.gpio.LED;
using com.apamax.rpi.gpio.Button;
 
monitor ButtonToggle {
    boolean toggle := false;
    LED led1;
    LED led2;
    
    action onload() {
        // Setup GPIO access to the pins
        (new Setup).setInputPins([22]).setOutputPins([17, 27]).init();
        
        led1 := (new LED).init(17, toggle);
        led2 := (new LED).init(27, not toggle);
        
        integer buttonPin := 22;
        Button button := (new Button).init(buttonPin);
        
        // Setup a listener for the ButtonPush on buttonPin
        on all com.apamax.rpi.gpio.ButtonPush(pin=buttonPin) {
            log "Button Pressed" at INFO;
            led1.set(toggle);
            toggle := not toggle;
            led2.set(toggle);
        }
    }
}

This shows how little EPL code you have to write to interact with 3 separate devices connected to GPIO pins. The tutorials included are based around LEDs, Buttons and Buzzers. We have taken inspiration from the GPIO tutorial found here – https://www.raspberrypi.org/learning/physical-computing-with-python/worksheet/ – but coded in Apama EPL rather than Python. You should follow these instructions on how to wire LEDs, Buttons and Buzzers to the GPIO pins.

A Readme file is included in the download with a lot more information. Feel free to change or add to any of the C++ or EPL code and we’d love to hear about your projects that use Apama Community Edition and the GPIO Plugin.

As always, please post on our forums with any questions you may have. Thanks and happy downloading!

UPDATE 17TH JAN 2018

The GPIO plugin is now a community effort hosted on GitHub. Please feel free to contribute at the below address. This article will remain for historical reasons however some information is considered out of date.

GPIO Plugin on GitHub

— Martin

Disclaimer:
Utilities and samples shown here are not official parts of the Software AG products. These utilities and samples are not eligible for technical support through Software AG Customer Care. Software AG makes no guarantees pertaining to the functionality, scalability, robustness, or degree of testing of these utilities and samples. Customers are strongly advised to consider these utilities and samples as “working examples” from which they should build and test their own solutions