Moonlight Pi

Moonlight on a Raspberry Pi

Contents
1 Howto: 50$ Steambox with Moonlight on a Raspberry Pi
1.1 Ingredients
1.2 Preparations on the PC
1.3 Preparations on the Raspberry PI
1.4 Connect a game controller (optional)
1.5 Automatic Bootup
1.6 Power on the PC automatically
1.7 Power off the Raspberry Pi gracefully and the PC automatically

Howto: 50$ Steambox with Moonlight on a Raspberry Pi

I connected my TV in the ground floor to my computer in the second floor by a Raspberry Pi for realtime game streaming. This cost me about 50$ and now I can play high quality computer games in my living room – without sitting in an office (again …), without the computer’s noise and on a big TV screen.

I configured everything to start up automatically. That is when I power on the Raspberry Pi, my PC is electrically powered up too, Steam (providing a dashboard for games) is started on the PC and Moonlight (formerly known as Limelight) – which provides the game streaming facilities – is started on the Raspberry Pi. So all I have to do is to power on the Pi, wait a few moments and then the Steam dashboard appears on my Tv and I can select and play my favored PC game with my XBox game controller. It behaves like I had built a high performance steambox for only 50$ 😉

Here’s how you can repeat this on your own.

Ingredients

A Windows PC with an NVidia Gfx-Card capable of running Shield. (Install the ‘GForce Experience’ software from NVidia to find out wether you card supports Shield. I’m very happy with my very quiet and inexpensive ‘ASUS GeForce® GTX 750 TI’. The PC should have contemporary performance, I used a quad core i5 with >3 GHz clock, but less would do also because the video compression takes place on the Gfx-Card.)
USB-Mouse, USB-Keyboard (If you intend to use a game controller lateron the USB-Mouse/Keyboard of your PC are sufficient and will only have to be connected to the device during the installation.)
Any recent PC-Game
A Raspberry Pi. I used a starter kit consisting of the device, case, power adapter, heatsink, a NOOBS-preinstalled SD-Card, HDMI-Cable (and a USB-WLan adapter which I don’t need). http://www.amazon.de/Raspberry-Complete-Starter-Wesentlich-Zubehör/dp/B00P9FGSHU/ref=sr_1_3
An Ethernet connection between the PC and the Pi. (As far as I know wlan is not recommended, but might be working either).
Optional: A game controller, I used an XBox USB for PC controller. http://www.amazon.de/PC-Xbox-Controller-Windows-schwarz/dp/B004JU0JSK/ref=sr_1_1
Optional: A power on/off controller (On http://www.msldigital.com are several of this devices. LED-Pushbuttons, Rocker-Switch, optionally with additional IR remote control receivers …)

If you didn’t buy a NOOBS-preinstalled SD-Card you will need a card writer for micro SD (I used a very cheap adapter between micro SD and the bigger SD slot of my laptop).
Preparations on the PC

Install Steam, make the Game startable from Steam (by buying it in Stream or by referencing to an existing game in the steam settings)
Install GeForce Experienve from Nvidia (or update it if you allready have it), there in Preferences / GameStream: Select ‘On my network’). Leave the PC on and this window opened.
Find out the hostname (or IP) of the PC. E.g. by pressing the Windows-Key and the Pause-Key at the same time (or by ipconfig).

Preparations on the Raspberry PI

Connect the PI device to an internet router (by Ethernet or a WiFi-USB-Stick – Ethernet is recommended for Moonlight anyway), HDMI, Mouse, Keyboard, Power
If you bought a NOOBS preinstalled SD-Card:
In NOOBS install Raspbian (I used from Ethernet, Ethernet-Jack-Icon)
Continue below at ‘In the setup menu configure …’
If you have no NOOBS preinstalled, but an empty SD-Card, download the operating system on your PC and copy the image to the SD-Card
On http://www.raspberrypi.org/downloads/
Download Raspbian (‘download torrent’ was for me way faster than ‘download zip’: 8 MByte/s instead of 400 KByte/s)
Unzip the downloaded file
Flash to the Pi’s SD-Card as written in http://www.raspberrypi.org/documentation/installation/installing-images/README.md
Execute ‘sync’, remove the SD-Card and insert it into the Pi and in the NOOBS installer choose (only) raspbian and press the ‘install’ button (or the key ‘i’ on the keyboard).

The system reboots automatically into the raspbian-config menu (which can lateron be entered again, e.g. for adjusting the overclocking, by the command ‘rapi-config’). In this menu configure the following:
At 1) Expand the filesystem (possibly obsolete when NOOBS was used to setup the SD-Card)
At 2) Choose a password
At 3) Note: Leave the default to console login !
At 4) Optional: Set localization and timezone if wished
At 8) A3 GPU memoty to 256 MB
Optional: If you’re familiar with SSH is you can enable it in 8) and A4 and later connect by the user ‘pi’. SSH lets you connect the Pi console from another PC which eliminates the need to directly connect a keyboard to it. For simplicity, however, I will continue without mentioning SSH and assume that you have a keyboard directly connected to the Pi.

Finish the settings dialog, which automatically triggers a reboot of the Pi, and Login with user ‘pi’ and the password you set before.

sudo apt-get update
sudo apt-get dist-upgrade

sudo sh -c “echo ‘deb http://archive.itimmer.nl/raspbian/moonlight wheezy main’ >> /etc/apt/sources.list”
(This is one single, long command line)

sudo apt-get update
sudo apt-get install moonlight-embedded
(Press ‘y’ in case of any questions)

Then start the pairing process on Moonlight

mkdir ~/Moonlight
cd ~/Moonlight
moonlight pair <PC’s Hostname or IP>

You will have to enter the code given on the console to the PC. It is not possible to use the windows remote desktop, this gave for me and others the error 530, you’ll have to walk between the rooms … (or you might try VNC instead of the Windows Remote Desktop)

On the Pi execute for testing the connection:

moonlight stream <PC’s Hostname or IP>

After a short while the remote content should be displayed (if the display remains black this might be the raspbian screen saver, press any key)

Connect a game controller (optional)

The following section shows how to connect a controller – in this particular example an XBox 360 USB controller for PCs. An alternative approach using a wireless PS3 dual shock controller and a bluetooth-usb-dongle can be found here: PS3 controller for Moonlight. You can, however, also skip this section and play your games with USB connected mouse and keyboard instead (then don’t use ‘-mapping xbox.map’ when starting ‘moonlight stream’).

The XBox 360 controller will be recognized by default. I made, however, the experience that the following procedure gives better results (and also eliminates the annoying blinking led of the default driver). Instructions taken from https://github.com/irtimmer/moonlight-embedded/wiki/Controllers-set-up

sudo modprobe -r xpad

sudo sh -c “echo ‘blacklist xpad’ >> /etc/modprobe.d/gamepad.conf”
(This is one single, long command line)

sudo nano /etc/rc.local

Enter the following line BEFORE the line ‘exit 0’
xboxdrv –trigger-as-button –wid 0 –led 2 –deadzone 4000 –dpad-as-button –silent &
Save and exit (Press Ctrl+X and Y)

Note: The setting –trigger-as-button is not apropriate for games that need a linear input from this button, like racing games. Maybe just removing the option ‘–trigger-as-button’ is sufficient for this type of games, maybe also a different mapfile is needed (see below), I haven’t tried this yet.

A map file is needed, that tells Moonlight which button-code does which action. We store this mapping file (.map) in the Moonlight folder, we created above.

cd ~/Moonlight

You can simply download the one I provided online

wget https://raw.githubusercontent.com/RoelofBerg/moonlightpisteambox/master/xbox.map
(Enter this as one single command line)

(I created the file with the command ‘moonlight map xbox.map’.)

To enable/use the controller, the .map file we just created has to be added to the command ‘moonlight stream’, which was used above to establish our very first connection successfully – you remember that one ? Here’s how the command to start streaming will look like now:

moonlight stream -mapping xbox.map <PC’s Hostname or IP>

Automatic Bootup

Now after every boot you can login and ‘cd Moonlight’ and repeat the command above to start Moonlight (the PS3-controller has to be paired before by pressing the PS-button. If you skipped the controller installation above, use the command without the ‘-mapping xbox.map’ option).

moonlight stream -mapping xbox.map <PC’s Hostname or IP>

To do this automatically at every boot, do the following:

Autostart (taken from http://www.opentechguides.com/how-to/article/raspberry-pi/5/raspberry-pi-auto-start.html)

Create a script that starts Moonlight

cd /home/pi/Moonlight
wget https://raw.githubusercontent.com/RoelofBerg/moonlightpisteambox/master/autostart.sh
(Enter wget … start.sh as one single command line)
nano autostart.sh

Modify the script:

#!/bin/sh
cd /home/pi/Moonlight
moonlight -mapping xbox.map -60fps stream <PC’s Hostname or IP>

Give the script execution permissions

chmod +x autostart.sh

You may want to test the script. Enter

./autostart.sh

The PC’s steam console should become visible on the TV. Press ctrl+c to abort/finish the test.

sudo nano /etc/inittab

Outcomment the line that starts getty and add a startup of /bin/login. On my system the position of this lines was at the very end of the file. If you have something different from T0:23, use what you have in the /sbin/getty line also in the /bin/login line.

#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
T0:23:respawn:/bin/login -f pi tty1 /dev/tty1 2>&1

sudo nano /etc/profile

At the bottom of the file add the following line to start the autostart script created above (with a trailing dot and space character)

. /home/pi/Moonlight/autostart.sh

Test by executing

sudo reboot

After a reboot the steam display should appear automatically on your TV and you can use the xbox controller to control it.

Power on the PC automatically

The above procedure needs you to power on the PC and also to login to the PC before powering on the Raspberry PI. This, however, can also be automated by the wakeonlan feature of recent PCs. (This section is optional and can be skipped – but then, off course, you’ll have to power on your PC manually and log on to Windows.)

For remotely powering up your PC you will have to enable wakeonlan in the PC’s UEFI/BIOS configuration and in Windows. When powering on your PC interrupt the boot sequence, enter UEFI and enable wakeonlan – for example I had to enable booting for my whole PCIe-bus. Save, exit and continue in Windows (remember, as mentioned above, that once you used a remote-desktop connection you’ll have to reboot before being able to connect Moonlight again.)

ToDo: How to enable WakeOnLan in Windows (there was something I did)

Enable automatic login at boot (note: this reduces the system’s security, see http://technet.microsoft.com/en-us/magazine/ee872306.aspx)

1. Click Start, type netplwiz, and then press Enter. 2. In the User Accounts dialog box, click the account you want to automatically log on to.If it is available, clear the Users Must Enter A User Name And Password To Use This Computer check box. 3. Click OK. 4. In the Automatically Log On dialog box, enter the user’s password twice and click OK.

Open a Windows terminal (press Windows-Key and R, type ‘cmd’ and enter) and enter the command ‘ipconfig’. Read out the PC’s MAC address. It should be something like 12:34:56:67:90:12. Power off the Windows PC.

Back on the Raspberry PI console install the wakeonlan tool.

sudo apt-get install wakeonlan

Test the wakeonlan feature by calling it using the MAC address obtained above (replace 12:34 … by your MAC address):

wakeonlan 12:34:56:67:90:12

The PC should power on automatically. If this is the case you can add the wakeonlan command to the boot-sequence of your raspberry PI, so that everytime you power on the PI also the PC will be powered on remotely. (Exit Moonlight by pressing ctrl+c)

cd ~/Moonlight

We will now extend the script autostart.sh we used above allready. You may want to download my example again to save you some typing by calling wget again (careful, your changes in the file like the hostname/ip of your PC will be lost).

wget https://raw.githubusercontent.com/RoelofBerg/moonlightpisteambox/master/autostart.sh
(Enter this as one single command line)

Then

nano autostart.sh

and enter your PC’s Hostname/IP and Ethernet MAC adress, safe and exit.

Power off the Raspberry Pi gracefully and the PC automatically

A standard debian based Linux system shouldn’t be powered off without issuing the ‘poweroff’ command on the console. Otherwise the file system might be corrupted. On http://www.msldigital.com (my distributor was www.insaneware.de) I found poweroff-switches that come along with shutdown-scripts for the Pi. Theese provide a convenient power-on-off by just pressing a button. I chose a model that additionally adds IR remote control capabilities to my Pi, the ‘RemotePi Board Plus 2015’. I had to drill two additional holes into my Pi casing, which was quite easy with a standard drilling machine.

I don’t know if this was necessary but I upgraded to Firmware of the RemotePi board to V1.5 as shown in http://www.msldigital.com/pages/upgrade-firmware (and I restored /etc/inittab afterwards – which is not mentioned in the upgrade guide.)

Then I installed the poweroff-scripts by following the guide on http://www.msldigital.com/pages/support-for-remotepi-board-plus-2015 – there the secion with the caption ‘XBian’.

Work in progress: I have to find a straightforward way to poweroff the PC from within the RemotePi’s shutdown script.