Tuesday, May 3, 2016

Minecraft on Raspberry Pi Zero using OTG USB Ethernet connection with RealVNC server and client


In my previous post I connected to a Raspberry Pi 3 using RealVNC to connected over Ethernet.
To achieve this you would need:
Laptop
Raspberry Pi
PSU for Pi
Ethernet cable

What if you could replace the PSU and Ethernet with just a microUSB cable and replace the Pi3 by a PiZero.
Well, this is absolutely possible and quite simple due to the hard work of a number of people including Andrew Mulholland who created a guide on setting up Ethernet over USB using OTG that I hope to make even more simple here. Then it's a matter of installing RealVNC to get the Raspbian desktop on your laptop.

OTG stands for On-The-Go or it's full name USB On-The-Go
Put simply OTG allows a USB device to act like a host and have peripherals  such as keyboard and external storage attached to the device or act like a slave so it can be a network device or a mass storage device.
This is most used on phones an tablets where it's great to be able to plug in a USB stick and access the files (host) or plug the phone into your computer and download the files (slave)

For OTG is enabled by the ID-pin which is available on the PiZero as the USB port is directly connected to SOC and it uses a microUSB connector which has the extra pin for the ID.

Back in December Andrew posted a guide on setting up OTG.
It includes a lot extra information that is worth reading and understanding, but here I want to just do the facts and steps needed to enable Ethernet over USB.

I'm assuming you have already installed Raspbian on your PiZero 

Next you need your PiZero to be connected to the network to be able to download and install some files. For this I used an Wii USB Ethernet adapter.  This adapter is supported by default. They're about £10 on eBay.  Just search Wii USB Ethernet.

With the PiZero connected to the network I found the Pis IP address using Fing on my phone.
With the IP address known I used putty to ssh in. (Yes, this is the correct link for downloading)

Once logged in do the usual housekeeping.
sudo raspi-config to expand the filesystem and set GPU memory to 128MB
sudo apt-get update
sudo apt-get upgrade

Then it's a matter of running the following commands one after another to get Ethernet over USB set up. Andrew's guide explains what they do and also the other OTG options available.


sudo BRANCH=next rpi-update

echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt

echo "dwc2" | sudo tee -a /etc/modules

echo "g_ether" | sudo tee -a /etc/modules

echo -e "interface usb0 \nstatic ip_address=169.254.64.64" | sudo tee -a /etc/dhcpcd.conf

Now Ethernet over USB should be set up.

Next RealVNC server needs to be installed.  The guide for installing the latest Alpha that supports Minecraft can be  found here. https://github.com/RealVNC/raspi-preview

With the relevant commands being:
curl -OL https://github.com/RealVNC/raspi-preview/releases/download/5.3.1.18206/VNC-Server-5.3.1-raspi-alpha1.deb

sudo dpkg -i VNC-Server-5.3.1-raspi-alpha1.deb

sudo systemctl enable vncserver-x11-serviced.service

These install RealVNC server and enable it as a service that starts at boot up.

Shutdown your PiZero using
sudo shutdown 0

Unplug the cabled.
Plug single microUSB cable into the USB port on the PiZero and the other end into your laptop USB port. The PiZero uses so little power it can safely be powered from the laptop USB with no issues.
Mine laptop is running Windows 10 and the device is automatically detected and the drivers installed.  I didn't have to do anything.
This creates a new Ethernet device.

Download and install RealVNC Viewer 

Run RealVNC Viewer on your laptop and enter the fixed IP address of 169.254.64.64 used when setting up the PiZero.
It will ask for your login. On mine it defaults to the laptop username. so, just change to pi/raspberry assuming you haven't changed it.

RealVNC should now login to the PiZero.
It's that simple.

RealVNC recommend some optimisation that I definitely found to improve performance.

On the VNC Viewer toolbar (top of the Window), click the Options button.
Click Advanced..., then the Expert tab.
Set PreferredEncoding to JPEG, AutoSelect to False, and ColorLevel to Full

These changes really make RealVNC smoother.

If using Minecraft you have to make one more selection.
Press [F8] to go back to the menu and select Relative Pointer Motion
Then start Minecraft as normal.
All should work perfectly.

You will notice that the RealVNC window is quite small this is because there is no resolution details set by a display so the Raspberry Pi defaults to 640x480 as the safest mode.

To increase the resolution you have to make some minor changes to config.txt
RealVNC give full details on this on the following support page

But to put it simply to set the resolution to 800x600 do the following

Open a terminal window
cd /boot
sudo cp config.txt config.txt.backup
sudo nano config.txt

This makes a backup of config.txt and then opens it with nano

Remove the # in front of hdmi_force_hotplug and set it to 1
hdmi_force_hotplug=1


Enter the following line into config.txt

hdmi_ignore_edid=0xa5000080


No idea what it does and even RealVNC describe it as better safe than sorry.  I've run without it and it worked.

Remove the # in front of hdmi_group and set it to 2.
hdmi_group=2

Remove the # in front of hdmi_mode and set it to 9 which is the value for 800x600.
You can look up all the values and adjust according to your needs on the elinux.org page

Press [CTRL]-[x] to exit.
Select y for saving changes and then press [Enter]

Now your config.txt is up to date.

Restart the Raspberry Pi and it will restart with a resolution of 800x600 rather than 640x480 making everything a little easier.
If you want a higher resolution then hdmi_mode=16 is 1024x768 and hdmi_mode=39 is 1360x768

Finally, here is a (low quality recorded from my phone) video of it all running. You can see the speed is good and even programming in Python works well.