Skip to main content

Python library for controlling Raspberry Pi® GPIO Pins through a serializable message protocol.

Project description

Serialize Gpio

Serialize Gpio is a Python library that provides tools for manipulating Raspberry Pi® GPIO pins using a serializable message protocol. These messages are modelled with Python dictionaries, but can also be encoded into either JSON or XML format. Using one of these two formats allows you to easily interact with the pins remotely or from an application which doesn't have access to a library for doing so.

At it's core; Serialize Gpio is a wrapper for gpiozero, which provides a simple interface for managing the GPIO pins. That library abstracts away lower level concerns using device classes, which go from simple ones like 'LED' or 'Button' to more complex ones like 'Robot' (a representation of a generic dual motor robot). Of course, it also supports more general classes such as 'DigitalOutputDevice'.

You can interact with these device objects through:

  • Their methods ( such as LED.on() )
  • Their properties, each falling into one of two categories:
    • Properties which take functions which will be executed when certain event happens (such as Button.when_pressed)
    • Properties which take literal values (such as Button.hold_time)

For more information on the available device classes and their corresponding attributes, visit gpiozero's documentation.

Installation

Dependencies

Serialize Gpio directly depends on gpiozero, which comes already installed by default in the Raspbian image. If you are using Raspbian Lite, you can install it with:

pi@raspberrypi:~$ sudo apt update
pi@raspberrypi:~$ sudo apt install python3-gpiozero

or

pi@raspberrypi:~$ sudo pip3 install gpiozero

If you are using a different operating system, you might need to use the pip version.

Also, it's recommended that you install RPi.GPIO if you don't have it installed already. Gpiozero can use multiple libraries for controlling the pins or a custom implementation if none are availabe. Nonetheless, the prefered implementation is RPi.GPIO. You can install it with:

pi@raspberrypi:~$ sudo pip3 install rpi.gpio

Disclaimer

While gpiozero supports Python 2, Serialize Gpio only supports Python 3.

Installing Serialize Gpio

Serialize Gpio is available in the Python Package index, so you can install it using pip:

pi@raspberrypi:~$ sudo pip3 install serialize_gpio

Alternatively; you can download it from the releases page and install it from that file or clone this repository and use it from source.

Usage

Serialize Gpio provides the GPIOController object to interface with the GPIO pins. Through the method 'handle_message', it receives a message and execute the action it describes.

Message manipulators are used for building, encoding and decoding messages. Use BasicMessageManipulator for messages in dictionary format, JsonMessageManipulator for JSON and XMLMessageManipulator for XML.

from serialize_gpio.controller import GPIOController
from serialize_gpio.messageManipulators import JsonMessageManipulator

#The GPIOController constructor takes desired message format as a parameter.
controller = GPIOController('json')
messageBuilder = JsonMessageManipulator()

# The build_create_device_message method takes the name of device class, the name you want
# to assign to device and the keyword arguments for the classes' __init__ method.
create_led_message = messageBuilder.build_create_device_message('LED', 'blinkingLED', pin=4)
controller.handle_message(create_led_message)

# Make the led blink
# build_execute_device_method_message takes the device's name, the name of the method and
# keyword arguments for any method arguments.
execute_blink_message = messageBuilder.build_execute_device_method_message('blinkingLED', 'blink', on_time=0.5)
controller.handle_message(execute_blink_message)

Documentation

You can find the project's documentation it the wiki.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serialize_gpio-0.1.0.tar.gz (8.9 kB view hashes)

Uploaded Source

Built Distribution

serialize_gpio-0.1.0-py3-none-any.whl (10.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page