Are you now a good talker? Let Google Home speak for you in just 10 lines if you have something difficult to say!

table of contents
Hello.
I'm Mandai, the Wild team member in charge of development.
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 you need
The only equipment you will need this time is a Google Home (or a mini) and a PC
You don't necessarily need a Raspberry Pi PC.
For simply getting it to speak,google-home-notifierany PC that can run
On Windows, rebuilding node-gyp seems to be the main hurdle.
Thesearticles 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
Furthermore, if you have the Node.jscronmodule installed, you can also send scheduled notifications.
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
On Android devices,the Google Home appyou can find this information through
Display the menu by clicking the three lines in the upper left corner and select Devices.
If you have successfully connected with Google Home, select "Settings" and your IP address should be displayed at the bottom.
Installing node.js
You can use yum, but it's best to use the newest version 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 [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
nvmfor installation might make cleanup easier!
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 it, integration with Google Home is not possible.
Optionally, installing node-cron will allow it to chat periodically, making it perfect for adding some noise to your home.
# 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 code as sample1.js.
Execute 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
A point to note is around lines 8 and 9.
It seems that quite a few people get stuck on this. Normally, name resolution (DNS lookup) should be possible using only the name you assigned to Google Home with just line 8, but it often doesn't work, so the IP address is specified in 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
The google-home-notifier module is so well-made that there wasn't much for me to do, so I decided to develop it further and make it say something periodically, preferably using cron. I'll
try to implement this using the npm module's cron rather than Linux's built-in 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 made things so much fun.
Above all, the level of abstraction in google-home-notifier is incredible; it's almost to the point where you don't need to do anything on your end. The fact that you only need to provide the string you want it to speak as an argument is amazing. I think this should become more common.
If this were included in the Google Home package, I think it would sell even better, but perhaps that would be too much?
That's all
0
