Examples

Additional examples can be found in the examples directory.

Basic Example

This is a very basic example to get started. It just connects to an Arduino running ArduRPC and extract some basic information.

#!/usr/bin/env python
"""
Basic example.

Connects to an Arduino using the ArduRPC protocol and displays some information.

Change the connect() function to reflect your settings.
"""

import ardurpc
from ardurpc.connector import Serial, UDP


def connect():
    # Connect to the serial port
    con = Serial("/dev/ttyACM0", 9600)

    # More examples:
    # con = Serial("/dev/ttyUSB0", 9600)
    # con = UDP(host="192.168.1.1", port=1234)

    # New instance
    rpc = ardurpc.ArduRPC(connector=con)

    print("Version(Protocol): {0}".format(rpc.getProtocolVersion()))
    print(
        "Version(Library): {0}".format(
            ".".join([str(i) for i in rpc.getLibraryVersion()])
        )
    )
    print(
        "Available handlers: {0}".format(
            ", ".join(rpc.get_handler_names())
        )
    )

    return rpc

if __name__ == "__main__":
    connect()

Pixel Strip

This examples uses the connect() function from the basic example.

#!/usr/bin/env python
"""
Control a pixel strip

This example uses the connect() function from the Basic example.
"""


from basic import connect


def run():
    # use the basic example
    rpc = connect()

    # Get a handler named 'strip'
    handler = rpc.get_handler_by_name("strip")
    if handler is None:
        print("A handler with the given name does not exist")

    # Get pixel count
    pixel_count = handler.getPixelCount()
    print("Strip has {0} pixels".format(pixel_count))

    # Set color to red
    for i in range(0, pixel_count):
        handler.setPixelColor(i, (255, 0, 0))

    # Set color to green
    for i in range(0, pixel_count):
        handler.setPixelColor(i, (0, 255, 0))

    # Set color to blue
    for i in range(0, pixel_count):
        handler.setPixelColor(i, (0, 0, 255))


if __name__ == "__main__":
    run()