Skip to content

The CED Task Example

Daniele Alessandrelli edited this page Aug 23, 2013 · 12 revisions

The CED task is an example of Compound Event Detection (CED). The tasks combines the data coming from two different sensors and decides if a certain event has happened.

The processing function of the CED task is the following:

from tres_pymite import *                                                        
                                                                                 
class state:                                                                     
  def __init__(self):                                                            
    self.s1 = -1                                                                 
    self.s2 = -1                                                                 
                                                                                 
print "Compound Event:",                                                         
s = getState(state)                                                              
x = getIntInput()                                                                
tag = getInputTag()                                                              
if tag == "sens_A":                                                              
  s.s1 = x                                                                       
if tag == "sens_B":                                                              
  s.s2 = x                                                                       
if (s.s1 >= 0) and (s.s2 >= 0):                                                  
  if s.s1 > s.s2:                                                                
    print "sens_A > sens_B"                                                      
    setOutput("sens_A > sens_B")                                                 
  else:                                                                          
    print "sens_A <= sens_B"                                                     
saveState(s) 

As you can see the task produces an output only when the last value from sensor "sens_A" is greater then the last value from sensor "sens_B". "sens_A" and "sens_B" are tags used to distinguish the two input sources. Refer to the T-Res Python API to learn more about input tagging in T-Res.

You are now going to install the CED task into node #2 setting the sensors on node #3 and node #4 as input and the actuator on node node #4 as output. The sensor on node #3 will be labeled as "sens_A", while the sensor on node #4 will be labeled as "sens_B".

node #3 (sensor [sens_A]) --\
                             |--> node #2 (CED task) --> node #4 (actuator)
node #4 (sensor [sens_B]) --/ 

Create the task

To create the CED task on node #2 you must issue a PUT request for its URI, i.e., coap://[aaaa::200:0:0:2]/tasks/ced.

The node should reply with 2.01 Created.

As usual, 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.

CED task created

Set the input of the task

To set sensors on nodes #3 and #4 as input of the CED task, you must perform two different requests.

First, send a PUT request to the CED task's /is sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/ced/is) with the following content:

<coap://[aaaa::200:0:0:3]/sensor>;tag="sens_A"

Then send a POST request with the following content:

<coap://[aaaa::200:0:0:4]/sensor>;tag="sens_B"

A POST request to /is adds a new input source without deleting the old one(s).

The node should reply with 2.04 Changed for both requests.

To ensure that the input sources are set correctly, perform a GET request. The node should reply as in the following picture.

Node response to a GET request for the \is resource of the CED task (when set correctly)

Set the output destination of the task

To set the actuator on node #4 as output destination of the CED task, send a PUT request to its /od sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/ced/od) with the following content:

<coap://[aaaa::200:0:0:4]/actuator>

Set the processing function of the task

To set the processing function of the CED task, send PUT request to the task's /pf sub-resource (i.e., coap://[aaaa::200:0:0:2]/tasks/ced/pf) setting the contiki-tres/examples/tres/bin/ced.pyc file as payload.

Remember to use block-wise transfer by enabling both the Debug options checkbox and the auto checkbox, and setting the Block1 (Req.) field to 0. Also check that the block size is equal to or less than 64 (under the Behavior drop-down menu).

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 to File.
  • 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 (or less).

After the successful upload of the processing function, change payload back to Text and disable Debug options.

Activate the task

To activate the CED task send a POST request to the task URI (i.e., coap://[aaaa::200:0:0:2]/tasks/ced) .

As usual, the node should reply with the following payload: Task now running.

In the Cooja simulation you should see something similar to the following picture.

Cooja output for the CED task

Please note that the CED task is called every time a new notification is received. Therefore its execution rate is double the sensor rate.

You can also note that the output is sent to the actuator only when the condition sens_A > sens_B is veified.

You can now start developing your own T-Res task.