scieee Science in your language
[en] (orig)

Towards Out-of-the-Box Wireless Sensor Networks

Read accessible full text

Towards Out-of-the-Box Wireless Sensor Networks

Author: Gil de Castro Ferro
Year: 2015
DOI: 10.34626/4kfm-2a38
Source: https://repositorio-aberto.up.pt/bitstream/10216/82324/2/37975.pdf
Towa ds
Ou -o - he-Box
Wi eless
Senso
Ne wo ks
Gil de Cas o Fe o
Mes ado In eg ado em Engenha ia de Redes e Sis emas In o má icos
Depa amen o de Ciência de Compu ado es
2015
O ien ado
Luís Lopes, P o esso Associado, Faculdade de Ciências da Uni e sidade do Po o
Todas as co eções de e minadas
pelo jú i, e só essas, o am e e uadas.
O P esiden e do Jú i,
Po o, ______/______/_________
This hesis is dedica ed o my belo ed ones, o all he suppo and pa ience
in all he mos impo an momen s o my li e.
Gil de Cas o Fe o
June 2015
I
Acknowledgmen s
I would like o hank my men o , P o esso Lu´ıs Lopes, o he oppo uni y o wo king
wi h him in his p ojec , gi ing me he oppo uni y o do esea ch on one o my a o i e
ields o s udy.
A special hanks o my colleague Ca los Machado, o all he ad ice and expe ise in some
ha dwa e issues.
To my amily, o all he pa ience and suppo , and specially o he uncondi ional lo e.
Las bu no leas , o my iends, who sha ed mos o my g ow h and happiness in his
jou ney.
This wo k was suppo ed by “P ojec RTS - Real Time Languages and Tools o C i ical
Real-Time Sys ems” (con ac NORTE-07-0124-FEDER-000062)
II
Resumo
P og ama Redes de Senso es-A uado es Sem Fios (WSN) n˜ao ´e uma a e a i ial, dada a
a iedade de con igu a¸c˜oes de ha dwa e que s˜ao al amen e dependen es da aplica¸c˜ao inal.
Al´em disso, os sis emas ope a i os e m´aquinas i uais exis en es, na maio ia dos casos
mui o p ´oximos do ha dwa e, o nam es a ecnologia pouco apela i a e, po consequˆencia,
impedem a sua mais ampla dissemina¸c˜ao .
Nes a ese ap esen amos uma no a e s˜ao da a qui e u a SONAR, que em como obje i o
minimiza o es o ¸co necess´a io pa a con igu a , p og ama e implan a uma WSN. Es a
a qui e u a o nece um se i¸co de publish/subsc ibe que pode se u ilizado pelos clien es
pa a acilmen e acede em `as s eams de dados ge adas nos n´os da ede, pe mi indo, ao
mesmo empo, a ges ˜ao da ede, incluindo a sua ep og ama¸c˜ao dinˆamica e debug. A
a qui e u a SONAR ´e compos a po ˆes camadas, que implemen am uma in e ace de
clien e ao es ilo shell, um se i¸co de b oke e um sis ema ope a i o e m´aquina i ual
que ˆem p ´e-ins alados nos n´os da WSN.
Realizamos ainda um conjun o de es es que medem o impac o da nossa solu¸c˜ao em e mos
de empo, consumo de ene gia e mem´o ia nos n´os da ede. Os esul ados ob idos mos am
que o impac o associado `a u iliza¸c˜ao do nosso sis ema ope a i o e m´aquina i ual ´e
ela i amen e pequeno, pa a os pa ˆame os medidos.
Pa a es a a po abilidade da nossa camada de dados, po amos a nossa implemen a¸c˜ao de
uma WSN baseada em A duinos Mega2560 pa a ou a baseada em A duinos Uno. Es es
´ul imos s˜ao signi ica i amen e mais es i os em e mos de ecu sos e con igu a¸c˜oes de
ha dwa e. Es a expe iˆencia mos a que a quan idade de c´odigo que necessi a de se eesc i o
´e mui o pequena (apenas algumas linhas), sendo ainda de e e i que essas al e a¸c˜oes
o am ei as em biblio ecas espec´ı icas pa a componen es de ha dwa e, endo-se man ido
inal e ado o c´odigo e e en e ao sis ema ope a i o e `a m´aquina i ual com exce¸c˜ao de
algumas de ini¸c˜oes b´asicas de pa ˆame os.
III

Abs ac
P og amming Wi eless Senso -Ac ua o Ne wo ks (WSN) is a non- i ial ask, gi en he
mul i ude o ha dwa e con igu a ions ha a e highly dependen on he inal applica ion.
Mo eo e , he exis ing ope a ing sys ems and p og amming languages, mos ly e y close
o he ha dwa e, make he echnology unappealing o he masses and he e o e p eclude
i s wide dissemina ion.
In his hesis we p esen a new e sion o he SONAR a chi ec u e, which aims o minimize
he e o needed o con igu e, p og am and deploy a WSN. The a chi ec u e also p o ides a
publish/subsc ibe se ice ha can be used by clien s o seamlessly access he da a-s eams
gene a ed by he sensing nodes while allowing, a he same ime, he managemen o he
ne wo k, including i s dynamic ep og amming and debugging. SONAR is a h ee-laye
a chi ec u e composed by a shell-like clien in e ace, a b oke se ice, and an ope a ing
sys em and i ual machine ins alled in he nodes o he WSN.
We pe o m a se o es s ha measu e he impac o ou solu ion in e ms o ime, ene gy
consump ion and memo y oo p in on he de ices. The esul s show ha he esou ce
oo p in associa ed wi h ou ope a ing sys em and i ual machine is small in all he
gi en pa ame e s.
To es he po abili y o ou da a laye , we po he implemen a ion om an A duino
Mega2560 based WSN o ano he one based on A duino Uno de ices. The la e a e
signi ican ly mo e cons ained in e ms o esou ces and ha dwa e con igu a ion. This
expe imen shows ha he amoun o code e-w i e is e y small (jus a ew lines) and
hese changes a e done in lib a ies speci ic o ha dwa e componen s. The ope a ing sys em
and i ual machine a e un ouched, excep o he de ini ion o basic cons an pa ame e s.
IV
Ac onyms
AJAX Asynch onous Ja aSc ip and XML. 23
API Applica ion P og amming In e ace. 9
DOM Documen Objec Model. 24
FRP Func ional Reac i e P og amming. 8
GUI G aphical Use In e ace. 3
HTML Hype Tex Ma kup Language. 24
HTTP Hype ex T ans e P o ocol. 14, 24, 25
IoT In e ne o Things. 2, 11, 12
MQTT Message Queue Teleme y T anspo . 13
MQTT-SN Message Queue Teleme y T anspo o Senso Ne wo ks. 13, 14
OS Ope a ing Sys em. 6, 9–13
QoS Quali y o Se ice. 14
REST Rep esen a ional S a e T ans e . 14
SONAR Senso Obse a ion aNd Ac ua ion aRchi ec u e. 3–6, 19, 23, 24
STL SONAR Task Language. 4
STMP Spa io-Tempo al Mac op og amming. 8
V
VM Vi ual Machine. 6, 9, 10, 12, 13
WSN Wi eless Senso Ne wo k. X, 1–7, 9–14, 23
XSS C oss-Si e Sc ip ing. 24
VI
Con en s
Resumo III
Abs ac IV
Lis o Tables XI
Lis o Figu es XIII
1 In oduc ion 1
1.1 Con ex ..................................... 2
1.2 Mo i a ion.................................... 3
1.3 P oblem S a emen and P oposed Solu ion . . . . . . . . . . . . . . . . . . 4
1.4 Ou line...................................... 5
2 Rela ed Wo k 6
2.1 P og amming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.1.1 P og amming Languages O e iew . . . . . . . . . . . . . . . . . . 7
2.2 Ope a ing Sys ems and Vi ual Machines . . . . . . . . . . . . . . . . . . 9
2.2.1 Ope a ing Sys ems O e iew . . . . . . . . . . . . . . . . . . . . . . 10
2.2.2 Vi ual Machines O e iew . . . . . . . . . . . . . . . . . . . . . . 12
VII
Chap e 1
In oduc ion
In he pas 25 yea s, ad ances in ha dwa e manu ac u e and wi eless communica ions
p o ided he means o de elop a new class o embedded de ices, capable o in e connec ing,
sensing physical condi ions and o in e ac ing wi h he en i onmen .
In his con ex WSN a ise as a new pa adigm o ne wo ks. A WSN can be de ined as
a dis ibu ed sys em composed o a a ying numbe o embedded de ices, usually called
nodes, p o ided wi h a p ocessing uni , a wi eless communica ion in e ace, and a se o
senso s/ac ua o s, making hese de ices capable o sensing eal physical en i onmen o
in e ac ing wi h i [1].
Figu e 1.1 depic s an example o a ypical WSN scena io. A se o nodes wi h di e en
senso s/ac ua o s communica e ia a adio in e ace, ansmi ing he sensed da a o a
ga eway (da a collec ion node), which is di ec ly connec ed o compu ing esou ces. A se
o clien s can hen access his da a ia a adi ional ne wo k.
Gi en he cha ac e is ics o WSN nodes, he basic mode o ope a ion o hese ne wo ks
di e s signi ican ly om adi ional ones. In [2], he au ho s p esen h ee main di e ences:
1. Nodes a e highly es ic ed in e ms o ene gy, compu a ional powe and memo y;
2. The design o a WSN is s ongly d i en by each pa icula applica ion;
3. The deploymen o WSN applica ions equi es sel -con igu a ion o nodes and so -
wa e upda es in he ne wo k wi hou human in e en ion.
1

CHAPTER 1. INTRODUCTION 2
Figu e 1.1: WSN ypical scena io example.
The applica ion a eas o WSN goes om medical diagnosis, wildli e moni o ing, a ic
con ol, mili a y sys ems, p ecision ag icul u e, among o he s [3]. Cu en ly, he e is a
end o inc ease he usage o WSN in o new a eas, alongside wi h de elopmen o he
so-called In e ne o Things (IoT) [4].
1.1 Con ex
Despi e hei wide ange o applica ions, WSN a e s ill a he cumbe some o use, especially
by non-specialis s. A ew easons can be iden i ied o why his is so:
•The use needs a high le el o expe ise in con igu ing senso de ices, as well as low
le el p og amming;
•The wide o e o ha dwa e pla o ms ha can be used o deploy WSN con ibu e o
a lack o po abili y o he applica ions;
•In mos o he cases, he dynamic ep og amming o debugging o he deploymen is
a di icul ask o do, impossible in some cases;
•The in eg a ion o a WSN wi h a adi ional ne wo k is no i ial.
CHAPTER 1. INTRODUCTION 3
1.2 Mo i a ion
P ojec Senso Obse a ion aNd Ac ua ion aRchi ec u e (SONAR) [5, 6] pu poses a h ee
laye amewo k ha allows a seamless deploymen o a WSN. Figu e 1.2 depic s he
a chi ec u e used.
Figu e 1.2: P e ious a chi ec u e o SONAR
The Clien Laye p o ides hin-clien s implemen ed wi h a use - iendly Ja a G aphical
Use In e ace (GUI) ha p o ide hooks o access he P ocessing Laye o manage he
deploymen s o o ecei e da a s eams. The managemen ac ions include: add/ emo e
a pe iodic ask o a deploymen , change he pe iod o a unning ask and iew he da a
gene a ed in a unning ask.
The P ocessing Laye is esponsible o he ecep ion and s o age o he da a gene a ed in
he Da a Laye , as well as p o iding emo e me hods so he clien can access his da a. This
laye also p opaga es managemen ac ions o igina ing in he Clien Laye o he unde lying
Da a Laye .
The Da a Laye is composed o a se o nodes and a single ga eway ( he ini ial implemen a-
ion uses nodes based on A duino Mega 2560 [7]). The nodes un a small ope a ing sys em
and i ual machine ha schedule and un by e-code o asks w i en in a domain-speci ic
p og amming language, he SONAR Task Language (STL).
CHAPTER 1. INTRODUCTION 4
The ini ial SONAR a chi ec u e p esen ed se e al limi a ions, i s iden i ied in [6]. These
include:
•non-scalable s o age o da a gene a ed by WSN deploymen s;
•complex managemen o p e-compiled modules equi ed o di e en pla o ms; a
ha dwa e independen o ma is desi able;
•non-scalable, cen alized p ocessing o da a in he P ocessing laye ;
Some p oposed solu ions a e o wa ded in [6] bu an implemen a ion is lacking, oge he
wi h an e alua ion o pe o mance in he ime/ene gy axes.
1.3 P oblem S a emen and P oposed Solu ion
The de elopmen and in eg a ion o sys ems ha make use o WSN is s ill a ha d ask o
do, gi en he signi ican di e ences wi h adi ional ne wo ks.
In his hesis we build on he wo k ini ia ed in [6] and p opose o answe he ollowing
ele an esea ch ques ions:
1. Is i possible o de elop a amewo k o p og amming, con igu ing and deploying
WSN in a seamless way, p o iding clien s wi h he co esponding da a-s eams?
2. Wha is he impac o his app oach on ha dwa e/ene gy esou ces?
3. Can we do his in such a way ha i is po able ac oss WSN and allows dynamic
ep og amming and debugging?
To answe hese ques ions, we di ide he wo k done in his hesis in h ee main phases:
1. Rew i e he Clien and P ocessing Laye o SONAR, implemen ing command-line-
based hin-clien s and a publish/subsc ibe b oke ;
2. E alua e he impac o ou app oach in e ms o compu a ion o e head, ene gy
consump ion, memo y usage, and code size, wi h espec o a na i e A duino, C++-
based solu ion;
CHAPTER 1. INTRODUCTION 5
3. Tes he e o o po ing ou a chi ec u e o di e en ha dwa e nodes.
Wi h espec o he o me implemen a ion o SONAR, he wo k p esen ed in his disse -
a ion in ol ed:
1. he edesign and implemen a ion o he clien and p ocessing laye s, namely pub-
lish/subsc ibe b oke and clien s, and an adminis a ion clien ;
2. ew i ing pa o he i ual machine ins alled in he nodes (da a laye ), and ex ensi e
es ing;
3. signi ican changes o he SONAR Task Language (STL) compile ;
4. pe o mance and esou ces usage e alua ion o a p o o ype po ed o a A duino 2560
based WSN;
5. po he p o o ype o he A duino Uno mic ocon olle .
1.4 Ou line
The emainde o his hesis is o ganized as ollows: Chap e 2 p esen s he s a e-o - he-
a in so wa e sys ems o WSN ele an o his wo k. Chap e 3 p esen s a high-le el
o e iew o he cu en SONAR a chi ec u e, de ailing each laye and i s componen s.
Chap e 4 p esen s he echnical aspec s ela ed o he modi ica ion o he a chi ec u e.
Chap e 5 p esen s a o mal desc ip ion o he SONAR so wa e ha comes p e-ins alled
in he nodes, depic ing he p og amming language used o w i e new asks, he ope a ing
sys em and i ual machine o hese nodes, and he compile we c ea ed o gene a e he
by e-code ep esen a ion o he w i en asks. Chap e 6 p esen s he e alua ion o he
impac associa ed wi h ou p o o ype, in e ms o ime, ene gy consump ion, memo y
usage, and code size compa ing o he same implemen a ions using he A duino na i e
language. In Chap e 7 we desc ibe he p ocess o po ing ou p o o ype o A duino Uno
based nodes wi h g ea e memo y cons ain s, ocusing on he changes made, which we y
o quan i y. Finally, Chap e 8, p esen s a summa y o he conclusions ob ained in wi h
his wo k, as well as a se o changes o be implemen ed as u u e wo k.
Chap e 2
Rela ed Wo k
In his chap e we p esen a su ey on he s a e-o - he-a ele an o his hesis.
Sec ion 2.1 s a s by analyzing he ele an wo k done in he ield o P og amming Lan-
guages o WSN. Sec ion 2.2 desc ibes p ojec s ha de eloped ope a ing sys ems and
i ual machines o WSN. Finally, Sec ion 2.3 ends he chap e wi h exis ing publish/-
subsc ibe sys ems o WSN. Along he chap e we y o es ablish a pa allelism be ween
ou wo k and o he app oaches.
2.1 P og amming Languages
Despi e he ad ances obse ed in he las yea s, p og amming WSN applica ions s ill
emains a ha d ask o do, gi en he wide ange o exis ing p og amming languages, aside
wi h he high he e ogenei y o pla o ms. Mos o he a ailable languages wo k a low
le el and ocus on he de elopmen o speci ic ypes o applica ions. Wi h espec o he
pla o ms, he con igu a ion o he ha dwa e is no a i ial ask. Bo h hese aspec s
in oduce he need o expe use s.
Cu en ly, he e is a conside able ange o p og amming languages o WSN, each wi h a se
o ad an ages/disad an ages in wha conce ns o he de elopmen o a speci ic applica ion.
Nex , we p esen a b ie desc ip ion o h ee di e en languages, each one belonging o a
di e en p og amming pa adigm. To simpli y he analysis, we de ine some axonomy on
he p og amming languages o WSN.
6

CHAPTER 2. RELATED WORK 7
In [8], Gummadi e al. p esen a axonomy ha di ides he p og amming languages in wo
main g oups, acco ding o he ne wo k pe cep ion: mac op og amming and node-cen ic
p og amming. The o me co esponds o an app oach whe e applica ion de elopmen is
done wi hou de ining he beha io o each node indi idually, he la e co esponds o an
app oach whe e he e is he need o he de ini ion, and deploymen o he beha io o each
node.
A di e en axonomy was pu posed by Mo ola e al. in [1]. They p esen an ex ensi e
su ey on a se o di e en aspec s ela ed o p og amming WSN. In his a icle, hey
pu posed a axonomy ela ed wi h he language aspec s, di ided in ou ca ego ies: Com-
munica ion, Compu a ion Scope, Da a Access Model, and P og amming Pa adigm. In
he cu en con ex , we only analyze he P og amming Pa adigm dimension as i is he
mos ele an o his hesis when analyzing he ollowing se o p og amming languages.
Acco ding o Mo olla’s axonomy, he e a e h ee main ypes o p og amming languages:
Impe a i e, Decla a i e, and Hyb id. The i s ype co esponds o he languages whe e
p og ams a e desc ibed in e ms o s a emen s ha change he p og am s a e, which can
be di ided in Sequen ial and E en -D i en languages; he second ype co esponds o he
he languages ha exp esses he logic o a p og am wi hou desc ibing he con ol low,
which can be di ided in Func ional, Rule-Based, SQL-Like and Special-Pu pose; he hi d
ype co esponds o he p og amming languages ha adop a mix u e o he wo p e ious
ones. Figu e 2.1 p esen s a scheme desc ibing he wo axonomies p e iously p esen ed.
Nex , we desc ibe and ca ego ize he se o p og amming languages ha may be ele an
in he cu en con ex o WSN, analyzing he ne wo k pe cep ion and he p og amming
pa adigm o each.
2.1.1 P og amming Languages O e iew
TinyDB [9] is a que y p ocessing sys em ocused in he op imiza ion o ene gy consump-
ion, unning in he op o TinyOS. I inco po a es acquisi ional echniques along wi h
adi ional que y echniques, aking ad an age om he ac ha senso s possess con ol
o e when, whe e and how pe iodically he da a is sensed and deli e ed o que y p ocesso s.
Madden e al. de ail in [9] all he aspec s ela ed o he que y language implemen ed in
TinyDB, as well as he op imiza ions made in o de o minimize he ene gy consump ion,
he que y dissemina ion in he sys em and, inally, he model c ea ed o que y execu ion
and esul collec ion.
CHAPTER 2. RELATED WORK 8
(a) Ne wo k Pe cep ion (b) P og amming Pa adigms
Figu e 2.1: Ne wo k Pe cep ion and P og amming Pa adigms
nesC [10] is an e en -d i en p og amming language ha ex ends he C language, buil
a op o TinyOS. I is based in componen s ha a e assembled o o m p og ams, as
well as bidi ec ional in e aces, which speci y he componen s beha io in e ms o hei
in e aces. The in e aces speci y he unc ions o be implemen ed by he in e ace’s
p o ide (commands) and by he use o he in e ace (e en s). nesC s a ic links he
componen s ia hei in e aces, inc easing hei un ime e iciency and obus ness [11].
Gay el al. de ail in [10] he design o nesC, as well as summa y o hei expe ience wi h i .
Regimen [12] is spa ial mac op og amming language and un ime en i onmen , wi h a
compile compile ha a ge s a ligh weigh in e media e ep esen a ion called he Token
Machine Language. I is based in he concep o Func ional Reac i e P og amming, a p o-
g amming pa adigm ha uses some building blocks o unc ional p og amming languages,
like map, educe, o il e . I was designed o suppo Spa io-Tempo al Mac op og amming
applica ions. In Regimen , he ne wo k is seen by he p og amme as a se o spa ial-
dis ibu ed and ime- a ying signals, which ep esen s he s a e o an indi idual node o
egion agg ega e. Gi en his ac , he de elopmen o an applica ion using Regimen is
based in he usage o h ee main language concep s:
1. Signals, which a e p incipal objec he de elope con ol;
CHAPTER 2. RELATED WORK 9
2. Regions, which ep esen a collec ion o signals;
3. Nodes, which allows he de elope o access he s a e o an indi idual node.
A Regimen p og am is ansla ed o a node-le el p og am using he language compile . In
his p ocess, he p og am code is i s educed o an in e media e language called RQue y,
which is inally ansla ed o node-le el code. In he p ocess, he compile pe o ms many
s ages o node no maliza ion, analysis and op imiza ions. New on e al. p esen in [12]
an o e iew o he Regimen language, desc ibing in de ail he espec i e compile and
deglobaliza ion echniques, as well as an e alua ion o pe o mance o some e en -de ec ion
in simula ion.
Table 2.1 summa izes he in o ma ion o he h ee p og amming languages p esen ed ha
a e ep esen a i e o he s a e-o - he-a .
P og. Language Ne wo k Pe cep ion P og. Pa adigm
TinyDB Mac op og amming Decla a i e, SQL-Like
nesC Senso -Based Impe a i e, Sequen ial
Regimen Mac op og amming Decla a i e, Func ional
Table 2.1: P og amming Languages classi ica ion
2.2 Ope a ing Sys ems and Vi ual Machines
The es ic ed esou ces ha cha ac e izes WSN makes he usage o a adi ional Ope a ing
Sys em (OS) imp ac icable, gi en he ac ha adi ion OS a e designed o de ices
wi h signi ican ly mo e esou ces. These di e ences should be aken in o accoun when
de eloping a OS o nodes in a WSN.
The e a e a se o unc ionali ies ha an OS should p o ide and ha include: esou ce
abs ac ions o di e en ha dwa e de ices, in e up managemen , ask scheduling, con-
cu ency con ol and ne wo king suppo . Mo eo e , he OS should p o ide he applica ion
p og amme s high-le el Applica ion P og amming In e ace (API), independen o he
unde lying ha dwa e [13].
Concu en ly, i ual machines p esen ed ano he impo an app oach in de eloping WSN.
Despi e he g ea ad an ages o using OS, like pe o mance op imiza ion and he educ ion
CHAPTER 2. RELATED WORK 10
o ene gy consump ion, he lack o in e ope abili y and ep og amming o he ne wo k
makes hem no o ally sa is ac o y. In his con ex , Vi ual Machine (VM) allow a mo e
lexible model o applica ion de elopmen , e en ually wi h some pe o mance and ene gy
e iciency penal ies.
In he nex wo subsec ions we desc ibe he mos impo an wo ks done in hese a eas,
ocusing in he main cha ac e is ics o each one. This pa o he s udy will help unde -
s anding he cu en app oaches ha a e being used when de eloping OS and VM o
WSN.
2.2.1 Ope a ing Sys ems O e iew
TinyOS is one o he mos used OS o WSN, which can nowadays be hough as a s anda d.
I is a iny mul i h eaded OS whose implemen a ion ies o gua an ee concu en da a low
among ha dwa e de ices, p o iding modula ized componen s wi h a small p ocessing and
s o age o e head. I ollows an E en -based model designed o suppo high le els o
concu en applica ions in a small amoun o memo y, using a simple FIFO mechanism o
ask scheduling. Le is e al. [14] p esen s a comple e desc ip ion abou he sys em whe e
hey analyze he sys em componen s, execu ion models and suppo o concu ency.
Con iki [15] is an open-sou ce OS designed o ne wo ked embedded de ices, implemen ed
in he C language. A unning ins ance o Con iki is composed o a ke nel, a se o lib a ies,
p og am loade and a se o p ocesses. In Con iki all he communica ion be ween p ocesses
goes h ough he ke nel, gi en he ac ha i does no p o ide a ha dwa e abs ac ion
laye . Howe e , i allows de ices and applica ions o access he ha dwa e di ec ly. This
OS allows p eemp i e mul i- h eading, implemen ed as a lib a y on op o he e en -based
ke nel, which can be op ionally linked when implemen ing applica ions ha equi e a
mul i- h eading model o ope a ion. Con iki’s ke nel is a ligh weigh e en schedule ha
dispa ches e en s o unning p ocesses, wi h a pe iodical call o p ocess polling handle s.
One pa icula i y o Con iki is he implemen a ion o a ligh weigh uIP TCP/IP s ack,
allowing IP 4 and IP 6 add essing wi h a small oo p in . Applica ion de elopmen in
Con iki is made using he C language and i allows an o e - he-ai p og amming o he
en i e ne wo k. Dunkel e al. [15] p esen s a ull desc ip ion o he Con iki, analyzing in
de ail he cons uc ion o he ke nel and he p eemp i e mul i- h eading. The au ho s also
analyze how Con iki handles he lib a ies and how i suppo communica ion, inishing he
a icle p esen ing some bed es s using his OS.
CHAPTER 3. SONAR ARCHITECTURE 17
To manage he WSN he owne uses a simple shell in e ace ha is also supplied wi h
he so wa e. He can now lis all he unning asks, se he pe iod o a ask, and add o
emo e a ask in he ne wo k. These asks a e w i en using a e y simple speci ic domain
p og amming language, STL, which we add ess la e in his hesis.
To moni o he da a p oduced a his WSN, he owne simply connec s o he B oke o he
publish/subsc ibe sys em. I hen selec s he da a s eams i is in e es ed in om he WSN.
This can be done o mul iple WSN (e.g., mo e g eenhouses, he ga den, he house), and
o many asks unning on he nodes o a WSN (e.g., empe a u e and humidi y, mo emen
de ec ion, luminosi y), p o iding a mul iplici y o da a s eams ha can be subsc ibed by
use s.
This is he le el o seamlessness ha we aim o add ess wi h ou a chi ec u e, SONAR. In
he ollowing sec ions we desc ibe he componen s o each laye o he a chi ec u e.
3.2 A chi ec u e O e iew
SONAR ollows a ypical 3 laye a chi ec u e, depic ed in igu e 3.1. I is based on a
publish/subsc ibe a chi ec u e whe e a se o clien s, connec ed o he In e ne , access he
da a gene a ed a each SONAR deploymen h ough he SONAR B oke .
The da a laye is composed by a se o nodes ha come wi h a p e-ins alled ope a ing
sys em and i ual machine, and a ga eway, ha collec s he da a p oduced in he nodes.
Each node can schedule and un mul iple asks. A ask can be desc ibed as a p og am
ha pe iodically uns in he nodes, wi h no in e up ions, and ha gene ally p oduces a
da a-s eam. The ga eway ac s as a simple o wa de , exchanging da a om he nodes
wi h he o he laye s.
A clien , when connec ed o he B oke , can access a lis con aining in o ma ion abou he
egis e ed deploymen s. Fo each deploymen , i is p esen ed he se o unning asks, wi h
he ollowing pa ame e s: ask desc ip ion, ask pe iod, and ype o gene a ed da a. The
clien can subsc ibe he desi ed da a s eams and ecei es he espec i e da a p oduced in
he da a laye .
The managemen o he each deploymen is made using an adminis a ion clien ha
connec s di ec ly o a deploymen adap e . Wi h his clien , a use is able o manage his
deploymen , adding o emo ing asks, o changing he pe iod o a unning ask.

CHAPTER 3. SONAR ARCHITECTURE 18
Figu e 3.1: SONAR a chi ec u e.
3.3 Clien Laye
The Clien laye is composed by wo di e en modules included in he so wa e he use
mus ins all in his compu e :
•a Publish/Subsc ibe Clien , used o connec o a SONAR B oke (middle laye ),
allowing he use o lis and subsc ibe he a ailable deploymen s, ecei ing he da a
p oduced a e he subsc ip ion;
•an Adminis a ion Clien ha allows au hen ica ed use s o access a deploymen ,
h ough a componen called Adap e , and o manage i , sending con ol messages.
CHAPTER 3. SONAR ARCHITECTURE 19
3.3.1 Publish/Subsc ibe Clien
The Publish/Subsc ibe Clien is a shell-based in e ace whe e a use can access he me hods
o lis and subsc ibe a da a s eam, a ailable in a SONAR B oke . Using his module, a use
can send da a messages, allowing him o lis all he a ailable deploymen s and espec i ely
unning asks, subsc ibe and unsubsc ibe, asks and que y he B oke o a speci ic ype
o da a being p oduced in all he a ailable deploymen s. Figu es 3.2a and 3.2b depic ,
espec i ely, he da a low o lis ing o subsc ibing a ask and he da a low o publishing
da a gene a ed a he Da a laye o a clien . To subsc ibe a da a s eam, he use sends a
eques o he b oke indica ing he ID o he desi ed da a s eam. The B oke pa ses ha
eques and hen esponds o he clien wi h a message con i ming a success ul subsc ip ion.
When new da a is a ailable in he da a laye , he espec i e adap e o wa ds ha da a
o he B oke , who checks he clien s subsc ibing hese da a-s eams. I hen sends he
ecei ed da a o each subsc ibe .
3.3.2 Adminis a ion Clien
The Adminis a ion Clien is he componen used by he use s o adminis e deploymen s,
namely, o egis e and un egis e i own deploymen in a B oke , as well as o manage i ,
allowing him o add, emo e o change he pe iod o a unning ask. Figu es 3.3a and 3.3b
depic , espec i ely, he da a low in he p ocess o egis e /un egis e he deploymen and
he da a low when adding, emo ing, o changing he pe iod o a ask.
To egis e a deploymen , he use sends a con ol message o he Adap e , who sends a
egis e eques o he B oke con aining he MAC Add ess o he ga eway. The B oke
p ocesses he eques and esponse wi h a con i ma ion ha he deploymen had been
egis e ed. To manage his own deploymen , he use sends a message con ol message
o he Adap e con aining he managemen command, which is hen o wa ded o he
deploymen ga eway node. A e ecei ing he command, he ga eway adio he command
o all he nodes in he deploymen .
CHAPTER 3. SONAR ARCHITECTURE 20
(a) Da a low o lis ing and subsc ibing a ask (b) Da a low o publish mechanism
Figu e 3.2: Da a low in he Publish/Subsc ibe Clien
CHAPTER 3. SONAR ARCHITECTURE 21
(a) Da a low in a deploymen egis e (b) Da a low in asks managemen
Figu e 3.3: Da a low in he Adminis a ion Clien
CHAPTER 3. SONAR ARCHITECTURE 22
3.4 B oke Laye
The B oke is he middle laye o ou a chi ec u e, which main ains a connec ion wi h
one (o mo e) SONAR Adap e s, as well as wi h a se o SONAR Clien s. This com-
ponen is esponsible o he h ee main asks: egis e ing deploymen s, publishing da a
p o ided by he da a laye , and handling subsc ip ion eques s om clien s. To allow hese
unc ionali ies, he B oke s o es h ee main s uc u es:
•a asks s uc u e ha s o es all he pa ame e s o one da a s eams egis e ed in
he B oke . Each ask ep esen a ion is composed by ou pa ame e s: an ID ha
iden i ies inequi oquely he ask; a pe iod ha indica es he pe iodici y o he ask; a
alues desc ip ion ha p esen s he da a p oduced in ha ask; he uni s desc ip ion
ha p esen s he uni s o each alue p oduced; and, he ask in o, an op ional ield
used o s o e some ex a in o ma ion abou he ask.
•adeploymen s able ha s o es all he egis e ed deploymen s. Fo each deploy-
men , his able maps a pai o pa ame e s: an gene al in o ma ion ield (op ional),
con aining some gene alis in o ma ion abou he deploymen and a lis o asks,
con aining he ID o each ask unning in ha deploymen .
•asubsc ibe s able ha , o each a ailable da a s eam, maps a lis o subsc ibe s
IDs, iden i ying he clien s subsc ibing ha da a s eam.
3.4.1 Da a Flow in SONAR B oke
The e a e wo ypes o da a lows ela ed wi h he B oke : he i s is ela ed wi h he
messages sen by he SONAR Clien s, when lis ing, subsc ibing o unsubsc ibing a da a
s eam; he second is ela ed wi h he da a p oduced a he da a laye , which is ecei ed
om he Adap e o be o wa ded o he espec i e subsc ibe s.
Clien laye ↔B oke messages Figu es 3.4 and 3.5 desc ibe he messages low
exchanged be ween he Clien and he B oke when he Clien is subsc ibing a da a s eam.
The Clien s a s by lis ing all he a ailable da a s eam. Figu e 3.4 depic s he da a low
in ol ed

CHAPTER 3. SONAR ARCHITECTURE 23
1. he clien send a message o he B oke asking o all he a ailable da a s eams;
2. he B oke ecei es he message, que ies i s deploymen s able and p oduces a mes-
sage con aining a summa y wi h he a ailable deploymen s and he espec i e da a
s eams.
3. The B oke esponds o he clien wi h he message con aining he pa ame e s needed
so ha he Clien can subsc ibe he desi ed asks;
Figu e 3.4: A ailable da a s eams
A e ecei ing a lis o he a ailable da a s eams, he clien is able o subsc ibe hem
(Figu e 3.5):
1. The Clien sends a message o he B oke con aining he IDs o he da a s eams o
be subsc ibed;
2. he B oke ecei es ha message and add he clien ID o he en y o he subsc ibe s
able;
3. he B oke esponds o he Clien , con i ming ha he is now subsc ibing ha da a
s eams.
CHAPTER 3. SONAR ARCHITECTURE 24
Figu e 3.5: Subsc ibing/Unsubc ibing a da a s eam
Da a laye ↔B oke messages Figu e 3.6 desc ibes he mechanism o publishing
a message. When he B oke ecei es a message con aining Da a om one Adap e , i
pa ses he message and e ie es wo pa ame e s: he iden i ie o he deploymen whe e
he da a was gene a ed and he iden i ie o he ask who gene a ed he da a. Using ha
in o ma ion, i que ies i s Subsc ibe s Table and e ie e he iden i ie s o he Clien s who
a e subsc ibing ha ask. Using hese iden i ie s, he B oke o wa ds he messages o he
espec i e clien s.
Figu e 3.6: Da a deli e y o subsc ibe o a gi en ask.
CHAPTER 3. SONAR ARCHITECTURE 25
3.5 Da a Laye
The Da a laye abs ac all he senso s and ac ua o s p esen in each deploymen . I is
composed by h ee di e en componen s:
•SONAR Adap e ;
•ga eway nodes;
•a mesh o Nodes.
3.5.1 Adap e
The Adap e is a so wa e componen ha es ablishes connec ions wi h o he wo compo-
nen s: he Adminis a ion Clien and he Ga eway. I is esponsible o wo main ac ions:
1. Adminis a ion: allows use s o adminis a e he aspec s ela ed o his SONAR
Wi eless Senso Ne wo k deploymen using he Adminis a ion Clien . This connec-
ion is only ac i e when he adminis a o is using he Adminis a ion Clien .
2. Fo wa ding: ga he s coming messages om he Ga eway wi h he da a gene a ed
in each node and o wa d i o he B oke . This connec ion is always ac i e when
he deploymen is unning.
Figu e 3.7 depic s he da a low associa ed wi h he o wa ding unc ion o he Adap e .
Red a ows and adio signals indica e he pa h done by he da a gene a ed a he nodes.
Fo each unning ask ha p oduces da a, each node sends ha da a o he deploymen
Ga eway, who analyze he message and add pa ame e s o unequi ocally iden i y he o igin
o he message. A e ha , i simply o wa d he message o he adap e , who hen o wa d
i o he B oke .
3.5.2 Ga eway and Nodes
The Ga eway is a ypical node equipped wi h a adio de ice capable o ecei ing and
o wa ding asks o a p e iously con igu ed se o nodes. I is esponsible o adio he
CHAPTER 3. SONAR ARCHITECTURE 26
Figu e 3.7: Da a low gene a ed in nodes
commands and asks sen by he adminis a o o all he nodes, as well as ecei ing he
da a p oduced in he nodes and o wa ding i o he adap e . Figu e 3.8a depic s a high
le el ha dwa e ep esen a ion o a ypical node and Figu e 3.8b p esen s a pic u e o a
SONAR Ga eway.
(a) Typical node scheme (b) SONAR Ga eway Senso .
Figu e 3.8: Typical node scheme and SONAR Ga eway node
The nodes used in SONAR ollow he same ha dwa e con igu a ion as he ga eway, wi h
he addi ion o some senso s and ac ua o s. Cu en ly, hey con ain empe a u e, humidi y,
and ligh senso s, as well as a LED unc ioning as an ac ua o .
In he nex chap e we a e going o o mally desc ibe he so wa e ins alled in he ga eway
CHAPTER 4. CLIENT LAYER AND BROKER LAYER 33
Py hon Sc ip Code 4.2.1 Py hon Sc ip o inse da a ecei ed in he STDIN o a
MySQL Da abase
Impo MySQLdb
con ne c io n = MySQLdb . connec (
hos = ” l o c a l h o s ” ,
use = ” oo ” ,
passwd= ” oo passwd ” ,
db= ” oom1 .83 dep ”)
c u s o = conn . c u s o ( )
whi l e u e :
da a = aw inp u ( ) . s p l i (” ”)
deploymen = da a [ 0 ]
a s k i d = da a [ 1 ]
mac senso = da a [ 2 ]
da a ype = da a [ 3 ]
alue = da a [ 4 ]
y :
c u s o . e xe cu e (” ’ ’ INSERT INTO da a VALUES (%s ,%s ,%s ,%s ,% s ) ” ’ ’ ,
( deploymen ,
a s k i d ,
mac senso ,
da a ype ,
alue))
excep :
conn . o l l b a c k ( )
conn . commi ()
conn . c l o s e ( )

Chap e 5
Da a Laye
In his chap e we p esen he speci ica ion and implemen a ion o he SONAR da a laye .
This so wa e uns in he ga eway and nodes o he deploymen s and is p e-ins alled.
I includes an ope a ing sys em, a domain-speci ic p og amming language, and a i ual
machine.
In Sec ion 5.1 we desc ibe he p og amming language used o implemen asks in SONAR,
SONAR Task Language (STL). The ea e , in Sec ion 5.2 we desc ibe he SONAR Vi ual
Machine and he compile used o p oduce he by e-code execu ed in he nodes. Sec ion 5.3
desc ibes he ope a ing sys em unning bo h in he nodes and in he ga eway. This chap e
ends wi h Sec ion 5.4 ha desc ibes he da a low associa ed wi h each node.
5.1 P og amming Language
In his sec ion we desc ibe he syn ax and seman ics o he domain-speci ic p og amming
language used o implemen pe iodic asks - he SONAR Task Language (STL).
5.1.1 Syn ax
The syn ax o asks is desc ibed in Figu e 5.1. The no a ion ˜αis used o deno e a sequence
o pai wise dis inc elemen s, α, o a gi en syn a ic ca hego y. A ask Tuses wo se s o
iden i ie s, sand a, o speci y he a ailable senso s and ac ua o s in a gi en pla o m. Each
34
CHAPTER 5. DATA LAYER 35
T::= senso s {s1:σ1. . . sn:σn}Tasks
ac ua o s {a1:σ1. . . am:σm}
ini {˜q}
[˜τ]loop {˜ }
σ::= ˜τ7→ τTypes
τ::= bool |in | loa | oid
q::= τ x = Ini ializa ions
::= x=eIns uc ions
|a(˜e)
| adio [˜e]
|i e{˜ }else {˜ }
|while e{˜ }
e::= s(˜e)|eop e|op e|(e)| Exp essions
::= x|uValues
u::= bools |in s | loa s Cons an s
Figu e 5.1: The syn ax o STL.
o hese iden i ie s maps o a unique senso o ac ua o in he ha dwa e. This decla a ion
is hus simila o all asks unning on he same ha dwa e con igu a ion and in a mo e
conc e e syn ax would simply be included by he p og amme using a compile di ec i e.
The code ha is ac ually speci ic o he ask s a s wi h he ini block, used o ini ialize
global ask a iables. This code is no execu ed, a he he compile copies he ini ial
alues o each a iable di ec ly o he da a segmen o he by e-code gene a ed o he
p og am. The loop block, on he o he hand, is he code execu ed o e e y (pe iodic)
ac i a ion o he ask. I is immedia ely p eceded by he ype o message sen back by
he ask o he ga eway using he cons uc [˜τ]. The ask only sends messages o his
ype o he ga eway and he ype is checked agains all adio s a emen s in he ask. The
ins uc ions a ailable o he p og amme include: assignmen , ac ua ion - a(˜e), sending a
se o e alua ed exp essions o he ga eway - adio [˜e], a condi ional execu ion cons uc -
i e{˜ }else {˜ }, and a while loop - while e{˜ }. The exp essions a e s anda d excep o
s(˜e) ha is used o ead a alue om a gi en senso .
As we said, o a gi en pla o m and con igu a ion, he ha dwa e desc ip ion p o ided by
he cons uc s senso s and ac ua o s is he same. We use a p ep ocessing di ec i e - use - o
include his desc ip ion a he op o all p og amming examples in his hesis (Figu e 5.1.1).
CHAPTER 5. DATA LAYER 36
STL Code 5.1.1 Ha dwa e desc ip ion o A duino 2560 p o o ype WSN - ile ”a d2560.hw”.
senso s {
empe a u e : oid −> loa ,
humidi y : oid −> loa ,
l i g h : oid −> loa
}
ac ua o s {
led :bool −> oid
}
The example in Figu e 5.1.2 shows a STL p og am ha a each ac i a ion eads he em-
pe a u e and humidi y and adios he alues o he ga eway. A simila implemen a ion in
A duino C++ is p esen ed in Appendix A.0.1. The example uses wo senso s, designa ed as
empe a u e and humidi y, whose ypes a e decla ed in ha dwa e desc ip ion ile “a d2560.hw”.
No ice ha he pe iodici y o he ask is no included in he code. I is an ex e nal a ibu e
se wi h he adminis a ion clien when he ask is sen o he ga eway o be adioed o he
nodes. In his way, use s wi h adminis a ion access can dynamically change he pe iod o
unning asks using simple con ol messages.
STL Code 5.1.2 STL p og am ha eads he empe a u e and humidi y and adio he
esul s o he ga eway.
use ” a d2560 . hw”
ini {
loa = 0 . 0 ;
loa h = 0 . 0 ;
}
[ loa @ ” empe a u e : C e l s i u s ” ,
loa @ ” Humidi y : Pe cen ag e ” ]
loop {
= empe a u e ( ) ;
h = humidi y ( ) ;
adio [ , h ] ;
}
The language speci ica ion is comple e wi h bo h he ope a ional and s a ic seman ics ha
oge he de ine how well- o med p og ams a e execu ed.
The ope a ional seman ics is de ined h ough a educ ion ela ion →on he p og am s a e.
The la e is de ined as ei he he hal ed s a e, ⊥, o , i he ask is ac i e, as a uple
CHAPTER 5. DATA LAYER 37
(S, A, V, ˜ ). In he la e , Sand Aa e o ype Se (Va s) and keep he iden i ie s o he
buil -in unc ions decla ed a he beginning o an STL p og am and ha p o ide access o
senso s and ac ua o s, espec i ely; V, o ype Map(Va s,Values) keeps he alues o he
a iables du ing he execu ion o he p og am. Thus, he ini ial s a e o he ask:
senso s {s1:σ1. . . sn:σn}
ac ua o s {a1:σ1. . . am:σm}
ini {˜q}[˜τ]loop {˜ }
is he uple (S0, A0, V0,˜ ), whe e:
S0={s1, . . . , sn}
A0={a1, . . . , am}
V0={(x: )|τ x = ∈˜q}
The educ ion ules a e p esen ed in Figu es 5.2 and 5.3, whe e he iden i ie s sand aa e
buil -in unc ions, as well as he unc ion adio. We also simpli y he no a ion somewha by
no including Sand Aexplici ly in he s a e, i.e., we ep esen he uple (S, A, V, ˜ ) uple
as he sho e e sion (V, ˜ ). The ules ha e he s uc u e:
c1. . . cn
(V1,˜ 1)→(V2,˜ 2)
whe e he cia e p econdi ions o ac ions ha mus be ul illed o make he ansi ion om
he cu en s a e, (V1,˜ 1), o a gi en s a e, (V2,˜ 2), possible. Fo example, ule (2) o
ins uc ions execu es a(˜e) s a emen s, unde lined and he nex in he code sequence. I
e alua es he exp essions ˜ein o alues ˜ i s . I hen calls a buil -in unc ion w i e(a, ˜ )
ha ac ually pe o ms he low-le el ope a ion o he p og am. When i e u ns he s a e
o he p og am is (V, ˜ ). The easoning is simila in ule (2) o exp essions, whe e we
ead da a om a senso . He e, howe e , he alue e u ned om he buil -in unc ion,
= ead(s, ˜ ), is he alue o he exp ession. Rule (8) o ins uc ions, ano he example,
is in oked when he code sequence in he ex block ends, he nex s a e is ⊥.
CHAPTER 5. DATA LAYER 38
=e al(V, e)
(V, x =e˜ )→(V+{x: },˜ )(1)
˜ =e al(V, ˜e)a∈Aw i e(a, ˜ )
(V, a(˜e) ˜ )→(V, ˜ )(2)
˜ =e al(V, ˜e)send(˜ )
(V, adio [˜e] ˜ )→(V, ˜ )(3)
e al(V, e) = ue
(V, i e{˜ 1}else {˜ 2}˜ 3)→(V, ˜ 1˜ 3)(4)
e al(V, e) = alse
(V, i e{˜ 1}else {˜ 2}˜ 3)→(V, ˜ 2˜ 3)(5)
e al(V, e) = alse
(V, while e{˜ 1}˜ 2)→(V, ˜ 2)(6)
e al(V, e) = ue
(V, while e{˜ 1}˜ 2)→(V, ˜ 1while e{˜ 1}˜ 2)(7)
(V, )→ ⊥ (8)
Figu e 5.2: Reduc ion ules o STL ins uc ions.

CHAPTER 5. DATA LAYER 39
˜e=e1. . . en i=e al(V, ei),1≤i≤n
e al(V, ˜e) = ˜ (1)
˜ =e al(V, ˜e)s∈S = ead(s, ˜ )
s(˜e) = (2)
1=e al(V, e1) 2=e al(V, e2)
e al(V, e1op e2) = 1op 2
(3)
=e al(V, e)
e al(V, op e) = op (4)
e al(V, x) = V(x) (5)
e al(V, ) = (6)
Figu e 5.3: Reduc ion ules o STL exp essions.
5.1.2 S a ic Seman ics
The s a ic seman ics o a ask is p o ided in he o m o a ype sys em (Figu e 5.4). The
ules a e ai ly s anda d and use a yping en i onmen Γ ha keeps ack o he ypes o
iden i ie s. The ules a e w i en as Γ ` o ins uc ions, meaning ha he ins uc ion is
well- o med, and Γ `e:τ o exp essions, meaning ha exp ession ehas ype τ. Some
ules ha e side e ec s, in which he en i onmen Γ is en iched wi h new en ies and becomes
Γ0, as in Γ ` · · · a Γ0. An example is ule (4): Γ `[˜τ]aΓ, adia es : ˜τ(Γ0is Γ plus he ype
collec ed om he adia es cons uc ). Besides his ule, h ee o he s a e wo hy o no e.
Rule (10) checks ha messages sen by he ask ha e ypes ha ma ch he one decla ed
in he adia es cons uc . Rule (12) checks ha he senso , s, is o ype ˜τ7→ τ0, ha he
a gumen s ˜ema ch he ype ˜τ o in e ha he alue e u ned by s(˜e) is o ype τ0. The
logic is simila o ule (9), whe e he ype sys em jus checks ha he ins uc ion a(˜e) is
well o med (ins uc ions do no e alua e o alues). Rules (15), (16), and (17) a e axioms
and allow booleans, in ege s, and loa ing poin alues o be yped.
CHAPTER 5. DATA LAYER 40
∅ ` senso s {s1:σ1. . . sn:σn} a Γ1
∅ ` ac ua o s {a1:σ1. . . am:σm} a Γ2
∅ ` ini {q1. . . ql} a Γ3
Γ1,Γ2,Γ3, adia es : ˜τ`loop {˜ }
`
senso s {s1:σ1. . . sn:σn}
ac ua o s {a1:σ1. . . am:σm}
ini {q1. . . ql}
[˜τ]loop {˜ }
(1)
∅ ` senso s {s1:σ1. . . sn:σn}a{s1:σ1. . . sn:σn}(2)
∅ ` ac ua o s {a1:σ1. . . am:σm}a{a1:σ1. . . am:σm}(3)
Γ`q1aΓ1Γ`qlaΓl
Γ`ini {q1. . . ql} a Γ1,...,Γl
(4)
∅ ` :τ
Γ`τ x = aΓ, x :τ
Γ`˜
Γ`loop {˜ }(5,6)
Γ` 1. . . Γ` n
Γ`˜
Γ`x:τΓ`e:τ
Γ`x=e(7,8)
Γ`a: ˜τ7→ oid Γ`˜e: ˜τ
Γ`a(˜e)(9)
Γ`˜e: ˜τΓ( adia es) = ˜τ
Γ` adio [˜e](10)
Γ`e:bool Γ`˜ 1Γ`˜ 2
Γ`i e{˜ 1}else {˜ 2}(11)
Γ`s: ˜τ7→ τ0Γ`˜e: ˜τ
Γ`s(˜e) : τ0(12)
Γ(x) = τ
Γ`x:τ(13)
Γ`e1:τ1. . . Γ`en:τn
Γ`e1. . . en:τ1. . . τn
(14)
∅ ` :bool ∅ ` :in ∅ ` : loa (15,16,17)
Figu e 5.4: Type sys em o STL.
CHAPTER 5. DATA LAYER 41
p::= h d b P og am
h::= i1i2Heade
d::= ˜ Da a Segmen
::= bools |in s | loa s Values
b::= ˜ Tex Segmen
::= ld i|s i|w i1i2| d i1i2Ins uc ions
| ad i|b i|jp i| e
|bop |uop
Figu e 5.5: By e-code syn ax.
5.2 Compile and Vi ual Machine
In his sec ion we gi e he speci ica ion o he SONAR Vi ual Machine (SVM), one o he
modules p e-ins alled in he nodes. The i ual machine execu es STL asks, ansla ed
in o by e-code by a compile . We begin by de ining he by e-code o ma and hen gi e
he ansla ion unc ion o he STL sou ce code.
5.2.1 Fo mal Desc ip ion
The by e-code is composed o 4 segmen s: heade , da a, s ack, and ex (Figu e 5.5). The
heade con ains he o al size o he by e-code as well as he o se o he beginning o
he ex segmen . The s ack segmen is alloca ed be ween he da a and ex segmen ,
g owing owa ds he lowe add esses. I s size is calcula ed a compile ime since he e a e
no calls o use de ined unc ions. The da a segmen p o ides space o all he a iables
in a STL p og am. Cons an s and he ini ial alues o global a iables a e s o ed he e
by he compile . The da a segmen can be seen as he only ac i a ion eco d equi ed o
he i ual machine since, again, he e a e no calls o use unc ions o use unc ions in
asks. All a iables, o ypes bool,in , and loa , use 4 by es in he da a segmen in his
e sion, bu his can and should be op imized o minimize he size o he by e-code. The
ex segmen is composed o ins uc ions ha ha e a 1 by e opcode and e en ually 1 o
2 ex a by es o a gumen s. The e a e ins uc ions o loading a alue o he s ack (ld ),
s o ing a alue om he s ack (s ), sending an ac ua ion command (w ), eading a senso
( d ), sending a message o e he adio ( ad ), he usual con ol low (b ,jp , e ) and, he
usual in ege and loa ing-poin a i hme ic and logic and ela ional ope a o s (bop ,uop ).
CHAPTER 5. DATA LAYER 42
By e-code ins uc ions map almos one- o-one wi h educ ion ules om he ope a ional
seman ics.
This co espondence is impo an o p o ing ha he i ual machine co ec ly execu es
he by e-code, bu his is a p oblem we will no add ess he e.
The ansla ion unc ion ecei es a syn ac ic e m and e u ns a pai o sequences (D, B)
(Figu es 5.6 and 5.7). The i s , D, is he con ibu ion o he e m o he da a segmen , he
la e , B, is he con ibu ion o he ex segmen . The op le el ansla ion unc ion [[·]], o
STL asks, b eaks he ansla ion in o a sequence o pai wise conca ena ions (ope a o ”:”)
and uses app op ia e ansla ion unc ions o each syn ac ic ca ego y. We use he same
[[·]] o simpli y he no a ion, bu hese should be seen as dis inc unc ions. The ansla ion
unc ion uses 3 se s which hold in ege iden i ie s o senso s and ac ua o s, Sand A, and
da a segmen o se s o a iables (se Va ) and cons an s (se Cons ), Vand U, de ined
as ollows:
S={(si, i)|si∈gs:τ}
A={(ai, i)|ai∈ga:τ}
V={(x, i)|x∈Va ∧i= o se (x)}
U={(u, i)|u∈Cons ∧i= o se (u)}
The ansla ion unc ion is qui e s aigh o wa d. The ansla ion o an ac ua ion com-
mand, a(˜e), is simply he ansla ion o he a gumen s ˜e, ollowed by a w ins uc ion
wi h he in ege iden i ie o he ac ua o A(a) and he numbe o exp essions, |˜e|, as
he a gumen s. Simila ly, eading a senso , s(˜e), ansla es in o he ansla ion o he
exp essions ollowed by a d ins uc ion wi h he in ege iden i ie o he senso S(s) and
he numbe o exp essions, |˜e|, as he a gumen s. Likewise, he ansla ion o adio [˜e]
is simply he ansla ion o he exp essions o be sen , ollowed by a ad ins uc ion wi h
he numbe o exp essions, |˜e|, as he a gumen .
The s a e o he i ual machine is ep esen ed as he e m [D|S|B]j, whe e jis he p og am
coun e and is used o a el he ins uc ions in he ex segmen . The hal ed machine is
ep esen ed by a special s a e deno ed ⊥. To un a ask Tin he i ual machine we use
he ansla ion unc ion o ge i s by e code [[T]] = (D, B) and se i s ini ial s a e o:
[D|0. . . 0
| {z }
k
|B]0
CHAPTER 5. DATA LAYER 49
closes ac i a ion ime. This becomes he nex ask o be execu ed by he ope a ing sys em.
O he wise he p edica e TableEmp y will e alua e o ue.
Algo i hm 5 Sleep un il nex ask ac i a ion
unc ion sleep()
i ¬ ableEmp y() hen
←ge Nex Ac i (cu )
cAla m( )
end i
mic oSleep()
end unc ion
P ocedu e Sleep (Algo i hm 5) compu es he ime un il he nex ask ac i a ion and
p og ams an ala m o wake up he node. The node hen goes o sleep. This speci ica ion
builds on he unde lying assump ion ha asks, being so small, execu e in only a iny
ac ion o hei co esponding pe iods. In o he wo ds, i a ask has a pe iod pand an
execu ion ime, pe ac i a ion, o , hen p. O he wise we make no e o o schedule
asks wi hin hei pe iods. Since is in he o de o milliseconds we ind his assump ion
adequa e o p ac ical pu poses.
Finally, p ocedu e Lis en (Algo i hm 6) checks o any incoming messages while he
main loop was unning. We assume ha he nodes ha e he means o ecei e and o
bu e messages asynch onously, by p og amming an app op ia e handle o p ocess he
co esponding ha dwa e in e up s. I a message is ecei ed, i s ag is checked o iden i y
i s ype and i is p ocessed acco dingly. A his poin , he e a e 4 ypes o con ol messages:
TASK - sends he iden i ie , he pe iod and he by e-code o a new ask o be execu ed
in he node; PERIOD - sends he iden i ie and he new pe iod o a unning ask in he
node; KILL - sends he iden i ie o a ask o be in alida ed in he node, and; RESET -
ha in alida es all asks unning on a node. When a new ask is eassembled and copied
o he ask able, i s nex ac i a ion is se o ge Nex Ac i (cu ) + δ, whe e δis a
delay in oduced o make su e ha he ask is schedulable in he nex loop un, i.e., i s
ac i a ion ime is in he u u e when he Schedule p ocedu e is called.
5.4 Implemen a ion and Da a Flow
Figu e 5.9 depic s an high le el o e iew o he implemen a ion and he da a low in a
deploymen ga eway.

CHAPTER 5. DATA LAYER 50
Algo i hm 6 Handle Incoming Radio Message
unc ion handleRadioIn e up ()
in e up ed ←T ue
end unc ion
unc ion lis en()
i in e up ed hen
msg ← eadRadioRCVBu e ()
ag ←ge Tag(msg)
swi ch ( ag )
case TASK :
i←ge Id(msg)
p←ge Pe iod(msg)
b←ge By es(msg)
addTask(i, p, b)
case PERIOD :
i←ge Id(msg)
p←ge Pe iod(msg)
changePe iod(i, p)
case KILL :
i←ge Id(msg)
emo eTask(i)
case RESET :
o i= 0 . . . TableSize −1do
emo eTask(i)
end o
end swi ch
in e up ed ←False
end i
end unc ion
CHAPTER 5. DATA LAYER 51
Figu e 5.9: Message low in SONAR ga eway
The ga eway so wa e is composed by wo main pa s:
•a o wa ding loop, which lis ens o con ol messages ecei ed om he deploymen
adap e and da a messages ecei ed om he deploymen nodes;
•a se o ha dwa e lib a ies, used o access and con ol he senso s, ac ua o s and
o he ha wa e modules p esen in he boa d.
As p esen ed in he same igu e, he ga eway is always lis ening o wo di e en ype o da a:
con ol messages, sen by he adap e , which con ains he managemen commands sen by
he Adminis a ion Clien and da a messages, sen by he deploymen nodes, con aining
he da a p oduced in each unning ask.
Figu e 5.10 depic s he same high le el o e iew o he implemen a ion and he da a low,
his ime abou he nodes.
Each node is composed by h ee main componen s:
•a ecei ing and scheduling loop, which is esponsible o lis ening o new da a adioed
by he ga eway and con aining he con ol messages sen by he Adminis a ion
Clien , as well as by he scheduling o he unning asks in he node;
•a i ual machine, used o un he by e-code asks s o ed a he node;
•a se o ha dwa e lib a ies, used o access and con ol he senso s, ac ua o s and
o he ha wa e modules p esen in he boa d.
CHAPTER 5. DATA LAYER 52
Figu e 5.10: Message low in SONAR nodes
As shown in he igu e, a node ecei es con ol messages om he ga eway ia he XBee
an enna and p ocesses i . These messages a e used o change o ep og am he asks in
he node, allowing o add o emo e a ask, change he pe iod o a ask and emo e all he
asks.
All he da a p oduced in a unning ask is di ec ly sen o he ga eway by he i ual
machine, which uses he ha dwa e lib a ies p esen in he node.
5.5 Summa y
In his chap e we p esen ed he o mal desc ip ion o he he da a laye componen s. We
s a ed by o mally p esen ing he STL syn ax, depic ing he ea e some STL examples
and he espec i e educ ion ules o he language ins uc ions. Nex we desc ibed he
ope a ing sys ems p e-ins alled in SONAR nodes, depic ing he mos impo an ou ines
ha compose bo h he nodes and he ga eway. We inished his chap e wi h he o mal
desc ip ion o SONAR i ual machine, as well as he desc ip ion o he STL compile .
Chap e 6
Se up, E alua ion and Discussion
In his chap e we analyze he impac o using ou i ual machine and ope a ing sys em in
e ms o ene gy consump ion and memo y oo p in . We s a by de ailing he se up p ocess
o ou sys em, p esen ing an example on how o use all he componen s. Nex , we desc ibe
he ha dwa e con igu a ion o he nodes, a ele an poin in he ene gy consump ion.
The ea e , we p esen he expe imen al esul s ob ained, ollowed by he analysis and
discussion o he da a ga he ed. We end his chap e wi h a b ie summa y.
6.1 Se up
We s a his chap e by depic ing he ull p ocess o using he SONAR p o o ype, ex-
plaining all he s eps in he ini ializa ion p ocess, he da a low happening when he
Adminis a ion submi s a new ask, and he da a low ela ed wi h he subsc ip ion and
ecep ion o he espec i e da a by he Publish/Subsc ibe clien .
6.1.1 Ini ializa ion
To s a using SONAR, he use mus own a compu e wi h an USB in e ace, as simple as
a Raspbe y-Pi, whe e he can connec he ga eway node. This compu e mus ha e access
o he In e ne , allowing he deploymen o send he da a o he B oke Web Se ice.
Using he p e iously ins alled SONAR so wa e, he use s a s he Adap e Web Se ice,
53
CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 54
which au oma ically connec s o a SONAR B oke , and connec s he ga eway o he
compu e . A e inishing he boo , he ga eway sends a message o he Adap e con aining
some in o ma ion ha needs o egis e he deploymen in he B oke .
A his poin , he use can s a he Adminis a ion Clien . I s a s by opening a
connec ion wi h he Adap e , hen displaying in he use in e ace a lis con aining he
a ailable commands.
Be o e s a ing he managemen o he deploymen , he use mus egis e his own deploy-
men in he B oke using he eg command, which sends he necessa y in o ma ion o he
B oke in o de o inse he deploymen in he B oke unning deploymen s lis . Now, he
ini ializa ion p ocess is inished and he use can ully use his own SONAR deploymen .
6.1.2 Managing he unning asks
To add a new ask, he use mus use he ask command, p o iding he pa hs o p e iously
c ea ed by e-code and desc ip ion iles.
Using his in o ma ion, he Adap e s a s wo dis inc da a lows: one a ge ing he
B oke , sending he in o ma ion ha a new ask had been added o he deploymen and
ano he o he ga eway, con aining he by e-code ha mus be adioed o he nodes.
The same da a lows a e gene a ed when he use submi s he pe iod,kill, and ese
commands, a ec ing he nodes in he Da a laye and changing he in o ma ion abou he
asks in he B oke .
6.1.3 Clien subsc ip ion
To subsc ibe a se o asks, a use mus s a he Publish/Subsc ibe clien Web Se ice,
which opens a connec ion o he B oke , and send a ld command. This command con ac s
he B oke and e ie es all he a ailable deploymen s (and he espec i e asks) ha a e
connec ed in ha momen .
Using ha in o ma ion, he clien is able o subsc ibe a ask using he sub command, which
ells he B oke ha his Clien in ends o ecei e all he da a p oduced by a gi en ask.
The B oke ecei es ha in o ma ion and adds he ID o he clien in he Tasks Table.
F om his momen , un il he clien closes he connec ion o unsubsc ibes he ask, e e y

CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 55
ime he B oke ecei es da a om ha ask, i au oma ically o wa ds i o he Clien .
6.2 Node Con igu a ion
In he cu en con igu a ion o ou p o o ype, each node is composed by an A duino
Mega2560 boa d connec ed o an A duino Wi eless P o oshield, equipped wi h a XBee
Se ies 2 an enna, a SHT-15 empe a u e and humidi y senso , a Ligh -Dependen Resis o
(LDR), one ed LED and a Ada ui Ch onodo Real-Time Clock. Figu e 6.1 p esen s
he ha dwa e scheme o he nodes, depic ing he connec ion be ween all he ha dwa e
componen s.
6.2.1 Jumpe Connec ions
As depic ed in Figu e 6.1, we a e cu en ly using 3 jumpe s om he wi eless shield o he
Mega2560 boa d: one jumpe om pin 3 o pin 18, which connec s he XBee Rx o he
A duino Tx 0, used o wake up he node when a new message is ecei ed om he ga eway;
a second jumpe om pin 2 o pin 19, which connec s he XBee TX o he A duino Rx 0,
used o pass o he an enna all he da a p oduces in he asks; and a hi d jumpe om
pin 8 o pin 20, which connec s he Ch onodo SQW pin o he A duino SDA pin, used o
wake up he node when a ask mus be execu ed.
6.2.2 XBee se up
To ge a ull desc ip ion o he nodes in ou p o o ype, i is ele an o desc ibe how he
XBee an ennas a e con igu ed, since i signi ican ly a ec he ene gy consump ion o his
de ice.
When de eloping he so wa e o ou nodes, one impo an decision made was ela ed
wi h he ne wo k o ganiza ion, which de e mines how a node communica e wi h he o he s.
Cu en ly, we a e aking bene i s om he ac ha he XBee an ennas allows he usage
o a mesh a chi ec u e, wi h mul i-hop communica ion and message eliabili y. I was a
ea u e we hough ha would be impo an gi en he ac ha in some deploymen s, a
node can be physically placed in locals ha a e ou -o - each o a di ec communica ion wi h
CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 56
XBEE-1B1
SHT1XSMD
CHRONODOT
GND
GND
GND
GND
GND
+5V
CTS
DIN 3
DIO0
DIO1
DIO2
DIO3
DIO4
DIO5
DIO9
DIO11 7
DIO12 4
DOUT 2
DTR 9
GND 10
RES 8
RES
RESET 5
RSSI 6
RTS
VDD 1
DATA
GND
SCK
VDD
BAT
32K
SQW
RST
SDA
SCL
VCC
GND
SV4
1
2
3
4
5
6
7
8
SV5
1
2
3
4
5
6
7
8
SV2
1
2
3
4
5
6
SV3
1
2
3
4
5
6
SCL1
SDA1
AREF AREF
GND
13
12
11
10
9
8
SCL1
SDA1
GND
13
12
11
10
9
8
7 7
6 6
5 5
4 4
3 3
2 2
1(TX)
1
0(RX)
0
A5A5
A4A4
A3A3
A2A2
A1 A1
A0A0
VIN
GND
GND
5V
3.3V
RESET
VIN
GND
GND
5V
3.3V
RESET
IOREF
I/OREF
(N/C)N/C
<PWR>-V<PWR>-V
<PWR>+V<PWR>+V
<USB>GND<USB>GND
<USB>D-
<USB>D-
<USB>D+<USB>D+
<USB>+V<USB>+V
14
15
16
17
18
19
20
14(TX3)
15(RX3)
16(TX2)
17(RX2)
18(TX1)
19(RX1)
20(SDA)
21(SCL)
21
A15 A15
A14 A14
A13 A13
A12 A12
A11 A11
A10 A10
A9 A9
A8 A8
A6A6
A7A7
5V
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
5V
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
GND
GND
GND
52
50
48
46
44
42
40
38
36
34
32
30
28
26
24
22
GND
52
50
48
46
44
42
40
38
36
34
32
30
28
26
24
22
5V 5V
D2
23
5V
5V
8
8
A5
A5
7
7
6
6
9
9
10
10
12
20
19
18
17
11
15
13
14
16
ARDUINO MEGA 2560 ICSP
JUMPER -> [20->PD1(SDA / INT1)] , [8->PH5(OC4C)] - Ch onoDo Ala me
JUMPER -> [19->PD2 (RXD1 / INT2)] , [2->PE4(OC3B/INT4)] - Xbee_TX>A d_RX (Comm Wakeup)
JUMPER -> [18->PD3(TXD1 / INT3)] , [3->PE5(OC3C/INT5)] - Xbee_RX>A d_TX
3
2
Figu e 6.1: SONAR node scheme - A duino Mega 2560
CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 57
he ga eway. The mesh a chi ec u e can be e y use ul in his si ua ions, using message
o wa ding h ough he ne wo k and allowing his nodes o send da a o he ga eway.
Despi e e y use ul, he mesh a chi ec u e b ings an inc ease in he ene gy consump ion,
since all he nodes ha e o be con igu ed has ou e s. This could in some cases be e y
undesi able, since a node ma ked as ou e disallows he ansi ion o he XBee an enna
o a sleep s a e, enabling powe sa ing. A ull desc ip ion abou all he possible XBee
an enna con igu a ions can be accessed in [34].
O he impo an pa ame e s o ou XBee con igu a ion a e:
•The RF in e ace is con igu ed o use he Highes Powe Le el, wi h Boos Mode
Enabled, inc easing he amoun o ene gy consumed by he an enna;
•The e is no enc yp ion in he adio messages and he ne wo k do no use any ype o
secu i y, which would inc ease he ene gy consump ion.
Al hough he e a e a bigge se o XBee con igu a ion pa ame e s, we a e only analyzing
a educed numbe pa ame e s since hese a e he ones ha in luence he mos he ene gy
consump ion o his de ice.
6.3 E alu a ion
The ollowing es s ha e he pu pose o de e mine he ene gy consump ion and compu a-
ion o e head o ou p o o ype. The asks es adio ansmission, access o senso s and
ac ua o s and compu a ion. Each es was implemen ed bo h in STL, unning on op o
SVM in each node, and di ec ly in A duino’s na i e C/C++/Wi ing. To measu e he
imings and he ene gy consump ion, we connec ed a mul ime e in se ies wi h one node
(Figu e 6.2) and egis e ed he elec ic cu en a ia ion associa ed wi h he execu ion o
each ask. The mul ime e we used was a TENMA 72-7732A [35]. A Keysigh In iniiVision
MSO-X 2002A oscilloscope [36] was also used o some ime ela ed measu emen s.
6.3.1 Expe imen al Resul s
The i s asks es adio ansmissions. Figu e 6.3.1 p esen s he STL code o a ask
ha adioes 64 by es, simula ing a case whe e, o example, a ask is p og ammed o
CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 58
Figu e 6.2: Mul ime e and oscilloscope se up.
adio 16 senso eadings ( loa ing poin alues) o he ga eway. The 64 by es e e only
o he payload o he messages ha ca y an addi ional 10 by es o heade in o ma ion.
Appendix A.0.2 p esen s a simila implemen a ion in A duino C++.
STL Code 6.3.1 T ansmission o da a.
use ” a d2560 . hw”
[ loa @ ” l o a 1 : s en so 1 ” ,
loa @ ” l o a 2 : s en so 2 ” ,
loa @ ” l o a 3 : s en so 3 ” ,
. . . ,
loa @ ” l o a 1 6 : s enso 16 ” ]
loop {
adio [ 2.0 , 2.0 , 2.0 , 2.0 ,
2.0 , 2.0 , 2.0 , 2.0 ,
2.0 , 2.0 , 2.0 , 2.0 ,
2.0 , 2.0 , 2.0 , 2.0 ] ;
}
Figu e 6.3 (blue line) shows cu en in ensi y s. ime when unning he ask wi h a pe iod
o 10 seconds. The i s eco ds he success ul ecep ion o he message by he node (ou
code pu s he ed LED on o 1.0 second). A e he ecep ion, one mo e peak (a ound
= 20) is isible, co esponding o he momen s whe e he node adioed he 64 by es.
CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 65
he empe a u e senso , we canno ye explain he lowe o e head (only a ac o o 1.1
slowe ) ela i e o he o he senso s and ac ua o s (globally a ound 3 imes slowe ). As
he ene gy consumed is p opo ional o he ime he ask akes o execu e, clea ly he
op imiza ions mus ocus on his aspec , all o he being he same o STL asks and o
A duino p og ams. We belie e, howe e , ha e en in his unop imized s a e ou p o o ype
compa es well wi h A duino na i e code wi h he added bene i s o simpli ied p og amming
and dynamic ep og amming.
0 0.2 0.4 0.6 0.811.2 1.4 1.6
·104
4
6
8
10
I e a ions
STL
A d
Figu e 6.9: SVM o e head s. size o p oblem.
The possibili y o dynamically ep og am he ne wo k allow us o change he unning asks
a a small ene gy cos , a oiding simul aneously he need o lash he boa d.
When p og aming in he A duino na i e language, his p ocess is much mo e complex,
o cing he use o upload he code o he lash memo y. Figu e 6.10 p esen s he p o ile
ob ained using he oscilloscope when lashing one node o change he unning p og am in
A duino na i e language. This da a was ob ained hen lashing he boa d wi h he code
used o es he empe a u e senso access.
The i s wo isible peaks ep esen s he momen when he boa d is ese ing, a manda o y
s ep so i can access he boo loade . A e accessing he boo loade , he e is a isible
pla eau whe e he boa d is wai ing o a command indica ing ha he e is a new p og am
o be lashed o he boa d (p esen ed in he plo om = 0.750 o = 1.000 app oxima ely).
Nex , he lash s a s and he da a is w i en o he boa d ( om = 1.000 o = 2.500
app oxima ely), ollowed by a pe iod whe e i is e i ied ha all he da a had been co ec ly
w i en. The whole p ocess ook 4294ms o occu , being he boa d ully ac i e o a pe iod
o 4024ms. Applying he o mulas p e iously p esen ed, lashing he boa d o upload a
new p og am consumed abou 1991.9mJ.

CHAPTER 6. SETUP, EVALUATION AND DISCUSSION 66
0.000 0.500 1.000 1.500 2.000 2.500 3.000 3.500 4.000
600
650
700
750
Time (ms)
∆ Vol age (mV)
Boa d Flashing P o ile
Figu e 6.10: Boa d Flash P o ile
To pe o m he same ac ion using ou p o o ype, he only consump ion is he ecep ion o
he message, plus a e y small o e head when he code is being s o ed and scheduled. In
his case, i ook us 100ms o ecei e he message, s o e he code and schedule i , wi h a
consump ion o abou 49.5mJ, 40 imes less ha lashing he boa d in A duino.
6.4 Summa y
In his chap e we desc ibe he usage o SONAR and p esen he benchma king o ou
p o o ype. We desc ibe he se up used o measu e he ene gy consump ion and execu ion
ime o each es ed ask, p esen ing all he plo s ob ained in each execu ion. We compa e
he alues ob ained using ou p o o ype wi h he alues ob ained when unning a ask
w i en using he A duino na i e language. The a io be ween hese alues allowed us o
s udy he impac o ou p o o ype in e ms o ene gy consump ion and execu ion ime.
Chap e 7
Po o A duino Uno
In his chap e we desc ibe he p ocess o po ing ou p o o ype o a new node based on
a A duino Uno boa d. We s a by p esen ing an o e iew o he pa ame e s we analyzed
be o e s a ing o po ou solu ion. Nex , we desc ibe he implemen a ion o a ha dwa e
es ool we c ea ed o p o ide a simple and as way o es he ha dwa e componen s
p esen on he boa d. We inish his chap e by desc ibing he changes made o po
ou p o o ype o he Uno boa d, de ailing he main p oblems ound and quan i ying he
amoun o code added o e-w i en.
7.1 O e iew
In o de o po he cu en solu ion o a new senso based on he A duino Uno, wo main
s eps we e aken o adap all he needed code which makes ou solu ion possible o un in
a di e en node. Fi s , we analyzed he used pins in A duino Mega2560 and mapped i in
he A duino Uno pins, ocusing in he used Uni e sal Asynch onous Recei e /T ansmi e
(UART) connec ions used. Second, gi en he memo y cons ains o he A duino Uno,
we analyzed he memo y consump ion o ou p o o ype and pa ame e ized he ope a ing
sys em and i ual machine o educe he maximum numbe o asks allowed o un
simul aneously in a node
Figu e 7.1 p esen s a pic u e o bo h boa ds: he Mega2560 and he Uno. Bo h boa ds
ha e he same ha dwa e con igu a ion, despi e he di e ences in he jumpe s connec ions,
depic ed in he pic u e.
67
CHAPTER 7. PORT TO ARDUINO UNO 68
Figu e 7.1: SONAR nodes - Mega2560 and Uno
7.2 Ha dwa e Tes Tool
A his poin we concluded ha cu en ly, he SONAR nodes we e using wo UART pins
in o de o allow ex e nal in e up e en s: one ese ed o he XBee e en , used o wake
he nodes when a con ol message is being ecei ed and one o he RTC ala m, used o
wake up he node when a deployed ask mus s a o execu e. A his poin , one majo
di e ence be ween he A duino Mega2560 and he A duino Uno a ises: he o me has ou
UART pins and he la e only has one. This lead us o make some signi ican changes in
he way ou nodes implemen he ex e nal e en s o wake up.
In o de o es all he needed changes, we decided o implemen a simple es p ojec ha
allows us o in e ac wi h each module o ou node, using he same ha dwa e lib a ies we
we e using be o e. In he nex wo subsec ion we desc ibe he implemen a ion p ocess o
his ool.
7.2.1 XBee ex e nal e en
The i s change we ha e done was he XBee connec ion o he boa d. In he p e ious
implemen a ion, he XBee an enna was connec ed o he A duino Mega2560 boa d h ough
jumpe s using he se ial communica ion pins 18 and 19 (RX1 and TX1 espec i ely). Gi en
CHAPTER 7. PORT TO ARDUINO UNO 69
he cu en wi e welding, we would need o use so wa e se ial communica ion o allow
he se ial communica ion be ween he XBee an enna and he Uno boa d. Since e sion
1.0, A duino o e s by de aul a lib a y ha emula es he se ial communica ion on o he
digi al pins, he So wa eSe ial [38] lib a y. Using his lib a y, we mapped wo se ial
communica ion pins in digi al pin 2 and 3, connec ing he XBee TX pin o he Uno RX
pin and he XBee RX pin o he Uno Tx pin. Wi h his con igu a ion, we c ea ed a simple
example ha ies o send some loa ing poin alues o he ga eway using unicas adio
messages. The ga eway co ec ly ecei ed he message sen , p o ing ha he XBee an enna
and he Uno boa d a e co ec ly connec ed and wo king.
7.2.2 RTC ala m
A e changing he XBee connec ion and e en in he A duino Uno boa d, we s a ed o
sol e he RTC ala m p oblem. Gi en he ac ha a his poin we did no ha e any
mo e se ial communica ion pins a ailable in ou boa d, we sea ched o a di e en way
o allowing e en s o wake up he boa d using he RTC. A e some esea ch, we ound
a simple solu ion ha can be used o o e come his es ic ion: Pin Change In e up
(PCIn ). A Pin Change In e up ion can be enabled on any o he A duino Uno signal pin,
allowing he igge o e en s ON CHANGE o he pin alue. To es his ea u e we used
he same lib a y, PinChangeIn [39]. Wi h his lib a y, we we e able o enable so wa e
in e up s in a gi en digi al pin, allowing o ecei e he ala m e en gene a ed a he RTC.
A his poin we analyzed he ci cui connec ions used in he p e ious implemen a ion
(Figu e 6.1) and decided o use pin 8 as he a ge pin. Wi h his choice, we we e able o
main ain he same welded wi es and allow he RTC o wake he boa d using pin CHANGE
in e up .
To es i his app oach wo ks wi h ou node con igu a ion, we ha e w i en a e y simple
es in C++ whe e we simply s a he node, schedule an ala m o 5 seconds la e , pu
he node in sleep mode and wai o he ala m o wake i . Wi h his es we we e able o
e i y ha he chosen con igu a ion allows he RTC o co ec ly gene a e wake up e en s
using PCIn .
CHAPTER 7. PORT TO ARDUINO UNO 70
7.2.3 Ga he ing all oge he
A e es ing each o he p e ious changes, we ied o inco po a e all he ea u es in a
simple ool ha allows he es he comple e ha dwa e componen s p esen in he boa d.
Fo his ool, we uses he same lib a ies used in SONAR, in o de o gua an ee ha no
lib a y con lic s occu s.
In he p ocess o ga he ing all oge he , an e o a ises when inco po a ing he PCIn
lib a y and he So wa eSe ial lib a y. A e some code analysis, we disco e ed ha he
e o was ela ed wi h he ac ha he So wa eSe ial lib a y uses some PCIn de ini ion
o wo k p ope ly. Mo e speci ically, bo h lib a ies a e de ining a se o in e up ec o s,
which en e in con lic . To sol e his p oblem, we analyzed he se o pins ha each o his
ec o s is using, ge ing he ollowing esul s:
•ISR (PCINT0 ec ) pin change in e up o digi al pins om 8 o 13
•ISR (PCINT1 ec ) pin change in e up o analogical pins om 0 o 5
•ISR (PCINT2 ec ) pin change in e up o digi al pins om 0 o 7
Gi en he pin con igu a ion we p e iously used, he solu ion o his p oblem was o simply
spli which ec o pins a e used by each lib a y: PCIn lib a y de ined he PCINT0 ec
and PCINT1 ec , allowing he usage o in e up s in pin 8, and he So wa eSe ial lib a y
de ined he PCINT2 ec , allowing he c ea ion o so wa e se ial communica ion in pins
2 and 3. Wi h hese changes, we we e able o use bo h lib a ies simul aneous wi hou
con lic s, inishing he implemen a ion o he ha dwa e es ool.
Figu e 7.2 shows an example o he in e ace: he use is asked which ha dwa e componen
he wan s o es and hen he ou pu is e ie ed by he selec ed componen .
7.3 SONAR po ing
The Ha dwa e Tes Tool allowed us o p o e ha i is possible o use all he ha dwa e
componen s simul aneously using he A duino Uno. A his poin , we s a ed o adap all
he so wa e code o un in he boa d. We s a ed by adding he PCIn and So wa eSe ial
lib a ies, allowing he XBee o co ec ly communica e wi h he boa d and he RTC o

CHAPTER 7. PORT TO ARDUINO UNO 71
Welcome o he Ha dwa e Tes Tool o A duino Uno
Selec one o he ollowing op ions:
1 - Read SHT-15 empe a u e
2 - Read SHT-15 humidi y
3 - Read LDR luminosi y
4 - Read RTC ime
5 - Tu n on LED o 1.5 seconds
6 - Tes boa d IDLE mode
7 - Tes XBee Radio an enna
Op ion: 2
Humidi y: 46.89 %
Figu e 7.2: Ha dwa e Tes Tool Example
Sec ion Size (kB) To al Size (kB)
#Tasks .da a . es .bss FLASH SRAM
8 0.48 23.3 2.31 23.3 2.79
4 0.48 23.3 1.57 23.3 1.95
20.48 23.3 1.04 23.4 1.53
Table 7.1: Memo y usage: max. o 8, 4, and 2 asks
igge he wake up e en s when a ask mus be execu ed. Wi h hese changes, we expec ed
o be able o un he ull implemen a ion using he A duino Uno.
To es he po , we compiled all he code wi h he new lib a ies added. We adjus ed
he numbe o maximum asks o 4 and 2, in o de o sa e some SRAM space. Table 7.1
p esen s all he alues ob ained when compiling he code.
As shown in 7.1, he 2kB o SRAM a ailable in A duino Uno do no allow o schedule 8
asks simul aneously wi hou some code op imiza ion. Howe e , educing he maximum
numbe o asks o 4 o 2 educes he SRAM usage o abou 1.95kB and 1.53kB espec i ely,
which i s in he Uno pa ame e s (98% and 77% o o al Uno SRAM size). Despi e i was
possible o un he so wa e wi h 4 asks, we decided o con igu e he maximum numbe
o asks as 2 o ou po ing es .
CHAPTER 7. PORT TO ARDUINO UNO 72
7.3.1 Po Tes s
A e uploading he so wa e o he Uno boa d, we used he p e iously p esen ed STL
p og ams o es i e e y hing is wo king co ec ly. When he node is u ned on, i
b oadcas s a message o i s PAN announcing himsel as a new node. As expec ed, he
ga eway ecei es ha message and s o es he node Mac Add ess as a new deploymen
node.
A his poin we ied o adio a ask om he ga eway o e i y i he node is ecei ing and
execu ing i co ec ly. Con a ily o wha we expec ed, he node did no eac o he ask
ecep ion, u ning on he LED. In o de o e i y i he message was eaching he boa d
causing i o wake om he sleep mode, we again connec ed he node o he mul ime e
and he oscilloscope. A e esending he ask o he node, we egis e ed a peak in he
ol age, p o ing ha he node is waking om sleep and ecei ing some adio message.
As we s a ed o debug his p oblem, we disco e ed ha i was ela ed wi h he ac ha
when a message is ecei ed h ough he XBee, he boa d is waking bu o an unknown
eason he in e up ion was no being co ec ly gene a ed. We suspec ed ha his p oblem
may be ela ed wi h he So wa eSe ial connec ions p e iously c ea ed, so we decided o
y a di e en app oach. Ano he way o allow he XBee o wake he boa d is using he
same in e up s we had p e iously used wi h he RTC, an PCIn . Gi en he cu en boa d
wi ing al eady p esen ed, we changed he XBee in e up ion a achmen om pin 3 o pin
11, which we connec ed wi h a jumpe , and added ano he PCIn in pin 11, allowing he
boa d o wake when a adio message is ecei ed.
A e hese changes, we we e able o co ec ly ecei e some messages om he ga eway: MAC,
kill and pe iod commands. When es ing he ecep ion o new asks, ano he p oblem
occu ed: when ying o send new asks, he node only ecei es and execu es hem i he
by e-code size o ha ask is smalle han 55 by es. A e inspec ing he So wa eSe ial
lib a y, we ound ha his p oblem was ela ed wi h he ac ha he a So wa eSe ial
po uses a 64 by es ecep ion bu e , which will no be su icien o ecei ing asks wi h
size g ea e han 54 by es (plus he 10 by es heade always sen ). Gi en he ac ha his
is a so wa e limi a ion, we sol ed he p oblem by changing he communica ion p o ocol
c ea ed o di ide he asks in packe s, educing he maximum size o each packe o 54
by es. A e his change, we es ed again he ecep ion o asks wi h size g ea e han 54
by es and e e y hing wo ked co ec ly, inishing he po o he A duino Uno boa d. The
schema ic ep esen a ion o he new node is p esen ed in Figu e 7.3.
CHAPTER 7. PORT TO ARDUINO UNO 73
XBEE-1B1
SHT1XSMD
CHRONODOT
GND
GND
GND
GND
5V
VCC
VCC
VCC
VCC
GND
CTS
12
DIN 3
DIO0
20
DIO1
19
DIO2
18
DIO3
17
DIO4
11
DIO5
15
DIO9
13 DIO11 7
DIO12 4
DOUT 2
DTR 9
GND 10
RES 8
RES
14
RESET 5
RSSI 6
RTS
16
VDD 1
DATA
GND
SCK
VDD
BAT
32K
SQW
RST
SDA
SCL
VCC
GND
SV4
1
2
3
4
5
6
7
8
SV5
1
2
3
4
5
6
7
8
SV2
1
2
3
4
5
6
SV3
1
2
3
4
5
6
D2
SCL
SDA
AREF
GND
13
12
11
10
9
28(SCL)
27(SDA)
AREF
GND
13
12
11~
10~
9~
8 8
7
6~
5~
4
3~
2
1(TX)
0(RX)
7
6
5
4
3
2
1
0A5
A4
A3
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
I/OREF
N/C
<PWR>-V<PWR>-V
<PWR>+V
<PWR>+V
<USB>GND
<USB>GND
<USB>D-<USB>D-
<USB>D+<USB>D+
<USB>+V<USB>+V
23
3
7
7
6
6
9
9
10
10
JUMPER -> [11-> PB3(PCINT3)] , [2->PD2(INT0)] - Xbee_TX>A d_RX (Comm Wakeup)
ARDUINO UNO
ICSP
11
A5
A4
A3
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
I/OREF
N/C
Figu e 7.3: SONAR node scheme - A duino Uno
CHAPTER 7. PORT TO ARDUINO UNO 74
7.3.2 Po e o
The e o o po all he a ailable code o a new A duino boa d wi h less memo y capaci y
was essen ially ela ed wi h he new connec ions and he limi a ion in oduced by he
di e en speed when using So wa eSe ial communica ions. Table 7.2 p esen a b ie
quan i ica ion o changes needed, in e ms o lines o code, connec ions and lib a ies added.
Ga eway Node
Added Changed Added Changed
Lines o Code 0 3 13 4
Lib a ies 0 0 2 2
Wi e Connec ions 0 0 1 3
Table 7.2: Po e o quan i ica ion: lines o code, lib a ies, and wi e connec ions
As desc ibed in he p e ious sec ion, he bigges di icul associa ed wi h he po was he
connec ion scheduling, allowing all he ha dwa e componen s o co ec ly communica e
wi h he o he s.
Al hough in his case we op ed o po ou solu ion o a simila pla o m wi h less memo y
and UART pins, he easiness o he p ocess sugges ha he e o o po ou p o o ype o
a comple ely di e en pla o m wi h simila capaci ies would eside mos ly in disco e ing
ha dwa e lib a ies o all he componen s and adjus some pa ame e s o i he pla o m
memo y cons ains.
7.4 Summa y
In his chap e we p esen ed a po o SONAR o he A duino Uno pla o m. We desc ibed
in de ail he c ea ion o a simple ha dwa e es ool, used o es all he ha dwa e connec-
ions in he new boa d, as well as all he changes in he code o adap he p o o ype o
he new boa d, wi h less SRAM memo y capaci y and only one a ailable UART pin. To
es he po we used he same STL asks p esen ed in Chap e 6, leading o a se o e o s
ela ed wi h he So wa eSe ial Lib a y. We hen sol ed his issue and success ully po ed
he sys em o Uno. We inish his chap e by ying o quan i y he e o o his po ,
analyzing he amoun o code we needed o ew i e and he changes in he connec ions.
REFERENCES 81
[30] N. Sha ma, “Push echnology–long polling,” h p://www.ijcsm .o g/ ol2issue5/pape 398.pd ,
2013.
[31] (2012, Decembe ) Se e Sen E en s - W3C S anda d. Visi ed in No embe , 2014.
h p://www.w3.o g/TR/2009/WD-e en sou ce-20091029/
[32] (2012, Sep embe ) Web Socke s - W3C S anda d.
h p:// ools.ie .o g/pd / c6455.pd
[33] Amazon S3 Web Se ice. Visi ed in June, 2015.
h ps://aws.amazon.com/s3/?nc2=h ls
[34] (2009) XBee Da ashee . Las seen in 14 Feb ua y, 2015.
h ps://www.spa k un.com/da ashee s/Wi eless/Zigbee/XBee-Da ashee .pd
[35] (2006) TENMA 72-7732A Da ashee . Las seen in Feb ua y, 2015.
h p://www. a nell.com/da ashee s/1653479.pd
[36] (2014) Keysigh In iniiVision MSO-X 2002A Da ashee . Las seen in June, 2015.
h ps://www.upc.edu/sc /es/documen s equipamen /d 332 dsox2012a.pd
[37] J. C. Sp o , Chaos and Time-Se ies Analysis. Ox o d Uni e si y P ess, 2003.
[38] So wa eSe ial Lib a y. Las seen in May, 2015.
h p://www.a duino.cc/en/Re e ence/So wa eSe ial
[39] PinChangeIn Lib a y. Las seen in May, 2015.
h ps://code.google.com/p/a duino-pinchangein /
[40] G. Fe o, R. Sil a, and L. Lopes, “Towa ds Ou -o - he-Box P og amming o Wi eless
Senso -Ac ua o Ne wo ks,” 2015, manusc ip submi ed o publica ion.

Appendix A
Code Blocks
In his Appendix we p esen he simila implemen a ion o he asks p esen ed in Chap e 6.
All he examples a e using he same lib a ies used in ou p o o ype, which signi ican ly
educe he amoun o code w i en in each example.
82
APPENDIX A. CODE BLOCKS 83
A duino Code A.0.1 A duino Senso s and Radio Tes P og am
#include ” L i b a i e s /RTC/RTC. h”
#include ” L i b a i e s /SHT15/SHT15 . h”
#include ” L i b a i e s / Radio /Radio . h”
#de ine SHTx DATA PIN 7
#de ine SHTx CLOCK PIN 6
o l a i l e bool wake = a l s e ;
SHT15 s h 1 5 o b j ;
SendBu s e n d b u e ;
loa s e n s o a l u e s [ 2 ] ;
oid se up ( ) {
a a c h I n e u p (3 , wakeAla m , FALLING ) ;
s h 1 5 o b j (SHTx DATA PIN , SHTx CLOCK PIN ) ;
a d i o : : g e m y a dd e ss ( ) ;
i m e ime = c : : ime ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid loop ( ) {
s e n s o a l u e s [ 0 ] = s h 15 o bj . ge Tempe a u e ( ) ;
s e n s o a l u e s [ 1 ] = s h 15 o bj . ge Humidi y ( ) ;
s e n d b u e = a d i o : : new send bu ( a d i o : : myadd ess , 1 , 2 , s e n s o a l u e s ) ;
ad i o : : send da a ( s e n d b u e ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid wakeAla m() {
wake = u e ;
}
oid sleep() {
wake = a l s e ;
se sl e e p m o d e (SLEEP MODE IDLE ) ;
sleep enable ();
powe adc disable ();
p o w e s p i d i s a b l e ( ) ;
powe ime 0 disable ();
powe ime 1 disable ();
powe ime 2 disable ();
p o w e w i d i s a b l e ( ) ;
sleep mode ( ) ;
sleep disable ();
p o w e a l l e n a b l e ( ) ;
}
APPENDIX A. CODE BLOCKS 84
A duino Code A.0.2 A duino Radio 64 by es P og am
#include ” L i b a i e s /RTC/RTC. h”
#include ” L i b a i e s / Radio /Radio . h”
o l a i l e bool wake = a l s e ;
SendBu s e n d b u e ;
loa adio alues [16];
oid se up ( ) {
a a c h I n e u p (3 , wakeAla m , FALLING ) ;
a d i o : : g e m y a dd e ss ( ) ;
o ( in i =0; i <16; i++) {
a d i o a l u e s [ i ] = 2 . 0 ;
}
i m e ime = c : : ime ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid loop ( ) {
s e n d b u e = adi o : : new send bu ( adi o : : myadd ess , 1 , 16 , a d i o a l u e s ) ;
ad i o : : send da a ( s e n d b u e ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid wakeAla m() {
wake = u e ;
}
oid sleep() {
wake = a l s e ;
se sl e e p m o d e (SLEEP MODE IDLE ) ;
sleep enable ();
powe adc disable ();
p o w e s p i d i s a b l e ( ) ;
powe ime 0 disable ();
powe ime 1 disable ();
powe ime 2 disable ();
p o w e w i d i s a b l e ( ) ;
sleep mode ( ) ;
sleep disable ();
p o w e a l l e n a b l e ( ) ;
}
APPENDIX A. CODE BLOCKS 85
A duino Code A.0.3 A duino Senso s Tes P og am
#include ” L i b a i e s /RTC/RTC. h”
#include ” L i b a i e s /SHT15/SHT15 . h”
#de ine SHTx DATA PIN 7
#de ine SHTx CLOCK PIN 6
o l a i l e bool wake = a l s e ;
SHT15 s h 1 5 o b j ;
oid se up ( ) {
a a c h I n e u p (3 , wakeAla m , FALLING ) ;
s h 1 5 o b j (SHTx DATA PIN , SHTx CLOCK PIN ) ;
i m e ime = c : : ime ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid loop ( ) {
loa e mp e a u e = s h 1 5 o b j . ge Tempe a u e ( ) ;
loa hu mid i y = s h 1 5 o b j . g e Humidi y ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid wakeAla m() {
wake = u e ;
}
oid sleep() {
wake = a l s e ;
se sl e e p m o d e (SLEEP MODE IDLE ) ;
sleep enable ();
powe adc disable ();
p o w e s p i d i s a b l e ( ) ;
powe ime 0 disable ();
powe ime 1 disable ();
powe ime 2 disable ();
p o w e w i d i s a b l e ( ) ;
sleep mode ( ) ;
sleep disable ();
p o w e a l l e n a b l e ( ) ;
}
APPENDIX A. CODE BLOCKS 86
A duino Code A.0.4 A duino Compu a ion Tes P og am
#include ” L i b a i e s /RTC/RTC. h”
o l a i l e bool wake = a l s e ;
bool s a e = ue ;
oid se up ( ) {
a a c h I n e u p (3 , wakeAla m , FALLING ) ;
i m e ime = c : : ime ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid loop ( ) {
loa x = 0 . 2 ;
loa k = 4 . 0 ;
in i =0;
whi l e ( i <1000) {
x = k ∗x∗(1. 0 −x ) ;
i ++;
}
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid wakeAla m() {
wake = u e ;
}
oid sleep() {
wake = a l s e ;
se sl e e p m o d e (SLEEP MODE IDLE ) ;
sleep enable ();
powe adc disable ();
p o w e s p i d i s a b l e ( ) ;
powe ime 0 disable ();
powe ime 1 disable ();
powe ime 2 disable ();
p o w e w i d i s a b l e ( ) ;
sleep mode ( ) ;
sleep disable ();
p o w e a l l e n a b l e ( ) ;
}

APPENDIX A. CODE BLOCKS 87
A duino Code A.0.5 A duino Ac ua o Tes P og am
#include ” L i b a i e s /RTC/RTC. h”
o l a i l e bool wake = a l s e ;
bool s a e = ue ;
in ledPin = A5 ;
oid se up ( ) {
a a c h I n e u p (3 , wakeAla m , FALLING ) ;
pinMode ( ledPin , OUTPUT) ;
i m e ime = c : : ime ( ) ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid loop ( ) {
i ( s a e ) {
d i g i a l W i e ( ledPin , HIGH ) ;
}
else {
d i g i a l W i e ( ledPin , LOW) ;
}
s a e = ! s a e ;
c : : ala m ( ime + 5 ) ;
s l e e p ( ) ;
}
oid wakeAla m() {
wake = u e ;
}
oid sleep() {
wake = a l s e ;
se sl e e p m o d e (SLEEP MODE IDLE ) ;
sleep enable ();
powe adc disable ();
p o w e s p i d i s a b l e ( ) ;
powe ime 0 disable ();
powe ime 1 disable ();
powe ime 2 disable ();
p o w e w i d i s a b l e ( ) ;
sleep mode ( ) ;
sleep disable ();
p o w e a l l e n a b l e ( ) ;
}