= IoT Wireless !
Apostolos rootApostolos@swarmlab.io
// Metadata:
:description: IoT 
:keywords: iot, swarm
:data-uri:
:toc: right
:toc-title: Πίνακας περιεχομένων
:toclevels: 4
:source-highlighter: highlight
:icons: font
:sectnums: 

include::header.adoc[]


{empty} +



== Contiki

Contiki is a state-of-the-art, open
source  operating  system  for  sensor  networks  and  other  networked  embedded
devices 


More Info: 
https://en.wikipedia.org/wiki/Contiki[^]


== Start Instant Contiki


.Start VMware and  load Instant Contiki.
[source,bash]
----
unzip InstantContiki2.7.zip
vmplayer
Start  image
----

== Log In
When the login screen appears, log in to Instant Contiki:

.Log In usermode
[source,bash]
----
Username: user
Password: user
----

.Log In sudo
[source,bash]
----
sudo apt update 
Password: user
----

== Open a Terminal

After  logging  in,  click  on  the  terminal  icon  to  start  a  terminal  window.   

== System test


.compile hello-world 
[source,bash]
----
cd contiki/examples/hello-world/
make TARGET=native
----

.Run the Hello World program in Contiki:
[source,bash]
----
./hello-world.native
----

The program should print the words *“Hello, world”* on the screen and then appear to hang.  

In reality, Contiki is still running correctly,  but will not produce any output because the Hello World program has finished.  
[source,bash]
----
Press Ctrl+C on the keyboard to quit.
----


== Simulating Contiki with COOJA

=== Start COOJA

.start
[source,bash]
----
cd contiki/tools/cooja/
ant run
----

[NOTE]
.INFO
====
COOJA  compiles,  and  after  a  few  seconds  the  simulator  appears.   

All  COOJA  simulations  are  controlled  using plugins:  small Java programs that interact with simulations and simulated nodes.  

When COOJA is started,  no simulation is loaded and no plugins are started.
====

=== Create a simulation

A new simulation is created via the menu.

[source,bash]
----
File > New > Simulation
----

- Enter a Simulation title, and click Create.

We have now created our first simulation in COOJA. However, the simulation does not contain any nodes yet.  

To add nodes we need to first create a node type, and then add nodes to the simulation.


[NOTE]
====
- The *Network* window,  at  the  top  left  of  the  screen,  shows  all  the  motes  in  the simulated network
- The *Timeline* window, at the bottom of the screen, shows all communication events in the simulation over time   
- The *Mote* output window, on the right side of the screen, shows all serial port printouts from all the motes.  
- The *Notes* window on the top right is where you can put notes for your simulation.  
- And the *Simulation* control window is where you start, pause, and reload your simulation.
====



=== Create a node type

Any simulated node in COOJA belongs to a node type.  

The node type determines, among others, which Contiki applications to simulate.  


Click menu item  

[source,bash]
----
Motes > Add Motes > Create new mote type > Sky mote.  
----

You have selected to emulate Tmote Sky nodes, and now need to select what Contiki program to simulate.

- Enter a Description.
- *Click Browse*, and navigate to *contiki/examples/hello-world/hello-world.c*
- *Click Compile* to start compiling the Contiki program
- *Click Create* when complication finishes.

You  will  be  asked  to  enter  the number of motes of your simulation.  

Once you are done, click the Start button in the Simulation control window to start the simulation.



=== Save, Load and Reload in Cooja

Cooja allows for saving and loading simulation configurations.  
When a simulation is saved,  all active plugins are also stored with the configuration.  

.save your current simulation
[source,bash]
----
File > Save simulation
----

.Open simulation
[source,bash]
----
File > Open simulation > Browse...
----

Select a simulation configuration.

When a simulation is loaded, all simulated Contiki applications are recompiled.


.reload your current simulation:
[source,bash]
----
File > Reload simulation  
or press
Ctrl+R
----

.Reminder
[NOTE]
====
Reloading can be used to reset the simulation – to restart all nodes.  

More importantly, reloading a simulation will recompile all Contiki code, useful while developing Contiki programs.  
====



=== Add simulated nodes

[source,bash]
----
Motes > Add motes > [your type description]
----

Enter the number of nodes you want to simulate (e.g., 5), and press Add.


=== Show properties

Go to the *“Network”* window, click the *view* menu, and choose the properties you want to show for the nodes.


==== Radio model in Cooja

From the Network window,  click on the 

[source,bash]
----
View > Radio Environment (UDGM)
----


If you now left-click on one of the nodes,  you will see a green circle around the selected node.  

[NOTE]
====
The green circle presents the transmission range of the node, i.e.  the selected node can communicate with all other nodes within that circle.

You may also see a grey circle around the green circle. 
A  node  in  the  grey  area  cannot  receive  packets  correctly
====



===  Extend the hello-world example

- Create a folder 
- Copy+paste the *contiki/examples/hello-world* folder in your new folder.  

==== Sensors, LEDs and Button

We will extend the Hello World program to let it print sensor data and toggle the leds when the button is pressed.  

.ADD
----
#include "dev/button-sensor.h"
#include "dev/light-sensor.h"
#include "dev/leds.h"
#include <stdio.h>           /* For printf() */
----



Sensors needs to be activated in the main process:


[source,bash]
----
SENSORS_ACTIVATE(button_sensor);
SENSORS_ACTIVATE(light_sensor);
----


To print out the current light sensor value:

[source,bash]
----
printf("Light: \%u\n", light_sensor.value(0));
----

To wait for an event and check if this event is a button press:

[source,bash]
----
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_even && data == &button_sensor);
----

To toggle LEDs:

[source,bash]
----
leds_toggle(LEDS_AL
----

=== Create an IPv6 network


The border router will setup the IPv6 prefix of the network and will initiate the creation of the RPL routing tree.  

The code of the border-router is in contiki/examples/ipv6/rpl-border-router/border-router.c+.  


==== Copy and paste the folder in your project folder.  

- Start  Cooja  and  create  a  new  simulation.   
- Create  a  new  mote  type,  browse to  your  project  folder  where  the  rpl-border-router  is  located  and  compile  it.
- Once the border-router node has been added to the simulation, you then need to  populate  your  network  with  more  motes.    
- For  this,  we  use  the  code  in ipv6/sky-websense/sky-websense.c
. 

The  sky-websense  application  generates sensing data and provides access to the latest data via built-in webserver.


From the Network window, right click on the border-router and select


[source,bash]
----
Mote tools for sky -> Serial socket (SERVER)
----

A new window should appear saying that the border-router node is listening on local port 60001.

At this point, your simulation is ready to start.  *Click on the start button*


cd to your project folder where the rpl-border-router is located

[source,bash]
----
make connect-router-Cooja TARGET=sky
----

You should see something like

[source,bash]
----
ifconfig tun0 inet ‘hostname‘ up
ifconfig tun0 add aaaa::1/64
ifconfig tun0 add fe80::0:0:0:1/64
ifconfig tun0
tun0      Link encap:UNSPEC  HWaddr 00-00-00
----

----
http://[‘‘webserver-ipv6-addr’’]
----

in your web browser to see the available readings.

----
e.g.
Current readings
Light:  65
Temperature:  24C
----



[source,bash]
----
http://[aaaa::212:7401:1:101]/
----

And see the following:

[source,bash]
----
Neighbors
fe80::c30c:0:0:9e
Routes
aaaa::c30c:0:0:9e/128 (via fe80::c30c:0:0:9e)
----


Now  try  to  access  the  light  service  

- with l for  ’light’  
- with t for ’temperature’

[source,bash]
----
http://[aaaa::212:7403:3:303]/l
----




:hardbreaks:

{empty} +
{empty} +
{empty} 

:!hardbreaks:

'''
