This page (revision-5) was last changed on 2020-04-30 05:30 by Murray Altheim

This page was created on 2020-04-30 03:29 by Murray Altheim

Only authorized users are allowed to rename pages.

Only authorized users are allowed to delete pages.

Page revision history

Version Date Modified Size Author Changes ... Change note
5 2020-04-30 05:30 255 bytes Murray Altheim to previous
4 2020-04-30 03:36 2 KB Murray Altheim to previous | to last
3 2020-04-30 03:31 2 KB Murray Altheim to previous | to last
2 2020-04-30 03:30 626 bytes Murray Altheim to previous | to last
1 2020-04-30 03:29 573 bytes Murray Altheim to last

Page References

Incoming links Outgoing links

Version management

Difference between version and

At line 1 changed 2 lines
This page describes an experiment in using PyMata Express to communicate (using
[Firmata]) between a [Raspberry Pi] and an [ItsyBitsy] microcontroller.
%%alias
This page has been replaced by:
[PyMataExpressOnArduino]
%%
%%small
(My ItsyBitsy is running CircuitPython, not Arduino sketches)
%%
At line 4 changed 97 lines
!! Installing PyMata Express on Linux/Raspberry Pi
To install Pymata Express on Linux (including Raspberry Pi) and
macOS computers, open a terminal window and type:
{{{
sudo pip3 install pymata-express
}}}
!! Installing Firmata Express on an Arduino
The [FirmataExpress|https://mryslab.github.io/pymata-express/firmata_express/] page
describes how to install it on an Arduino, using the Arduino IDE.
!! Example Code
Here is an example demonstrating using a callback to monitor
the state changes of a digital input pin.
{{{
import asyncio
import time
import sys
from pymata_express import pymata_express
"""
Setup a pin for digital input and monitor its changes
using a callback.
"""
# Setup a pin for analog input and monitor its changes
DIGITAL_PIN = 12 # arduino pin number
IDLE_TIME = .001 # number of seconds for idle loop to sleep
# Callback data indices
# Callback data indices
CB_PIN_MODE = 0
CB_PIN = 1
CB_VALUE = 2
CB_TIME = 3
async def the_callback(data):
"""
A callback function to report data changes.
This will print the pin number, its reported value and
the 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}')
async def digital_in(my_board, pin):
"""
This function establishes the pin as a
digital input. Any changes on this pin will
be reported through the call back function.
:param my_board: a pymata_express instance
:param pin: Arduino pin number
"""
# set the pin mode
await my_board.set_pin_mode_digital_input(pin, callback=the_callback)
while True:
try:
await asyncio.sleep(IDLE_TIME)
except KeyboardInterrupt:
await board.shutdown()
sys.exit(0)
# get the event loop
loop = asyncio.get_event_loop()
# instantiate pymata_express
board = pymata_express.PymataExpress()
try:
# start the main function
loop.run_until_complete(digital_in(board, 12))
except (KeyboardInterrupt, RuntimeError) as e:
loop.run_until_complete(board.shutdown())
sys.exit(0)
}}}
!! References
* [Introducing ItsyBitsy|https://learn.adafruit.com/introducing-adafruit-itsybitsy-m4] on Adafruit
* [ItsyBitsy M4 Pinout|https://learn.adafruit.com/introducing-adafruit-itsybitsy-m4/pinouts]
----
[{Tag RaspberryPi Arduino}]
If someone wants to try this on an [ItsyBitsy], certainly feel free to use this page for documentation.