None Notebook

This notebook contains material from cbe61622; content is available on Github.

< A.2 Downloading Python source files from github | Contents | A.4 Scheduling Real-Time Events with Simpy >

Open in Colab

Download

A.3 Getting Started with Pymata4

Pymata4 is a Python library that allows you to monitor and control Arduino hardware from a host computer. The library uses the Firmata protocol for communicating with the Arduino hardware. Pymata4 supports the StandardFirmata server included with the Arduino IDE, and also StandardFirmataWiFi, and an enhanced server FirmataExpress distributed with Pymata4.

Pymata4 uses concurrent Python threads to manage interaction with the Arduino. The concurrency model enables development of performant and interactive Arduino applications using Python on a host computer. Changes in the status of an Arduino pin can be processed with callbacks. It's sibling, pymata-express, is available using the Python asyncio package.

Support for common $I^2C$ devices, including stepper motors, is included in FirmataExpress. Applications using unsupported $I^2C$ devices may require modifications to the Firmata server sketch.

Useful links:

A.3.1 Hardware Setup and Software Installations

The Arduino must be attached to the host by USB with either the StandardFirmata or Firmata-express sketch installed using the Arduino IDE. For use with WiFi, install StandardFirmataWiFi.

The Python pymata4 package can be installed with pip.

A.3.2 Basic Usage

pymata4.Pymata()
board.shutdown()

A.3.3 Blinker

board.digital_write(pin, value)

Pymata4 has two methods for writing a 1 or a 0 to a digital output. digital_write(pin, value) hides details of the Firmata protocol from the user. The user can refer to digital pins just as they would in standard Arduino coding. A second method, digital_pin_write(pin, value) allows writing to multiples at the same time, but requires the user to understand further details of the Firmata protocol.

A.3.4 Handling a Keyboard Interrupt

Pymata4 sets up multiple concurrent processes upon opening connection to the Arduino hardware. If Python execution is interrupted, it isimportant to catch the interrupt and shutdown the board before exiting the code. Otherwise the Arduino may continue to stream data requiring the Arduino to be reset.

A.3.5 Getting Information about the Arduino

Firmata protocol

A.3.6 Temperature Control Lab Shield

< A.2 Downloading Python source files from github | Contents | A.4 Scheduling Real-Time Events with Simpy >

Open in Colab

Download