navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

raspi webradio player

Last update: Sun Feb 2 23:52:40 2020
The outside, with anonymized helper elf for scaleA Raspberry pi zero W used as a WebRadio player! Because I can't work without music and turning on the PC to open Youtube is taking too much of my productivity away. So webradio looks like the way to go!

Presentation

Principle

Seems you just need to MPD/MPC to play the web streams, a way to output the sound (I have somewhere an eBay soundcard or default sound output?), a display of what is the current track, and some way to control it (change station at least).

Points of interrest

Implementation

Bill of materials

  • Raspberry pi zero W
  • I2C OLED screen
  • Class D 2x15W amp module
  • USB soundcard (the cheapest on ebay)
  • Step-up module
  • Schematics

    Make sound from a raspi zero without soundcard

    Explained here and here, you need to redirect 2 pins on their alternate pin, and make a small low/high pass filter with resistors and caps.

    User account "radio" preparation

    Because you should never run any non-interactive program with sudoer-rights account (root, pi, ...) so make a separate account that has the minimum necessary rights.
    
    #make a user named "radio"
    sudo adduser radio
    #add user to the audio group if you want sound :)
    sudo usermod -a -G audio radio
    #add user to the netdev group if you want to access the internet
    sudo usermod -a -G netdev radio
    #add user to the gpio gorup to acces GPIO for the buttons
    sudo usermod -a -G gpio radio
    #add user to the i2c group to acces I2C bus for the screen
    sudo usermod -a -G i2c radio
    #allow user radio to shutdown the machine
    # 1- sudo visudo
    # 2- add line
    radio ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown
    
    #add to your .bashrc the below 2 lines to start pulseaudio as a user daemon (apparently it shouldn't be a system daemon)
    #START PULSEAUDIO as a per user daemon
    pulseaudio -D
    

    Source code : with VLC

    VLS is more power hungry but this Raspi will only play radio so... even if it uses 40% CPU, it won't make a difference.
    
    #install VLC and pulseaudio
    sudo apt-get install vlc pulseaudio
    
    Now it should work, so listen to music with:
  • Normal case: vlc your-URL-here -I dummy
  • If you need details when things go south : vlc your-URL-here -I dummy --verbose=2
  • How to change the volume:
  • SET volume to 85%: amixer -D pulse sset Master 85%
  • INCREASE volume by 7%: amixer -D pulse sset Master 7%+
  • Source code : with MPC

    MPC/MPD does work, though I used VLC because it's simpler to configure. However as far as I tested the below setup works, you can take it from there and finish. I ended up uninstalling it because mpd was consuming the CPU though I don't use it.
    
    #install MPC/MPD to play the audio streams
    sudo apt-get install mpc mpd
    
    
    edit the etc mpd.conf change the folder for music and add correct HW -> hw:1,0
    list hw avec aplay -l
    put files in fodler
    mpc add / (add all)
    mpc play
    mpc volume 100
    

    Display screen

    Using a small 2.13 inch I2C SSD1306 (address 0x3c) with Adafruit library (for python3):
    sudo apt-get install python-pip python3-pip
    sudo python -m pip install --upgrade pip setuptools wheel
    #doesn't work - unmaintained since 2014
    #sudo pip install Adafruit-SSD1306
    
    sudo pip3 install adafruit-circuitpython-ssd1306
    sudo python3 -m pip install pillow
    sudo pip3 install futures
    
    #install necessary packages
    sudo apt-get install python-smbus python3-smbus
    #to check that you can see the 0x3c on the I2C bus #1
    i2cdetect 1
    
    Now the Ladyada code should work, however change the samples for my component:
  • address is 0x3c (not 0x3d)
  • the height is 32 not 64 pixels
  • Power consideration

    I'd like to put the raspi zero W in sleep mode when unused to minimize power consumption AND maximize the benefit on start time. There's no real power management on the Raspi HW, so basically you have to cut the power on everything that is unused to save a few watts.
    Here's a few links I found:
  • Stop HDMI /usr/bin/tvservice -o (-p to re-enable) from here
  • Autostart

    PulseAudio is giving me a hard time: all workds, except that, meaning no sound. Apparently it doesn't like to run as user-mode and it should run as system-wide as it's the exceptionnal case where it make sense (RTFM).
    #in rc.local, start as SYSTEM (don't forget to launch on background)
    pulseaudio --system &
    #start the app
    su  radio -c /usr/local/src/git/alanarduinotools/Python/WebRadioPlayer/start.sh &
    
    ...
    #users that must access the SYSTEM pulse must be in "pulse-access" group (even root)
    sudo usermod -a -G pulse-access radio
    sudo usermod -a -G pulse-access pi
    sudo usermod -a -G pulse-access root
    
    It does use more CPU, peaks at 100% at opening of a radio and gets within seconds to its cruising speed ok 30-40% (I talk of the TOTAL CPU consumption of the RPi, all included)

    Pictures

    the insides
    The outside, with anonymized helper elf for scale

    Links

    Radios!

    Go to the radio site or TuneIn, F12, Network tab and look for a .mp3 or .m3u file. No magical solution apparently.
  • Radio classique https://radioclassique.ice.infomaniak.ch/radioclassique-high.mp3
  • Classic Rock radio http://144.217.158.59:5338/live
  • Alpes 1 http://alpes1grenoble.ice.infomaniak.ch/alpes1grenoble-high.mp3
  • Couleur 3 http://stream.srg-ssr.ch/m/couleur3/mp3_128
  • Rire et chanson http://185.52.127.163/fr/30401/mp3_128.mp3
  • Inspiration

  • Recent and pretty good https://www.lesbonscomptes.com/pages/raspmpd.html
  • Old things http://jan-holst.dk/pi-radio/pi-radio.html
  • Very old things http://doc-diy.blogspot.com/2014/04/tubenetradio-raspberry-pi-powered-tube.html
  • All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
    electrogeek.cc ~ Formerly known as Kalshagar.wikispaces.com (AlanFromJapan [2009 - 2020])