Skip to main content

A Non-Blocking Event Driven Applications Framework

Project description

What is pymata_rh?

Pymata_rh is a Python 3 (Version 3.7 or above) Application Programming Interface (API). It is Windows, macOS and, Linux compatible, allowing you to easily create Python scripts to control and monitor the Robo HAT MM1.

Robot HAT MM1 Pin Names To Pin Number Map And Supported Pin Modes

MM1 Pin Name Digital / Analog Pin # Digital Input Digital Output PWM Analog Input
Servo1 2 Yes Yes Yes No
Servo2 3 Yes Yes Yes No
Servo3 4 Yes Yes No No
Servo4 5 Yes Yes No No
Servo5 6 Yes Yes Yes No
Servo6 7 Yes Yes Yes No
Servo7 8 Yes Yes Yes No
Servo8 9 Yes Yes Yes No
NeoPixel 11 Yes Yes Yes No
LED 13 No Yes No No
RCC1 14 / A0 Yes Yes No Yes
RCC2 15 / A1 Yes Yes No Yes
RCC3 16 / A2 Yes Yes No Yes
RCC4 17 / A3 Yes Yes No Yes

NOTES:

  • All digital pins support digital input pull-up mode.
  • All analog input pins may be configured as digital inputs or outputs as well.
  • Currently, there is no library support provided within pymata_rh to control NeoPixels. However, the pin may be used as a digital pin.

API Support

The API not only supports the pins and modes listed above, but the pymata_rh API also provides support for:

  • Servo motors.
  • The onboard INA219 current, shunt, and power monitor.
  • The onboard MPU9250 nine-axis motion tracking device.
  • External i2c devices.
  • DHT 11 and 22 temperature sensors.
  • HC-SR04 sonar distance sensors.

**NOTE: ** You may need to connect a power supply that provides adequate current capacity for your application.

Implemented Using The Firmata Protocol

The API is implemented using the Firmata protocol in conjunction with FirmataExpress, custom firmware you install on the Robo HAT MM1. Communication between the Python script and the Robo HAT MM1 is accomplished over a serial link at 115200 baud.

A User's Guide is available, containing an annotated API as well as links to working examples.

Major features

  • Fully documented intuitive API

  • Python 3.7+ compatible.

  • Set the pin mode and go!

  • Data change events may be associated with a callback function, or each pin can be polled for its last event change.

    • Each data change event is time-stamped and logged.

Here is an example that demonstrates receiving asynchronous digital pin state data change notifications for pin 2 using callback notifications.

The API is quite simple to use. Here are the steps involved in creating a simple application.

  1. Set a pin mode for the pin and register a callback function.
  2. Have your application sit in a loop waiting for notifications.

When pymata_rh executes the callback method you specified, the data parameter is populated with a list of items that describe the change event, including a time-stamp.

from pymata_rh import pymata_rh
import time

class DigitalInput:
    """
    Set a pin for digital input and received all data changes
    in the callback method
    """
    def __init__(self, pin):
        """
        Set a pin as a digital input
        :param pin: digital pin number
        """

        # Indices into the callback report data
        self.CB_PIN_MODE = 0
        self.CB_PIN = 1
        self.CB_VALUE = 2
        self.CB_TIME = 3

        # Instantiate this class with the pymata_rh API
        self.device = pymata_rh.PymataRh()

        # Set the pin mode and specify the callback method.
        self.device.set_pin_mode_digital_input(pin, callback=self.the_callback)

        # Keep the program running and wait for callback events.
        while True:
            try:
                time.sleep(1)
            # If user hits Control-C, exit cleanly.
            except KeyboardInterrupt:
                self.device.shutdown()

    def the_callback(self, data):
        """
        A callback function to report data changes.
        This will print the pin number, its reported value
        the pin type (digital, analog, etc.) and
        the date and time when the change occurred

        :param data: [pin, current reported value, pin_mode, timestamp]
        """
        # Convert the date stamp to readable format
        date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[self.CB_TIME]))

        # Print the pin, current value and time and date of the pin change event.
        print(f'Pin: {data[self.CB_PIN]} Value: {data[self.CB_VALUE]} Time Stamp: {date}')

if __name__ == '__main__':
    # Monitor Pin 2 For Digital Input changes
    DigitalInput(2)

Sample console output as input change events occur:

Pin: 2 Value: 0 Time Stamp: 2020-03-10 13:26:22
Pin: 2 Value: 1 Time Stamp: 2020-03-10 13:26:27

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

pymata_rh-1.2.tar.gz (62.0 kB view details)

Uploaded Source

Built Distribution

pymata_rh-1.2-py2.py3-none-any.whl (75.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pymata_rh-1.2.tar.gz.

File metadata

  • Download URL: pymata_rh-1.2.tar.gz
  • Upload date:
  • Size: 62.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pymata_rh-1.2.tar.gz
Algorithm Hash digest
SHA256 abd81fbb36e8a63f44e9badc144ec31860a863bc1ae23acb80a9d1b215c66740
MD5 6ee6391b2fbee3e516202e39b01e8bd6
BLAKE2b-256 a4d971da97807446e377e9cd26634ff6b44cbd4e09146a515ac3cc04e1608e26

See more details on using hashes here.

File details

Details for the file pymata_rh-1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pymata_rh-1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 75.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for pymata_rh-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6c42718139e4f8dfaa8aa4b5f0d08fa2e5f31f919f0fa9869e68f2b6aae8982d
MD5 5c2a377669169f0114af1b53a777577c
BLAKE2b-256 26732c3510a11bc833172b7144afd889744cb0556e7dd8dab13ee2585b163596

See more details on using hashes here.

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