Difference between revisions of "Interfacing with Python"

From Contiki
Jump to: navigation, search
m (Step 1)
 
(13 intermediate revisions by 4 users not shown)
Line 9: Line 9:
 
This tutorial was written and tested using the following:
 
This tutorial was written and tested using the following:
 
* Contiki 2.7
 
* Contiki 2.7
* Python 2.7.3 (and pySerial)
+
* Python 2.7.3 (and [http://pyserial.sourceforge.net/ pySerial])
 
* Ubuntu 12.04
 
* Ubuntu 12.04
 
* Tmote Sky
 
* Tmote Sky
Line 15: Line 15:
 
Connect the Tmote Sky to your computer using a USB cable and install the Sky Shell example. See the @Sky Shell@ tutorial for instructions on how to do this. We use this example so that we’ll have data to ensure that we are correctly reading and writing to the mote.
 
Connect the Tmote Sky to your computer using a USB cable and install the Sky Shell example. See the @Sky Shell@ tutorial for instructions on how to do this. We use this example so that we’ll have data to ensure that we are correctly reading and writing to the mote.
  
== Step 1 ==
+
=== Step 1 ===
  
 
Create a Python file called serial.py containing the following code:
 
Create a Python file called serial.py containing the following code:
#!/usr/bin/python
+
<source lang="python">
 +
#!/usr/bin/python
 +
import serial
 +
ser = serial.Serial(
 +
  port='/dev/ttyUSB0',\
 +
  baudrate=115200,\
 +
  parity=serial.PARITY_NONE,\
 +
  stopbits=serial.STOPBITS_ONE,\
 +
  bytesize=serial.EIGHTBITS,\
 +
  timeout=0)
 +
print("connected to: " + ser.portstr)
 +
ser.write("help\n");
 +
while True:
 +
line = ser.readline();
 +
if line:
 +
print(line),
 +
ser.close()
 +
</source>
 +
 
 +
Note:
 +
This code assumes the mote you are using for this tutorial is connected to ''/dev/ttyUSB0''. Please check this using the ''make motelist'' command as shown in the figure below. If the mote you are using is connected to a different device file, please modify the Python code accordingly.
 +
 
 +
 
 +
[[File:Motelist.jpg|center|Sample output of ''make motelist'']]
 +
 
 +
=== Step 2 ===
 +
 
 +
Run the Python code using the command:
 +
 
 +
  python serial.py
 +
 
 +
If you encounter any errors, see the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting
 +
 
 +
You should see output similar to the following:
 +
  connected to: /dev/ttyUSB0
 +
  Available commands:
 +
  ?: shows this help
 +
  binprint: print binary data in decimal format
 +
  blink [num]: blink LEDs ([num] times)
 +
  collect: collect data from the network
 +
  collect-view-data: sensor data, power consumption, network stats
 +
  echo <text>: print <text>
 +
  energy: print energy profile
 +
  exit: exit shell
 +
  hd: print binary data in hexadecimal format
 +
  help: shows this help
 +
  kill <command>: stop a specific command
 +
  killall: stop all running commands
 +
  mac <onoroff>: turn MAC protocol on (1) or off (0)
 +
  netcmd <command>: run a command on all nodes in the network
 +
  nodeid: set node ID
 +
  null: discard input
 +
  packetize: put data into one packet
 +
  power: print power profile
 +
  powerconv: convert power profile to human readable output
 +
  powertrace [interval]: turn powertracing on or off, with reporting interval <interval>
 +
  quit: exit shell
 +
  randwait <maxtime> <command>: wait for a random time before running a command
 +
  reboot: reboot the system
 +
  repeat <num> <time> <command>: run a command every <time> seconds
 +
  rfchannel <channel>: change CC2420 radio channel (11 - 26)
 +
  routes: dump route list in binary format
 +
  send <rexmits>: send data to the collector node, with rexmits hop-by-hop retransmissions
 +
  sense: print out sensor data
 +
  senseconv: convert 'sense' data to human readable format
 +
  size: print the size of the input
 +
  time [seconds]: output time in binary format, or set time in seconds since 1970
 +
  timestamp: prepend a timestamp to data
 +
  txpower <power>: change CC2420 transmission power (0 - 31)
 +
  5.0: Contiki>
 +
 
 +
== Understanding the Code ==
 +
 
 +
<source lang="python">
 
  import serial
 
  import serial
ser = serial.Serial(
+
</source>
 +
 
 +
The first line will include the module for the serial port API. This API is provided by pySerial.
 +
 
 +
<source lang="python">
 +
ser = serial.Serial(
 
     port='/dev/ttyUSB0',\
 
     port='/dev/ttyUSB0',\
 
     baudrate=115200,\
 
     baudrate=115200,\
Line 27: Line 105:
 
     bytesize=serial.EIGHTBITS,\
 
     bytesize=serial.EIGHTBITS,\
 
     timeout=0)
 
     timeout=0)
 +
</source>
 +
 +
This next line instantiates an object with the appropriate serial port configuration for communication with the Sky mote. The necessary settings are shown in the table below.
 +
 +
{| class="wikitable" style="margin: auto;"
 +
!colspan="2"|Serial Configuration
 +
|-
 +
| Baud rate || 115200
 +
|-
 +
| Data bits || 8
 +
|-
 +
| Parity || None
 +
|-
 +
| Stop bits || 1
 +
|}
 +
 +
<source lang="python">
 
  print("connected to: " + ser.portstr)
 
  print("connected to: " + ser.portstr)
 +
</soource>
 +
 +
The print statement is simply a sanity check. It just reminds us which port we are connected to.
 +
 +
<source lang="python">
 
  ser.write("help\n");
 
  ser.write("help\n");
while True:
+
</source>
            line = ser.readline();
+
            if line:
+
              print(line),
+
ser.close()
+
  
Note:
+
This line demonstrates how data can be written to the serial port. In this example we send the command "help" to the Sky Shell so that it will send back the list of shell commands it supports.
This code assumes the mote you are using for this tutorial is connected to ''/dev/ttyUSB0''. Please check this using the ''make motelist'' command as shown in the figure below. If the mote you are using is connected to a different device file, please modify the Python code accordingly.
+
  
== Step 2 ==
+
<source lang="python">
 +
while True:
 +
    line = ser.readline();
 +
    if line:
 +
print(line)
 +
</source>
  
Run the Python code using the command:
+
Here, we read data from the serial port indefinitely and print it out, one line at a time. Note that in this case, ''line'' will typically end with a newline.
  
python serial.py
+
To terminate the program, you can use Ctrl + C to send an interrupt signal.
  
You should see output similar to the following:
+
<source lang="python">
connected to: /dev/ttyUSB0
+
  ser.close()
Available commands:
+
</source>
?: shows this help
+
binprint: print binary data in decimal format
+
blink [num]: blink LEDs ([num] times)
+
collect: collect data from the network
+
collect-view-data: sensor data, power consumption, network stats
+
echo <text>: print <text>
+
  energy: print energy profile
+
exit: exit shell
+
hd: print binary data in hexadecimal format
+
help: shows this help
+
kill <command>: stop a specific command
+
killall: stop all running commands
+
mac <onoroff>: turn MAC protocol on (1) or off (0)
+
netcmd <command>: run a command on all nodes in the network
+
nodeid: set node ID
+
null: discard input
+
packetize: put data into one packet
+
power: print power profile
+
powerconv: convert power profile to human readable output
+
powertrace [interval]: turn powertracing on or off, with reporting interval <interval>
+
quit: exit shell
+
randwait <maxtime> <command>: wait for a random time before running a command
+
reboot: reboot the system
+
repeat <num> <time> <command>: run a command every <time> seconds
+
rfchannel <channel>: change CC2420 radio channel (11 - 26)
+
routes: dump route list in binary format
+
send <rexmits>: send data to the collector node, with rexmits hop-by-hop retransmissions
+
sense: print out sensor data
+
senseconv: convert 'sense' data to human readable format
+
size: print the size of the input
+
time [seconds]: output time in binary format, or set time in seconds since 1970
+
timestamp: prepend a timestamp to data
+
txpower <power>: change CC2420 transmission power (0 - 31)
+
5.0: Contiki>
+
  
== Understanding the Code ==
+
This last line never actually gets executed but it shows how the serial resource can be freed.
  
  
 
[[Contiki_tutorials | Back to Contiki Tutorials]]
 
[[Contiki_tutorials | Back to Contiki Tutorials]]

Latest revision as of 19:06, 27 September 2016

Back to Contiki Tutorials

Introduction

In this tutorial you will learn how to programmatically communicate with the Tmote Sky using the Python programming language. This will enable you to do such things as process data from your mote in real-time. In addition, you can send commands or data back to the mote to impact its operation. The possibilities are endless.

Setup

This tutorial was written and tested using the following:

  • Contiki 2.7
  • Python 2.7.3 (and pySerial)
  • Ubuntu 12.04
  • Tmote Sky

Connect the Tmote Sky to your computer using a USB cable and install the Sky Shell example. See the @Sky Shell@ tutorial for instructions on how to do this. We use this example so that we’ll have data to ensure that we are correctly reading and writing to the mote.

Step 1

Create a Python file called serial.py containing the following code:

#!/usr/bin/python
import serial
ser = serial.Serial(
   port='/dev/ttyUSB0',\
   baudrate=115200,\
   parity=serial.PARITY_NONE,\
   stopbits=serial.STOPBITS_ONE,\
   bytesize=serial.EIGHTBITS,\
   timeout=0)
print("connected to: " + ser.portstr)
ser.write("help\n");
while True:
	line = ser.readline();
	if line:
		print(line),
ser.close()

Note: This code assumes the mote you are using for this tutorial is connected to /dev/ttyUSB0. Please check this using the make motelist command as shown in the figure below. If the mote you are using is connected to a different device file, please modify the Python code accordingly.


Sample output of make motelist

Step 2

Run the Python code using the command:

 python serial.py

If you encounter any errors, see the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting

You should see output similar to the following:

 connected to: /dev/ttyUSB0
 Available commands:
 ?: shows this help
 binprint: print binary data in decimal format
 blink [num]: blink LEDs ([num] times)
 collect: collect data from the network
 collect-view-data: sensor data, power consumption, network stats
 echo <text>: print <text>
 energy: print energy profile
 exit: exit shell
 hd: print binary data in hexadecimal format
 help: shows this help
 kill <command>: stop a specific command
 killall: stop all running commands
 mac <onoroff>: turn MAC protocol on (1) or off (0)
 netcmd <command>: run a command on all nodes in the network
 nodeid: set node ID
 null: discard input
 packetize: put data into one packet
 power: print power profile
 powerconv: convert power profile to human readable output
 powertrace [interval]: turn powertracing on or off, with reporting interval <interval>
 quit: exit shell
 randwait <maxtime> <command>: wait for a random time before running a command
 reboot: reboot the system
 repeat <num> 

Understanding the Code

 import serial

The first line will include the module for the serial port API. This API is provided by pySerial.

ser = serial.Serial(
    port='/dev/ttyUSB0',\
    baudrate=115200,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
    timeout=0)

This next line instantiates an object with the appropriate serial port configuration for communication with the Sky mote. The necessary settings are shown in the table below.

Serial Configuration
Baud rate 115200
Data bits 8
Parity None
Stop bits 1
 print("connected to: " + ser.portstr)
</soource>

The print statement is simply a sanity check. It just reminds us which port we are connected to.

<source lang="python">
 ser.write("help\n");

This line demonstrates how data can be written to the serial port. In this example we send the command "help" to the Sky Shell so that it will send back the list of shell commands it supports.

while True:
     line = ser.readline();
     if line:
	print(line)

Here, we read data from the serial port indefinitely and print it out, one line at a time. Note that in this case, line will typically end with a newline.

To terminate the program, you can use Ctrl + C to send an interrupt signal.

 ser.close()

This last line never actually gets executed but it shows how the serial resource can be freed.


Back to Contiki Tutorials