Control GPIO pins on a RaspberryPi 3 running Ubuntu Core 18, remotely (part 1/4)

Ubuntu Core 18 is out and one of the features that it packs with it is a set of snapd interfaces to access GPIO pins on the Raspberry Pi 2/3 in a fully confined snap. This enables one to just flash Ubuntu Core 18 on a micro sd card, boot, install a snap (which I author), connect a few interfaces and start controlling relays attached to a Raspberry Pi 2/3.

If you don't have Ubuntu Core 18 already installed, you can see the install instructions here

To get started (assuming you have Ubuntu Core 18 installed and have working ssh access to the Pi), you need to install a snap that exposes the said functionality over the network (local)

$ snap install pigpio

The above command installed the pigpio server, which automatically starts in the background. The server could take as much as 30 seconds to start, you have been warned.

We also need to allow the newly installed snap to access a few GPIO pins

$ snap connect pigpio:gpio pi:bcm-gpio-4
$ snap connect pigpio:gpio pi:bcm-gpio-5
$ snap connect pigpio:gpio pi:bcm-gpio-6
$ snap connect pigpio:gpio pi:bcm-gpio-12
$ snap connect pigpio:gpio pi:bcm-gpio-13
$ snap connect pigpio:gpio pi:bcm-gpio-17
$ snap connect pigpio:gpio pi:bcm-gpio-18
$ snap connect pigpio:gpio pi:bcm-gpio-19
$ snap connect pigpio:gpio pi:bcm-gpio-20
$ snap connect pigpio:gpio pi:bcm-gpio-21
$ snap connect pigpio:gpio pi:bcm-gpio-22
$ snap connect pigpio:gpio pi:bcm-gpio-23
$ snap connect pigpio:gpio pi:bcm-gpio-24
$ snap connect pigpio:gpio pi:bcm-gpio-26

The above pin numbers might look strange, but if you read a bit about the Raspberry Pi 3's GPIO pinout, you will realize, I only selected the "basic" pins, you are however free to connect all GPIO pin interfaces.

The pigpio snap that we installed above exposes the GPIO functionality over WAMP protocol and http. The HTTP implementation is very basic and allows to "turn on" and "turn off" a GPIO pin and get current state(s) of the pins.

Note: below commands assumes you have httpie installed (snap install http).

To get the state of all pins

    $ http POST http://raspberry_pi_ip:5021/call procedure=io.crossbar.pigpio-wamp.get_states

If we only want the state of a specific pin

    $ http POST http://raspberry_pi_ip:5021/call procedure=io.crossbar.pigpio-wamp.get_state args:='[4]'

To "turn on" a pin

    $ http POST http://raspberry_pi_ip:5021/call procedure=io.crossbar.pigpio-wamp.turn_on args:='[4]'

To "turn off"

    $ http POST http://raspberry_pi_ip:5021/call procedure=io.crossbar.pigpio-wamp.turn_off args:='[4]'

I am skipping talking about the WAMP based API for this, to keep this blogpost short, I must add though, that the WAMP implementation is much more powerful than the http one, especially because it has "event publishing", imagine multiple people controlling a single GPIO pin from different clients, we publish an event that can be subscribed to, hence ensuring all client apps stay in sync. I'll talk about this in a different blog post. In a later post, I will also be talking about making the GPIO pins accessible over the internet.

For me personally, I have a few projects for home and one for my co-working space that I plan to accomplish using this.

The code lives on github


Popular posts from this blog

Introducing PySide2 (Qt for Python) Snap Runtime