PyoT Demo - IPSN '14

Setup the environment


In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
from networkx import *
from pyot.rplApp import *
from pyot.models import *
from IPython.display import Image, display
from networkx.algorithms import *
import numpy as np
import os
from pyot.models.tres import *
from pyot.tres.tresApp import *

def pngShow(path):
    i = Image(filename=path)
    display(i)

Dynamic RPL DAG visualizazion


In [ ]:
n = Network.objects.get(hostname='rasp@pyot')

g = DAGupdate(n)
pngShow(g.getPNG())

In [ ]:
n = Network.objects.get(hostname='cooja@pyot')

g = DAGupdate(n)
pngShow(g.getPNG())

Toggle actuators on every node - Synchronous semantic


In [ ]:
actuators = Resource.objects.filter(uri='/actuators/toggle', host__active=True, host__network__hostname='rasp@pyot')


for i in range(4):
    for act in actuators:
        act.POST(query="mode=off")
        #act.POST()
        print 'toggling res: ' + str(act) 

    for act in actuators:
        act.POST(query="mode=on")
        #act.POST()
        print 'toggling res: ' + str(act) 
        
print 'Done'

Toggle actuators on every node - Asynchronous semantic


In [ ]:
actuators = Resource.objects.filter(uri='/actuators/toggle', host__active=True, host__network__hostname='rasp@pyot')

for i in range(1):
    tasks = []
    for act in actuators:
        tasks.append(act.asyncPOST(query="mode=on"))
        print 'toggling res: ' + str(act) 
    for task in tasks:
        task.wait()
        print task.result
    print 'Done'

    tasks = []
    for act in actuators:
        tasks.append(act.asyncPOST(query="mode=off"))
        print 'toggling res: ' + str(act) 
    for task in tasks:
        task.wait()
        print task.result
    print 'Done'

Retrieve and display light values


In [ ]:
r = Resource.objects.filter(uri='/sensors/light', host__active=True)
print r
y = []
y2 = []
for light in r:
    resp= light.GET()
    v = resp.content
    print light, v
    y.append(int(v.split(';')[0]))
    y2.append(int(v.split(';')[1]))

#plot everything
x = np.arange(len(r))
fig, axes = plt.subplots(nrows=1, ncols=2)
axes[0].bar(x, y, align="center", width=0.5, alpha=0.5)
axes[0].set_xlabel('Nodes')
axes[0].set_ylabel('lux')
axes[0].set_ylim(0, 400)
axes[0].set_title('Light sensor value 1')

axes[1].bar(x, y2, align="center", width=0.5, alpha=0.5)
axes[1].set_xlabel('Nodes')
axes[1].set_ylabel('lux')
axes[1].set_ylim(0, 400)
axes[1].set_title('Light sensor value 2')

fig.tight_layout()

In-network processing with T-Res

Define a new processing function and install it on a T-Res Node


In [ ]:
halve = """
from tres_pymite import *
print "Halve:",
i = getIntInput()
print i/2.
setOutput(i/2.)
"""

t1 = TResPF.fromSource(halve, 'halve')

rin = Resource.objects.filter(uri='/light', host__active=True, host__network__hostname='cooja@pyot')
rout = Resource.objects.filter(uri='/actuators/leds')[0]
print rin, rout

tresTask = TResTask(TresPf=t1, inputS=rin, output=rout, period=0)
print tresTask


r = Resource.objects.get(uri='/tasks')
print r.host, '    is a t-res node'
resp = tresTask.deploy(r)
print resp

Activate the task


In [ ]:
resp = tresTask.start()
print resp

Get the last output from the T-Res node


In [ ]:
lo = tresTask.getLastOutput()
print lo.GET()

Stop the task


In [ ]:
resp = tresTask.stop()
print resp

Uninstall the task


In [ ]:
resp = tresTask.uninstall()
print resp

In [ ]: