-
Notifications
You must be signed in to change notification settings - Fork 4
The Halve Task Example
The Halve task is a simple task that just divides by two the data generated by the monitored sensor.
The processing function of the Halve task is the following:
from tres_pymite import *
print "Halve:",
i = getIntInput()
print i/2.
setOutput(i/2.)
The getIntInput()
and setOutput()
functions are part of the T-Res Python API. getIntInput()
gets (as an integer) the value of the notification that triggered the task. setOutput()
sets the output of the task. If setOutput()
is not called the task produces no output (useful in event-detection applications when no event is detected).
As you can see, the task also prints the result of the division to the standard output (i.e., the Mote output
window).
You are now going to install the Halve task in node #2 setting the sensor on node #3 as input and the actuator on node #4 as output.
node #3 (sensor) --> node #2 (halve task) --> node #4 (actuator)
To create the Halve task on node #2 you must issue an empty PUT request for its URI, i.e., coap://[aaaa::200:0:0:2]/tasks/halve.
The node should reply with 2.01 Created
.
Important: you must use CoAP 13.
Now you can perform a GET request on the same URI to update the resource tree with the task resource just created and its sub-resources. You should end up with something like this.
To set the sensor on node #3 as input of the Halve task, send a PUT request to its /is
sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/halve/is) with the following content:
<coap://[aaaa::200:0:0:3]/sensor>
Note the angle brackets, as specified by RFC 6690 (CoRE Link Format).
The node should reply with 2.04 Changed
. You can also check if the input source is correctly set by performing a GET request.
To set the actuator on node #4 as output destination of the Halve task, send a PUT request to its /od
sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/halve/od) with the following content:
<coap://[aaaa::200:0:0:4]/actuator>
The node should reply with 2.04 Changed
. You can also check if the output destination is correctly set by performing a GET request.
To set the processing function of the Halve task, you must upload the file contiki-tres/examples/tres/bin/halve.pyc
to node #2 using a PUT request to the task's /pf
sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/halve/pf).
The file halve.pyc
contains the processing function already compiled into Python bytecode. If you prefer, you can recompile it from source using these instructions.
To set such a file as the payload of your PUT request using Copper, change the Payload
field from Text
to File
and choose the correct file.
For being able to actually perform the PUT request, you must use block-wise transfer. To do that, enable the Debug options
checkbox, set the Block1 (Req.)
field to 0, and enable the Auto
checkbox, as in the following picture. Moreover, using the Behavior
drop-down menu, set a block size equal to or less than 64.
Once you have successfully performed the PUT request, the node should reply with 2.04 Changed (Blockwise) (Upload finished)
.
If the response is different, please double check that:
- The
Payload
field is set toFile
. - The
Debug options
checkbox is enabled. - The
Block1 (Req.)
field is set to 0. - The
auto
checkbox is enabled. - The Block size is set to 64 (under the
Behavior
drop down menu).
After the successful upload of the processing function, change payload
back to Text
and disable Debug options
.
To activate the Halve task send a POST request to the task URI (i.e., coap://[aaaa::200:0:0:2]/tasks/halve).
The node should reply with the following payload: Task now running
.
The task is now running on node #2. Every time node #3 produces a new sensor value, the task is executed and its output (i..e, the sensor value divided by 2) is sent to node #4. In the Cooja simulation you should see something similar to the following picture.
The task can be stopped by issuing another POST request to its URI.
You can now move on to the EMA task.