Firmata: Difference between revisions

From ICO wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 26: Line 26:


def signal_handler(sig, frame):
def signal_handler(sig, frame):
    print('You pressed Ctrl+C!!!!')
     if board is not None: board.reset()
     if board is not None:
        board.reset()
     sys.exit(0)
     sys.exit(0)
   
signal.signal(signal.SIGINT, signal_handler)


signal.signal(signal.SIGINT, signal_handler)
board = PyMata()
board = PyMata("/dev/ttyACM0")
board.set_pin_mode(5, board.PWM, board.DIGITAL)
board.set_pin_mode(5, board.PWM, board.DIGITAL)



Revision as of 20:35, 20 February 2016


Setting up Arduino

Clone Arduino library to your machine:

 git clone http://github.com/firmata/arduino ~/Documents/Arduino/libraries/Firmata

Select Examples -> Firmata -> StandardFirmata from the menu and hit the Upload button.

Classic Python 3.4

Install PyMata:

git clone https://github.com/MrYsLab/PyMata
cd PyMata
sudo python3 setup.py install

Code example for test.py:

import signal
import sys
from PyMata.pymata import PyMata

def signal_handler(sig, frame):
    if board is not None: board.reset()
    sys.exit(0)
    
signal.signal(signal.SIGINT, signal_handler)

board = PyMata()
board.set_pin_mode(5, board.PWM, board.DIGITAL)

while True:
    for i in range(0, 255):
        board.analog_write(5, i)
    for i in range(255, 0, -1):
        board.analog_write(5, i)

Run the example:

python3 test.py


Asynchronous Python 3.5

For Ubuntu 14.04 first add Python 3.5 repository:

 sudo add-apt-repository ppa:fkrull/deadsnakes
 sudo apt-get update
 sudo apt-get install python3.5 python3-serial

Ubuntu 15.10 already has Python 3.5 available, simply install it:

 sudo apt-get install python3.5 python3-serial

Ubuntu 16.04 will ship Python 3.5 by default just add serial module:

 sudo apt-get install python3-serial

Install PyMata for Python 3.5:

 git clone https://github.com/MrYsLab/pymata-aio
 cd pymata-aio
 sudo python3.5 setup.py install

Create test.py with following code:

from pymata_aio.pymata3 import PyMata3
from pymata_aio.constants import Constants

# instantiate the pymata_core API
board = PyMata3()

# set the pin mode
board.set_pin_mode(13, Constants.PWM)

for j in range(0, 10):
    for i in range(0, 255):
        board.analog_write(13, i)
    for i in range(255, 0, -1):
        board.analog_write(13, i)

# reset the board and exit
board.shutdown()

Plug the Arduino to your machine via USB cable and run the code:

python3.5 test.py