Package: pymata-rh
What is pymata_rh?Pymata_rhis 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 ModesMM1 Pin NameDigital / Analog Pin #Digital InputDigital OutputPWMAnalog InputServo12YesYesYesNoServo23YesYesYesNoServo34YesYesNoNoServo45YesYesNoNoServo56YesYesYesNoServo67YesYesYesNoServo78YesYesYesNoServo89YesYesYesNoNeoPixel11YesYesYesNoLED13NoYesNoNoRCC114 / A0YesYesNoYesRCC215 / A1YesYesNoYesRCC316 / A2YesYesNoYesRCC417 / A3YesYesNoYesNOTES: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 SupportThe 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 ProtocolThe API is implemented using theFirmata protocolin 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.AUser's Guide is available,containing an annotated API as well as links to working examples.Major featuresFully documentedintuitive APIPython 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.Set a pin mode for the pin and register a callback function.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.frompymata_rhimportpymata_rhimporttimeclassDigitalInput:"""Set a pin for digital input and received all data changesin the callback method"""def__init__(self,pin):"""Set a pin as a digital input:param pin: digital pin number"""# Indices into the callback report dataself.CB_PIN_MODE=0self.CB_PIN=1self.CB_VALUE=2self.CB_TIME=3# Instantiate this class with the pymata_rh APIself.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.whileTrue:try:time.sleep(1)# If user hits Control-C, exit cleanly.exceptKeyboardInterrupt:self.device.shutdown()defthe_callback(self,data):"""A callback function to report data changes.This will print the pin number, its reported valuethe pin type (digital, analog, etc.) andthe date and time when the change occurred:param data: [pin, current reported value, pin_mode, timestamp]"""# Convert the date stamp to readable formatdate=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 changesDigitalInput(2)Sample console output as input change events occur:Pin:2Value:0TimeStamp:2020-03-1013:26:22
Pin:2Value:1TimeStamp:2020-03-1013:26:27