Hi everyone! For the help request of someone who contacted me via Twitter, I decided to do this base project in which wit.ai is used to access the Cayenne MQTT server.
The project is a basic switcher that controls a channel on the Cayenne server.
1. Create a New Project in wit.aihttps://wit.ai/laurenceHR/Cayenne5
2. Create history for 'switch on'npm install node-wit
npm install cayenne-mqtt
https://gist.github.com/laurenceHR/3ec0c44a2adbe0e8d9dd69cd5e48c504
'use strict';
const Wit = require('node-wit').Wit;
const interactive = require('node-wit').interactive;
const Cayenne = require('cayenne-mqtt');
const accessToken = 'QRV6QVSA6L...';
var cayenne_options = {
'clientId' : 'c1d7df70...',
'username' : 'f671f020...',
'password' : '6c7dbdfd...',
'debug': true
}
var Client = new Cayenne.Client(cayenne_options);
Client.connect();
var Ch4 = Client.Channel('4');
// Quickstart example
// See https://wit.ai/ar7hur/quickstart
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value
;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
console.log('sending...', JSON.stringify(response));
},
turn({context, entities}) {
//console.log('context'); console.log(context);
//console.log('entities'); console.log(entities);
var v = firstEntityValue(entities, 'on_off');
console.log('value: ' + v);
if(v == 'on'){
Ch4.switchOn();
}
if(v == 'off'){
Ch4.switchOff();
}
return context;
},
};
const client = new Wit({accessToken, actions});
interactive(client);
6. Run the projectI like interacting with IA interfaces. For one of the following projects, I will make a skill for Amazon Alexa which interacts with the Cayenne Server.
Stay tuned!
Comments