This section demonstrates how to establish communication between the MPU and the MCU over serial port using Firmata protocol. There are several Firmata implementations using different interfaces such as pyFirmata and PyMata, both programmed in Python; other interfaces in languages including Node.js and cylon.js are also provided the next sections.
The diagram below illustrates how the MPU and MCU in LinkIt Smart 7688 Duo development board communicate using Firmata protocol and Python.
Setup your development environment
LinkIt Smart 7688 Duo development board supports Arduino IDE v1.6.5 and Python 2.7. Firmata protocol requires implementation on the MPU and the MCU. The following prerequisites need to be installed to program the MCU.
On your PC
- Arduino IDE v1.6.5
On the LinkIt Smart 7688 Duo development board
- Python 2.7: It's preloaded in the system image.
There are several projects implementing Firmata in Python, you can choose either one:
- pyFirmata – This is a Python interface for Firmata protocol used on the MCU. You can install it by entering the command pip install pyfirmata in the system console. At the time of writing, LinkIt Smart 7688 Duo development board supports version pyFirmata-1.0.3-py2.7.
- PyMata – This is a Python interface for the Firmata protocol used on the MPU. You can install it by entering the command pip install PyMatain in the console. At the time of writing, LinkIt Smart 7688 Duo development board supports version pymata-2.1.
To use Firmata, upload a sketch that will interpret the commands sent from the Linux side to the MCU peripheral controls.
Upload Arduino Firmata sketch
- Launch the Arduino IDE.
Click File, then Examples. Select Firmata then StandardFirmata. This built-in example is for Arduino Uno, which uses Serial as Serial port. However, LinkIt Smart 7688 Duo development board uses Serial1 to communicate with OpenWrt. Therefore, you’ll need to change the listening port of this example from Serial to Serial1. Find the following line in the example:
Firmata.begin(57600);and replace it with
- Upload the sketch. After uploading the sketch, the Arduino IDE is ready to receive commands from the Linux side.
Running Your Blink Example Using pyFirmata
- Install pyFirmata with pip install pyfirmata.
- Create a python file called blink_with_firmata.py.
Open the file and copy and paste the following example code. This example code is modified to run on LinkIt Smart 7688 Duo development board, such as:
from pyfirmata import Arduino, util from time import sleep board = Arduino('/dev/ttyS0') print "Start blinking D13" while True: board.digital.write(1) sleep(0.5) board.digital.write(0) sleep(0.5)
# python ./blink_with_firmata.py Start blinking D13
If the LED on D13 pin of the LinkIt Smart 7688 Duo development board blinks accordingly, congratulations. You’ve successfully ran the blink example. To terminate the Python program, press Ctrl-C.
After you’ve installed PyMata libraries and changed the serial port in Arduino, you’re ready to run the blink example using PyMata.
Upload blink example to the board. You’ll also need to change the serial port used in this example to work on the LinkIt Smart 7688 Duo development board. Find the following line on the example:
board = PyMata("/dev/ttyACM0", verbose=True)
and replace it with:
board = PyMata("/dev/ttyS0", verbose=True)
- Execute the python script and you should see following output:
# python ./pymata_blink.py Python Version 2.7.9 (default, Aug 12 2015, 15:09:09) [GCC 4.8.3] PyMata version 2.10 Copyright(C) 2013-15 Alan Yorinks All rights reserved. Opening Arduino Serial port /dev/ttyS0 Please wait while Arduino is being detected. This can take up to 30 seconds ... Board initialized in 0 seconds Total Number of Pins Detected = 30 Total Number of Analog Pins Detected = 12 Blinking LED on pin 13 for 10 times ... 1 2 3 4 5 6 7 8 9 10 PyMata close(): Calling sys.exit(0): Hope to see you soon!
If the LED on D13 pin of the LinkIt Smart 7688 Duo development board blinks 10 times before the Python program terminates, congratulations. You’ve successfully ran the blink example. Please continue to explore more examples in the PyMata/examples you’ve downloaded earlier. Just remember to change the serial port to /dev/ttyS0, and keep in mind the hardware differences between LinkIt Smart 7688 Duo and Arduino Uno devices:
- LinkIt Smart 7688 Duo development board operates in 3.3V, not 5V as in Arduino Uno.
- You’ll need extra power source when driving motor or servo peripherals because they draw more current.
Please refer to Firmata website for other Python interfaces available for Firmata protocol.