SlideShare a Scribd company logo
15LLP108_Demo4_LedBlinking.pdf
1. Introduction
In Demo3, we have learned how to read sensor values of light,
temperature and humidity of a node
and output these values to the console. In this demonstration,
we will use the code from Demo3 and
learn how to turn on/off the LEDs and make them blinking
regularly on the sensor node XM1000,
meanwhile to count how many times the LED has blinked and
output the count to the console.
2. Timer
In order to make the blue LED on the XM1000 sensor node to
blink in every half second (i.e. On 0.5S
and Off 0.5S), we also need a timer. Follow the instructions in
Demo3 for configure and reset a timer.
We aslo need to create an infinite while() loop so that it runs
our functions repeatedly, such as
counting the times the LED has blinked, output the counter’s
value and actually turn on or off the
LEDs to make it blinking.
Please follow timer and while() loop structure in Demo3.
3. LED Blinking
To get access to the LED functionalities in Contiki, we need to
include the LED header file in the
source code:
#include "leds.h" // file is in directory
/home/user/contiki/core/dev
After the process begin, we have to initialise the LEDs on the
sensor node by calling the following
function:
leds_init(); // Initialise the LEDs
And finally we can turn on, off, or blink the LEDs by the
following functions:
void leds_on(unsigned char leds);
void leds_off(unsigned char leds);
void leds_toggle(unsigned char leds);
void leds_invert(unsigned char leds);
For example, if you want to blink the Blue LED, yon need to
call the toggle function as:
void leds_toggle(LEDS_BLUE); // Toggle the blue LED
4. Exercise
Modify the program from Demo3 with periodic timer to make
the BLUE led blinking in every half
second, also to count the blinking times and output the counted
number to the console.
Can your change the code so that the BLUE LED is lighted for 1
second and off for 0.5 second
periodically?
15LLP108 – Internet of Things and Applications
Lab Session 2: Demo 4 – LED Blinking
Prepared by Xiyu Shi
5. Source code
Here is the source code for reference
#include "contiki.h"
#include "leds.h"
#include <stdio.h> /* for printf() */
static struct etimer timer;
/*___________________________________________________
_*/
PROCESS(led_blinking_process, "LED Blinking Process");
PROCESS(LED_process, "LED process");
AUTOSTART_PROCESSES(&LED_process);
/*___________________________________________________
_*/
PROCESS_THREAD(LED_process, ev, data)
{
static int count = 0;
PROCESS_BEGIN();
etimer_set(&timer, CLOCK_CONF_SECOND/2); // 0.5S timer
leds_init(); // intialise the LEDs
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TI
MER); // wait for timer event
count++; // count the blinking times
process_start(&led_blinking_process, NULL); // to blink the
BLUE Led
printf("Count: %dn", count); // output the counter number to
console
etimer_reset(&timer); // reset the timer
}
PROCESS_END();
}
/*___________________________________________________
_*/
PROCESS_THREAD(led_blinking_process, ev, data)
{
PROCESS_BEGIN();
leds_toggle(LEDS_BLUE); // Blinking the Blue LED
PROCESS_END();
}
15LLP108_Demo2_HelloWorld_CodeExample.pdf
1. Introduction
In this Demonstration, we are going to code and run our first
application on the XM1000 sensor
node. As in any programming language, we start our
demonstration with a “Hello World” program
on a sensor node that displays Hello World on the console. We
have the following goals for this
demonstration:
Code and understand Contiki programming in C
Compile and deploy code on a sensor node
Connect the sensor node to output console for information
display
2. Hello World! Code Example
Figure 1 Source code of hello-world.c
Here we give a description of the example code line by line:
15LLP108 – Internet of Things and Applications
Lab Session 1: Demo 2 – Hello World!
Prepared by Xiyu Shi
Line 1: the Contiki header file, contiki.h, is included. This will
include the Contiki OS into the compile
program and allow to access the scheduling and abstraction of
APIs of Contiki. Contiki follows the
Protothreads concurrent programming model with a low-
overhead.
Line 3: includes the standard input/output library needed to
write to the standard output.
Line 7: defines the processes to be executed and included
during runtime. In this case we include
only one process; the hello_world_process. It is possible and
usually the case to define several
concurrent processes, one for processing and collecting data,
one for transmitting or receiving data.
Line 8: define which process to start when the sensor node is
started up (i.e. power on or reset)
Line 11: start the definition of a process called
“hellow_world_process”.
Line 12 and 18: all codes of a process must be enclosed within a
pair of bracket.
Line 13: declare the beginning of the process
Line 17: declare the end of the process
Line 15: is where the node really does what I want it to do, to
output “Hello World” to the console
via the printf() function. Most of the Contiki functions rely on
standard-C.
3. The Makefile
In Linux-alike OS, we need “Makefile” to tell the OS how to
compile the program for a specific
platform or hardware/software configuration. Contiki relies on
two Makefiles to for compilation: the
file “Makefile.target” (Fig. 2) defines the platform the code has
to be compiled for, and the file “Makefile”
(Fig. 3) tells the compiler where the Contiki header files can be
found.
Figure 2 Makefile.target
Figure 3 Makefile
4. Exercise by Compiling and Running on the Sensor Node
Create the c source file and the two Makefiles in fold
“/home/user/contiki/examples/demo1” and run the
following commands:
make TARGET=xm1000 savetarget
make
make hello-world.upload
make login
Or simple follow the instructions in document “15LLP108-
Demo1_HelloWorld_HowToCompile”.
Bonus: Extend your code to print some random numbers
between 0 and 1.
15LLP108_Demo1_HelloWorld_HowToCompile.pdf
In this Demonstration, we will learn how to compile, upload and
run a simple node application on
the Contiki platform.
1. Enter the example code subdirectory
Open a Linux terminal window to run commands.
cd contiki/example/hello-world
2. Compile the hello-world project
make TARGET=xm1000 savetarget
make // this will take several seconds
3. Connect the Node to your PC/Laptop
a. Connect the XM1000 mote node to one of the USB socket on
the PC or laptop. Wait for the
completion of software/drivers for the node on Windows.
b. A popup window is displayed on the WMware Player to tell
you how to install the device
node on the virtual machine. Follow the advice:
c. On the top-left WMware Player windows menu list, clicking
“Player”->”Removable Devices”-
>”Future Devices MTM-XM1000MSP”->”Connect”
d. A window message is poped up, click “OK”
4. Upload to the node
Get into hello-world project directory and running the following
command:
make hello-world.upload
If error happened, such as “could not open port /dev/ttyUSB0:
[Errno 13] Permission denied:
'/dev/ttyUSB0'”, it means that the node device is not in “write”
mode. Changing the write
permission by running: “sudo chmod 777 /dev/ttyUSB0”, when
promoted a password, input “user”.
Now run the upload to programming the node device again by
“make hello-world.upload”. The
program code will be programmed to the xm1000 node device.
You will notice that the red and
green LEDs are flashing during the programming.
5. Run the application
a. Once the application is uploaded to the node, login to the
console of the connected sensor
node:
make login
15LLP108 – Internet of Things and Applications
Lab Session 1: Demo 1 – How to Compile an Application on
Contiki
Prepared by Xiyu Shi
The console will display the following message:
using saved target 'xm1000'
../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0
connecting to /dev/ttyUSB0 (115200) [OK]
b. Now pressing the Red button on the sensor device and see the
output of the console. It will
show something like this:
Rime started with address 43.84
MAC 2b:54:00:00:00:00:00:00 Contiki-2.6 started. Node id is
not set.
CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26
Starting 'Hello world process'
Hello, world
Notice that the last line printed on the console, which is what
the device is programmed to do.
15LLP108_Demo5_MultiThreading.pdf
1. Introduction
The aim of this demonstration is to show how multi-threads can
be implemented for complex tasks.
You will develop a temperature indicator, which is able to
measure the temperature 4 times every
second, to average them and to print the average temperature
through another thread process.
You need to have two threads in the application, a measurement
thread and a print thread. The
main thread is used to measure the temperature four times in a
second, then to average the four
measurements and pass the value to the print process thread.
2. Passing data from one process thread to another
In order to pass data from one process thread to another you
have to:
a. Remember every process thread is defined as:
PROCESS_THREAD(A_process, ev, data)
where the argument “ev” is the event that passed to this thread
from its parent thread and
the “data” argument is the data that passed to this thread from
its parent thread.
b. Initialise the storage
event_data_ready = process_alloc_event();
c. Post the measurement data to another thread called
print_process:
Process_post(&print_process, event_data_ready,
&measurement);
d. In another thread (in this case it is print_process), wait for
event to occur:
PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready);
e. And get data from the process thread input argument "data".
The application structure is shown in Figure 1, in next page.
3. Exercise
To read the temperature, you may need to look at Demo3 again
and use the following function:
sht11_sensor.value(SHT11_SENSOR_TEMP);
You need also to include the relevant header files in your code.
15LLP108 – Internet of Things and Applications
Lab Session 3: Demo 5 – Multi-threading
Prepared by Xiyu Shi
Figure 1 Multi-threads program structure
4. Source code
Here is the source code for reference
#include "contiki.h"
#include <stdio.h> /* For printf() */
/* Include temperature sensor driver */
#include "dev/sht11-sensor.h"
/* Number of values used in averaging process */
#define MAX_VALUES 4
/* Variables: the application specific event value */
static process_event_t event_data_ready;
/* digits before decimal point */
unsigned short d1(float f)
{
return((unsigned short)f);
}
/* digits after decimal point */
unsigned short d2(float f)
{
return(1000*(f-d1(f)));
}
/*------------------------------------------------------------------------
---*/
/* Declare the two processes */
PROCESS(temp_process, "Temperature process");
PROCESS(print_process, "Print process");
/* the temperature measurement processes is started
automatically at beginning */
AUTOSTART_PROCESSES(&temp_process, &print_process);
/*------------------------------------------------------------------------
---*/
/* Implementation of the first process thread, the temperature
measurement*/
PROCESS_THREAD(temp_process, ev, data)
{
/* Variables are declared static to ensure their values are kept
*/
/* between kernel calls. */
static struct etimer timer;
static int count = 0;
static float average = 0, valid_measure = 0;
/* This variable is recomputed at every run, therefore it is
not */
/* necessary to declare them static. */
float measure;
/* Any process must start with this. */
PROCESS_BEGIN();
/* Allocate the required event */
event_data_ready = process_alloc_event();
/* Initialise the temperature sensor */
SENSORS_ACTIVATE(sht11_sensor);
/* Initialise variables */
count = 0;
average = 0;
valid_measure = 0;
measure = 0;
/* Set the etimer module to generate an event in 0.25
second. */
etimer_set(&timer, CLOCK_CONF_SECOND/4);
while (1)
{
/* Wait here for the timer to expire */
PROCESS_WAIT_EVENT_UNTIL(ev ==
PROCESS_EVENT_TIMER);
/* Get temperature measurement and convert it to degrees */
measure = 0.01 *
sht11_sensor.value(SHT11_SENSOR_TEMP) - 39.6;
/* Sum temperature measurements */
average += measure;
/* Count how many temperature measurement we have summed
up */
count++;
if (count == MAX_VALUES)
{
/* Average the sum and store */
valid_measure = average / MAX_VALUES;
/* Reset variables */
average = 0;
count = 0;
/* Post an event to the print process */
/* and pass a pointer to the last measure as data */
process_post(&print_process, event_data_ready,
&valid_measure);
}
/* Reset the timer so it will generate another event */
etimer_reset(&timer);
}
/* Any process must end with this, even if it is never
reached. */
PROCESS_END();
}
/*------------------------------------------------------------------------
---*/
/* Implementation of the second process, the print temperature
thread */
PROCESS_THREAD(print_process, ev, data)
{
PROCESS_BEGIN();
while (1)
{
/* Wait until we get a data_ready event */
PROCESS_WAIT_EVENT_UNTIL(ev ==
event_data_ready);
/* Use 'data' variable to retrieve data and then display it
*/
printf("temperature = %u.%un", d1(*(float *)data),
d2(*(float *)data));
}
PROCESS_END();
}
/*------------------------------------------------------------------------
---*/
Assignment
Resources for Experiment
http://www.advanticsys.com/shop/asxm1000-p-24.html
Related Work.
http://www.treehugger.com/gadgets/smart-garden-system-
monitors-and-tracks-changing-weather-and-soil-conditions.html
http://www.treehugger.com/gadgets/kickstarter-home-depot-
edyn-smart-garden-sensor-brings-iot-your-garden.html
Ad

More Related Content

Similar to 15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx (20)

Arduino Teaching Program
Arduino Teaching ProgramArduino Teaching Program
Arduino Teaching Program
Max Kleiner
 
Please look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docxPlease look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
VcTrn1
 
Readme
ReadmeReadme
Readme
rec2006
 
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery BoardProgramming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Gaurav Verma
 
Prática de Studio 5000, inserir novos drives e instruções.
Prática de Studio 5000, inserir novos drives e instruções.Prática de Studio 5000, inserir novos drives e instruções.
Prática de Studio 5000, inserir novos drives e instruções.
Adinan de Avila Tomé
 
generate IP CORES
generate IP CORESgenerate IP CORES
generate IP CORES
guest296013
 
Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3
Max Kleiner
 
MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptx
gopikahari7
 
Internet of Things exercise on IBM Bluemix
Internet of Things exercise on IBM BluemixInternet of Things exercise on IBM Bluemix
Internet of Things exercise on IBM Bluemix
LennartF
 
Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
Hari K
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
Pradeep Kumar
 
maXbox_Arduino_Pascal_Magazine
maXbox_Arduino_Pascal_MagazinemaXbox_Arduino_Pascal_Magazine
maXbox_Arduino_Pascal_Magazine
Max Kleiner
 
Get Starte with MicroPython ESP32
Get Starte with MicroPython ESP32Get Starte with MicroPython ESP32
Get Starte with MicroPython ESP32
fanghe22
 
Get Started with MicroPython ESP32
Get Started with MicroPython ESP32Get Started with MicroPython ESP32
Get Started with MicroPython ESP32
fanghe22
 
MattsonTutorialSC14.pdf
MattsonTutorialSC14.pdfMattsonTutorialSC14.pdf
MattsonTutorialSC14.pdf
George Papaioannou
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 
Arduino Teaching Program
Arduino Teaching ProgramArduino Teaching Program
Arduino Teaching Program
Max Kleiner
 
Please look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docxPlease look at the attach See.doc. I am getting this error all th.docx
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Micro Processor & Micro Controller Practical Notes_Electrical Engineering GTU...
Parth Gajjar
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
VcTrn1
 
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery BoardProgramming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Gaurav Verma
 
Prática de Studio 5000, inserir novos drives e instruções.
Prática de Studio 5000, inserir novos drives e instruções.Prática de Studio 5000, inserir novos drives e instruções.
Prática de Studio 5000, inserir novos drives e instruções.
Adinan de Avila Tomé
 
generate IP CORES
generate IP CORESgenerate IP CORES
generate IP CORES
guest296013
 
Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3
Max Kleiner
 
MattsonTutorialSC14.pptx
MattsonTutorialSC14.pptxMattsonTutorialSC14.pptx
MattsonTutorialSC14.pptx
gopikahari7
 
Internet of Things exercise on IBM Bluemix
Internet of Things exercise on IBM BluemixInternet of Things exercise on IBM Bluemix
Internet of Things exercise on IBM Bluemix
LennartF
 
Embedded c lab and keil c manual
Embedded  c  lab  and keil c  manualEmbedded  c  lab  and keil c  manual
Embedded c lab and keil c manual
Hari K
 
maXbox_Arduino_Pascal_Magazine
maXbox_Arduino_Pascal_MagazinemaXbox_Arduino_Pascal_Magazine
maXbox_Arduino_Pascal_Magazine
Max Kleiner
 
Get Starte with MicroPython ESP32
Get Starte with MicroPython ESP32Get Starte with MicroPython ESP32
Get Starte with MicroPython ESP32
fanghe22
 
Get Started with MicroPython ESP32
Get Started with MicroPython ESP32Get Started with MicroPython ESP32
Get Started with MicroPython ESP32
fanghe22
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 
Lab report 201001067_201001104
Lab report 201001067_201001104Lab report 201001067_201001104
Lab report 201001067_201001104
swena_gupta
 

More from felicidaddinwoodie (20)

Business UseWeek 1 Assignment #1Instructions1. Plea.docx
Business UseWeek 1 Assignment #1Instructions1. Plea.docxBusiness UseWeek 1 Assignment #1Instructions1. Plea.docx
Business UseWeek 1 Assignment #1Instructions1. Plea.docx
felicidaddinwoodie
 
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docx
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docxBusiness UsePALADIN ASSIGNMENT ScenarioYou are give.docx
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docx
felicidaddinwoodie
 
Business UsePractical Connection WorkThis work is a writte.docx
Business UsePractical Connection WorkThis work is a writte.docxBusiness UsePractical Connection WorkThis work is a writte.docx
Business UsePractical Connection WorkThis work is a writte.docx
felicidaddinwoodie
 
Business System AnalystSUMMARY· Cognos Business.docx
Business System AnalystSUMMARY· Cognos Business.docxBusiness System AnalystSUMMARY· Cognos Business.docx
Business System AnalystSUMMARY· Cognos Business.docx
felicidaddinwoodie
 
Business StrategyOrganizations have to develop an international .docx
Business StrategyOrganizations have to develop an international .docxBusiness StrategyOrganizations have to develop an international .docx
Business StrategyOrganizations have to develop an international .docx
felicidaddinwoodie
 
Business StrategyGroup BCase Study- KFC Business Analysis.docx
Business StrategyGroup BCase Study- KFC Business Analysis.docxBusiness StrategyGroup BCase Study- KFC Business Analysis.docx
Business StrategyGroup BCase Study- KFC Business Analysis.docx
felicidaddinwoodie
 
Business Strategy Differentiation, Cost Leadership, a.docx
Business Strategy Differentiation, Cost Leadership, a.docxBusiness Strategy Differentiation, Cost Leadership, a.docx
Business Strategy Differentiation, Cost Leadership, a.docx
felicidaddinwoodie
 
Business Research Methods, 11e, CooperSchindler1case.docx
Business Research Methods, 11e, CooperSchindler1case.docxBusiness Research Methods, 11e, CooperSchindler1case.docx
Business Research Methods, 11e, CooperSchindler1case.docx
felicidaddinwoodie
 
Business RequirementsReference number Document Control.docx
Business RequirementsReference number Document Control.docxBusiness RequirementsReference number Document Control.docx
Business RequirementsReference number Document Control.docx
felicidaddinwoodie
 
Business ProposalThe Business Proposal is the major writing .docx
Business ProposalThe Business Proposal is the major writing .docxBusiness ProposalThe Business Proposal is the major writing .docx
Business ProposalThe Business Proposal is the major writing .docx
felicidaddinwoodie
 
Business ProjectProject Progress Evaluation Feedback Form .docx
Business ProjectProject Progress Evaluation Feedback Form .docxBusiness ProjectProject Progress Evaluation Feedback Form .docx
Business ProjectProject Progress Evaluation Feedback Form .docx
felicidaddinwoodie
 
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docxBUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
felicidaddinwoodie
 
Business Process Management JournalBusiness process manageme.docx
Business Process Management JournalBusiness process manageme.docxBusiness Process Management JournalBusiness process manageme.docx
Business Process Management JournalBusiness process manageme.docx
felicidaddinwoodie
 
Business Process DiagramACCESS for ELL.docx
Business Process DiagramACCESS for ELL.docxBusiness Process DiagramACCESS for ELL.docx
Business Process DiagramACCESS for ELL.docx
felicidaddinwoodie
 
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docxBusiness Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
felicidaddinwoodie
 
Business PlanCover Page  Name of Project, Contact Info, Da.docx
Business PlanCover Page  Name of Project, Contact Info, Da.docxBusiness PlanCover Page  Name of Project, Contact Info, Da.docx
Business PlanCover Page  Name of Project, Contact Info, Da.docx
felicidaddinwoodie
 
Business Planning and Program Planning A strategic plan.docx
Business Planning and Program Planning          A strategic plan.docxBusiness Planning and Program Planning          A strategic plan.docx
Business Planning and Program Planning A strategic plan.docx
felicidaddinwoodie
 
Business Plan In your assigned journal, describe the entity you wil.docx
Business Plan In your assigned journal, describe the entity you wil.docxBusiness Plan In your assigned journal, describe the entity you wil.docx
Business Plan In your assigned journal, describe the entity you wil.docx
felicidaddinwoodie
 
Business Plan Part IVPart IV of the Business PlanPart IV of .docx
Business Plan Part IVPart IV of the Business PlanPart IV of .docxBusiness Plan Part IVPart IV of the Business PlanPart IV of .docx
Business Plan Part IVPart IV of the Business PlanPart IV of .docx
felicidaddinwoodie
 
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docxBUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
felicidaddinwoodie
 
Business UseWeek 1 Assignment #1Instructions1. Plea.docx
Business UseWeek 1 Assignment #1Instructions1. Plea.docxBusiness UseWeek 1 Assignment #1Instructions1. Plea.docx
Business UseWeek 1 Assignment #1Instructions1. Plea.docx
felicidaddinwoodie
 
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docx
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docxBusiness UsePALADIN ASSIGNMENT ScenarioYou are give.docx
Business UsePALADIN ASSIGNMENT ScenarioYou are give.docx
felicidaddinwoodie
 
Business UsePractical Connection WorkThis work is a writte.docx
Business UsePractical Connection WorkThis work is a writte.docxBusiness UsePractical Connection WorkThis work is a writte.docx
Business UsePractical Connection WorkThis work is a writte.docx
felicidaddinwoodie
 
Business System AnalystSUMMARY· Cognos Business.docx
Business System AnalystSUMMARY· Cognos Business.docxBusiness System AnalystSUMMARY· Cognos Business.docx
Business System AnalystSUMMARY· Cognos Business.docx
felicidaddinwoodie
 
Business StrategyOrganizations have to develop an international .docx
Business StrategyOrganizations have to develop an international .docxBusiness StrategyOrganizations have to develop an international .docx
Business StrategyOrganizations have to develop an international .docx
felicidaddinwoodie
 
Business StrategyGroup BCase Study- KFC Business Analysis.docx
Business StrategyGroup BCase Study- KFC Business Analysis.docxBusiness StrategyGroup BCase Study- KFC Business Analysis.docx
Business StrategyGroup BCase Study- KFC Business Analysis.docx
felicidaddinwoodie
 
Business Strategy Differentiation, Cost Leadership, a.docx
Business Strategy Differentiation, Cost Leadership, a.docxBusiness Strategy Differentiation, Cost Leadership, a.docx
Business Strategy Differentiation, Cost Leadership, a.docx
felicidaddinwoodie
 
Business Research Methods, 11e, CooperSchindler1case.docx
Business Research Methods, 11e, CooperSchindler1case.docxBusiness Research Methods, 11e, CooperSchindler1case.docx
Business Research Methods, 11e, CooperSchindler1case.docx
felicidaddinwoodie
 
Business RequirementsReference number Document Control.docx
Business RequirementsReference number Document Control.docxBusiness RequirementsReference number Document Control.docx
Business RequirementsReference number Document Control.docx
felicidaddinwoodie
 
Business ProposalThe Business Proposal is the major writing .docx
Business ProposalThe Business Proposal is the major writing .docxBusiness ProposalThe Business Proposal is the major writing .docx
Business ProposalThe Business Proposal is the major writing .docx
felicidaddinwoodie
 
Business ProjectProject Progress Evaluation Feedback Form .docx
Business ProjectProject Progress Evaluation Feedback Form .docxBusiness ProjectProject Progress Evaluation Feedback Form .docx
Business ProjectProject Progress Evaluation Feedback Form .docx
felicidaddinwoodie
 
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docxBUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
BUSINESS PROCESSES IN THE FUNCTION OF COST MANAGEMENT IN H.docx
felicidaddinwoodie
 
Business Process Management JournalBusiness process manageme.docx
Business Process Management JournalBusiness process manageme.docxBusiness Process Management JournalBusiness process manageme.docx
Business Process Management JournalBusiness process manageme.docx
felicidaddinwoodie
 
Business Process DiagramACCESS for ELL.docx
Business Process DiagramACCESS for ELL.docxBusiness Process DiagramACCESS for ELL.docx
Business Process DiagramACCESS for ELL.docx
felicidaddinwoodie
 
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docxBusiness Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
Business Plan[Your Name], OwnerPurdue GlobalBUSINESS PLANDate.docx
felicidaddinwoodie
 
Business PlanCover Page  Name of Project, Contact Info, Da.docx
Business PlanCover Page  Name of Project, Contact Info, Da.docxBusiness PlanCover Page  Name of Project, Contact Info, Da.docx
Business PlanCover Page  Name of Project, Contact Info, Da.docx
felicidaddinwoodie
 
Business Planning and Program Planning A strategic plan.docx
Business Planning and Program Planning          A strategic plan.docxBusiness Planning and Program Planning          A strategic plan.docx
Business Planning and Program Planning A strategic plan.docx
felicidaddinwoodie
 
Business Plan In your assigned journal, describe the entity you wil.docx
Business Plan In your assigned journal, describe the entity you wil.docxBusiness Plan In your assigned journal, describe the entity you wil.docx
Business Plan In your assigned journal, describe the entity you wil.docx
felicidaddinwoodie
 
Business Plan Part IVPart IV of the Business PlanPart IV of .docx
Business Plan Part IVPart IV of the Business PlanPart IV of .docxBusiness Plan Part IVPart IV of the Business PlanPart IV of .docx
Business Plan Part IVPart IV of the Business PlanPart IV of .docx
felicidaddinwoodie
 
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docxBUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
BUSINESS PLAN FORMAT          Whether you plan to apply for a bu.docx
felicidaddinwoodie
 
Ad

Recently uploaded (20)

UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Ad

15LLP108_Demo4_LedBlinking.pdf1. Introduction In D.docx

  • 1. 15LLP108_Demo4_LedBlinking.pdf 1. Introduction In Demo3, we have learned how to read sensor values of light, temperature and humidity of a node and output these values to the console. In this demonstration, we will use the code from Demo3 and learn how to turn on/off the LEDs and make them blinking regularly on the sensor node XM1000, meanwhile to count how many times the LED has blinked and output the count to the console. 2. Timer In order to make the blue LED on the XM1000 sensor node to blink in every half second (i.e. On 0.5S and Off 0.5S), we also need a timer. Follow the instructions in Demo3 for configure and reset a timer. We aslo need to create an infinite while() loop so that it runs our functions repeatedly, such as counting the times the LED has blinked, output the counter’s value and actually turn on or off the LEDs to make it blinking.
  • 2. Please follow timer and while() loop structure in Demo3. 3. LED Blinking To get access to the LED functionalities in Contiki, we need to include the LED header file in the source code: #include "leds.h" // file is in directory /home/user/contiki/core/dev After the process begin, we have to initialise the LEDs on the sensor node by calling the following function: leds_init(); // Initialise the LEDs And finally we can turn on, off, or blink the LEDs by the following functions: void leds_on(unsigned char leds); void leds_off(unsigned char leds); void leds_toggle(unsigned char leds); void leds_invert(unsigned char leds); For example, if you want to blink the Blue LED, yon need to call the toggle function as:
  • 3. void leds_toggle(LEDS_BLUE); // Toggle the blue LED 4. Exercise Modify the program from Demo3 with periodic timer to make the BLUE led blinking in every half second, also to count the blinking times and output the counted number to the console. Can your change the code so that the BLUE LED is lighted for 1 second and off for 0.5 second periodically? 15LLP108 – Internet of Things and Applications Lab Session 2: Demo 4 – LED Blinking Prepared by Xiyu Shi 5. Source code Here is the source code for reference #include "contiki.h" #include "leds.h" #include <stdio.h> /* for printf() */
  • 4. static struct etimer timer; /*___________________________________________________ _*/ PROCESS(led_blinking_process, "LED Blinking Process"); PROCESS(LED_process, "LED process"); AUTOSTART_PROCESSES(&LED_process); /*___________________________________________________ _*/ PROCESS_THREAD(LED_process, ev, data) { static int count = 0; PROCESS_BEGIN(); etimer_set(&timer, CLOCK_CONF_SECOND/2); // 0.5S timer leds_init(); // intialise the LEDs while(1) {
  • 5. PROCESS_WAIT_EVENT_UNTIL(ev==PROCESS_EVENT_TI MER); // wait for timer event count++; // count the blinking times process_start(&led_blinking_process, NULL); // to blink the BLUE Led printf("Count: %dn", count); // output the counter number to console etimer_reset(&timer); // reset the timer } PROCESS_END(); } /*___________________________________________________ _*/ PROCESS_THREAD(led_blinking_process, ev, data) { PROCESS_BEGIN(); leds_toggle(LEDS_BLUE); // Blinking the Blue LED PROCESS_END(); }
  • 6. 15LLP108_Demo2_HelloWorld_CodeExample.pdf 1. Introduction In this Demonstration, we are going to code and run our first application on the XM1000 sensor node. As in any programming language, we start our demonstration with a “Hello World” program on a sensor node that displays Hello World on the console. We have the following goals for this demonstration: Code and understand Contiki programming in C Compile and deploy code on a sensor node Connect the sensor node to output console for information display 2. Hello World! Code Example Figure 1 Source code of hello-world.c Here we give a description of the example code line by line: 15LLP108 – Internet of Things and Applications Lab Session 1: Demo 2 – Hello World!
  • 7. Prepared by Xiyu Shi Line 1: the Contiki header file, contiki.h, is included. This will include the Contiki OS into the compile program and allow to access the scheduling and abstraction of APIs of Contiki. Contiki follows the Protothreads concurrent programming model with a low- overhead. Line 3: includes the standard input/output library needed to write to the standard output. Line 7: defines the processes to be executed and included during runtime. In this case we include only one process; the hello_world_process. It is possible and usually the case to define several concurrent processes, one for processing and collecting data, one for transmitting or receiving data. Line 8: define which process to start when the sensor node is started up (i.e. power on or reset) Line 11: start the definition of a process called “hellow_world_process”. Line 12 and 18: all codes of a process must be enclosed within a pair of bracket. Line 13: declare the beginning of the process Line 17: declare the end of the process Line 15: is where the node really does what I want it to do, to output “Hello World” to the console via the printf() function. Most of the Contiki functions rely on standard-C.
  • 8. 3. The Makefile In Linux-alike OS, we need “Makefile” to tell the OS how to compile the program for a specific platform or hardware/software configuration. Contiki relies on two Makefiles to for compilation: the file “Makefile.target” (Fig. 2) defines the platform the code has to be compiled for, and the file “Makefile” (Fig. 3) tells the compiler where the Contiki header files can be found. Figure 2 Makefile.target Figure 3 Makefile 4. Exercise by Compiling and Running on the Sensor Node Create the c source file and the two Makefiles in fold “/home/user/contiki/examples/demo1” and run the following commands: make TARGET=xm1000 savetarget make make hello-world.upload make login Or simple follow the instructions in document “15LLP108- Demo1_HelloWorld_HowToCompile”. Bonus: Extend your code to print some random numbers
  • 9. between 0 and 1. 15LLP108_Demo1_HelloWorld_HowToCompile.pdf In this Demonstration, we will learn how to compile, upload and run a simple node application on the Contiki platform. 1. Enter the example code subdirectory Open a Linux terminal window to run commands. cd contiki/example/hello-world 2. Compile the hello-world project make TARGET=xm1000 savetarget make // this will take several seconds 3. Connect the Node to your PC/Laptop a. Connect the XM1000 mote node to one of the USB socket on the PC or laptop. Wait for the completion of software/drivers for the node on Windows. b. A popup window is displayed on the WMware Player to tell you how to install the device node on the virtual machine. Follow the advice:
  • 10. c. On the top-left WMware Player windows menu list, clicking “Player”->”Removable Devices”- >”Future Devices MTM-XM1000MSP”->”Connect” d. A window message is poped up, click “OK” 4. Upload to the node Get into hello-world project directory and running the following command: make hello-world.upload If error happened, such as “could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'”, it means that the node device is not in “write” mode. Changing the write permission by running: “sudo chmod 777 /dev/ttyUSB0”, when promoted a password, input “user”. Now run the upload to programming the node device again by “make hello-world.upload”. The program code will be programmed to the xm1000 node device. You will notice that the red and green LEDs are flashing during the programming. 5. Run the application a. Once the application is uploaded to the node, login to the console of the connected sensor
  • 11. node: make login 15LLP108 – Internet of Things and Applications Lab Session 1: Demo 1 – How to Compile an Application on Contiki Prepared by Xiyu Shi The console will display the following message: using saved target 'xm1000' ../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0 connecting to /dev/ttyUSB0 (115200) [OK] b. Now pressing the Red button on the sensor device and see the output of the console. It will show something like this: Rime started with address 43.84 MAC 2b:54:00:00:00:00:00:00 Contiki-2.6 started. Node id is not set. CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26 Starting 'Hello world process'
  • 12. Hello, world Notice that the last line printed on the console, which is what the device is programmed to do. 15LLP108_Demo5_MultiThreading.pdf 1. Introduction The aim of this demonstration is to show how multi-threads can be implemented for complex tasks. You will develop a temperature indicator, which is able to measure the temperature 4 times every second, to average them and to print the average temperature through another thread process. You need to have two threads in the application, a measurement thread and a print thread. The main thread is used to measure the temperature four times in a second, then to average the four measurements and pass the value to the print process thread. 2. Passing data from one process thread to another In order to pass data from one process thread to another you
  • 13. have to: a. Remember every process thread is defined as: PROCESS_THREAD(A_process, ev, data) where the argument “ev” is the event that passed to this thread from its parent thread and the “data” argument is the data that passed to this thread from its parent thread. b. Initialise the storage event_data_ready = process_alloc_event(); c. Post the measurement data to another thread called print_process: Process_post(&print_process, event_data_ready, &measurement); d. In another thread (in this case it is print_process), wait for event to occur: PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready); e. And get data from the process thread input argument "data". The application structure is shown in Figure 1, in next page. 3. Exercise To read the temperature, you may need to look at Demo3 again and use the following function: sht11_sensor.value(SHT11_SENSOR_TEMP); You need also to include the relevant header files in your code.
  • 14. 15LLP108 – Internet of Things and Applications Lab Session 3: Demo 5 – Multi-threading Prepared by Xiyu Shi Figure 1 Multi-threads program structure 4. Source code Here is the source code for reference #include "contiki.h" #include <stdio.h> /* For printf() */ /* Include temperature sensor driver */ #include "dev/sht11-sensor.h" /* Number of values used in averaging process */ #define MAX_VALUES 4 /* Variables: the application specific event value */ static process_event_t event_data_ready; /* digits before decimal point */ unsigned short d1(float f)
  • 15. { return((unsigned short)f); } /* digits after decimal point */ unsigned short d2(float f) { return(1000*(f-d1(f))); } /*------------------------------------------------------------------------ ---*/ /* Declare the two processes */ PROCESS(temp_process, "Temperature process"); PROCESS(print_process, "Print process"); /* the temperature measurement processes is started automatically at beginning */ AUTOSTART_PROCESSES(&temp_process, &print_process); /*------------------------------------------------------------------------ ---*/ /* Implementation of the first process thread, the temperature measurement*/ PROCESS_THREAD(temp_process, ev, data) { /* Variables are declared static to ensure their values are kept */ /* between kernel calls. */ static struct etimer timer; static int count = 0; static float average = 0, valid_measure = 0; /* This variable is recomputed at every run, therefore it is not */ /* necessary to declare them static. */
  • 16. float measure; /* Any process must start with this. */ PROCESS_BEGIN(); /* Allocate the required event */ event_data_ready = process_alloc_event(); /* Initialise the temperature sensor */ SENSORS_ACTIVATE(sht11_sensor); /* Initialise variables */ count = 0; average = 0; valid_measure = 0; measure = 0; /* Set the etimer module to generate an event in 0.25 second. */ etimer_set(&timer, CLOCK_CONF_SECOND/4); while (1) { /* Wait here for the timer to expire */ PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); /* Get temperature measurement and convert it to degrees */ measure = 0.01 * sht11_sensor.value(SHT11_SENSOR_TEMP) - 39.6; /* Sum temperature measurements */ average += measure;
  • 17. /* Count how many temperature measurement we have summed up */ count++; if (count == MAX_VALUES) { /* Average the sum and store */ valid_measure = average / MAX_VALUES; /* Reset variables */ average = 0; count = 0; /* Post an event to the print process */ /* and pass a pointer to the last measure as data */ process_post(&print_process, event_data_ready, &valid_measure); } /* Reset the timer so it will generate another event */ etimer_reset(&timer); } /* Any process must end with this, even if it is never reached. */ PROCESS_END(); } /*------------------------------------------------------------------------ ---*/ /* Implementation of the second process, the print temperature thread */ PROCESS_THREAD(print_process, ev, data) { PROCESS_BEGIN();
  • 18. while (1) { /* Wait until we get a data_ready event */ PROCESS_WAIT_EVENT_UNTIL(ev == event_data_ready); /* Use 'data' variable to retrieve data and then display it */ printf("temperature = %u.%un", d1(*(float *)data), d2(*(float *)data)); } PROCESS_END(); } /*------------------------------------------------------------------------ ---*/ Assignment Resources for Experiment http://www.advanticsys.com/shop/asxm1000-p-24.html Related Work. http://www.treehugger.com/gadgets/smart-garden-system- monitors-and-tracks-changing-weather-and-soil-conditions.html