There are many applications where you want to interface with mbed from a computer. Python is one language which you may wish to use. This page provides a library of code that can be used to communicate with mbed using RPC and a guide to set up Python.
On windows, install python, win32 extensions and pyserial (if you want to communicate using serial):
- Run python-2.6.1.msi - Python Windows Binary
- Run pywin32-212.win32-py2.6.exe - Python Win32 Extensions
- Run pyserial-2.4.win32.exe - Python Serial Port Extension (gave some errors)
On Mac OS X (10.5 or later):
- Python is already installed.
- Run Terminal.app and type
sudo easy_install pyserialat the prompt to install PySerial.
- Also type
ls /dev/tty.usbmodem*to find the device name of the mbed USB serial connection.
On Linux, or other unix-like:
- Python is most-likely already installed; if not see your OS's install system or visit http://www.python.org/download/
- In a terminal type
sudo easy_install pyserialto install PySerial.
- Also type
ls /dev/ttyACM*to find the device name of the mbed USB serial connection.
Python RPC Library¶
The mbed libraries support RPC, which means you can create objects and control them remotely; Interfacing-Using-RPC.
Here is a Python RPC library which uses the mbed RPC mechanism to map Python classes on to the mbed Library C++ Interface classes. The library is designed to be very similar to the mbed interface. This is an example using HTTP.
#!python python >>> from mbedrpc import * >>> mbed = HTTPRPC("192.168.0.4") >>> x = DigitalOut(mbed,LED1) >>> z = DigitalOut(mbed,LED2) >>> ain = AnalogIn(mbed, LED3) >>> x.write(1) >>> z.write(0.5) >>> ain.read() 0.786757474 >>>
- First of all you must import the mbedrpc library. To do this the mbedrpc.py file must be in the Python library.
- You then create an mbed object on the transport mechanism that you require, (more information on this below). If you are using more than one mbed then create an object for each one passing the appropriate parameters to the transport mechanism for each.
>>> mbed = HTTPRPC("192.168.2.2") >>> mbed2 = HTTPRPC("192.168.2.3")
- You can then create mbed interface objects in the same way as you would on mbed only you also have to pass them the mbed object which represents the mbed they are to be created on. This means you can control multiple mbeds.
>>> x = DigitalIn(mbed, p20)
- Alternatively you may wish to tie a Python object to an existing object on mbed in which case you pass in the name of that object.
>>> y = DigitalOut(mbed, "myled")
- Finally you can execute the methods on interface objects as defined in the Handbook.
If you want to execute an RPC command that isn't wrapped by the classes we've provided then use: RPCresponse = mbed.RPC(<Object name>,<Method name>,<Arguments as an array>)
Python RPC via HTTP¶
First you need code running on mbed to set up a HTTP server. Guidelines for this are in the Interfacing-Using-RPC page.
Once this is on the mbed, plug it in to your home router and power it up. With the serial port enabled, it should show the i/f comeing up, and print out the ip address that it has been assigned via DHCP.
#!python python >>> from mbedRPC import * >>> mbed = HTTPRPC("192.168.0.4") >>> x = DigitalOut(mbed, LED3) # LED3 >>> x.write(1) >>> x.read() 1 >>>
Python RPC via Serial¶
Test the Serial Connection¶
On Windows, connect to COM15 with default 9600 8N1 setting:
#!python python >>> import serial >>> serdev = 15 >>> s = serial.Serial(serdev) >>> s.write("hello") >>> s.close()
or when on a Macintosh (in this case /dev/tty.usbmodem1912):
#!python >>> import serial >>> serdev = '/dev/tty.usbmodem1912' >>> s = serial.Serial(serdev) >>> s.write("hello") >>> s.close()
or when on a Linux/unix system (in this case /dev/ttyACM0):
#!python >>> import serial >>> serdev = '/dev/ttyACM0' >>> s = serial.Serial(serdev) >>> s.write("hello") >>> s.close()
Status light flickered. Cool!
For more basic info, see http://pyserial.wiki.sourceforge.net/pySerial
We know how to make Python talk to Serial. So if we set up mbed to receive RPC over serial then we can use this to control it. Again guidelines for setting up mbed to use serial RPC can be found here: Interfacing-Using-RPC
We can use the same mbedRPC.py file as before. We just need to set up the mbed object using SerialRPC rather than HTTPRPC
#!python python >>> from mbedRPC import * >>> serdev = 15 # or '/dev/tty.usbmodem1912' for Mac or '/dev/ttyACM0' for Linux/unix >>> mbed = SerialRPC(serdev, 9600) >>> x = DigitalOut(mbed, LED3) # LED3 >>> x.write(1) >>> x.read() 1 >>>