In my last article "Control Sonos with Google Assistant - Node server - Part 1" I explained how to create a service to consume webhooks from Google to control your Sonos with your voice. Because this server has to be reachable for Google from outside of your home network, you have to create a domain which is pointing to your server. This is called dynamic dns and is a pretty simple priciple but requires some effort to setup. I'll explain how to create a DynDNS domain based on Cloudflare or the service noip.com which is only limited free.
You might or not know what a domain is and how this works. I try to eplain it in a very simplified way. Think about it like your contacts in your mobile phone. Instead of saving all the plain phone numbers you're saving them with the name of the people owning this number. A domain is almost the same. It's just the alias for a specific ip which is like a phone number in the web. The problem is, that like with your contacts, the alias is linked to a specific telephone number (or ip in the web case) until you manually change it. This is a problem when you have a flexible ip, which you probably have at your home. A possible solution is to periodically update the ip of the dns entry (domain) and this is what I want to show you.
Cloudflare is a service whcih should protect your server from attacks but offers a lot of more features like easy configuration of subdomains. If you don't have an cloudflare account you must create one here. You can register and verify your domain with a free plan which obviously doesn't cost anything :). After that you have to create a dns enty for a subdomain (e.g. my-home) from type A without http proxy (toggle the cloud to grey). Now you can setup the DynDNS to point to your raspberry pi or where ever you hosted the service you setup in the previous part. Luckily we're not the first one who are trying to update the dns entry in cloudflare and that cloudflare offers an API. The project cloudflare-ddns on GitHub offers a python script to do this task for us. Just clone it with Git on the machine where your NodeJS server is running, configre it and add it to your crontab to be executed every half an hour. To configure cloudflare-ddns you have to copy the example.yml in the folder zones and adapt it to your cloudflare and domain setting.
- git clone [email protected]:thatjpk/cloudflare-ddns.git
- sudo mv cloudflare-ddns /usr/share/cloudflare-ddns
- crontab -e
- # append a line like this (new line at the end is required)
- */30 * * * * python /usr/share/cloudflare-ddns/cloudflare-ddns.py -z stagetwo.eu
Now you're done and the dns entry in cloudflare should update every half an hour.
NoIP.com is a service which offers you a service for exactly our use case. The only problem I had was that the free plan requires a verification every 30 days which is pretty annoying. Once you created the account you can create a subdomain of your choice on ddns.net. After that you have to download, install and configure the tool noip2 on your raspberry pi, which will send your current IP to the nopip service and update the dns entry.
The tutorial to do this can be found here.
If you've done everything right it should update your dns entry and you are done
The next part will describe how to setup the Google Assistant agent with Dialogflow: Control Sonos with Google Assistant - Dialogflow - Part 3
I hope that helped you
Feel free to give feedback!