|
|
|
# Introduction
|
|
|
|
|
|
|
|
Instead of uploading the light sensor value to the cloud and taking decisions on cloud side, we can process values on mangOH side, thanks to *edge actions*. An action is a few lines of JavaScript that is downloaded to the mangOH board and executed there.
|
|
|
|
|
|
|
|
# Edge action
|
|
|
|
|
|
|
|
First, ensure that the mangOH board is configured in Octave as described in [this page](experimenting/Transfer light value 1).
|
|
|
|
|
|
|
|
Go to Octave **DEVICE/Observations** page, and create a new observation, still based on the light resource:
|
|
|
|
* click on **ADD OBSERVATION...**
|
|
|
|
* for **Observed resource**, select **/light/value/**
|
|
|
|
* for **Observation name**, let's use *light_for_action*
|
|
|
|
* for **Send events to**, select **Edge Action**
|
|
|
|
* enter a description
|
|
|
|
* click on **SAVE**
|
|
|
|
|
|
|
|
Go to **DEVICE/Edge Actions** page and click on **ADD EDGE ACTIONS...**. Then:
|
|
|
|
* select *light_for_action* for **Source observation**
|
|
|
|
* set **Edge action name** to *LightToBuzzer* (that gives you a clue about what we'll try to do :slight_smile:)
|
|
|
|
* in **Code** area, replace `// Your code here…` by following JavaScript code:
|
|
|
|
|
|
|
|
```JavaScript
|
|
|
|
var eventTxt = JSON.stringify(event);
|
|
|
|
console.log("**** " + eventTxt);
|
|
|
|
```
|
|
|
|
|
|
|
|
The whole resulting code is:
|
|
|
|
|
|
|
|
```JavaScript
|
|
|
|
function(event) {
|
|
|
|
|
|
|
|
var eventTxt = JSON.stringify(event);
|
|
|
|
console.log("**** " + eventTxt);
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
As we are not sure yet about the structure of the **event** parameter, we convert it to a string and we display it.
|
|
|
|
|
|
|
|
Click on **SAVE**.
|
|
|
|
|
|
|
|
# Event structure
|
|
|
|
|
|
|
|
Go back to **DEVICE/Resources** page. After a while, the result of the `console.log()` statement is visible as **actions/console** resource:
|
|
|
|
|
|
|
|
![light1-actionProperties](uploads/378c9eba076dc662e84979fa8bfecc63/light1-actionProperties.png)
|
|
|
|
|
|
|
|
So, now we know that the event generated for a new light value and passed to the action contains a `value` property and a `timestamp` property.
|
|
|
|
|
|
|
|
# Action on buzzer
|
|
|
|
|
|
|
|
Now, let's trigger the buzzer if the light level is below a preconfigured value. Replace previous code by the following one:
|
|
|
|
|
|
|
|
```JavaScript
|
|
|
|
function(event) {
|
|
|
|
|
|
|
|
if (event.value < 2.0) {
|
|
|
|
return {
|
|
|
|
"dh://buzzer/enable": [true]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
"dh://buzzer/enable": [false]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Check buzzer configuration in **DEVICE/Resources**. You can configure it in a way similar to this:
|
|
|
|
* **percent**: **15**
|
|
|
|
* **period**: *1*
|
|
|
|
|
|
|
|
Then, put your finger on the [light sensor](http://mangoh.io/uploaded-documents/Reference/mangOH%20Yellow/Discover/HW%20Reference%20Manual/41113116%20mangOH%20Yellow%20HW%20Guide%20-%20HTML5-Responsive%20HTML5/#t=41113116_mangOH_Yellow_HW_Guide_-_HTML5%2FHardware%2FHardware.htm) so that light level value falls below 2.0. After around 15 seconds (and in any case less than 30 seconds), the buzzer starts making a sound.
|
|
|
|
|
|
|
|
You can remove the *light* observation on the device (**DEVICE/Observations** page). The *light* stream on the device won't be updated anymore. Of course, this will be the same for the *light* cloud stream. But the device actions is still active. |
|
|
|
\ No newline at end of file |