Principles of Wireless Sensor Networkshttps://www.kth.se/social/course/EL2745/
Lecture 2
Introduction to Programming WSNs
Piergiuseppe Di MarcoEricsson Research
e-mail:[email protected]://pidm.droppages.com/
Royal Institute of TechnologyStockholm, Sweden
September 1, 2015
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 1 / 31
Course content
Part 1
I Lec 1: Introduction to WSNsI Lec 2: Introduction to Programming WSNs
Part 2
I Lec 3: Wireless ChannelI Lec 4: Physical LayerI Lec 5: Medium Access Control LayerI Lec 6: Routing
Part 3
I Lec 7: Distributed DetectionI Lec 8: Static Distributed EstimationI Lec 9: Dynamic Distributed EstimationI Lec 10: Positioning and LocalizationI Lec 11: Time Synchronization
Part 4
I Lec 12: Wireless Sensor Network Control Systems 1I Lec 13: Wireless Sensor Network Control Systems 2
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 2 / 31
Today’s Lecture
WSN ProgrammingIntroduction
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 3 / 31
WSN Programming Challenges
Scarce resources
I Low cost, small size platformsI Limited memory (kB)
Event-driven programming model
I Reactive approach, depending on interaction with environmentI Data collection / control specific, opposite to general purpose computation
Reliability / durable stand-alone operation
I Applications run for months/years without human interventionI Reduce run time errors and complexity
Soft real-time requirements
I Few time-critical tasks (sensor acquisition and radio timing)I Timing constraints through complete control over application
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 4 / 31
Programming WSNs
Software
I Programming language (e.g., nesC, C, C++. Java,...)• Compiler, etc.
I OS/runtime libraries (e.g., TinyOS, ContikiOS, mbedOS, MantisOS,LiteOS,...)
• Access to system resources• Application programming interfaces (APIs)
for communication, sensors, etc.
Hardware
I WSN module(e.g., TelosB, Tmote Sky, MicaZ, ARM mbed,...)
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 5 / 31
Today’s Topics
NesC
I designed specifically for WSN programming
TinyOS
I widespread, especially in academia, large community, easy support,...
Examples
Hands-on
I Your turn!
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 6 / 31
TinyOS and NesC
TinyOS and NesC
Component Model
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 7 / 31
Network Embedded Systems C (NesC)
NesC is the programming language used to build applications for the TinyOSplatform
It is based on C language (a dialect of C) with extra features
The basic unit of NesC code is a component
Components connect via interfaces
Connections are called “wiring”
Interfaces are used to:
I Access to the functions of componentI Provide functionality to the outside
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 8 / 31
Example
B providesfunctionalityExample:-Turn on LED-Send packet
BA
I
A uses functionalityprovided by BExample: To send apacket
Interface I describesthe “API”
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 9 / 31
TinyOS Components
There are two types of components in TinyOS:
Modules (M):
I base executable codeI contains variables / implementations
Configurations (C):
I contain multiple sub-components (modulesor configurations): nesting
I describe connections(e.g., which module uses the function)
I parameterize sub-components:(e.g., options)
Components are very similar to classes (Java, C,...),statically linked via interfaces
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 10 / 31
TinyOS Components
Components use or provide interfaces
I component uses functionalities of othersI provide functionalities to others
Interfaces contain the API, expressed throughcommands and events
Function calls Commands EventsUse Call Command Implement Event Handler
Provide Implement Command Body Signal Event
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 11 / 31
TinyOS Modules
TinyOS modules are composed of
Specification
I List of interfaces that thecomponent provides to others anduses from others
I Alias interfaces as new name
Implementation
I Commands of provided interfacesI Events of used interfaces
module FooM {//Specificationprovides {
interface Foo;}uses {
interface Poo as PooFoo;interface Boo;
}}//Implementationimplementation {
//Command handlerscommand result t Foo.comm{
. . .}//Event handlersevent void Boo.event{
. . .}
}Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 12 / 31
Example
TinyOS and NesC
Example: Hello World
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 13 / 31
Example: “Hello World”
No display, motes cannot print “Hello World”
We let them periodically blink the LEDs!
Ingredients:
A timer to trigger the periodic blinking
I TinyOS provides a Timer library
A way to turn the LEDs on and off
I TinyOS provides a LedsC component to control the LEDs
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 14 / 31
“Hello World”
interface Leds {command void led0On();command void led0Off();command void led0Toggle();// . . .
}
interface Timer <precision> {command void startPeriodic(uint32 t dt);command void startOneShot(uint32 t dt);event void fired();// . . .
}
Called tocontrol a led
Can be TMilli,TMicro, T32Khz
Signaled whenthe timer expires
Called tostart a timer
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 15 / 31
“Hello World” Timer withmillisecondprecision
Signaled whenhw ready
Cal
lsst
artP
erio
dic
()
Signalsfired()
module BlinkC {uses interface Timer<TMilli> as BlinkTimer;uses interface Leds;uses interface Boot;
}
implementation {event void Boot.booted() {
call BlinkTimer.startPeriodic(1000);
}
event void BlinkTimer.fired() {call Leds.led0Toggle();
}}
Application
Timers
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 16 / 31
“Hello World” No interfacesused or provided
Provides Boot
TimerMilliC isa generic com-ponent: canbe instantiatedmultiple times!
Wirings
configuration BlinkAppC {}
implementation {components MainC,
BlinkC,LedsC;
components new TimerMilliC as Timer0;
BlinkC -> MainC.Boot;BlinkC.BlinkTimer -> Timer0;BlinkC.Leds -> LedsC;
}
Instantiation is done at compile-time
I very different from typical object-oriented programming
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 17 / 31
“Hello World”Cross-compile for aspecific WSN platform
Node id USB port the nodeis attached to
$ make telosb// . . .compiled BlinkAppC to build/telosb/main.exe
2648 bytes in ROM54 bytes in RAM
msp430-objcopy –output-target=ihex build/telosb/main.exe build/telosb/main.ihex
writing TOS image$ make telosb install, 0 bsl,/dev/ttyUSB0
// . . .
Use “motelist” command to determine USB port
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 18 / 31
TinyOS and NesC
Programming (cont.)
TinyOS and NesC
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 19 / 31
Split-phase Operation
TinyOS has a single stack and non blocking operation
When a program calls a long-running operation, the call returns immediately, and acallback is issued when the operation is complete
Invocation and completion are split into two separate phases of execution
The approach guarantees memory efficient concurrency
event void Boot.booted() {call BlinkTimer.startPeriodic(1000);
}event void BlinkTimer.fired() {
call Leds.led0Toggle();}
void main() {while() {
sleep(1);printf(’hello’);
}}
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 20 / 31
Tasks
Tasks are functions that can be posted into thetask queue (FIFO)
I they run until completion and do notinterrupt each other
I long tasks can reduce responsiveness, keepthem short!
Interrupts low level hardware events that runon stack
I interrupt can post and interrupt tasksI interrupts can interrupt each other
// . . .task void compute () {
// do something;}
event void Timer.fired(){
post compute()}// . . .
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 21 / 31
TOSSIM
TOSSIM is a simulator for TinyOS applications
Allows NesC code to run on a standard machine (Linux etc.)
Includes models for
I CommunicationI SensingI . . .
It works by replacing components with simulation implementations
I Run the same code in simulator and sensor node
Good for testing before deployment
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 22 / 31
Code examples
TinyOS has an “apps” folder
I Many good examples
Other sources of information
I TinyOS Book• TinyOS Programming by Philip Levis, David Gay• In part: csl.stanford.edu/∼ pal/pubs/tinyos-programming.pdf
I TinyOS web book• csl.stanford.edu/∼ pal/pubs/tos-programming-web.pdf
I TinyOS tutorial• IPSN 2009 (enl.usc.edu/talks/cache/tinyos-ipsn2009.ppt)
I TinyOS website: http://www.tinyos.net/I TinyOS mailing list (and its archives)
• On the TinyOS website
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 23 / 31
Hands-On
Programming (cont.)
Hands-On
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 24 / 31
Today’s Task
Goal: program an anti-theft device
I Detect when someone steels your sensor node
Two parts:
I Detecting theft• Assume: thieves put the motes in their pockets• So, a “dark” mote is a stolen mote• Every N ms check if light sensor is below some threshold
I Reporting theft• Assume: bright flashing lights deter thieves• Theft reporting algorithm: light the red LED for a little while!
What you will use
I Basic components, interfaces, wiringI Essential system interfaces for startup, timing, sensor sampling
Start from Blink
I In apps/BlinkI See apps/RadioSensoToLeds on sensor use
• Replace “DemoSensorC” with “HamamatsuS10871ParC” or“HamamatsuS10871TsrC”
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 25 / 31
Anti-Theft Module
module AntiTheftC {uses interface Boot;uses interface Timer<TMilli> as Check;uses interface Read<uint16 t>;
}implementation {
event void Boot.booted() {call Check.startPeriodic(1000);
}event void Check.fired() {
call Read.read();}event void Read.readDone(error t ok, uint16 t val) {
if (ok == SUCCESS && val < 200)theftLed();
}}
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 26 / 31
interface Boot {/* Signaled when OS booted */event void booted();
}
interface Timer< tag> {command void startOneShot(uint32 t period)command void startPeriodic(uint32 t period)event void fired();
}
Anti-Theft Module
module AntiTheftC {uses interface Boot;uses interface Timer<TMilli> as Check;uses interface Read<uint16 t>;
}implementation {
event void Boot.booted() {call Check.startPeriodic(1000);
}event void Check.fired() {
call Read.read();}event void Read.readDone(error t ok, uint16 t val) {
if (ok == SUCCESS && val < 200)theftLed();
}}
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 27 / 31
Split-phase example:- Read.read starts the operation- Read.readDone signals completion
interface Read< val t > {command error t read();event void readDone(error t ok, val t val);
}
Anti-Theft Configuration
configuration AntiTheftAppCimplementation{
components AntiTheftC, MainC, LedsC;
AntiTheftC.Boot -> MainC.Boot;AntiTheftC.Leds -> LedsC;
components new TimerMilliC() as MyTimer;AntiTheftC.Check -> MyTimer;
components new HamamatsuS10871ParC();AntiTheftC.Read -> HamamatsuS10871ParC;
}
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 28 / 31
generic configuration TimerMilliC() {provides interface Timer<TMilli>;
}implementation { . . . }
generic configuration PhotoC() {provides interface Read;
}implementation { . . . }
Your Tasks
1. System test
I Install Blink on your sensor node• See apps/Blink• Compile, install (see prev. slides, see tutorial from TA)
2. Main task:
I Program Anti-Theft App for your sensor node
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 29 / 31
Thanks!
Questions?
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 30 / 31
These slides are based on material from
I Olaf Landsiedel, Phil Levis, David Gay, David Culler, Luca Mottola, HamadAlizai, and many others
Piergiuseppe Di Marco (KTH) Principles of Wireless Sensor Networks September 1, 2015 31 / 31