-
Backup SD memory card
-
Install node server
How to Install Node.js on Your Raspberry Pi - Jeremy's Raspberry Pi Blog
cd /usr/src
sudo wget http://nodejs.org/dist/v0.8.16/node-v0.8.16.tar.gz
sudo tar xvzf node-v0.8.16.tar.gz
cd node-v0.8.16
sudo ./configure
sudo make
sudo make install
sudo make test
nano test.js
// Load the http module to create an http server.
var http = require(‘http’);
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, { “Content-Type”: “text/plain” });
response.end(“Hello World\n”);
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log(“Server running at http://127.0.0.1:8000/”);
And save it. Then run the follow
sudo node test.js
And then point your web browser to http://127.0.0.1:8000 and you will see a hello world! message.
- Setup custom node.js to listen to TTS call from Vera:
http://forum.micasaverde.com/index.php?topic=14132.0
nano node.js
var net = require(‘net’);
var cp = require(‘child_process’);
/*
- Callback method executed when a new TCP socket is opened.
/
function newSocket(socket) {
socket.write(‘Welcome to the Telnet server!’);
socket.on(‘data’, function(data) {
receiveData(socket, data);
})
}
/
- Cleans the input of carriage return, newline
*/
function cleanInput(data) {
return data.toString().replace(/(\r\n|\n|\r)/gm,“”);
}
/*
-
Method executed when data is received from a socket
*/
function receiveData(socket, data)
{
var cleanData = cleanInput(data);
var speakCmd = '/home/pi/speech.sh ';
var speakData = “”;
switch(cleanData)
{
case "@frontdoor":
speakData = 'There is movement detected at the front door.';
break;
case "@sidehouse":
speakData = 'There is movement detected at the side of the house.';
break;
default:
speakData = cleanData;
}
cp.exec(speakCmd + speakData);
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
console.log(hours + “:” + minutes + " " + cleanData);
}
// Create a new server and provide a callback for when a connection occurs
var server = net.createServer(newSocket);
// Listen on port 5150
server.listen(5150);
-
Test node + TTS
cp.exec(‘/home/pi/speech.sh Welcome.’);
-
Start node listener
sudo node node.js
-
Put function tts in Vera’s start up lua:
SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
SP_SID = “urn:upnp-org:serviceId:SwitchPower1” – Switch Power Service ID
function turnOnLight(devID)
local lightStatus = luup.variable_get(SP_SID, “Status”, devID)
if (lightStatus == “0”) then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 1}, devID)
end
end
function turnOffLight(devID)
local lightStatus = luup.variable_get(SP_SID, “Status”, devID)
if (lightStatus == “1”) then
luup.call_action (SP_SID, “SetTarget”, {[“newTargetValue”] = 0}, devID)
end
end
function tts(text)
local socket = require(“socket”)
host = “192.168.1.5”
local tcp = assert(socket.tcp())
if ( tcp ~= "nil" and tcp ~= nill) then
tcp:settimeout(0.5)
tcp:connect(host, 5150)
--tcp:send("@frontdoor\r")
tcp:send(text)
tcp:close()
end
end
function log(message, clear)
local socket = require(“socket”)
local time = socket.gettime() or os.time()
local tms = string.format(“.%03d “,math.floor (1000 * (time % 1)))
local stamp = os.date(”%d %b %Y %T”,math.floor(time)) … tms
local mode = “a+”
if clear then mode = “w+” end
local file = io.open(“/www/log.txt”, mode)
file:write(stamp … (message or “”) … “\n”)
file:close()
end
– init global functions so that we can call these global functions in any scene.
tts(“Hello Vera”)
log(“Lua Startup”)
After this step, we have working Voice Control System and Voice Feedback System. We backup SD memory card to keep all our hardwork.
Our Vera now can give voice command to Echo Dot, we can focus on thinking how to program Vera to make use of Amazon’s Big Data Cloud Computers via Echo Dot.
We can also scale up the Voice Control System and Voice Feedback System by order more Echo Dot for each room, install more Raspberry PI + USB Speaker around our house.
For example: install a Raspberry PI + USB Speaker near main gate (in outdoor waterproof case). At night, if motion sensor detects any movement near the main gate, Vera can simply ask tts_maingate(“Who are you?”) then Vera can warn again if movement does not stop after minutes, tts_maingate(“This is private property. Any trespassing will be reported to Police”). We can not put expensive Sonos speaker outdoor, but we can put Raspberry PI + USB Speaker outdoor to enhance security system.