Air Pollutant Index- API Malaysia today. 26/6/2013

Haze/Smog
comments

Air Pollutant Index- API Malaysia/KL is less than 150 ppmv

June 24 2013- 5 pm

Four of the index's pollutant components (i.e., carbon monoxide, ozone, nitrogen dioxide and sulfur dioxide) are reported in ppmv but PM10 particulate matter is reported in μg/m3.

Right now its less than 150 for KL.. But unless it rains, the levels are going up.



Indera Mahkota, Pahang:
< 150 ppmv
Batu Muda, Wilayah Persekutuan:
< 150 ppmv
Kemaman, Terengganu:
< 200 ppmv
Balok Baru, Kuantan, Pahang
< 200 ppmv
Petaling Jaya, Selangor:
< 200 ppmv
Port Dickson, Negeri Sembilan
< 300 ppmv
Bandaraya Melaka, Melaka :
< 400 ppmv
Muar, Johor:
< 550 ppmv

Caution: People are coming up with these anti-virus air purifiers.. These will do nothing to affect the suphur or carbon oxides.. Best way is to steam your room, and then use air-conditioner to condense it back.. The steam will absorb much of the gases present.


Source: NEWS
comments

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)

APPS+=powertrace

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/python
#!/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':
            cpuOverTime[row[3]].append(row[11])

for i in cpuOverTime:
    plt.plot(cpuOverTime[i])
plt.show()
########## 
 

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





comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 7

Part 1 in this series can be found here.
Part 2 can be found here.
Part 3 can be found here.
Part 4 can be found here.
Part 5 can be found here.
Part 6 can be found here.



This post will discuss how to pass a variable from one process to another. This is also the method how to unblock another process from one process. We need this code to invoke that:

process_post(&process_2, the_event_signaller_thing, &any_variable_we_want_to_pass);


"the_event_signaller_thing"will have to be declared as type: static process_event_t and later allocated from process_alloc_event();

Here is a fully functional code for you to copy paste and play around:


#include "contiki.h"
#include < stdio.h >

static process_event_t ourEventIsReady;
/*This will be used to signal the event*/

PROCESS(process_1, "This will pass the variable and post an event");
PROCESS(process_2, "This will receive the variable and wake up");
AUTOSTART_PROCESSES(&process_1,&process_2);

//***********************************************************
PROCESS_THREAD(process_1, ev, data)
{
PROCESS_BEGIN();
static int ourVariable = 99;
/*Make sure this is static or you will get a value
of 0 each time you compile. Luckily it took me no more
than 2 minutes to figure my error*/

ourEventIsReady = process_alloc_event(); 
//Allocate the event

process_post(&process_2, ourEventIsReady, &ourVariable);
//And then send it to process_2..

PROCESS_END();
}
//***********************************************************
/*Process_2 will be blocked until ourEventIsReady is passed to 'ev'*/
PROCESS_THREAD(process_2, ev, data)
{
PROCESS_BEGIN();
PROCESS_WAIT_EVENT_UNTIL(ev == ourEventIsReady);
printf("Variable is %d \n", (*(int*)data));
/*
(*(int*)) is necesaary to convert 'data' into a proper integer.
Argument 'data' type is actually a process_type_t, so 
it will give a warning and display the pointer
if we dont include the *(int*) thing first.
*/

PROCESS_END();
}
/*
As can be seen I didnt use any while() loop.
*/


The output looks like this:





comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 6


Part 1 in this series can be found here.
Part 2 can be found here.
Part 3 can be found here.
Part 4 can be found here.
Part 5 can be found here.


I want to focus on power consumption on this post before I go into the details for networking. It is important to know how your algorithm does on the power consumption side.

Contiki has a software-based power-profiler which can be used for this. Few lines of code have to be added in your program to enable that. But there is also another way.

We will be using MSPSim's power-profiler.

This method wont work on all the mote types, because the tool 'Msp CLI' works for only MSP430 microprocessor series [Reference]

We will be using the Sky Mote Type for this. I used this code for generating a "Multiplication Table of 5" in the Sky Mote for this example.

Now..

Right Click on the Mote in the 'Network' window and then 'Mote Tools' & 'Msp CLI'. A Msp CLI dialog box should open.

Type:

>duty 1 "MSP430 Core.active" CC2420

Press Enter.

The output for my case was:




After reading this and bit of a guess leads me to:

The first column is the percentage of CPU activity. This is generated because of the "MSP430 Core.active" argument.

The second column is the percentage of time for radio power down.
The third column is the percentage of time for radio power up without transmission. (If I am not mistaken)
The fourth column is the percentage of time for radio transmit.
The fifth column is always 0.0 no matter what code I run, so I dont know what that does.


Notice the last 4 should add up to 100.

Here is another example (Source: ...contiki2.6/examples/sky/radio-test.c):



It shows the CPU is active 18.93% of the time.
Radio is off 87.72% of the time.
Radio is on 2.98% without activity
Radio is transmitting 9.3%
Radio shows 0.0 (always; even with a bit of reception).



So how to calculate the power consumption??

Multiply the total simulation time with the % above to get total time for a certain activity.

Then multiply with the standard power consumption which can be obtained from some data sheet.

e.g Data Sheet of Tmote Sky states:

e.g for Radio Transmission

Total_Energy = 19.5 * 3 V * (Activity_Time_in_seconds)
(Remember Power= Voltage x Current)

I dont know why they divide specifically by 4096..They describe it as each second containing 4096 ticks of cycles only. I am still uneducated why, as a Tmote Sky is not 4KHz speed device. May they are just referring to the CC2420 radio. Sigh! Only if things were nicely documented. Not everyone understands English in the same way.

Laters in PART 7

comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 5


Part 1 in this series can be found here.
Part 2 can be found here.
Part 3 can be found here.
Part 4 can be found here.






Let see how to use a timer to display a multiplication table of 5 on the serial port.
#include "contiki.h"
#include < stdio.h >
PROCESS(hello, "Table of 5");
AUTOSTART_PROCESSES(&hello);
PROCESS_THREAD(hello, ev, data)
{
static struct etimer  timer;
static int i;
PROCESS_BEGIN();
while(1)
{
etimer_set(&timer, CLOCK_CONF_SECOND);
PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TIMER);
for(i=1; i < 11; i++)
printf("5 x %d   =  %d\n", i, i*5);
}
PROCESS_END();
}


I deliberately didn't include any comments here. This is where you should stop and write your own comments based on what you understood. If you think the code above doesn't make sense, then I believe you went too fast. Upon successful compilation, the output should continuously scroll. Something similar to this should be displayed:
comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 4

Part 1 in this series can be found here.
Part 2 can be found here.
Part 3 can be found here.



Now we have got our 'Hello World' process running nicely on a mote. Now we are ready to make things more interesting. Let's now focus on the stuff that comes between  PROCESS_BEGIN() and  PROCESS_END().

Presented next is a modified snippet of the source code I took from this website. This is another example of a 'Hello World' process, but it introduces some new concepts related to process blocking and then unblocking via a timer (I am trying to reduce the technical jargon so do excuse me if it may not sound correct).

PROCESS_THREAD(hello_world_process, ev, data)
{
/* variables are declared static to ensure their values are kept
each time the kernel switches back to this process */
static struct etimer timer;  //Code to declare a timer. MEMORIZE
static int count = 0; //Just a counter

PROCESS_BEGIN();


/*Set the length of the timer to one second
In Contiki  (CLOCK_CONF_SECOND is equal to 1 second. */
etimer_set(&timer, CLOCK_CONF_SECOND);



while (1)
{
// This will block the process until a kind of event happens
PROCESS_WAIT_EVENT();

// This specifies the event that we are waiting for to be a timer
if(ev == PROCESS_EVENT_TIMER)
{
// do the process work
printf("Hello, world #%i\n", count);
count ++;

/*By adding 'etimer_reset(&timer);' in the end of the functional code
the process will runs again and again. This will reset the timer.
This is done to loop the event generation mechanism. As can be seen, the
while() loop is just for the event listener.*/
etimer_reset(&timer);

}
} //keep doing this

PROCESS_END();
 
}

An alternative way to reset the timer is just include the initialisation of the timer variable inside the while() loop.
This way we dont have to worry about putting etimer_reset(&timer) at the end of the code. This way the while() loop will take care of both the event listener and the event generator.

while (1)
{
etimer_set(&timer, CLOCK_CONF_SECOND);

PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);
/* The above statement is able to replace the two earlier statements of:
PROCESS_WAIT_EVENT();
if(ev == PROCESS_EVENT_TIMER)
{}
So consider it an alternative. I got confused when I first learnt this so
I hope this makes it easier for the readers.
*/


Sorry I dont indent my code properly. Maybe next time. :)


PART 5
comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 3

Part 1 in this series can be found here.
Part 2 can be found here.


The next question that arises is that can we include additional processes in a file. The answer is Yes! But these processes will be concurrent. This means that they can be executed in parallel.

/*We declare two processes*/
PROCESS(process_1, "The first process");
PROCESS(process_2, "The second process");

/*The next step is to use AUTOSTART_PROCESSES. Luckily we can 
specify both of our processes in a single function call. */
AUTOSTART_PROCESSES(&process_1, &process_2);

/*The process definition for the first one*/
PROCESS_THREAD(process_1, ev, data)
{    
PROCESS_BEGIN();
/* Some Code*/
PROCESS_END();
}

/*The process definition for the second one*/
PROCESS_THREAD(process_2, ev, data)
{
PROCESS_BEGIN();
/* Some Code*/
PROCESS_END();
}


So far so good. But we still haven’t done anything useful. Let's start with a basic hello world being printed. Being printed where? On the serial port. Under Cooja, you can go to Tools> Mote Interface Viewer > (Select your mote) > (In the drop menu) Serial Port.

You can also see it in the 'Mote Output' dialog box.

#include "contiki.h"
#include < stdio .h >
/*Notice I have include the standard I/O file header. This
is for enabling printf functionality */


PROCESS(process2, "Our first Hello World"); //Declare

AUTOSTART_PROCESSES(&process2); //Start


PROCESS_THREAD(process2, ev, data) //Definition
{
PROCESS_BEGIN(); //Here we go

printf("Hello World \n"); //A simple printf 
            
PROCESS_END();

}

After simulating this code, the output looks happy:




See you later in Part 4.

comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 2

The first part goes here


Now that I have understood the concept of threads, I will move on to something called ProtoThreads. Here is the basic code.



//To use protothreads (PT)
//first are foremost you need the header file, 

#include "pt.h"

//Now we have to initialize a PT, which is simply:

struct pt pt; 


//-------------------------------------------------------
//-------------------------------------------------------

//Last time we used PROCESS_THREAD(basic, ev, data) 
//but now we change to PT_THREAD
//'Example' is the name of process being declared
PT_THREAD(example(struct pt *pt)) 
{
PT_BEGIN(pt);
    
    /*
    Have to include code here what we want to do
    This portion lets the protothread wait for events indefinitely.
    It uses an infinite loop
    */

PT_END(pt);
}


/*
In memory constrained systems, traditional multi-threading may 
have a too large memory overhead. Each thread requires its own 
stack that may use large parts of the available memory. 
*/


/*That is where the ProtoThread solution proposed by the creator of Contiki is helpful. ProtoThreads 
 do not require their own stack. 
Each protothread takes two bytes of memory.
One thing to be careful is to limit use of local variables here.
And do not use SWITCH() statement ever.
*/




What I did was save this code as a .c file and dumped it in the Z1 examples folder and tried to compile it on a z1 mote. An error came up:

(Output truncated)
obj_z1/contiki-z1-main.o: In function `print_processes':
/home/user/contiki-2.6/examples/z1/../../platform/z1/./contiki-z1-main.c:169: undefined reference to `autostart_processes'
obj_z1/contiki-z1-main.o: In function `main':
/home/user/contiki-2.6/examples/z1/../../platform/z1/./contiki-z1-main.c:415: undefined reference to `autostart_processes'
collect2: ld returned 1 exit status
rm obj_z1/contiki-z1-main.o basicTemplate_ProtoThreads.co
Process returned error code 2
make: *** [basicTemplate_ProtoThreads.z1] Error 1


I dont know (yet) the working of the internals of Contiki, but I got the code working (compiling incorrectly but still ).

I just added this towards the end

AUTOSTART_PROCESSES(&basic);  

And it worked.. I need to research more why this happens, or if there is a suitable function for protoThreads that can be used to replace, or if I am just missing something in the big picture.


Will update it here once I find out.



Part Three can be found here.
comments

Basic Tutorial for programming in #Contiki Operating System for #IoT| PART 1

During my undergrad I took a course on Operating Systems. It was pretty fascinating to know how operating systems have developed over time and how they worked. Nevertheless, I didn't find much to try to devote to this subject. I was a computer engineering major not in ICT, so my subjects varied all the way from VLSI design to Multimedia technology.

But now I have this great opportunity to learn.

For someone with limited programming experience learning to adapt to programming at this level is not easy. Here I will try to post whatever I am learning for comments, and future notes. Hope I get this all correct. I wont be using advanced technical jargon here, but just enough to make some sense. I will try to keep the description as layman as possible.

Let me start with the basic structure of any code. This code will be run on any individual node, and will have to be downloaded (installed/ported) to a node. This can be done in a simulation and then on a proper hardware device. The easiest way to get started is to download Instant Contiki and run it as a virtual machine. I used Oracle's Virtual Box myself for the virtualisation.


//First are foremost you need this header file

#include "contiki.h"

//Then just like we initialise functions, we need to
//initialize a process

PROCESS(basic, "This is just a basic template");
//The word "basic" is the name of your process which you will 
//be using in later functions. Basically
//it is the declaration of the process
//The text in "" is for a short description


AUTOSTART_PROCESSES(&basic);  //Start the process

PROCESS_THREAD(basic, ev, data) //Here goes the definition of the 
//process

{
PROCESS_BEGIN();
//Always try to put any code after this.


//Can initialise stuff using static keyword. 
//Will tell you why in the next block of comments.

/*
Have to include code here what we actually want to do
This code in this portion lets the process wait for events indefinitely
by compulsorily including code for an infinite loop
This is something called as a blocking macro.
Actually, here the process returns to the kernel (to enable 
the kernel to call others) 
and process enters a blocked state. 
When an event is posted, the kernel will 
call back this process which will jump right
after it returned before. 
This may sometimes change the values of the local variables, 
that’s why we are supposed to define them as static. 
*/

PROCESS_END();
//End the process. Notice no arguments.

}


The structure of the blocking macro will be roughly like this:


while(1)

{

//Wait for some external event
//Or wait for a timer to finish
//Or wait for some internal event

//And then do something..

} 

Now the most important question is how to use this code.. If you save the above code on your desktop, and try to simulate it on Cooja on lets say a 'Z1 mote', you will get this error:


make basicTemplate_Threads.z1 TARGET=z1
make: *** No rule to make target `basicTemplate_Threads.z1'.  Stop.
Process returned error code 2



So to get your code working, just copy the source file to the Z1 folder located at
Home/contiki-2.6/examples/z1 and then use that to compile on your simulated z1 mote.

For other mote types, just copy to the corresponding folders. This is the easiest way to get it working.


Part 2 of this series can be found here.
comments