HowTo – Simple Squeezebox Server Automation with OpenHAB and Smartphone

In this HowTo we’ll show you how to automatically start playback of your Squeezebox Server every time your smartphone reconnects to your Wi-Fi. It also pauses playback when your phone disconnects from the Wi-Fi network. For this we can use the OpenHAB server, which is available as a premium plugin for Max2Play.

OpenHAB is a home automation server that installs and connects your smart devices in just a few steps. Together with Max2Play, you have a large number of playback automatisms at your disposal that make listening to music more comfortable.

Note: If you already have an OpenHAB server installed on another device, you can skip the first step.

Step 1

First you have to install our OpenHAB plugin. You can find out how to do this in this wiki article. After successful installation, click on the big blue button to open the OpenHAB web interface. Select the standard installation here.

OpenHAB works with so-called bindings, things, items and channels.

Bindings are the interface between hardware (such as measuring devices) and software (OpenHAB). So these are addons that allow communication between the server and the devices.

Things are entities that can be physically added to a system and provide more than one function (e.g., temperature meter, motion sensor). A Thing may also be a web service or other source of information, such as the Squeezebox Server.

Channels represent the abilities or functions of Things. They are linked with items and thus get activated. This means that the function representing the item can be accessed through this channel.

Items represent functions that can be used by applications, either in user interfaces or in automation logic. Items have a status and can receive commands, such as a switch.

Step 2

We use the Paper Ui to make our configurations. Click on Add-ons, Bindings and install the Network Binding. It lets you add devices to your network as Things.

Now go to the Inbox, click on the „+“ icon, select the newly installed Network Binding and search for your network devices. In this case we choose our smartphone. If you can not find it in the list, check your router’s user interface to see which IP has been assigned to your phone and add it to OpenHAB manually.

Step 3

Now only the Squeezebox Server and the corresponding player are missing. Install the Squeezebox Binding (same procedure as Network Binding), search for the server or add it manually if necessary. The IP address of the Squeezebox Server corresponds to the IP address of the Max2Play device on which it is installed. Then we do the same with Squeezelite. You may also need to manually connect Squeezelite to the server via a so-called bridge. Go to the settings of Squeezelite in the OpenHAB web interface (Configuration/Things/Squeezelite/) and select the appropriate bridge.

Under Configuration/Things you can now activate the corresponding channels for each of the 3 entities. After activating the channels, you can now control Squeezebox playback in the Control Panel.

Step 4

To automatically start the playback when connecting your smartphone to the Wifi, we now have to create and edit two files in the OpenHAB folder: default.items and default.rules.

The easiest way to access the System Folder of your OpenHAB device on Windows is a Samba Share. In this wiki, you can read about how to connect such a network drive. Once you have access to the folder, go to drive:/etc/openhab2/items and create the file default.items, e.g. by creating a new text file and renaming it. Also, add a default.rules file to the folder drive:/etc/openhab2/rules.

Step 5

Now open default.items with an editor of your choice and insert the following source code:

Switch Smartphone "Online" <network> { channel="<Smartphone>" }

Here, you have to use the OpenHAB-ID for <Smartphone>, which you can find under the online channel (starts with „network: device …“). The general syntax is:

ItemType ItemName "ItemDescription" <ItemIcon> {ItemToThingChannelLink}

The ItemName should be unique, so that there is no overlap and therefore no errors.

Step 6

The default.rules should be as follows:

rule "Smartphone ON"

when

   Item Smartphone changed from OFF to ON

then

   sendHttpGetRequest("<API-URL for the "Play"-command from our plugin API-Examples>")

end



rule "Smartphone OFF"

when

   Item Smartphone changed from ON to OFF

then

   sendHttpGetRequest("<API-URL for the "Pause"-command from our plugin API-Examples>")

end

Now you may delete the previous (now superfluous) online switch in OpenHAB under Configuration/Things/Smartphone/Channels.

Step 7

Now the playback of the Squeezebox server should start automatically when the smartphone connects to the Wifi. In addition, it should pause when the smartphone disconnects again.

This is just one of the many automations that can be set up with OpenHAB. The server also offers many more flexible application and configuration options.

The „Tasker“ app offers similar possibilites. However, this type of automation would only be possible with GPS because the pause command can not be sent after disconnecting from the Wifi.