Monday, June 17, 2013

Basic Tutorial for using the PowerTrace Tool in Cooja and Graphing the Output. #Contiki #IoT

We will be using a tool called PowerTrace. This tool is believed to be 94% accurate to determine the power consumption for many functions such as during cpu usage, radio transmission, radio listening etc.

First we import the matplotlib to enable functionality for creating a nice graph using Python. I am assuming Python is present in your system:

sudo apt-get install python-matplotlib

To include the functionality of PowerTrace in your code. Simply add the following line after "PROCESS_BEGIN()" for your code.

powertrace_start(CLOCK_SECOND * 2); 

Dont forget to include the header file:

#include "powertrace.h"

Next, go to file called "Makefile" in your working folder, and add the following line (near the top would be good)


After running the simulation, it generate a lot of powertrace data on the mote output window (roughly every two seconds) outputting a wide variety of number as shown.

These values are actually:

str, clock_time(), P ( rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1] ), seqno, all_cpu, all_lpm, all_transmit, all_listen, all_idle_transmit, all_idle_listen, cpu, lpm, transmit, listen, idle_transmit, idle_listen, followed by some mathematical numbers..

Check Line 116 here for other details.

We save this data to a text file "loglistener.txt" on the Desktop (Check the File menu for the mote output window.)

Run the following python script on the Desktop to plot CPU vs Time (as an example):

#!/usr/bin/env python

import csv
import matplotlib.pyplot as plt

# for P lines
#0-> str,
#1 -> clock_time(),2-> P, 3->rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1], 4-> seqno,
#5 -> all_cpu,6-> all_lpm,7-> all_transmit,8-> all_listen,9-> all_idle_transmit,10-> all_idle_listen,
#11->cpu,12-> lpm,13-> transmit,14-> listen, 15 ->idle_transmit, 16 -> idle_listen, [RADIO STATISTICS...]

from collections import defaultdict
cpuOverTime =  defaultdict(list)

with open('loglistener.txt', 'rb') as f:
    reader = csv.reader(f,delimiter=' ')
    for row in reader:
        if row[2] is 'P':

for i in cpuOverTime:

If everything goes well, a nice looking graph should come up.