Package: pymata4
pymata4Please note that the pymata4 feature set is now frozen and the project has been placed in maintenance mode.Please consider usingTelemetrixinstead.A high performance, Python client for the Arduino Firmata Protocol.Pymata4 is a Firmata client that, like its asyncio sibling,pymata-express,allows you to control an Arduino using the high-performance FirmataExpress sketch.
It uses a conventional Python API for those that do not need or wish to use the asyncio programming paradigm of pymata-express.AUser's Guide is available,containing an annotated API as well as links to working examples.It supports both an enhanced version of StandardaFirmata 2.5.8, called FirmataExpress, as well as StandardFirmata and StandardFimataWiFi.FirmataExpressadds support for:HC-SR04 Ultrasonic Distance Sensors.DHT Humidity/Temperature Sensors.Stepper Motors.Piezo Tone Generation.Baud rate of 115200Special Note For FirmataExpress Users:pymata4 now verifies the version of FirmataExpress in use.  You may need to upgrade to the latest version of FirmataExpress using the Arduino IDE Library management tool.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.User's Guide, Including Examples.Implements 100% of the StandardFirmata Protocol (StandardFirmata 2.5.8).Advanced auto-detection of Arduino devices (when using FirmataExpress).Here is an example that monitors data changes on a digital input pin. It demonstrates both callback and
polling techniquesimporttimeimportsysfrompymata4importpymata4"""Setup a pin for digital input and monitor its changesBoth polling and callback are being used in this example."""# Setup a pin for analog input and monitor its changesDIGITAL_PIN=12# arduino pin numberPOLL_TIME=5# number of seconds between polls# Callback data indices# Callback data indicesCB_PIN_MODE=0CB_PIN=1CB_VALUE=2CB_TIME=3defthe_callback(data):"""A callback function to report data changes.This will print the pin number, its reported value andthe date and time when the change occurred:param data: [pin, current reported value, pin_mode, timestamp]"""date=time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(data[CB_TIME]))print(f'Pin:{data[CB_PIN]}Value:{data[CB_VALUE]}Time Stamp:{date}')defdigital_in(my_board,pin):"""This function establishes the pin as adigital input. Any changes on this pin willbe reported through the call back function.:param my_board: a pymata_express instance:param pin: Arduino pin number"""# set the pin modemy_board.set_pin_mode_digital_input(pin,callback=the_callback)whileTrue:try:# Do a read of the last value reported every 5 seconds and print it# digital_read returns A tuple of last value change and the time that it occurredvalue,time_stamp=my_board.digital_read(pin)date=time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(time_stamp))# valueprint(f'Polling - last value:{value}received on{date}')time.sleep(POLL_TIME)exceptKeyboardInterrupt:board.shutdown()sys.exit(0)board=pymata4.Pymata4()try:digital_in(board,DIGITAL_PIN)exceptKeyboardInterrupt:board.shutdown()sys.exit(0)And here is the console output:pymata4:Version1.00

Copyright(c)2020AlanYorinksAllRightsReserved.

Openingallpotentialserialports.../dev/ttyACM0

Waiting4seconds(arduino_wait)forArduinodevicestoreset...

SearchingforanArduinoconfiguredwithanarduino_instance=1Arduinocompatibledevicefoundandconnectedto/dev/ttyACM0

RetrievingArduinoFirmwareID...
ArduinoFirmwareID:2.5FirmataExpress.ino

Retrievinganalogmap...
Auto-discoverycomplete.Found20DigitalPinsand6AnalogPins


Polling-lastchange:0changereceivedon1969-12-3119:00:00Pin:12Value:0TimeStamp:2020-03-0708:52:10
Pin:12Value:1TimeStamp:2020-03-0708:52:12
Polling-lastvalue:1receivedon2020-03-0708:52:12This project was developed withPycharm