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 20 added one line
!! Example Code
At line 22 added 7 lines
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
At line 30 added 4 lines
"""
Setup a pin for digital input and monitor its changes
using a callback.
"""
At line 35 added 3 lines
# 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
At line 39 added 56 lines
# 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)
}}}