47
TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

Embed Size (px)

Citation preview

Page 1: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

TinyOS Tutorial, Part I

Phil Levis et al.MobiSys 2003

Page 2: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 2

Goals

• Deploy a (small) sensor network• See, modify, and install some nesC

code• Write a simple application

Page 3: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 3

TinyOS Applications

• Application specific images• Top-level configuration• tinyos-1.x/apps/

Page 4: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 4

TinyDB

• Data collection and aggregation• SQL-like queries• Ad-hoc multi-hop routing

Page 5: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 5

Two Steps

• Prepare PC-side application• Install TinyDB on two or more motes

Page 6: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 6

Building Java Tools

• cd tinyos-1.x/tools/java• make

Page 7: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 7

TinyDB Java Application

• cd tinyos-1.x/tools/java• cd net/tinyos/tinydb• java –jar tinydb.jar• After you run this, close it

Page 8: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 8

Installing TinyDB

• cd tinyos-1.x/apps/tinydb• make mica

– Make sure application builds properly

• Plug in programming board • make mica install.1

– Installs application with mote ID 1

Page 9: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 9

Build a TinyDB Base Station

• Plug in a new mote• cd apps/TinyDB• make mica install.0• Make sure serial cable is

connected

Page 10: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 10

Running the Application

• Plug in (and turn on) base station • Turn on TinyDB mote• Run TinyDB PC application

Page 11: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 11

A Simple Query

• Report light at one Hz• Look at data output

Page 12: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 12

Multi-Mote Network

• Close TinyDB PC application• Turn off motes• Install TinyDB on more motes

– make install.2– make install.3

• Run TinyDB PC application• Send query again

Page 13: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 13

Applications

• TinyOS applications have a top-level configuration

• Wires application components to boot sequence, etc.

• Some applications have no app-specific modules

Page 14: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 14

Example Configuration

Page 15: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 15

A Simple App: CntToLeds

• CntToLeds• Displays bottom 3 bits of a counter

on LEDs• apps/CntToLeds

Page 16: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 16

CntToLeds Configuration(apps/CntToLeds/CntToLeds.nc)

configuration CntToLeds {}implementation { components Main, Counter, IntToLeds, TimerC;

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> Counter.StdControl; Main.StdControl -> TimerC.StdControl; Counter.Timer -> TimerC.Timer[unique("Timer")]; Counter.IntOutput -> IntToLeds.IntOutput;}

Page 17: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 17

Step By Step(apps/CntToLeds/CntToLeds.nc)

• This is a configuration (wiring)• Named CntToLeds (CntToLeds.nc)

configuration CntToLeds {}

Page 18: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 18

Step By Step(apps/CntToLeds/CntToLeds.nc)

• This is the implementation block– As this is a configuration, implementation is

wiring

• This wiring uses these components– There can be more than one components

statement– Component order unimportant

implementation { components Main, Counter, IntToLeds, TimerC;

Page 19: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 19

Step By Step(apps/CntToLeds/CntToLeds.nc)

• Connect Main’s StdControl (uses) to the StdControl interfaces (provides) of three components

• In the boot sequence, Main will call these components’ init() and start()

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> Counter.StdControl; Main.StdControl -> TimerC.StdControl;

Page 20: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 20

Step By Step(apps/CntToLeds/CntToLeds.nc)

• Wire counter to a unique TimerC timer• Counter.Timer.startTimer() will call TimerC.Timer[x].startTimer

• TimerC.Timer[x].fired() will call Counter.Timer.fired()

Counter.Timer -> TimerC.Timer[unique("Timer")];

Page 21: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 21

Finally….(apps/CntToLeds/CntToLeds.nc)

• Wire the counter output to the LEDs

Counter.IntOutput -> IntToLeds.IntOutput;

Page 22: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 22

CntToLeds, Revisited(apps/CntToLeds/CntToLeds.nc)

configuration CntToLeds {}implementation { components Main, Counter, IntToLeds, TimerC;

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> Counter.StdControl; Main.StdControl -> TimerC.StdControl; Counter.Timer -> TimerC.Timer[unique("Timer")]; Counter.IntOutput -> IntToLeds.IntOutput;}

Page 23: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 23

How it Works

• On boot, Main initializes components• When Counter starts, it starts its

Timer• When Timer fires, Counter

increments is value, then calls IntToLeds

• IntToLeds displays bottom three bits

Page 24: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 24

Install CntToLeds

• cd apps/CntToLeds• make mica install• Watch the mote blink

Page 25: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 25

Simulation

• TinyOS has a simulator, TOSSIM• Builds directly from TinyOS code

– make pc– build/pc/main.exe –h

• Configurable output– export DBG=led

• Scalable to thousands of nodes

Page 26: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 26

Simulating CntToLeds

• Syntax: main.exe <number of nodes>• Sample command line options

– -h: help– -x <floating point>: Scale simulated

time to real world time (2.0 = twice as fast)

– -r <simple|static|lossy>: Radio models

• Running CntToLeds– build/pc/main.exe –x 1.0 1

Page 27: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 27

TinyViz

• GUI for visualization and actuation• Connects directly to TOSSIM

• build/pc/main.exe –gui 4 • cd tinyos-1.x/tools/java/• java net.tinyos.sim.TinyViz

Page 28: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 28

TinyViz Screenshot

Page 29: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 29

Adding Debugging Output• tinyos-1.x/tos/lib/Counter.nc:

• export dbg=led,usr3• make pc• build/pc/main.exe –x 1.0 1

event result_t Timer.fired() { state++; dbg(DBG_USR3, “Counter: %i.\n”, state); return call IntOutput.output(state); }

Page 30: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 30

Simple Messaging

• Build a CntToRfm mote• Build a RfmToLeds mote• One mote displays the other’s

counter

Page 31: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 31

CntToRfm (apps/CntToRfm/CntToRfm.nc)

configuration CntToRfm {}implementation { components Main, Counter, IntToRfm, TimerC;

Main.StdControl -> Counter.StdControl; Main.StdControl -> IntToRfm.StdControl; Counter.Timer -> TimerC.Timer[unique("Timer")]; Counter.IntOutput -> IntToRfm.IntOutput;}

Page 32: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 32

RfmToLeds (apps/RfmToLeds/RfmToLeds.nc)

configuration RfmToLeds {}implementation { components Main, RfmToInt, IntToLeds;

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> RfmToInt.StdControl; RfmToInt.IntOutput -> IntToLeds.IntOutput;}

Page 33: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 33

Sensing

• Instead of a counter, display sensor reading on LEDs

• See what sensing code looks like• Install SenseToRfm on CntToRfm node

Page 34: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 34

Sensing: SenseToRfm (apps/SenseToRfm/SenseToRfm.nc)

configuration SenseToRfm {// this module does not provide any interface}implementation{ components Main, SenseToInt, IntToRfm, ClockC, Photo;

Main.StdControl -> SenseToInt; Main.StdControl -> IntToRfm;

SenseToInt.Clock -> ClockC; SenseToInt.ADC -> Photo; SenseToInt.ADCControl -> Photo; SenseToInt.IntOutput -> IntToRfm;}

Page 35: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 35

Sensing, Continued

• Build a new application• Goals

– Have two motes sense– Each displays other’s reading on LEDs

• Starting blocks– SenseToRfm– RfmToLeds

Page 36: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 36

Making a New Application

• Create a new application directory• Create the Makefile• Write the code

Page 37: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 37

Directory

• mkdir tinyos-1.x/apps/SenseExchange• cd tinyos-1.x/apps/SenseExchange

Page 38: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 38

Makefile

• Create and edit Makefile:

COMPONENT=SenseExchangeinclude ../Makerules

Page 39: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 39

Application File

• What components do we need?– Main– Photo– SenseToInt– IntToRfm– RfmToInt– IntToLeds– ClockC

Page 40: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 40

Wiring It Up: The Components (apps/SenseExchange/SenseExchange.nc)

configuration SenseExchange {// this module does not provide any interfaces}implementation{ components Main; // The output part components SenseToInt, IntToRfm, ClockC, Photo; // The input part components RfmToInt, IntToLeds;

Page 41: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 41

Wiring It Up: Output Connections

(apps/SenseExchange/SenseExchange.nc)

Main.StdControl -> SenseToInt; Main.StdControl -> IntToRfm;

SenseToInt.Clock -> ClockC; SenseToInt.ADC -> Photo; SenseToInt.ADCControl -> Photo; SenseToInt.IntOutput -> IntToRfm;

Page 42: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 42

Wiring It Up: Input Connections (apps/SenseExchange/SenseExchange.nc)

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> RfmToInt.StdControl; RfmToInt.IntOutput -> IntToLeds.IntOutput;

Page 43: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 43

Wiring It Up: Result (apps/SenseExchange/SenseExchange.nc)

configuration SenseExchange {// this module does not provide any interfaces}implementation{ components Main; components SenseToInt, IntToRfm, ClockC, Photo; components RfmToInt, IntToLeds; Main.StdControl -> SenseToInt; Main.StdControl -> IntToRfm; SenseToInt.Clock -> ClockC; SenseToInt.ADC -> Photo; SenseToInt.ADCControl -> Photo; SenseToInt.IntOutput -> IntToRfm;

Main.StdControl -> IntToLeds.StdControl; Main.StdControl -> RfmToInt.StdControl; RfmToInt.IntOutput -> IntToLeds.IntOutput;}

Page 44: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 44

Running SenseExchange

• Compile and run on two motes• Cover one light sensor• Cover the other

Page 45: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 45

Conclusion of Part I

• Deployed a small sensor network• Installed simple applications• Used TOSSIM• Wrote SenseExchange• Part II: Communication, actuation

Page 46: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 46

Questions

Page 47: TinyOS Tutorial, Part I Phil Levis et al. MobiSys 2003

5/5/2003 MobiSys 2003 47

SerialForwarder

• Sensor network proxy

• Connects to serial port

• Provides TCP socket interface

java net.tinyos.sf.SerialForward &