Make a Chromecast Like Raspberry Pi Device

Introduction: Make a Chromecast Like Raspberry Pi Device

About: I had a long break in my online activity, bad things happened in my life and i'm still trying to recover, sorry folks that i was away for a long time and please be patient with me, i will be better in time, ...
  • I already own a Chromecast v2 (the round one, the USB stick like is v1, and v3 looks like v2 but with a G on i and has a power brick with Ethernet port on it) and i love it, its great fun to use and easy to setup, BUT ...
  • Yeah there is a BUT, when i'm in a place where i don't have internet access and can't use my data plan, I CAN'T USE it to stream local media from my phone or tablet, even when i don't need internet at all, google in the "great wisdom" of them decided to not let it even boot without internet, that's bad if you end up to need it to stream.
  • I have a GL-MT300A mini router, is very very small and can be powered by a phone,external battery or phone charger, i use it to have my Chromecast paired with it and when i need internet i just connect the router to the wired network or if they have only WiFi, it supports to connect to that as client and act as a router at the same time too, now the problem still persists, no internet = no chromecast of local media for you. Here comes in play the Raspberry Pi Zero W (W comes from Wireless, it has a build it card, *yaay* , Bluetooth too but i don't need it :P)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What You Need and What You Get

You need :

  • Raspberry Pi Zero W (my is in acrylic case)
  • HDMI Cable (one with plastic caps is nice to have)
  • Power Source (phone charger in my case)
  • Mini HDMI (male) to full HDMI (female) adapter (comes with PI)
  • SD card of 8Gb or more (i use a 16Gb from Medion)
  • SD card reader (i got my as a gift with a power bank from Hama)
  • Internet connection and PC to download "Raspbian Stretch Lite"
  • Etcher Portable (sd card image writer) -> get Etcher for Windows x86 (32-bit) (Portable) will work on x64 too
  • Download Putty from the link provided or google for it

You will get :

  • A casting target for video files from online services like YouTube and local media (Video, Audio and Images) for Android (Raspicast), windows and any operating system that supports a SSH client app.
  • It does not require internet if the media you are sending to it is stored on your device or somewhere in the local network.
  • You can get a remote control of the target like playback, choose the source, play/pause, volume, quit. (Raspberry SSH or SSH Button, Putty)
  • You get a very fast boot time of the device, for a Raspberry Pi Zero, around 10 seconds.
  • You get a default wallpaper and a way to display random wallpapers, by the press of a button that are not taking any space on your device and remain there until reboot. (it can be changed to be persistent, i gonna show later how)
  • It accepts various types of streams (like HTTP, HTTPS, RTSP) and with the help of a computer, laptop in my case, Sopcast, Acestream as well as local media with external subtitles (like srt, sub)
  • You can even give files over HTTP stored on your computer, with the help of a portable, single file, web server to the player.
  • There is more to discover, to add or to remove, depending on your needs.

Step 2: Geting the Software and the Tools to Put in on Sdcard

Downloads:

Now that you have all this we are gonna start Etcher and insert the card in the card reader and the reader in the computer :P

In Etcher click select image and choose the downloaded zip file (for me at the time i write this is "2018-11-13-raspbian-stretch-lite.zip", then select the card reader from the list displayed and at least click flash, it will ask for permission the User Account Control (UAC), allow it, it needs to physically access the sdcard in exclusive mode to write the image. When done unplug it, it is already ejected by Etcher, and put it back in, there will be a boot partition of ~50Mb and you will be prompted to format a "unformatted partition" DON'T FORMAT IT, windows can't read linux partitions and there is a ext4 partion.

You need to make 2 files in the boot partition.

  • a new file without extension called ssh
  • another one called wpa_supplicant.conf

The content of the "wpa_supplicant.conf" is as follows

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="YOUR_WIFI_SSID"
psk="YOUR_SUPER_SECRET_PASSWORD"
}

Now plug it in your Raspberry Pi and connect HDMI output device (aka TV, Projector, etc) and to power.

The last line before the login it will be displayed a IP. (As a rule of thumb it good to make that address allocation static fro your router, see image of a Linksys router.

Step 3: Updating and Seting All Up

Update your system:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get update
sudo rpi-update

After all is done it will reboot and you will have a putty window that is inactive, close it and start again with same data from previous step.

Now we gonna install omxplayer, no need to compile it from source is part of Raspbian's repositories.

sudo apt-get install omxplayer fonts-freefont-ttf

I wanna have a way to change the HDMI port of my TV, with the help of my Raspberry, like i wanna cast something to it and don't wanna use the remote to change source to the one where is it connected. There is a way to do that from the Pi and with the help of a command from my app of choice. Long story short do the following:

sudo apt-get install cec-utils

Now it follows the framebuffer image viewer called fbi (there is a more advanced version called fim too)

sudo apt-get install fbi

Samba support, windows share for accessing and having a shared folder.

sudo apt-get install samba samba-common-bin
cd /home/pi<p>sudo mkdir -m 1777 /home/pi/share

Edit samba configuration file

sudo nano /etc/samba/smb.conf

insert the following lines at the end (the ones starting with # are comments and can be ignored)

[share]
Comment = Pi shared folder
Path = /home/pi/share
Browseable = yes 
Writeable = Yes
only guest = no
create mask = 0777
directory mask = 0777
Public = yes
Guest ok = yes

Finally, save it; using nano in terminal:

press “Ctrl+X” to close the file, then “Y” to confirm saving, then “Enter” to save the file under the right file name.

Now set the password for the samba the same as for the user pi (default: raspberry)

sudo smbpasswd -a pi

restart the samba service

sudo /etc/init.d/samba restart

Now you have the basics, customization is following.

Step 4: Customisation

make the splash screen be displayed at startup instead of a black screen with white text.

Download WinSCP and log in using same IP, user and password from the SSH/Putty login.

Go /home/pi/ and upload the image provided in the zip file at this step, after unziping. Then exit. The image is 1080p as resolution.

type the following command in putty:

nano ~/.bashrc

scroll to the last line with arrow keys
insert the following lines there (the ones starting with # are comments and can be ignored)

# fifo for commands over ssh to omxplayer
mkfifo /tmp/cmd
# splash screen to replace the black screen and text
sudo /usr/bin/fbi -noverbose -a -T 1 /home/pi/splashscreen.jpg

Finally, save it; using nano in terminal:
press “Ctrl+X” to close the file, then “Y” to confirm saving, then “Enter” to save the file under the right file name.

Now we make user 'pi' auto-login at boot, we need this to display the image, it can be done in a way without auto-login but its harder and can get bad fast by a single typing error.

sudo raspi-config
  1. Select Boot Option
  2. Select Desktop/CLI
  3. Select Console auto-login for CLI

Now we are gonna reboot and we are done with the pi and gonna make the goodies for playing.

sudo reboot

The idea of my project is that most of the stuff is done in the SSH client as it can be customized and changed how it is needed.

Step 5: How to Use It:

I will show the commands and what they do and how to configure the additional software from the streaming device.

I use 3 Android apps and 4 PC apps for my purposes but you implement what you need from what i will show you ore you can have it all and even add more to it.

• YouTube: under Android use Raspcast, under PC use a site like https://www.onlinevideoconverter.com/ or

https://www.youtubnow.com/ or https://youtubemp4.to or even better https://mpgun.com/youtube-to-mp4.html?yid=hTJAnWWK7YQ (hTJAnWWK7YQ is youtube id) you will get a direct link to the media that can be played by the following command:

omxplayer -o hdmi "https://sv89.onlinevideoconverter.com/download?file=f5a0d3c2e4d3g6h7" < /tmp/cmd; echo . > /tmp/cmd

Adding < /tmp/cmd will enable remote control and ; echo . > /tmp/cmd will start playing the stream or else it will wait for a command from the fifo. The -o hdmi parameter of omxplayer will output to HDMI video AND audio goes there, this is important for people that are using the bigger variants of Raspberry Pi Zero W.

Now i show you how to configure Raspicast for android, is free and has no ads.

Install Raspicast from google play store, and configure it like in the images provided.

https://play.google.com/store/apps/details?id=at.huber.raspicast

• Sopcast: start Sopcast on PC choose a channel and when you see a image give the following command

The IP 192.168.1.6 is the IP address OF YOUR PC.LAPTOP NOT THE ONE FROM THE PI, in my example the Pi has 192.168.1.9 and the laptop is 192.168.1.6

omxplayer -o hdmi "192.168.1.6:8902" < /tmp/cmd/; echo . > /tmp/cmd

• AcePlayer: start it, chose a channel and ... there is a one time settings you need to change to have it work,

go to Tools -> Preferences -> Thick All, Stream output -> Default stream output chain, where you input the following

#duplicate{dst="http{mux=ts,dst=:8902/tv.asf}",dst=display}

Click Save. Now the command that will show you the stream, after you started it on AcePlayer ofc:

omxplayer -o hdmi "http://192.168.1.6:8902/tv.asf" < /tmp/cmd; echo . > /tmp/cmd

• Casting from PC a file
You need Putty, Zervit, VLC installed (not portable)

Zervit you get from http://zervit.sourceforge.net/

1st you need to put zervit.exe in the folder with the media files you wanna send to your Raspberry PI, then run it, it will ask some questions. "Port number to listen(80):" type 80 and enter "Accept directory listing [Y/N]:" type y and enter now open a browser and type in you LAN IP address http://192.168.1.6 you gonna see all the media files there, copy the link to one of it with right click -> copy link, then ssh with putty and type

omxplayer -o hdmi "http://192.168.1.6/Movie.mp4"  < /tmp/cmd; echo . > /tmp/cmd <- or whatever your media file is called

for files with subtitles put the subtitles, same name as video file with extension srt in the windows shared folder of the raspberry

\\RASPBERRYPI\share

or

\\192.168.1.9\share

where 192.168.1.9 is the ip of your raspberry.

in your home folder (/home/pi)

winscp with pi:rasspberry and upload a python script called 'omxplayersub.py' with the following content:

import subprocess
import sys
import urllib2
if (len(sys.argv) != 2):
	print "No file name specified"
	quit()
file = sys.argv[1]
subfile = "/home/pi/share/"+ file.split("/")[-1]
subfile = subfile[:-3] + "srt"
subfile = urllib2.unquote(subfile)
subprocess.Popen('omxplayer -o hdmi -r --font "/home/pi/Segoe UI, Medium.ttf" --italic-font "/home/pi/Segoe UI, Italic.ttf" --align center ' +'"'+ file +'"'+ ' --subtitles ' +'"'+ subfile +'"', shell=True)

now upload this with the Segoe UI and FreeSans family fonts to your home folder (/home/pi)
Change the font names for the desired font type and if needed add --font-size size 55 (default: 55) after --align with the desired size.

and use the following command to play:

python omxplayersub.py "http://192.168.1.6/Defiance%20S01E02.mp4" < /tmp/cmd  <- or whatever your media file is called

If you don't see it on the screen playing, there is a compatibility issue, on some h264 files the profile is displayed
by h264-omx as -99, yeah negative 99, and it wont play, in this case and in case of a file with external subtitles you gonna need to use VLC stream choose file/files, select HTTP, let default port, h.264/AAC (TS), keep scale to 1, Trans-coding - Settings thick subtitles, hit stream button and let it run for some seconds.

omxplayer -o hdmi "http://192.168.1.6:8080/"  < /tmp/cmd; echo . > /tmp/cmd <- it gonna start playing

We are doing trans-coding, its gonna be a little quality loss at display and the CPU is gonna work more on PC but it works.

CEC - Controls

Turn TV on

echo on 0 | cec-client -s -d 1

Turn TV off

echo standby 0 | cec-client -s -d 1

Change HDMI Source

HDMI1

echo "tx 4F:82:10:00" | cec-client -s -d 1

HDMI2

echo "tx 4F:82:20:00" | cec-client -s -d 1

HDMI3

echo "tx 4F:82:30:00" | cec-client -s -d 1

HDMI4

echo "tx 4F:82:40:00" | cec-client -s -d 1

Like you see the third byte in the hex string we echo is the number of the HDMI connector to set active.

Step 6: List of Commands to Put in Apps

You have already installed Raspicast at the previous step, now you have to chose between Raspberry SSH (its a paid one) or SSH Button (its free and i like it more, but i have already bought Raspberry SSH before to discover this one).

Raspberry SSH

https://play.google.com/store/apps/details?id=uk.co.knowles_online.raspberryssh

This app, or SSH button, is required if you need to cast stuff from PC and need a remote control
from a android device, it can be used without it thru putty from PC.

Configuration for both is near the same.

Button 1
Btn Text: Reboot

Btn Cmd:

sudo reboot

Button 2

Btn Text: Poweroff

Btn Cmd:

sudo poweroff

Button 3

Btn Text: HDMI1

Btn Cmd:

echo "tx 4F:82:10:00" | cec-client -s -d 1

Button 4

Btn Text: HDMI2

Btn Cmd:

echo "tx 4F:82:20:00" | cec-client -s -d 1

Button 5

Btn Text: HDMI3

Btn Cmd:

echo "tx 4F:82:30:00" | cec-client -s -d 1

Button 6

Btn Text: Kill omxplayer

Btn Cmd:

sudo killall -s 9 omxplayer.bin

Button 7

Btn Text: Sopcast

Btn Cmd:

omxplayer -o hdmi "http://192.168.1.6:8902"  < /tmp/cmd; echo . > /tmp/cmd

Button 8

Btn Text: AcePlayer

Btn Cmd:

omxplayer -o hdmi "http://192.168.1.6:8902/tv.asf" < /tmp/cmd; echo . > /tmp/cmd

Button 9

Btn Text: VLC Stream

Btn Cmd:

omxplayer -o hdmi "http://192.168.1.6:8080/"  < /tmp/cmd; echo . > /tmp/cmd

Button 10

Btn Text: Play/Pause

Btn Cmd:

echo -n p > /tmp/cmd

Button 11

Btn Text: Volume +

Btn Cmd:

echo -n + > /tmp/cmd

Button 12

Btn Text: Volume -

Btn Cmd:

echo -n - > /tmp/cmd

Button 13

Btn Text: Quit Omxplayer

Btn Cmd:

echo -n q > /tmp/cmd

Button 14

Btn Text: Random Wallpaper

Btn Cmd:

wget -O DELME.jpg https://source.unsplash.com/random/1920x1080 &> /dev/null; sudo killall fbi &> /dev/null;sudo fbi -noverbose -a -T 1 DELME.jpg &> /dev/null;sleep 1; rm DELME.jpg

Button 15

Btn Text: Default Wallpaper

Btn Cmd:

sudo killall fbi &> /dev/null;sudo fbi -noverbose -a -T 1 splashscreen.jpg &> /dev/null

Button 16
Btn Text: Slideshow Wallpaper

Btn Cmd:

cd /home/pi/slideshow/; sudo killall fbi &> /dev/null;sudo fbi -noverbose -a -T 1 -t 5 *.jpg &> /dev/null

-t 5 for 5 seconds

Button 17
Btn Text: SUBTITLE HTTP PLAY (Edit)

Btn Cmd:

python omxplayersub.py "http://192.168.1.6/Some%20video%20with%20subtitle.mp4" < /tmp/cmd

When editing change the "http://192.168.1.6/Some%20video%20with%20subtitle.mp4" with url of the file from external source or change it to something like /home/pi/share. Subtitle needs to be in /home/pi/share, ALWAYS.

Freeware alternative to Raspberry SSH
https://play.google.com/store/apps/details?id=com.pd7l.sshbutton&hl=en_US

Works exactly the same as Raspberry SSH, has no ads, less colors and all buttons are on a single row not 2 but its free and works great.

Now at the end some exotic things, stream what you record with your phone in real time, there are some seconds of delay, to your TV.

IP WEBCAMERA PRO (paid)

https://play.google.com/store/apps/details?id=com.pas.webcam.pro

Start streaming and use the following command to cast it to TV

omxplayer -o hdmi "rtsp://192.168.1.8:8080/h264_ulaw.sdp

RTSP Camera Server (free)

https://play.google.com/store/apps/details?id=com.miv.rtspcamera&hl=en&rdid=com.miv.rtspcamera

Does the same thing as the paid one but for free, no ads

omxplayer -o hdmi "rtsp://192.168.1.8:5554/camera"

Active camera - rtsp://(device IP address):5554/camera
Back camera - rtsp://(device IP address):5554/back Front camera - rtsp://(device IP address):5554/front

Camera URL Parameters:

res Resolution - width(number) x height(number)

fps Frame per second - fps(number) mic Enable/Disable microphone - on(string) or off(string)

Example: rtsp://(device IP address):5554/back?res=640x480&fps=10&mic=on

Step 7: Some More Stuff ... WEB Interface

Install it with a single line from git

wget https://raw.githubusercontent.com/brainfoolong/omxwebgui-v2/master/install.sh && clear && sh install.sh

Default user interface in on port 4321, it needs no webserver and is working all under php

Requirements:

sudo apt-get install php-cli
sudo apt-get install php-mbstring
sudo apt-get install php5-cli
sudo apt-get install php5-mbstring

Download/Clone/Unpack the whole script to a folder you like. Set correct chmod for the scripts with

chmod +x *.sh

Create a php webserver listening on port 4321, you can change the port to whatever you want.
Start this with the same user that you need to play the videos. Please do not use apache or other server's to run the php script, it will probably not work.

php -S 0.0.0.0:4321 -t /home/pi/omxwebgui > /dev/null 2>&1 &

Open the webpage with http://192.168.1.9:4321 (where 192.168.1.9 is the raspberry pi IP)

To enable autostart on reboot just add the following line to your crontab. Do this with the
same user that you need to play the videos. No sudo required. Add the following line to crontab with crontab -e to start the simple php webserver on reboot.

crontab -e

now add with

@reboot php -S 0.0.0.0:4321 -t omxwebgui > /dev/null 2>&1 &

Add the files in settings, only local hosted or maped shares and ofc streams can be added.
add /home/pi/share to subtitles folder too and if needed enable fix for 2x playback speed and no sound bug.

Be the First to Share

    Recommendations

    • Tiny Speed Challenge

      Tiny Speed Challenge
    • Clocks Contest

      Clocks Contest
    • PCB Design Challenge

      PCB Design Challenge

    Discussions