2. Configure the candle flow

Switch to your candle flow.

Add an input link node, four function nodes, and an output link
Set the input link node to receive input from the natural language understanding flow.

Add code in the first function node to check the intent and initialize the command for the PLAYBULB Candle. Note that the flow has two outputs. The second output is for errors.
var intent = global.get(‘intent’) || ‘not-understood’;
var cmd = { “cmd” : “setcolor”,
“mode”:””,
“speed”:””,
};
if (‘not-understood’ == intent) {
msg.payload = ‘I do not understood your intention’;
return [null, msg];
} else {
if (‘turn-on’ == intent) {
cmd.rr = 255;
cmd.gg = 255;
cmd.bb = 255;
} else {
cmd.rr = 0;
cmd.gg = 0;
cmd.bb = 0;
}
msg.payload = cmd;
}
return [msg, null];

Code the second function node to check that the instruction relates to the candle. Note that the flow has two outputs. The second output is for errors.
var found = false;
console.error(‘checking object’);
console.error(msg.features);
if (msg.features && msg.features.keywords) {
console.error(msg.features.keywords);
for (var kw in msg.features.keywords) {
var object = msg.features.keywords[kw].text;
//node.error(object);
switch (object) {
case ‘lamp’:
case ‘lighting’:
case ‘bulb’:
case ‘light’:
case ‘candle’:
found = true;
break;
}
}
}
if (found) {
return [msg, null];
} else {
msg.payload = ‘I do not control that’;
return [null, msg];
}

Code the third function node to check for a color:
var colors = [‘red’,’blue’,’green’,
‘yellow’,’magenta’,’cyan’];
var rgb = [ {rr:255, bb:0, gg:0},
{rr:0, bb:255, gg:0},
{rr:0, bb:0, gg:255},
{rr:150, bb:0, gg:150},
{rr:150, bb:150, gg:0},
{rr:0, bb:150, gg:150}
];
var colorFound = ”;
var mix = ”;
var sentence = global.get(‘sentence’);
for (var c in colors){
if (-1 !== sentence.indexOf(colors)) {
colorFound = colors;
mix = rgb;
}
}
if (colorFound) {
Object.assign(msg.payload, mix);
}
return msg;

You use the fourth function node to counter the fact that the input text will be from another IoT connected device.
Select the IBM IoT node and read its associated info pane.
The IBM IoT node allows you to override the node configuration by setting msg.deviceID, msg.deviceType, and so on. This means that if they are set, then those will be used to determine which device the device command is sent to.
The input request comes from a SensorTag. That means that the msg.deviceID and other settings will point to the SensorTag. Therefore, all instructions will be sent to the SensorTag unless you remove those two properties. You want the instructions to be sent to the PLAYBULB Candle.

Because you don’t want the IBM IoT node to override node configurations, configure the fourth function node to remove the msg object.
var m = {};
m.payload = msg.payload;
return m;

Give the link out node a meaningful name, such as Cmd Error.

Link the nodes together.

Wire the output error node to your text to speechnode.

Make sure the SensorTag is broadcasting.

Your Raspberry Pi can be running in headless mode, without a monitor, keyboard, or mouse with only the power connected.

Ensure that the application that you cloned and configured in lab 2 is running on your Raspberry Pi.
sudo node pipbiot

Try your application with sample text, voice, or SensorTag input.

Test the text or commands that should be rejected.

If you have problems, rewire the natural language understanding flow to process the Natural Language Understanding and Natural Language Classifier in a series rather than in parallel.

Summary
You should now have an application through which you can control a PLAYBULB Candle through speech and with rotational movements of a SensorTag manipulated with hand gestures.