WebSocket : Home Automation

Hi,

In order to integrate Volumio to my home automation software, I’m trying to use socket.io to send some commands to Volumio (play, pause, play a playlist…).
I read to API documentation but I still have some problems to connect Volumio.

Can you help me please?
Here’s my code where I try to set the volume to 30.

var socket = io.connect('http://192.168.23.10',{secure: false});
    
    console.log('try to connect');
    socket.emit('volume',{vol:15,mute:false});
    
    socket.on('connection', function (data) {
      console.log(data);
      
    });

Thanks
Capture.PNG

Hi,

first, I suggest to use socket.io-client for your purpose, since you don’t need to make any server…

So, first step install it

npm install socket.io-client

Then, I prepared a code snippet with various examples for you:

[code]var io=require(‘socket.io-client’);

var socket= io.connect(‘http://192.168.31.242:3000’);

//Report successful connection
socket.on(‘connect’, function () {
console.log(‘Client Connected’);
});

//Report disconnection
socket.on(‘disconnect’, function () {
console.log(‘Client Disconnected’);
});

//Notify on player state changes, this includes volume changes, songs etc
socket.on(‘pushState’, function (data) {
console.log(data);
});

//Notify just the volume value
socket.on(‘pushState’, function (data) {
console.log(data.volume);
});

//Set Volume to 15
socket.emit(‘volume’, 15);

//Increase Volume by value specified by “On click Volume Change”, default is 10
//socket.emit(‘volume’, ‘+’);

//Decrease Volume by value specified by “On click Volume Change”, default is 10
//socket.emit(‘volume’, ‘-’);
[/code]

(sorry but the volume value in the docs was referring to old APIs, I will fix it as we speak)

Let me know how it works, I’m really interested!

Thank you for the quick answer.

For now, I tried the function to set the volume and it works very well! :smiley:

Do you have some documentation for basic function (play/pause, play a selected playlist)?

Thank you

Sure, here it is

volumio.github.io/docs/API/WebSocket_APIs.html

let me know if its missing something or if something does not work…

Yeah, I got some problem to run a “play” command

For the volume I use the following command and it works perfectly

socket.emit('volume', 15);

But I tried these and it doesn’t work

socket.emit('command', 'play');
or
socket.emit('play', 'play');

do you have an example?
Thanks

Ok, I’m so dumb
Sorry for the disturbance, it works fine.

That’s what I used

socket.emit('play');
socket.emit('pause');
...

Thank you!

To be perfectly ok it should be

socket.emit('play', '');
socket.emit('pause', '');

I could run the above code volume using Asterisk and voice recognition. I’m trying to run the playlist function with the sample code but I am not having success, as would be the code for the user function playlist using socket.io?

playPlaylist
This method clears the queue, adds the playlist and play

Input:

{
“name”:“my playlist”
}

This is what your code should look like:

var io=require(‘socket.io-client’);

var socket= io.connect(‘http://IPOFPI:3000’);

socket.emit(‘playPlaylist’, {‘name’:‘asd’});

where asd is the name of your playlist. Let me know

Works fine with Playlist of Music Library, with spotify does not work!

Aaaah ok… I see

Spotify APIs use the plugin api, which is a bit different:

To play a playlist
socket.emit(‘addPlay’, {‘service’:‘spop’,‘title’:‘title’,‘uri’:‘spotify/playlists/1’});

where: title is playlist title
uri: is the number of the playlist

To get the playlists

socket.emit(‘browseLibrary’, {‘uri’:‘spotify/playlists’});

socket.on(‘pushBrowseLibrary’, function (data) {
console.log(data);
});

Then you need to parse data (we are in a middle of API change)

Let me know how it goes

Michelangelo,

Works fine the code of spotify:

#To play a playlist
socket.emit(‘addPlay’, {‘service’:‘spop’,‘title’:‘title’,‘uri’:‘spotify/playlists/1’});

thank you so much

Great!
I’m curious to see what you’re developing!

Basically I have a Asterisk server running voice recognition. With options on and off the lights in the house and running the playlists Volumio.Once you are finished put a tutorial

Wow nice!

These examples were really helpful!

I have another question:

How would you search the database and add the results to the playing queue?

I would like to clear the queue first, then look for something like “genre=Soundtrack” or “artist=”, add all the found songs to the queue, shuffle and then start playing.

In the past I did this with mpc commands and I bound keys on an IR remote control to certain search sets. I would like to have this feature back. :smiley:

How to play a particular track in queue?
emit(“play”, 5) doesn’t work.
UPDATE:
Ok, got it: need to send 5 as JSON object.

How do I play a saved webradio by using its name?

How do I set/unset the “consume” flag?

How can I create a script to run with “node” that returns after sending a series of commands?

Can anyone help with the above?

I have gone back to the old Volumio because I find mpc much easier to use for my needs.

Executing a node script was surprisingly slow on my Raspberry 2.