Difference between revisions of "Sensor acquisition"

From Contiki
Jump to: navigation, search
(Step 1)
 
(18 intermediate revisions by 4 users not shown)
Line 7: Line 7:
 
This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.
 
This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.
  
== How to start an application on Contiki ==
+
== How to start an application in Contiki ==
  
 
Every process should start with the PROCESS macro. It takes two arguments
 
Every process 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.
                            <code><nowiki> PROCESS(name,strname) </nowiki></code>
 
  
 +
  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.
 
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.
 
*&name: Reference to the process name.
                    <code><nowiki> AUTOSTART_PROCESS(struct process &) </nowiki></code>
 
  
 +
  AUTOSTART_PROCESS(struct process &)
  
  
Line 25: Line 25:
 
*name: The variable name of the process structure.
 
*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_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.
              <code><nowiki> PROCESS_THREAD(name, process_event_t, process_data_t) </nowiki></code>
 
  
 +
  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.
 
Then comes the PROCESS_BEGIN macro. This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.
                            <code><nowiki>PROCESS_BEGIN() </nowiki></code>
 
  
 +
  PROCESS_BEGIN()
  
  
 
Then we write the set of C statements as per the requirement of the application.
 
Then we write the set of C statements as per the requirement of the application.
 
  
  
 
At the end we use another macro 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.
 
At the end we use another macro 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.
                            <code><nowiki> PROCESS_END() </nowiki></code>
 
  
 +
  PROCESS_END()
  
 +
== Generic Template for Creating Application in Contiki  ==
  
== Generic Template for Creating Application on Contiki  ==
+
<source lang="c">
 
+
<code><nowiki>
+
 
+
 
PROCESS(name,strname);
 
PROCESS(name,strname);
 
AUTOSTART_PROCESSES(struct process &);
 
AUTOSTART_PROCESSES(struct process &);
  
PROCESS_THREAD(sensor_acq_process,ev,data)
+
PROCESS_THREAD(name, process_event_t, process_data_t)
 
{  
 
{  
  
----Initialization of requires variables----
+
----Initialization of required variables----
 
+
  
 
PROCESS_BEGIN();
 
PROCESS_BEGIN();
 
  
 
---Set of C statements---
 
---Set of C statements---
 
  
 
PROCESS_END();
 
PROCESS_END();
  
 
}
 
}
 
+
</source>
</nowiki></code>
+
 
+
  
 
== Sensor Acquisition Program on the Tmote Sky! ==
 
== Sensor Acquisition Program on the Tmote Sky! ==
Line 75: Line 67:
 
Using the opened terminal window compile and upload the Sensor Acquisition program on the Tmote Sky.
 
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)
+
  make TARGET=sky savetarget    (This saves the target for any future compilations)
:: <code>make sensor-acq.upload</code>   (This will upload the code on the Tmote Sky)
+
  make sensor-acq.upload    (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)
+
  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
 
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting
  
Line 84: Line 77:
 
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>
+
  Temperature=28.04 C (6764)
:: <code>Humidity=73.20% (2259)</code>
+
  Humidity=73.20% (2259)
:: <code>Light=38.26 lux (94)</code>
+
  Light=38.26 lux (94)
  
 
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
 
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
Line 94: Line 87:
  
 
Press Ctrl-C to quit.
 
Press Ctrl-C to quit.
 
 
  
 
== Code ==
 
== Code ==
  
<code><nowiki>
+
<source lang="c">
 
+
 
PROCESS(sensor_acq_process,"Sensor Acquisition");
 
PROCESS(sensor_acq_process,"Sensor Acquisition");
 
AUTOSTART_PROCESSES(&sensor_acq_process);
 
AUTOSTART_PROCESSES(&sensor_acq_process);
Line 108: Line 98:
 
       static struct etimer et;
 
       static struct etimer et;
 
       static int val;
 
       static int val;
      static struct sensors_sensor *sensor;
 
 
       static float s = 0;
 
       static float s = 0;
 
       static int dec;
 
       static int dec;
Line 141: Line 130:
 
         dec = s;
 
         dec = s;
 
         frac = s - dec;
 
         frac = s - dec;
         printf("Humidity=%d.%02u % (%d)\n", dec, (unsigned int)(frac * 100),val);               
+
         printf("Humidity=%d.%02u %% (%d)\n", dec, (unsigned int)(frac * 100),val);               
 
           }
 
           }
  
Line 162: Line 151:
  
 
}
 
}
 
+
</source>
</nowiki></code>
+
  
 
== Understanding the Code ==
 
== Understanding the Code ==
  
::<code>etimer_set(&et, CLOCK_SECOND * 2);</code>
+
The following piece of code sets the timer to repeat the iterations every 2 seconds.
This will set the timer to repeat the iterations every 2 seconds.
+
  
 +
<source lang="c">
 +
etimer_set(&et, CLOCK_SECOND * 2);
 +
</source>
  
::<code>SENSORS_ACTIVATE(light_sensor);</code>
+
We need to activate light_sensor for measuring the light intensity and the sht11_sensor for the measurement of temperature and humidity.
::<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.
+
  
 +
<source lang="c">
 +
SENSORS_ACTIVATE(light_sensor);
 +
SENSORS_ACTIVATE(sht11_sensor);
 +
</source>
  
::<code>val = sht11_sensor.value(SHT11_SENSOR_TEMP);</code>
+
The following line is used to capture the actual sensor value,
Here we are capturing the actual sensor value.
+
  
 +
<source lang="c">
 +
val = sht11_sensor.value(SHT11_SENSOR_TEMP);
 +
</source>
  
::<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
 
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
  
 +
<source lang="c">
 +
s = ((0.01*val) - 39.60);
 +
</source>
  
::<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.
 
After we are done with the calculations, we need to deactivate the sensors.
  
 +
<source lang="c">
 +
SENSORS_DEACTIVATE(light_sensor);
 +
SENSORS_DEACTIVATE(sht11_sensor);
 +
</source>
  
 +
== Source Code ==
  
[[Contiki_tutorials | Back to Contiki Tutorials]]
+
To download a copy of <code><nowiki>sensor-acq.c</nowiki></code>, click [https://drive.google.com/file/d/0B0-za4nfpimxc2NDX2xzdEhIeGc/view?usp=sharing here]
  
 +
[[Media:sensor-acq.docx]]
 +
 +
Copy the downloaded .c file in the <code><nowiki>/contiki/examples/rime</nowiki></code> folder and follow the steps mentioned above.
 +
 +
 +
 +
[[Contiki_tutorials | Back to Contiki Tutorials]]
  
 
Edited by : Nitin
 
Edited by : Nitin
 +
 +
Source Code added by : Mugdhe, Samarth

Latest revision as of 21:13, 27 September 2016

Back to Contiki Tutorials

Introduction

This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.

How to start an application in Contiki

Every process 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 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()

Generic Template for Creating Application in Contiki

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();

}

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.

 make TARGET=sky savetarget    (This saves the target for any future compilations)
 make sensor-acq.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

Step 2

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

 Temperature=28.04 C (6764)
 Humidity=73.20% (2259)
 Light=38.26 lux (94)

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

Press Ctrl-C to quit.

Code

PROCESS(sensor_acq_process,"Sensor Acquisition");
AUTOSTART_PROCESSES(&sensor_acq_process);

PROCESS_THREAD(sensor_acq_process,ev,data)
{ 
      static struct etimer et;
      static int val;
      static float s = 0;
      static int dec;
      static float frac;

      PROCESS_BEGIN();

      printf("Starting Sensor Example.\n");
      
      while(1)
      {
	   etimer_set(&et, CLOCK_SECOND * 2);
	   SENSORS_ACTIVATE(light_sensor);
      	   SENSORS_ACTIVATE(sht11_sensor);
        
	   PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));


           val = sht11_sensor.value(SHT11_SENSOR_TEMP);
      	   if(val != -1) 
      	   {
		s= ((0.01*val) - 39.60);
      	  	dec = s;
      	  	frac = s - dec;
      	  	printf("\nTemperature=%d.%02u C (%d)\n", dec, (unsigned int)(frac * 100),val);               
           }

	   val=sht11_sensor.value(SHT11_SENSOR_HUMIDITY);
	   if(val != -1) 
      	   {
		s= (((0.0405*val) - 4) + ((-2.8 * 0.000001)*(pow(val,2))));  
      	  	dec = s;
      	  	frac = s - dec;
      	  	printf("Humidity=%d.%02u %% (%d)\n", dec, (unsigned int)(frac * 100),val);               
           }

           val = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);
      	   if(val != -1) 
      	   {
      		s = (float)(val * 0.4071);
      	  	dec = s;
      	  	frac = s - dec;
      	  	printf("Light=%d.%02u lux (%d)\n", dec, (unsigned int)(frac * 100),val);               
           } 
	
	   etimer_reset(&et);
    	   SENSORS_DEACTIVATE(light_sensor);
    	   SENSORS_DEACTIVATE(sht11_sensor);

      } //end of while
    
      PROCESS_END();

}

Understanding the Code

The following piece of code sets the timer to repeat the iterations every 2 seconds.

etimer_set(&et, CLOCK_SECOND * 2);

We need to activate light_sensor for measuring the light intensity and the sht11_sensor for the measurement of temperature and humidity.

SENSORS_ACTIVATE(light_sensor);
SENSORS_ACTIVATE(sht11_sensor);

The following line is used to capture the actual sensor value,

val = sht11_sensor.value(SHT11_SENSOR_TEMP);

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

s = ((0.01*val) - 39.60);

After we are done with the calculations, we need to deactivate the sensors.

SENSORS_DEACTIVATE(light_sensor);
SENSORS_DEACTIVATE(sht11_sensor);

Source Code

To download a copy of sensor-acq.c, click here

Media:sensor-acq.docx

Copy the downloaded .c file in the /contiki/examples/rime folder and follow the steps mentioned above.


Back to Contiki Tutorials

Edited by : Nitin

Source Code added by : Mugdhe, Samarth