Control Sonos with Google Assistant - Node server - Part 1

  • Quite a time ago I bought a Sonos Play:1 because I was super sure I can control this with my new Google Home Mini and this would be super fancy. Sadly I was negatively suprised :(. There wasn't and still isn't native support and even on google there were not that many options to work around this. I checked out if IFTTT is able to connect to my Sonos but no luck here. I read that Logitech Harmony products are able to control the Sonos players but this wasn't working as I expected and I keept searching. I found an option with the service Yonomi which seem to be similar to IFTTT but i didn't want to register for another not fitting service. Yesterday I was thinking about the way I searched and that I might find solutions which requires development skills and I found something. Actually Sonos players got an HTTP API and there is a NodeJS project out there which wraps this API in a simple to use gateway. I'll explain in the next articles how to setup the Sonos HTTP gateway, a Google Assistant gateway and the Dialogflow project to be able to talk to the google assistant something like "Ok Google, tell my Sonos to play Rodrieguez Jr."


    Requirements

    • NodeJS
    • Git
    • Docker (optional)
    • PC or Raspberry PI

    Setting up the HTTP API gateway

    First of all you have to create a developer application for the service you're using. I'm using Spotify and went to their developer console to get a client and secret id. If you got those you have to clone the sonos http api project by jishi. This project aims to create a easy to use wrapper for the Sonos HTTP API.

    Code
    1. git clone https://github.com/jishi/node-sonos-http-api

    Now you have to create a settings.json inside the cloned project to tell the API Gateway which port to use and which credentials to use for the target music service.

    You either can run the application using node server.js or you do it like me and use Docker compose to start both services together :)

    Here is my Dockerfile:

    Setting up the Google Assistant Gateway

    To be able to receive the notifications from Google Assistant we need to provide a URL where Google can post his data to. This data will be consumed by the project home-sonos and will trigger the corresponding HTTP API of our Sonos wrapper service. To set this up you have to clone the repository

    Code
    1. git clone https://github.com/danielduhh/home-sonos

    Then you need to configure this service and tell him how to call the Sonos HTTP wrapper, which music service to use (e.g. spotify) and whats the zone of the Sonos player. You can do this by modifying the ./common/settings.js

    Code: home-sonos/common/settings.js
    1. module.exports = {
    2. "sonosUrl": "http://localhost:5005",
    3. "zoneName": "Esszimmer",
    4. "musicService": "spotify"
    5. };

    Also this service can be started by calling node server.js but as mentioned before I prefer the Docker compose tooling. So you can create a Dockerfile as well.


    As the last step you have to create a docker-compose.yaml to tell Docker how the services have to be started.


    We're almost done :D Just start the docker container by running docker-compose up and you're ready to go.

    To kill the service run docker-compose down.


    Here you can find the next article which will explain how to create a DynDNS domain: Control Sonos with Google Assistant - DynDNS - Part 2

Teilen