TINYOS
CREATING APPLICATIONS FOR WIRELESS SENSORS
M.Tech Part I 1st Semester
University of kalyani
5/31/2018 Riman Mandal, mandal.riman@gmail.com 1
Overview
Sensor code
(nesC/TinyOS)
Base station code
(nesC/TinyOS)
Gateway code
(Java, c, …)
Serial/USB
Wireless
Communication
iris/sensor
5/31/2018 Riman Mandal, mandal.riman@gmail.com 2
Required Hardwares
■ Programming Board
■ Mote
■ Sensing Board
■ And a PC of course….
A Typical Mote
5/31/2018 Riman Mandal, mandal.riman@gmail.com 3
Typical Programming Board
For Practical Purpose We have a different model
5/31/2018 Riman Mandal, mandal.riman@gmail.com 4
Typical Sensor Board
For Practical Purpose We have a different model
5/31/2018 Riman Mandal, mandal.riman@gmail.com 5
Typical Mote
Antenna Interface
Power Button
2 AA battery
Section
For Practical Purpose We have a different model
5/31/2018 Riman Mandal, mandal.riman@gmail.com 6
Tiny OS
■ An operating system for low power, embedded,
wireless devices
– Wireless sensor networks (WSNs)
– Sensor-actuator networks
– Embedded robotics
■ Open source, open developer community
■ http://www.tinyos.net
■ E-book: TinyOS Programming:
http://csl.stanford.edu/~pal/pubs/tinyos-
programming.pdf
5/31/2018 Riman Mandal, mandal.riman@gmail.com 7
Tiny OS (Cont.)
■ An open-source development environment
■ Not an operation system for general purpose, it
is designed for wireless embedded sensor
network.
■ Programming language: NesC (an extension of C)
■ It features a component-based architecture.
■ Supported platforms include Linux, Windows
2000/XP with Cygwin.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 8
Installation
■ Need Ubuntu 12.04 LTS for hassle free installation.
■ Try installing TinyOS ON Windows machines using VMWare
or Virtual Box.
■ I will send the details of Installation via e-mail.
■ If you are interested we can try on Windows using
Cygwin.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 9
Installing TinyOS 2.1.2
on Ubuntu 12.04 LTS
using Virtual Box
5/31/2018 Riman Mandal, mandal.riman@gmail.com 10
Preprocessing
■ Install VirtualBox on your Windows machine.
■ Install Ubuntu 12.04LTS inside the Virtual Machine.
■ Start the Ubuntu from Virtual machine.
■ Open Ubuntu Terminal.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 11
Step 1 Adding TinyOS
Source
■ Execute the following command,
■ Paste the line given below at the end of the file
■ Save the file and quit
5/31/2018 Riman Mandal, mandal.riman@gmail.com 12
sudo gedit /etc/apt/sources.list
deb http://tinyos.stanford.edu/tinyos/dists/ubuntu
lucid main
Step 2 Installing TinyOS
■ First update the TinyOS source using.
■ Use the following command to install TinyOS on your
Ubuntu machine.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 13
sudo apt-get update
sudo apt-get install tinyos2.1.2
Step 3 Environment Setup
■ Enter the following command to edit the Environment
setup in Ubuntu
■ Add the following line at the end of the ~/.bashrc file.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 14
sudo gedit ~/.bashrc
#Sourcing the tinyos environment variable setup script
source /opt/tinyos-2.1.2/tinyos.sh
export CLASSPATH=$CLASSPATH:.
Step 4 Creating the
Environment Source File
■ Create the setup script file using the command
■ Now enter the following contents into this file
5/31/2018 Riman Mandal, mandal.riman@gmail.com 15
sudo gedit /opt/tinyos-2.1.2/tinyos.sh
#echo "Setting up for TinyOS 2.1.2"
export TOSROOT=
export TOSDIR=
export MAKERULES=
TOSROOT="/opt/tinyos-2.1.2"
TOSDIR="$TOSROOT/tos"
CLASSPATH=$CLASSPATH:$TOSROOT/support/sdk/java:$TOSROOT/support/sdk
/java/tinyos.jar
MAKERULES="$TOSROOT/support/make/Makerules"
export TOSROOT
export TOSDIR
export CLASSPATH
export MAKERULES
Step 5 Permission for Setup
Script and Refresh
Environment
■ Run the command below to allow this script to execute
■ To refresh the environment with new setup - close and
start a new terminal or alternatively execute the
following command
5/31/2018 Riman Mandal, mandal.riman@gmail.com 16
sudo chmod 755 /opt/tinyos-2.1.2/tinyos.sh
source ~/.bashrc
Checking if Installation is
successful
■ Run the command to check TOS environment setup is
complete or not
■ If it gives warnings related to Java version then do
■ If your version is above 1.5, then ignore this warning else
upgrade to a newer java version. Ignore
the warning related to graphviz.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 17
tos-check-env
java -version
TinyOS Programming
5/31/2018 Riman Mandal, mandal.riman@gmail.com 18
Basic Idea
■ HURRY UP AND SLEEP!!
– Sleep as often as possible to save power
■ Programs are built out of components
– Libraries and components are written in nesC.
– Applications are too -- just additional components
composed with the OS components.
■ Each component is specified by an interface
– Provides “hooks” for wiring components together
■ Components are statically wired together based on their
interfaces
– Increases runtime efficiency
5/31/2018 Riman Mandal, mandal.riman@gmail.com 19
Basic Unit of Programming
■ Basic unit of nesC code is a component
■ Components connect via interfaces
– Connections called “wiring”
5/31/2018 Riman Mandal, mandal.riman@gmail.com 20
B
A
interface
What is a Component?
■ A component is a file (names must match)
■ Modules are components that have variables and
executable code
■ Configurations are components that wires other
components together
■ A component does not care if another component is a
module or configuration
■ A component may be composed of other components via
configurations
5/31/2018 Riman Mandal, mandal.riman@gmail.com 21
Component Example
5/31/2018 Riman Mandal, mandal.riman@gmail.com 22
TimerC
BlinkC
Timer
module BlinkC {
uses interface Timer<TMilli>
as Timer0
provide interface xxxx}
implementation {
int c;
void increment() {c++;}
event void Timer0.fired()
{
call Leds.led0Toggle();
}
}
configuration BlinkAppC
{
}
implementation
{
components MainC, BlinkC, LedsC;
components new TimerMilliC()
as Timer0;
BlinkC.Timer0 -> Timer0;
BlinkC -> MainC.Boot;
BlinkC.Leds -> LedsC;
}
Singletons and Generics
5/31/2018 Riman Mandal, mandal.riman@gmail.com 23
■ Singleton components are unique: they exist in a global
namespace
■ Generics are instantiated: each instantiation is a new,
independent copy
configuration BlinkC { … }
implementation {
components new TimerC();
components BlinkC;
BlinkC.Timer -> TimerC;
}
Component Syntax - Module
5/31/2018 Riman Mandal, mandal.riman@gmail.com 24
■ A component specifies a set of interfaces by which it is connected to
other components
– provides a set of interfaces to others
– uses a set of interfaces provided by others
module ForwarderM {
provides {
interface StdControl;
}
uses {
interface StdControl as CommControl;
interface ReceiveMsg;
interface SendMsg;
interface Leds;
}
}
implementation {
…// code implementing all provided commands
and used events
}
ForwarderM
StdControl ReceiveMsg
provides uses
CommControl
SendMsg
Leds
Component Syntax -
Configuration
5/31/2018 Riman Mandal, mandal.riman@gmail.com 25
configuration Forwarder { }
implementation
{
components Main, LedsC;
components GenericComm as Comm;
components ForwarderM;
Main.StdControl -> ForwarderM.StdControl;
ForwarderM.CommControl -> Comm;
ForwarderM.SendMsg -> Comm.SendMsg[AM_INTMSG];
ForwarderM.ReceiveMsg -> Comm.ReceiveMsg[AM_INTMSG];
ForwarderM.Leds -> LedsC;
}
Component
Selection
Wiring the
Components
together
ForwarderM
StdControl ReceiveMsg
provides uses
CommControl
SendMsg
Leds
Main StdControl
LedsC
Leds
GenericComm
SendMsg
ReceiveMsg
StdControl
Forwarder
Interface
5/31/2018 Riman Mandal, mandal.riman@gmail.com 26
■ Collections of related functions
■ Define how components connect
■ Interfaces are bi-directional: for A->B
– Commands are from A to B
– Events are from B to A
■ Can have parameters (types)
interface Timer<tag> {
command void startOneShot(uint32_t period);
command void startPeriodic(uint32_t period);
event void fired();
}
Interface (provide and use)
5/31/2018 Riman Mandal, mandal.riman@gmail.com 27
User
Provider
Interface
Commands
Events
Module BlinkC {
use interface xxxx;
provide interface xxxxxxx;
.........
}
Interface (provide and use)
(Cont.)
■ A component provides and uses interfaces.
■ A interface defines a logically related set of commands
and events.
■ Components implement the events they use and the
commands they provide:
5/31/2018 Riman Mandal, mandal.riman@gmail.com 28
Component Commands Events
Use Can call Must implement
Provide Must implement Can signal
Terminal Commands related
to TinyOS
■ system modifications
– sudo chmod –R 777 /bin
– sudo chmod –R 777 /dev
■ applications directory
– /opt/tinyos-2.1.1/apps/
■ making hex image for iris mote without sensor board
– make iris
■ making hex image for iris mote with sensor board
– SENSORBOARD=mda100 make iris
■ burning hex image into iris mote
– make <mote> install.moteid burnerid,/dev/tty<serialport>
– EX. make iris install.0 mib520,/dev/ttyUSB0
– EX. make iris reinstall.0 mib520,/dev/ttyUSB0
■ burning hex image into iris mote with sensor board
■ SENSORBOARD=mda100 make iris reinstall.0 mib520,/dev/ttyUSB0
5/31/2018 Riman Mandal, mandal.riman@gmail.com 29
Power Up Testing
Programing
■ Try to build a tinyOS app that turn on the Red Led on
Power up.
5/31/2018 Riman Mandal, mandal.riman@gmail.com 30
PowerUpC
uses
Boot
Leds
MainC
Boot
LedsC
Leds
Code
5/31/2018 Riman Mandal, mandal.riman@gmail.com 31
configuration PowerupAppC{}
implementation {
components MainC, PowerupC, LedsC;
MainC.Boot <- PowerupC.Boot;
PowerupC.Leds -> LedsC.Leds;
}
module PowerupC @safe()
{
uses interface Boot;
uses interface Leds;
}
implementation
{
event void Boot.booted() {
call Leds.led0On();
}
}
Blink App Using
Timer<TMilli> C
■ The Red light will blink continuously with a time interval
5/31/2018 Riman Mandal, mandal.riman@gmail.com 32
BlinkC
uses
Boot
Leds
MainC
Boot
LedsC
Using Printf Component
■ Include printf.h header file in the module file
■ Use Printf method to print anything on terminal
■ Add Component PrintfC in the Component File
■ Use the following command to print on the terminal
– java net.tinyos.tools.PrintfClient -comm
serial@/dev/ttyUSB1:iris
5/31/2018 Riman Mandal, mandal.riman@gmail.com 33
java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSBXXX:telosb

Tiny OS

  • 1.
    TINYOS CREATING APPLICATIONS FORWIRELESS SENSORS M.Tech Part I 1st Semester University of kalyani 5/31/2018 Riman Mandal, [email protected] 1
  • 2.
    Overview Sensor code (nesC/TinyOS) Base stationcode (nesC/TinyOS) Gateway code (Java, c, …) Serial/USB Wireless Communication iris/sensor 5/31/2018 Riman Mandal, [email protected] 2
  • 3.
    Required Hardwares ■ ProgrammingBoard ■ Mote ■ Sensing Board ■ And a PC of course…. A Typical Mote 5/31/2018 Riman Mandal, [email protected] 3
  • 4.
    Typical Programming Board ForPractical Purpose We have a different model 5/31/2018 Riman Mandal, [email protected] 4
  • 5.
    Typical Sensor Board ForPractical Purpose We have a different model 5/31/2018 Riman Mandal, [email protected] 5
  • 6.
    Typical Mote Antenna Interface PowerButton 2 AA battery Section For Practical Purpose We have a different model 5/31/2018 Riman Mandal, [email protected] 6
  • 7.
    Tiny OS ■ Anoperating system for low power, embedded, wireless devices – Wireless sensor networks (WSNs) – Sensor-actuator networks – Embedded robotics ■ Open source, open developer community ■ http://www.tinyos.net ■ E-book: TinyOS Programming: http://csl.stanford.edu/~pal/pubs/tinyos- programming.pdf 5/31/2018 Riman Mandal, [email protected] 7
  • 8.
    Tiny OS (Cont.) ■An open-source development environment ■ Not an operation system for general purpose, it is designed for wireless embedded sensor network. ■ Programming language: NesC (an extension of C) ■ It features a component-based architecture. ■ Supported platforms include Linux, Windows 2000/XP with Cygwin. 5/31/2018 Riman Mandal, [email protected] 8
  • 9.
    Installation ■ Need Ubuntu12.04 LTS for hassle free installation. ■ Try installing TinyOS ON Windows machines using VMWare or Virtual Box. ■ I will send the details of Installation via e-mail. ■ If you are interested we can try on Windows using Cygwin. 5/31/2018 Riman Mandal, [email protected] 9
  • 10.
    Installing TinyOS 2.1.2 onUbuntu 12.04 LTS using Virtual Box 5/31/2018 Riman Mandal, [email protected] 10
  • 11.
    Preprocessing ■ Install VirtualBoxon your Windows machine. ■ Install Ubuntu 12.04LTS inside the Virtual Machine. ■ Start the Ubuntu from Virtual machine. ■ Open Ubuntu Terminal. 5/31/2018 Riman Mandal, [email protected] 11
  • 12.
    Step 1 AddingTinyOS Source ■ Execute the following command, ■ Paste the line given below at the end of the file ■ Save the file and quit 5/31/2018 Riman Mandal, [email protected] 12 sudo gedit /etc/apt/sources.list deb http://tinyos.stanford.edu/tinyos/dists/ubuntu lucid main
  • 13.
    Step 2 InstallingTinyOS ■ First update the TinyOS source using. ■ Use the following command to install TinyOS on your Ubuntu machine. 5/31/2018 Riman Mandal, [email protected] 13 sudo apt-get update sudo apt-get install tinyos2.1.2
  • 14.
    Step 3 EnvironmentSetup ■ Enter the following command to edit the Environment setup in Ubuntu ■ Add the following line at the end of the ~/.bashrc file. 5/31/2018 Riman Mandal, [email protected] 14 sudo gedit ~/.bashrc #Sourcing the tinyos environment variable setup script source /opt/tinyos-2.1.2/tinyos.sh export CLASSPATH=$CLASSPATH:.
  • 15.
    Step 4 Creatingthe Environment Source File ■ Create the setup script file using the command ■ Now enter the following contents into this file 5/31/2018 Riman Mandal, [email protected] 15 sudo gedit /opt/tinyos-2.1.2/tinyos.sh #echo "Setting up for TinyOS 2.1.2" export TOSROOT= export TOSDIR= export MAKERULES= TOSROOT="/opt/tinyos-2.1.2" TOSDIR="$TOSROOT/tos" CLASSPATH=$CLASSPATH:$TOSROOT/support/sdk/java:$TOSROOT/support/sdk /java/tinyos.jar MAKERULES="$TOSROOT/support/make/Makerules" export TOSROOT export TOSDIR export CLASSPATH export MAKERULES
  • 16.
    Step 5 Permissionfor Setup Script and Refresh Environment ■ Run the command below to allow this script to execute ■ To refresh the environment with new setup - close and start a new terminal or alternatively execute the following command 5/31/2018 Riman Mandal, [email protected] 16 sudo chmod 755 /opt/tinyos-2.1.2/tinyos.sh source ~/.bashrc
  • 17.
    Checking if Installationis successful ■ Run the command to check TOS environment setup is complete or not ■ If it gives warnings related to Java version then do ■ If your version is above 1.5, then ignore this warning else upgrade to a newer java version. Ignore the warning related to graphviz. 5/31/2018 Riman Mandal, [email protected] 17 tos-check-env java -version
  • 18.
  • 19.
    Basic Idea ■ HURRYUP AND SLEEP!! – Sleep as often as possible to save power ■ Programs are built out of components – Libraries and components are written in nesC. – Applications are too -- just additional components composed with the OS components. ■ Each component is specified by an interface – Provides “hooks” for wiring components together ■ Components are statically wired together based on their interfaces – Increases runtime efficiency 5/31/2018 Riman Mandal, [email protected] 19
  • 20.
    Basic Unit ofProgramming ■ Basic unit of nesC code is a component ■ Components connect via interfaces – Connections called “wiring” 5/31/2018 Riman Mandal, [email protected] 20 B A interface
  • 21.
    What is aComponent? ■ A component is a file (names must match) ■ Modules are components that have variables and executable code ■ Configurations are components that wires other components together ■ A component does not care if another component is a module or configuration ■ A component may be composed of other components via configurations 5/31/2018 Riman Mandal, [email protected] 21
  • 22.
    Component Example 5/31/2018 RimanMandal, [email protected] 22 TimerC BlinkC Timer module BlinkC { uses interface Timer<TMilli> as Timer0 provide interface xxxx} implementation { int c; void increment() {c++;} event void Timer0.fired() { call Leds.led0Toggle(); } } configuration BlinkAppC { } implementation { components MainC, BlinkC, LedsC; components new TimerMilliC() as Timer0; BlinkC.Timer0 -> Timer0; BlinkC -> MainC.Boot; BlinkC.Leds -> LedsC; }
  • 23.
    Singletons and Generics 5/31/2018Riman Mandal, [email protected] 23 ■ Singleton components are unique: they exist in a global namespace ■ Generics are instantiated: each instantiation is a new, independent copy configuration BlinkC { … } implementation { components new TimerC(); components BlinkC; BlinkC.Timer -> TimerC; }
  • 24.
    Component Syntax -Module 5/31/2018 Riman Mandal, [email protected] 24 ■ A component specifies a set of interfaces by which it is connected to other components – provides a set of interfaces to others – uses a set of interfaces provided by others module ForwarderM { provides { interface StdControl; } uses { interface StdControl as CommControl; interface ReceiveMsg; interface SendMsg; interface Leds; } } implementation { …// code implementing all provided commands and used events } ForwarderM StdControl ReceiveMsg provides uses CommControl SendMsg Leds
  • 25.
    Component Syntax - Configuration 5/31/2018Riman Mandal, [email protected] 25 configuration Forwarder { } implementation { components Main, LedsC; components GenericComm as Comm; components ForwarderM; Main.StdControl -> ForwarderM.StdControl; ForwarderM.CommControl -> Comm; ForwarderM.SendMsg -> Comm.SendMsg[AM_INTMSG]; ForwarderM.ReceiveMsg -> Comm.ReceiveMsg[AM_INTMSG]; ForwarderM.Leds -> LedsC; } Component Selection Wiring the Components together ForwarderM StdControl ReceiveMsg provides uses CommControl SendMsg Leds Main StdControl LedsC Leds GenericComm SendMsg ReceiveMsg StdControl Forwarder
  • 26.
    Interface 5/31/2018 Riman Mandal,[email protected] 26 ■ Collections of related functions ■ Define how components connect ■ Interfaces are bi-directional: for A->B – Commands are from A to B – Events are from B to A ■ Can have parameters (types) interface Timer<tag> { command void startOneShot(uint32_t period); command void startPeriodic(uint32_t period); event void fired(); }
  • 27.
    Interface (provide anduse) 5/31/2018 Riman Mandal, [email protected] 27 User Provider Interface Commands Events Module BlinkC { use interface xxxx; provide interface xxxxxxx; ......... }
  • 28.
    Interface (provide anduse) (Cont.) ■ A component provides and uses interfaces. ■ A interface defines a logically related set of commands and events. ■ Components implement the events they use and the commands they provide: 5/31/2018 Riman Mandal, [email protected] 28 Component Commands Events Use Can call Must implement Provide Must implement Can signal
  • 29.
    Terminal Commands related toTinyOS ■ system modifications – sudo chmod –R 777 /bin – sudo chmod –R 777 /dev ■ applications directory – /opt/tinyos-2.1.1/apps/ ■ making hex image for iris mote without sensor board – make iris ■ making hex image for iris mote with sensor board – SENSORBOARD=mda100 make iris ■ burning hex image into iris mote – make <mote> install.moteid burnerid,/dev/tty<serialport> – EX. make iris install.0 mib520,/dev/ttyUSB0 – EX. make iris reinstall.0 mib520,/dev/ttyUSB0 ■ burning hex image into iris mote with sensor board ■ SENSORBOARD=mda100 make iris reinstall.0 mib520,/dev/ttyUSB0 5/31/2018 Riman Mandal, [email protected] 29
  • 30.
    Power Up Testing Programing ■Try to build a tinyOS app that turn on the Red Led on Power up. 5/31/2018 Riman Mandal, [email protected] 30 PowerUpC uses Boot Leds MainC Boot LedsC Leds
  • 31.
    Code 5/31/2018 Riman Mandal,[email protected] 31 configuration PowerupAppC{} implementation { components MainC, PowerupC, LedsC; MainC.Boot <- PowerupC.Boot; PowerupC.Leds -> LedsC.Leds; } module PowerupC @safe() { uses interface Boot; uses interface Leds; } implementation { event void Boot.booted() { call Leds.led0On(); } }
  • 32.
    Blink App Using Timer<TMilli>C ■ The Red light will blink continuously with a time interval 5/31/2018 Riman Mandal, [email protected] 32 BlinkC uses Boot Leds MainC Boot LedsC
  • 33.
    Using Printf Component ■Include printf.h header file in the module file ■ Use Printf method to print anything on terminal ■ Add Component PrintfC in the Component File ■ Use the following command to print on the terminal – java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSB1:iris 5/31/2018 Riman Mandal, [email protected] 33 java net.tinyos.tools.PrintfClient -comm serial@/dev/ttyUSBXXX:telosb