<contenttype="html"><![CDATA[<p>Would you like to listen to music in every room in your home, controlled from one source? Then multi-room audio is for you.</p>
<p>Multi-room audio can be achieved by having a computer attached to speakers in every room. On each computer, services run to play and/or control the audio. With this DIY approach, the kind of computer and speakers is very much up to you. It could be your desktop computer with attached powered speakers, your HTPC hooked up to your TV and receiver, a Raspberry Pi with Amp or DAC, or even an Android device.</p>
<p>You’ll need two key software packages, besides Home Assistant. The first is <ahref="https://www.mopidy.com/">Mopidy</a>, a music server that can play local files, or connect to streaming music services like Spotify. The second is <ahref="https://github.com/badaix/snapcast/">Snapcast</a>, which enables synchronized audio streaming across your network. Both can be integrated into Home Assistant. Each room audio device will run an instance of the Snapcast client, and optionally a Mopidy instance. Your server will run a special instance of Mopidy and the Snapcast server.</p>
<p>Finally, you also need a player to control Mopidy. Any MPD-compatible player will work, and there are several <ahref="https://docs.mopidy.com/en/latest/ext/web/#ext-web">Mopidy-only web-based options</a> available. On Android, <ahref="https://play.google.com/store/apps/details?id=se.anil.remotedy">Remotedy</a> is particularly nice since you can access multiple Mopidy instances in one place.</p>
<p>Home Assistant will provide device status, and volume control for each room. If you want to play music in all your rooms (on all your clients), access the server instance of Mopidy. If you want to play music only in a specific room, access that specific Mopidy instance. If you’re using a web UI for Mopidy, you can add links to each instance in Home Assistant with the <ahref="/components/weblink/">weblink</a> component.</p>
<li><ahref="https://www.mopidy.com/">Install</a> Mopidy (2.0.0 or greater)</li>
<li><ahref="https://github.com/badaix/snapcast/releases/">Download</a> and <ahref="https://github.com/badaix/snapcast/tree/v0.5.0-beta-1#installation">Install</a> Snapcast (0.5.0 or greater)</li>
</ul>
<h2>Configure Mopidy</h2>
<p>Mopidy can be run with multiple configuration files, each extending the previous file. This is helpful when we’re running multiple instances with varying functionality.</p>
<h3>core.conf</h3>
<p>The core configuration is shared between all instances:</p>
<p>Finally, the Mopidy instance that connects with Snapcast needs special configuration. Run on a different port to avoid conflicts if you have a second Mopidy instance running on your computer. The audio output is sent to a named pipe - Snapcast will read from there. Note that you may have to adjust the audio output attribute depending on your system and audio sources.</p>
<divclass="code"><pre>$ snapclient # or use systemd, add -h <server host> if necessary
</pre></div>
</div>
</div>
<h2>Configure Snapcast</h2>
<p>There are a number of snapcast configuration options, but the one relevant to Home Assistant is the client names. You can set them in the snapserver configuration file, by default located at <code>~/.config/Snapcast/settings.json</code>. Only edit this file while the <code>snapserver</code> is not running. Modify the <code>name</code> JSON value to your liking - this is how the client will be named in Home Assistant.</p>
<h2>Configure Home Assistant</h2>
<p>Use the <ahref="/components/media_player.mpd/">mpd</a> and <ahref="/components/media_player.snapcast/">snapcast</a> components. Optionally, use <ahref="/components/weblink/">weblink</a> to provide easy access to a Mopidy web UI.</p>
<p>I recently learned about the ESP8266, a $5 chip that includes WiFi and is Arduino compatible. This means that all your DIY projects can now be done for a fraction of the price.</p>
<p>For this tutorial, I’ll walk through how to get going with ESP8266, get the temperature and humidity and report it to MQTT where Home Asssistant can pick it up.</p>
<li><ahref="http://www.adafruit.com/product/2635">Adafruit HDC1008 Temperature & Humidity Sensor Breakout Board</a> (<ahref="https://learn.adafruit.com/adafruit-hdc1008-temperature-and-humidity-sensor-breakout/assembly">assembly instructions</a>)</li>
<p><em>Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, soldering iron + wire, Serial USB cable.</em></p>
<h3>Connections</h3>
<p>On your breadboard, make the following connections from your ESP8266 to the HDC1008:</p>
<table>
<thead>
<tr>
<th>ESP8266</th>
<th>HDC1008</th>
</tr>
</thead>
<tbody>
<tr>
<td>GND</td>
<td>GND</td>
</tr>
<tr>
<td>3V</td>
<td>Vin</td>
</tr>
<tr>
<td>14</td>
<td>SCL</td>
</tr>
<tr>
<td>#2</td>
<td>SDA</td>
</tr>
</tbody>
</table>
<p><em>I picked <code>#2</code> and <code>14</code> myself, you can configure them in the sketch.</em></p>
<h3>Preparing your IDE</h3>
<p>Follow <ahref="https://github.com/esp8266/Arduino#installing-with-boards-manager">these instructions</a> on how to install and prepare the Arduino IDE for ESP8266 development.</p>
<p>After you’re done installing, open the Arduino IDE, in the menu click on <code>sketch</code> -><code>include library</code> -><code>manage libraries</code> and install the following libraries:</p>
<ul>
<li>PubSubClient by Nick ‘O Leary</li>
<li>Adafruit HDC1000</li>
</ul>
<h3>Sketch</h3>
<p>If you have followed the previous steps, you’re all set.</p>
<ul>
<li>Open Arduino IDE and create a new sketch (<code>File</code> -><code>New</code>)</li>
<li>Copy and paste the below sketch to the Arduino IDE</li>
<li>Adjust the values line 6 - 14 to match your setup</li>
<li>Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.</li>
<li>To have the ESP8266 accept our new sketch, we have to put it in upload mode. On the ESP8266 device keep the GPIO0 button pressed while pressing the reset button. The red led will glow half bright to indicate it is in upload mode.</li>
<li>Press the upload button in Arduino IDE</li>
<li>Open the serial monitor (<code>Tools</code> -><code>Serial Monitor</code>) to see the output from your device</li>
</ul>
<p>This sketch will connect to your WiFi network and MQTT broker. It will read the temperature and humidity from the sensor every second. It will report it to the MQTT server if the difference is > 1 since last reported value. Reports to the MQTT broker are sent with retain set to <code>True</code>. This means that anyone connecting to the MQTT topic will automatically be notified of the last reported value.</p>
<spanclass="comment">// We start by connecting to a WiFi network</span>
Serial.println();
Serial.print(<spanclass="string"><spanclass="delimiter">"</span><spanclass="content">Connecting to </span><spanclass="delimiter">"</span></span>);
Serial.println(<spanclass="string"><spanclass="delimiter">"</span><spanclass="content"> try again in 5 seconds</span><spanclass="delimiter">"</span></span>);
<spanclass="comment">// Wait 5 seconds before retrying</span>
<p>The last step is to integrate the sensor values into Home Assistant. This can be done by setting up Home Assistant to connect to the MQTT broker and subscribe to the sensor topics.</p>