Turn on an LED With Watson Conversation
by ZRob314 in Circuits > Raspberry Pi
1905 Views, 4 Favorites, 0 Comments
Turn on an LED With Watson Conversation
![IBM Watson Turning on LED](/proxy/?url=https://content.instructables.com/FZZ/C0KK/J5Y81VFO/FZZC0KKJ5Y81VFO.jpg&filename=IBM Watson Turning on LED)
What you'll need:
You will need to have node already installed on your Pi. You may find NPM's rpio-gpio site helpful for syntax.
Run the command npm install rpi-gpio in terminal.
![Capture.JPG](/proxy/?url=https://content.instructables.com/FS2/K1AR/J654NJP2/FS2K1ARJ654NJP2.jpg&filename=Capture.JPG)
![Capture2.JPG](/proxy/?url=https://content.instructables.com/FWP/22XV/J654NJP6/FWP22XVJ654NJP6.jpg&filename=Capture2.JPG)
We started with the conversation.js file from Watson's TJBot example, and added the following lines before we instantiated our bot.
var gpio = require('rpi-gpio');
var pin = 7;
gpio.setup(pin, gpio.DIR_OUT);
The first parameter for setup() is the channel. Make sure to reference the RPi pin number and not the GPIO. The second parameter is the direction, DIR_OUT writes to pin #7. You can also change the name of your bot to something different. We picked "Bob" as it was less likely to be confused with other words.
// instantiate our TJBot!
var tj = new TJBot(hardware, tjConfig, credentials);
tj.configuration.robot.name ="Bob";
![Capture3.JPG](/proxy/?url=https://content.instructables.com/FJ5/4DYB/J654NJPV/FJ54DYBJ654NJPV.jpg&filename=Capture3.JPG)
After the utterances part of the code add the following code for speech recognition.
var containsOn = msg.indexOf("on") >= 0;
var containsOff = msg.indexOf("off") >= 0;
var containsLight = msg.indexOf("light") >= 0;
//turns on light
if (containsLight && containsOn ) {
console.log("Turn on Light")
gpio.write(pin,true);
};
// turns off light
if (containsLight && containsOff ) {
console.log("Turn off Light")
gpio.write(pin,false);
};
![Set up for pins.jpg](/proxy/?url=https://content.instructables.com/FZK/FEJF/J5Y81X77/FZKFEJFJ5Y81X77.jpg&filename=Set up for pins.jpg)
Setup for the pins.
The complete node js code.