Using Firmata with Python

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.

Using pyFirmata

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

  1. Launch the Arduino IDE.
  2. 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

    Serial1.begin(57600); 
    Firmata.begin(Serial1); 
  3. 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

  1. Install pyFirmata with pip install pyfirmata.
  2. Create a python file called blink_with_firmata.py.
  3. 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[13].write(1)
        sleep(0.5)
        board.digital[13].write(0)
        sleep(0.5)  
Now execute this Python script and you should see the following message:

# 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.

Using PyMata

After you’ve installed PyMata libraries and changed the serial port in Arduino, you’re ready to run the blink example using PyMata.

  1. 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)
  2. 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.