Difference between revisions of "Build your own application in Contiki"

From Contiki
Jump to: navigation, search
(How to start an application in Contiki)
 
(21 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
== Introduction ==
 
== Introduction ==
  
This tutorial help you understand how to build you own application in Contiki.
+
This tutorial help you understand how to build you own application in Contiki. Section#2 '''How to start an application in Contiki''' walk you through the complete process of writing you own application in Contiki. Section#3 '''Generic structure of Application in Contiki''' gives a generic skeleton of any application in contiki.
  
 
== How to start an application in Contiki ==
 
== How to start an application in Contiki ==
Line 24: Line 24:
  
 
=== Step 3 ===
 
=== Step 3 ===
Write C code. Every process in Contiki should start with the PROCESS macro. It takes two arguments
+
'''Write my_first_app.c'''
 +
 
 +
<code><nowiki>
 +
<Header Files>
 +
 
 +
  PROCESS(my_first_app_process,"My_First_App");
 +
  AUTOSTART_PROCESSES(&my_first_app_process);
 +
 
 +
  PROCESS_THREAD(my_first_app_process,ev,data)
 +
  {
 +
      /* Declare variables required */
 +
      static int i=652;       
 +
 
 +
      /* Begin Process */
 +
      PROCESS_BEGIN();         
 +
 
 +
      /* Set of C statement(s) */
 +
      printf("EE-%d is an awesome course at USC\n",i);           
 +
     
 +
      /* Process End */
 +
      PROCESS_END();           
 +
  }
 +
</nowiki></code>
 +
 
 +
Every process in Contiki should start with the PROCESS macro. It takes two arguments
 
*name: The variable name of the process structure.
 
*name: The variable name of the process structure.
 
*strname: The string representation of the process name.
 
*strname: The string representation of the process name.
Line 56: Line 80:
 
                             <code><nowiki> PROCESS_END() </nowiki></code>
 
                             <code><nowiki> PROCESS_END() </nowiki></code>
  
== Generic C code for Application in Contiki  ==
+
=== Step 4 ===
 +
'''Makefile''' - Create makefile in the same folder (contiki/exmples/my_first_app)
  
 
  <code><nowiki>
 
  <code><nowiki>
 +
CONTIKI_PROJECT = my_first_app
 +
all: $(CONTIKI_PROJECT)
  
<Header Files>
+
#UIP_CONF_IPV6=1
  
PROCESS(name,strname);
+
CONTIKI = ../..
AUTOSTART_PROCESSES(struct process &);
+
include $(CONTIKI)/Makefile.include
  
PROCESS_THREAD(name, process_event_t, process_data_t)
+
</nowiki></code>
{
+
  
----Initialization of required variables----
+
=== Step 5 ===
 +
Create a file Makefile.target and then type
 +
TARGET = sky
  
 +
=== Step 6 ===
 +
'''Compilation''' - Use terminal to go to "contiki/examples/my_first_app" directory. Once you are in this directory type "make". This will compile your code and generates all the supporting files like .csc file, symbols.c, symbols.h etc.
  
PROCESS_BEGIN();
 
  
 +
=== Step 7 ===
  
---Set of C statements---
+
'''Upload the firmware on Tmote Sky''' - Plug the Tmote Sky in your computer. Then use the terminal window to go to contiki/examples/my_first_app  and then write the following commands to upload your program on the Tmote Sky.  
 
+
 
+
PROCESS_END();
+
 
+
}
+
 
+
</nowiki></code>
+
 
+
== Sensor Acquisition Program on the Tmote Sky! ==
+
 
+
=== Step 1 ===
+
 
+
Using the opened terminal window compile and upload the Sensor Acquisition program on the Tmote Sky.
+
  
 
:: <code>make TARGET=sky savetarget</code>    (This save the target for any future compilations)
 
:: <code>make TARGET=sky savetarget</code>    (This save the target for any future compilations)
:: <code>make sensor-acq.upload</code>    (This will upload the code on the Tmote Sky)
+
:: <code>make my_first_app.upload</code>    (This will upload the code on the Tmote Sky)
 
:: <code>make login</code>    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)
 
:: <code>make login</code>    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)
 
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting
 
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting
  
=== Step 2 ===
 
  
 
Press the reset button on the Tmote Sky. The following message will appear on the terminal window  
 
Press the reset button on the Tmote Sky. The following message will appear on the terminal window  
  
:: <code>Temperature=28.04 C (6764)</code>
+
:: <code>EE-652 is an awesome course at USC</code>
:: <code>Humidity=73.20% (2259)</code>
+
:: <code>Light=38.26 lux (94)</code>
+
  
Values inside the round brackets (ex 6764, 2259 and 94) are the actual sensor values and the calibrated values can be obtained by performing calculations on these sensor values. You can find the calculations at
 
http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example
 
  
=== Step 3 ===
+
Then Press CTRL-C to quit.
  
Press Ctrl-C to quit.
 
  
  
 
+
== Generic structure of Application in Contiki  ==
== Code ==
+
  
 
  <code><nowiki>
 
  <code><nowiki>
Line 117: Line 127:
 
<Header Files>
 
<Header Files>
  
PROCESS(my_first_app_process,"My_First_App");
+
PROCESS(name,strname);
AUTOSTART_PROCESSES(&my_first_app_process);
+
AUTOSTART_PROCESSES(struct process &);
  
PROCESS_THREAD(my_first_app_process,ev,data)
+
PROCESS_THREAD(name, process_event_t, process_data_t)
 
{  
 
{  
      /* Declare variables required */
 
      static int i=652;       
 
  
      /* Begin Process */
+
----Initialization of required variables----
      PROCESS_BEGIN();         
+
  
      /* Set of C statement(s) */
 
      printf("EE-%d is an awesome course at USC\n");           
 
     
 
      /* Process End */
 
      PROCESS_END();           
 
}
 
  
</nowiki></code>
+
PROCESS_BEGIN();
  
== Understanding the Code ==
 
  
::<code>etimer_set(&et, CLOCK_SECOND * 2);</code>
+
---Set of C statements---
This will set the timer to repeat the iterations every 2 seconds.
+
  
  
::<code>SENSORS_ACTIVATE(light_sensor);</code>
+
PROCESS_END();
::<code>SENSORS_ACTIVATE(sht11_sensor);</code>
+
We need to activate light_sensor for measuring the light intensity and sht11_sensor for the measurement of temperature and humidity.
+
  
 +
}
  
::<code>val = sht11_sensor.value(SHT11_SENSOR_TEMP);</code>
+
</nowiki></code>
Here we are capturing the actual sensor value.
+
 
+
 
+
::<code>s = ((0.01*val) - 39.60);</code>
+
We need to calibrate the sensor values by doing some calculations. You can find the calculations here: http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example
+
 
+
 
+
::<code>SENSORS_DEACTIVATE(light_sensor);</code>
+
::<code>SENSORS_DEACTIVATE(sht11_sensor);</code>
+
After we are done with the calculations, we need to deactivate the sensors.
+
 
+
  
  

Latest revision as of 02:15, 17 June 2016

Back to Contiki Tutorials

Introduction

This tutorial help you understand how to build you own application in Contiki. Section#2 How to start an application in Contiki walk you through the complete process of writing you own application in Contiki. Section#3 Generic structure of Application in Contiki gives a generic skeleton of any application in contiki.

How to start an application in Contiki

Step 1

Go to the directory contiki/examples and make new sub directory named my_first_app


Byaic 1.png


Step 2

Now create a new C file named my_first_app.c


Byaic 2.png


Step 3

Write my_first_app.c


 <Header Files>

  PROCESS(my_first_app_process,"My_First_App");
  AUTOSTART_PROCESSES(&my_first_app_process);

  PROCESS_THREAD(my_first_app_process,ev,data)
  { 
      /* Declare variables required */
      static int i=652;         

      /* Begin Process */
      PROCESS_BEGIN();          

      /* Set of C statement(s) */
      printf("EE-%d is an awesome course at USC\n",i);             
      
      /* Process End */
      PROCESS_END();            
  }
 

Every process in Contiki should start with the PROCESS macro. It takes two arguments

  • name: The variable name of the process structure.
  • strname: The string representation of the process name.
                            PROCESS(name,strname) 


Then comes another macro AUTOSTART_PROCESS(struct process &). AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.

  • &name: Reference to the process name.
                     AUTOSTART_PROCESS(struct process &) 


Then we call the PROCESS_THREAD function. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system.Each process in the module requires an event handler under the PROCESS_THREAD macro.

  • name: The variable name of the process structure.
  • process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.
               PROCESS_THREAD(name, process_event_t, process_data_t) 


Then comes the PROCESS_BEGIN macro. This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.

                            PROCESS_BEGIN() 


Then we write the set of C statements as per the requirement of the application.


At the end we use another macro called PROCESS_END. This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached.

                            PROCESS_END() 

Step 4

Makefile - Create makefile in the same folder (contiki/exmples/my_first_app)


 CONTIKI_PROJECT = my_first_app
 all: $(CONTIKI_PROJECT)

 #UIP_CONF_IPV6=1

 CONTIKI = ../..
 include $(CONTIKI)/Makefile.include

 

Step 5

Create a file Makefile.target and then type TARGET = sky

Step 6

Compilation - Use terminal to go to "contiki/examples/my_first_app" directory. Once you are in this directory type "make". This will compile your code and generates all the supporting files like .csc file, symbols.c, symbols.h etc.


Step 7

Upload the firmware on Tmote Sky - Plug the Tmote Sky in your computer. Then use the terminal window to go to contiki/examples/my_first_app and then write the following commands to upload your program on the Tmote Sky.

make TARGET=sky savetarget (This save the target for any future compilations)
make my_first_app.upload (This will upload the code on the Tmote Sky)
make login (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)

See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting


Press the reset button on the Tmote Sky. The following message will appear on the terminal window

EE-652 is an awesome course at USC


Then Press CTRL-C to quit.


Generic structure of Application in Contiki



<Header Files>

PROCESS(name,strname);
AUTOSTART_PROCESSES(struct process &);

PROCESS_THREAD(name, process_event_t, process_data_t)
{ 

----Initialization of required variables----


PROCESS_BEGIN();


---Set of C statements---


PROCESS_END();

}

 


Back to Contiki Tutorials


Edited by : Nitin