Are you a good talker now? Leave it to Google Home to tell you what's difficult to say in just 10 lines!

table of contents
Hello.
I'm Mandai, in charge of Wild on the development team.
At the end of last year, I received a Google Home at our company's year-end party, but it was just gathering dust at home, so I brought it to the office and let it talk whatever it wanted
What to prepare
The only equipment you will need this time is a Google Home (or a mini) and a PC
The PC does not have to be a Raspberry PI.
If you just want to get it to talk, any PC that can run
google-home-notifier For Windows, rebuilding node-gyp seems to be a challenge.
This article might be helpful.
In my case, a CentOS VM was running when I was planning to do it, so I was able to quickly create it in the VM
if you have the node cron
Preparation
- Find out the local IP address of your Google Home
- Installing node.js
- Installing the libraries required for communication between your PC and Google Home
- Install the required modules with npm
- npm install google-home-notifier
- npm install cron (optional)
Find out the local IP address of your Google Home
For Android, you can check this from the Google Home app
Display the menu from the three lines in the upper left and select Devices.
If it is connected to Google Home, select "Settings" and the IP address should be displayed at the bottom.
Installing node.js
You can use yum, but it's better to use something as recent as possible.
This time, I'm using node.js v8.9.4.
Installing the libraries required for communication between your PC and Google Home
If you try to install google-home-notifier with npm after preparing node, you may get the following error:
npm install google-home-notifier > [email protected] install /home/vagrant/Documents/googlehome/node_modules/mdns > node-gyp rebuild make: Entering directory `/home/vagrant/Documents/googlehome/node_modules/mdns/build' CXX(target) Release/obj.target/dns_sd_bindings/src/dns_sd.o In file included from ../src/dns_sd.cpp:1:0: ../src/mdns.hpp:32:20: fatal error: dns_sd.h: No such file or directory #include^ compilation terminated. make: *** [Release/obj.target/dns_sd_bindings/src/dns_sd.o] Error 1 make: Leaving directory `/home/vagrant/Documents/googlehome/node_modules/mdns/build' gyp ERR! build error gyp ERR! stack Error: `make` failed with exit code: 2 gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:258:23) gyp ERR! stack at emitTwo (events.js:126:13) gyp ERR! stack at ChildProcess.emit (events.js:214:7) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12) gyp ERR! System Linux 3.10.0-693.17.1.el7.x86_64 gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" gyp ERR! cwd /home/vagrant/Documents/googlehome/node_modules/mdns gyp ERR! node -v v8.9.4 gyp ERR! node-gyp -v v3.6.2 gyp ERR! not ok npm WARN [email protected] No description npm WARN [email protected] No repository field. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] install: `node-gyp rebuild` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /home/vagrant/.npm/_logs/2018-03-17T06_56_20_006Z-debug.log
The error is stating that the header file dns_sd.h to be included does not exist, but the header file can be installed by running the following command
sudo yum install avahi-compat-libdns_sd-devel
You may encounter errors when installing using npm due to node-gyp, but node-gyp is simply a tool that automates the compilation of source code written in C++ using node.js, as in this case, so the problem is usually a lack of files required for the build
nvm , it might be easier to clean up afterwards!
Install the required modules with npm
All you need to do is install the necessary modules from npm
The minimum requirement is google-home-notifier, without which you cannot connect to Google Home.
If you add node-cron as an option, it will chat periodically, making it perfect for livening up the place.
# Build a working environment mkdir path/to/project cd path/to/project npm init # Install google-home-notifier npm install google-home-notifier # Install the module for periodic execution (cron) npm install cron
Simple sauce
The code we will provide here is extremely simple, so we will show it to you first
// sample1.js const googlehome = require('google-home-notifier'); const googlehome_name = 'Your Google Home Name'; // Name of your Google Home (anything is fine) const googlehome_ip = 'xxx.xxx.xxx.xxx'; // IP address of your Google Home const language = 'ja'; // Language you want Google Home to speak (English in US) const message = 'Let's talk'; // Message you want to say googlehome.device(googlehome_name, language); googlehome.ip(googlehome_ip); googlehome.notify(message, (res) => { console.log(res); });
Save the above source as sample1.js.
Run it with the following command.
node sample.js
There's almost no need for explanation, but there are three changes to make, so please enter them according to your environment
- googlehome_name
- googlehome_ip
- language
The thing to be careful about is around lines 8 and 9.
Some people seem to get stuck on this, and while line 8 should be enough to resolve the name (DNS lookup) using the name you gave Google Home, it often doesn't work, so we specify the IP address on line 9.
If you specify an IP address, you might think that line 8 is unnecessary, but since it seems that the language setting can only be done from the device() method, I have deliberately included both
Develop it so that it can be carried out regularly
Because the google-home-notifier module is so well-made, there wasn't much to do here, so I'll try to develop it a bit further and make it say something periodically, preferably using cron. I'll try to
implement it using the npm module cron instead of Linux cron.
// sample2.js const googlehome = require('google-home-notifier'); const CronJob = require('cron').CronJob; const messages = require('./messages'); // File that describes the execution interval and what to say const googlehome_name = 'Your Google Home Name'; // Name of your Google Home (anything is fine) const googlehome_ip = 'xxx.xxx.xxx.xxx'; // IP address of your Google Home const language = 'ja'; // Language you want Google Home to speak (English in US) googlehome.device(googlehome_name, language); googlehome.ip(googlehome_ip); messages.forEach((m) => { new CronJob({ cronTime: m.schedule, onTick: () => { googlehome.notify(m.message, (res) => { console.log(res); }); }, start : true, timeZone: 'Asia/Tokyo', }); });
Below is a definition file that contains the spoken sentences and schedule
// messages.js module.exports = [ // The syntax is almost the same as Linux cron { schedule: '0 0 10,13,16,19,22 * * *', message: 'Hogehoge' }, // Note that the concept of seconds is at the beginning { schedule: '0 0 22 * * *', message: 'Test' }, // Use numbers, asterisks, commas, hyphens, and slashes to write { schedule: '0 */5 9-18 1,2,3 * *', message: 'Speaks every 5 minutes from 9:00 to 18:00 in January, February, and March' }, ];
If you thicken messages.js, it will tell you all sorts of things and you'll probably have fun for a while
summary
Just having a device that can talk has given me a lot of fun.
What's more, the level of abstraction of google-home-notifier is so amazing that it's practically unnecessary to do anything on our side, but the fact that you just provide the string you want it to speak as an argument is amazing. I think it could become more common.
I think it would sell better if all of this was included in the Google Home package, but would that mean it's too much?
That's it.
0