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):
On Mac OS X (10.5 or later):
sudo easy_install pyserial at the prompt to install PySerial.
ls /dev/tty.usbmodem* to find the device name of the mbed USB serial connection.
On Linux, or other unix-like:
sudo easy_install pyserial to install PySerial.
ls /dev/ttyACM* to find the device name of the mbed USB serial connection.
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
>>>
>>> mbed = HTTPRPC("192.168.2.2")
>>> mbed2 = HTTPRPC("192.168.2.3")
>>> x = DigitalIn(mbed, p20)
>>> y = DigitalOut(mbed, "myled")
>>>x.read() >>>y.write(1)
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>)
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
>>>
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 >>>
Please login to post comments.
Hi, there's a bug in mbedrpc.py on line 266, you have to remove the . before (self.name... to get it working. The line should look like this: self.mbed.rpc(self.name, "write", [str(value)])
And not this: self.mbed.rpc.(self.name, "write", [str(value)])