📜 ⬆️ ⬇️

Orange Pi Zero WiFi Speaker / Player or Lost Time Story

Good day to the respected habrovchanam!

Prehistory


My story began with the fact that at the request of one friend it was necessary to make a small programmable device with audio output and GPIO. I have long wanted to work with any single-server * Pi and therefore immediately decided to do something similar (result + experience). A friend almost immediately abandoned the proposed project, but I ended up with an OrangePi Zero board purchased. For some time she was lying idle until they gave me an old Canon MX320 multifunction printer without network support. I really didn’t want to have an extra wire from the laptop to the printer, and as a result, the orange was extracted, configured and works since then as a CUPS server via USB (the result, by the way, is good, but that's another story).

Outset


Once I got tired of always connecting my phone to the speakers via minijack. They cost well, comfortable, and carry them reluctance. A telephone that always hangs on a lineman is no longer a mobile phone, but something similar to old wired devices. I also have a laptop so that it would be inconvenient to connect a cable to the speakers to it. And the machine itself is old (10 years already), an extra audio player - an extra load.
')
You can, of course, buy bluetooth speakers. Or bluetooth adapter. But this means a short range and playing music only on the device with which the phone is connected via bluetooth. Need something more serious. “It would be great to hang such a server on an orange, which could receive audio stream from a smartphone via WiFi,” I thought, “because it is constantly connected via ethernet to the router, lightly loaded (as Ubuntu Server 16.04 costs on it), you can conveniently place electricity consumes little. ”It is said - done.

Part 0, hardware


For a start, it was necessary to connect the jack to the jack with an orange. I took a bare fee, so everything had to be soldered by myself. There is nothing complicated, and I will say straight away: there is no special point in doing this - it is easier to buy a ready-made expansion card. I advise this method, because on my homemade board the interference affects the sound of the speakers (in particular, when sending a large print job to the printer, or when installing large packages). Most likely, there are two reasons - my bad work + food is brought right to the board, and not through microUSB.

Because the photo board, soldering and manuals will not be, if needed - easily located on the network.
If you choose to buy a board, you will save time and effort, and there will immediately be a mini-jack jack, two more USB connectors, an infrared sensor and a microphone. I bought the second orange already in a set with a case and an expansion card, I regretted that I didn’t do that from the very beginning.

Part 1, software


We connect via SSH (if the standard login-password is not changed, then this is the root orangepi).

Do not forget to do

apt update 

(anyway you will need it later)

By default, the sound is turned off, so to turn it on, do the following:

Run alsamixer.

 alsamixer 

image

Turn on the audio line out (the desired switch is in the center), to do this, move the arrows five positions to the right and press m

image

Exit by pressing Esc.

Save the current state

 alsactl store 

The current state will still be reset after a reboot, because we add lines

 alsactl restore 

in rc.local so that the parameters are restored automatically:

 cd /etc 

 nano rc.local 

 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # ** Overclock to 1.728 GHz #echo 1728000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq echo 0 > /proc/sys/kernel/hung_task_timeout_secs dmesg -n 1 alsactl restore exit 0 

I advise you to check after this - perform a reboot first

 reboot 

Install mplayer

 apt install mplayer 

and check the performance

 mplayer http://nashe1.hostingradio.ru/nashe-128.mp3 

If the radio has played, then everything is in order.

You can, however, just run after a reboot.

 speaker-test 

but I like the rest of the tuning for radio.

Part 2. mpd


Once the audio output is configured, you can begin to install something useful. And in my case, it was just mpd and mpc (the client for mpc works in the terminal). What is mpd? This is a network daemon player that works fine without a graphical interface and therefore does not load the system. Settings options - a lot, for every taste. Easy playing music from the network and from the local collection. There is even an article about the interaction of mpd with Yandex.Music and with Google Music.

It can be controlled from any OS - clients are under Windows, Linux, Android, ios, mac os, Symbian, therefore it is very convenient.

Install

 apt install mpd mpc 

All settings are in the file /etc/mpd.conf

 nano /etc/mpd.conf 

Be sure to change the following directive

 bind_to_address "localhost" 

on

 bind_to_address "any" 

because it provides the ability to control mpd over the network.

And go to the section Audio Output

Customize the output through alsa

 audio_output { type "alsa" name "My ALSA Device" normalize "yes" device "hw:0,0" # optional mixer_type "software" # optional # mixer_device "default" # optional # mixer_control "PCM" # optional # mixer_index "0" # optional } 

If you do not add the directive normalize "yes", then the sound will be very quiet. Unfortunately, I can’t write more about this directive, I didn’t have time to look for information.

Directive

 device "hw:0,0" # optional 

I added quite recently, without it, mpd works fine (if it works alone), and it was needed only because I put additional components, which are described below.

Restart mpd

 service mpd restart 

Everything, we can connect.

We go to any client, enter the IP of our server, connect. First, of course, in playlists, in files and in streams is empty. For mpd to see your local files, you need to either change the settings in /etc/mpd.conf, specifying in the section

Files and directories

path to your music folder

music_directory "/ home / orangepi / Music

 # Files and directories ####################################################### # # This setting controls the top directory which MPD will search to discover the # available audio files and add them to the daemon's online database. This # setting defaults to the XDG directory, otherwise the music directory will be # be disabled and audio files will only be accepted over ipc socket (using # file:// protocol) or streaming files over an accepted protocol. # music_directory "/var/lib/mpd/music" music_directory "/home/orangepi/Music" 

But I still don’t have local files, but I’m using a laptop hard drive mounted by Samba directly into the / var / lib / mpd / music folder. Plans to buy an external hard and connect it to an orange, but for now finances do not allow.

In any case, after the path to the folder with music is added (or the folder itself is mounted), you need to update the collection. This can be done from any client, the simplest

 mpc update 

We are waiting for some time (more collection - more time is needed to scan it).

Everyone, we can enjoy the music!

Me mpd wakes up in the morning instead of an alarm clock. It is very good and pleasant to wake up to different music / stations at different times and days.

Part 3. Actually, WiFi column


So, mpd is, of course, good, but it does not perform the desired task - there is no audio transmission from the phone when playing music on it. A few sleepless nights - and an article was found about turning * Pi into a receiver of an audio stream.

In short:

what I need is very similar to Google's Chromecast, because it supports Wi-Fi audio transmission and this program is available in many applications.

But I still do not have Chromecast, but an orange. If I understand correctly, so far the Chromecast protocol is closed. But this is a modified DLNA protocol, which serves to transfer media content over the network.
Aha

Immediately located in the repositories package minidlna.

We put

 apt install minidlna 

So, now there is an orange dlna server. We put the dlna-player to your phone.
Bummer.

The server is, it seems, there is access to the collection and playback of content, but on the phone. And broadcast audio stream is not yet possible.

We understand the protocol: it turns out there is a DLNA server, a DLNA controller (for playback control) and (!) DLNA renderer.

The last is what we need.

In the repositories it is not, because we put so

  wget -O - http://www.chiark.greenend.org.uk/~christi/debian/christi@coraline.org.gpg.key \ | sudo apt-key add - echo deb http://www.chiark.greenend.org.uk/~christi/debian/ wheezy main > /etc/apt/sources.list.d/upnprender.list apt-get update apt-get install libupnp-dev libgstreamer1.0-dev \ gstreamer1.0-plugins-base gstreamer1.0-plugins-good \ gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly \ gstreamer1.0-alsa apt install git cd git clone https://github.com/hzeller/gmrender-resurrect.git cd gmrender-resurrect apt-get install autoconf ./autogen.sh ./configure make install cp scripts/init.d/gmediarenderer /etc/init.d update-rc.d update-rc.d gmediarenderer defaults 

The author of gmediarenderer is launched at system startup via init, this method does not work for me yet, because a line has been added to /etc/rc.local

 /usr/local/bin/gmediarender -f Garden -d -u bd1dcf3e746aa69812943cb1d00f7ebc --gstout-audiosink=alsasink --gstout-audiodevice=sysdefault --gstout-i$ 

 #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. # ** Overclock to 1.728 GHz #echo 1728000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq echo 0 > /proc/sys/kernel/hung_task_timeout_secs dmesg -n 1 bash /root/sh/script0.sh /usr/local/bin/gmediarender -f Garden -d -u bd1dcf3e746aa69812943cb1d00f7ebc --gstout-audiosink=alsasink --gstout-audiodevice=sysdefault --gstout-i$ exit 0 

In order to now be able to listen to the audio stream from a smartphone on the OrangePi, set BubbleUPnP, or HiFi Cast, a Google Play search gives a lot of results for the DLNA query. Not everything can be rendered. Neither VK nor Yandex.Music / Radio is rendered. But it's still more than nothing. Currently, any local music from the phone + SoundCloud is played.

Unfortunately, after installing gmediarender mpd stopped working normally - there is playback, but there is no sound. The search did not give anything, the solution was found by chance - you need to uncomment the line in the section Audio Output in the block describing the output via alsa in the /etc/mpd.conf file:

 device "hw:0,0" # optional 

After uncommenting this line, the sound returned, mpd started working normally.

Results


In general, the task can be considered completed.

At the moment, you can listen to any music from the tablet / phone via WiFi on the speakers, you can turn on the network player, play music from the orange on the phone / tablet.

The plans are to finish the dlna-renderer, configure the parser for mpd (so that I can search for music on the network myself), add a hard disk (the speed on Samba is still low).

It turned out quite conveniently, functionally and simply, and cheap (for all together 1900 rubles, the OrangePi itself and the microSD card). If you take the kit (OrangePi, case, board and microSD), you can meet the same amount if you try.

That's all, I will be happy reviews and comments.

Source: https://habr.com/ru/post/341650/


All Articles