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.
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
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
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.
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)
You can find the project's documentation it the wiki.