Friday 31 July 2015

Raspberry Pi 2 B Entertainment Centre

Well I've had my RPi2 since it was launched and its been in its box waiting for FreeBSD to release an RPi2 compatible image or Microsoft to release a beta of Windows 10 IoT for it. That was until I decided to turn it into retro gaming box! I've detailed most of what I've done to my Pi mostly for my reference, if this helps other people out, then that's great!

I've also tried to include links to other peoples work that has helped me out, if I've not linked to your content, do let me know and I'll happily edit the post to include your work.

All commands or text should be typed as you see them, this is Linux and its case sensitive.

Black background with green text are commands.
Black background with white text is plain text to copy & paste or type.

What you'll need at a minimum:

  • RPi2 £26 ex-vat
  • 2A power supply with 1m lead £5, using an underpowered supply or too long a power cable will result in a rainbow box in the top right corner and hinder performance, possibly causing damage to the components. So try to use an original Pi power supply.
  • RPi2/B+ case, preferably with fan hole or make your own £5
  • Heatsink, preferably with self adhesive thermal tape £2
  • microSD card, I recommend a class 10, 16Gb or higher. ~£6+ depending on capacity
  • HDMI cable, with VGA/DVI or Display Port adapter if required ~£5
  • Retropi image
  • Controllers, decide what games you'll be playing and purchase the correct USB controllers - or use a PS3 controller with USB cable like I do at the moment (I'll switch over to wireless as soon as I get a USB bluetooth adapter, but wired doesn't bother me at the moment)
  • ethernet cable or wireless USB adapter
  • Know how to find the IP of your Pi.



I turned to Retropie which is a preconfigured version of Raspian which boots straight into emulationstation. You can purchase roms legally from the likes of Sega on Amazon or even rip your own roms with a small device for not a lot of money with the Kazzo or maybe you still own the games and want to source them elsewhere and not go through the hassle of ripping them yourself... well that's your choice. But wouldn't ripping them yourself be so much more FUN?

Starting out with the basics

So anyway, download Retropie and use Win32diskimager and a SD card adapter to write the Retropie image to your microSD card, I've got a 32Gb Class 10 Samsung which works great.
Power up your pi and you'll get emulationstation after a moment which will prompt you to configure your controller, which might even be a keyboard. Wooooooo!

Now you've got a bunch of shareware games to play ... not very exciting. So pop a USB stick into the pi, give it a minute and it will create a directory structure on the stick, now go plug the stick into your computer, navigate to the correct rom directory and place your files there, plug the USB stick back into your pi and give it a few minutes while Retropie copies the files to the correct location, the green LED should stop flashing when its finished copying the files. You could also copy them over the network to the shared folder on the pi.

Note: The beta of Retropie 3 that I'm using doesn't do the autocopy, so use 2.x or copy them manually and the USB stick should also be FAT32 formatted, Windows doesn't allow greater than 32Gb FAT32 disks so use another tool to do that job.

You may need to exit emulationstation to refresh the list of games, so hit F4 then type emulationstation from the command line and check if your games are showing, if they aren't make sure they're not zipped or in another format and make sure they have the correct file extension for the emulator you're going to use. Refer to the emultationstation website for further details if you're stuck.

You can install and configure a bunch of other things if you drop to command line by hitting F4 from emulationstation or SSH'ing using putty then typing:

sudo raspi-config
and expand the filesystem, exit and type
sudo reboot

Once you've rebooted, drop to command line again and type:

cd RetroPie-Setup/
sudo retropie_setup.sh


One thing you might want to do while you are there is to install Kodi under the experimental packages, we'll do more with that later. You should also configure your controllers from here, under Setup then Configure input devices.

Overclocking

This isn't really needed, but hey we're having fun so lets have even more fun! While in the command line you can, if you have a heatsink and fan, overclock your pi safely like this, but I take no responsibility for you choosing to do this. This gives you an extra 200Mhz per core without voiding your warranty:
sudo nano /boot/config.txt

type/paste this at the bottom of the file, check for typo's:
arm_freq=1100
core_freq=500
sdram_freq=500
over_voltage=6

touch checktemp.sh
chmod +x checktemp.sh
nano checktemp.sh

and type or paste this in:

#!/bin/bash
echo "Press [ctrl+c] to end monitoring"
echo ""
while true
do
  vcgencmd measure_temp
  sleep 3s
done

now to check your temps either remotely via ssh or command line - useful for keeping an eye on them while gaming or watching videos with the overclock, type:
./checktemp.sh


Gaming

So by now you should have your controllers configured (let the script time out if you don't have a controller with all the buttons it asks for) and a bunch of games ready and raring to go. If you have any issues with the size and position of the game or if it doesn't perform too well you can press x or m just after selecting the game you want to play, this will bring up a configuration screen where you can change emulator, resolution etc.

I have a PS3 controller I use with USB BT dongle I also have USB NES, SNES and PlaySEGA controllers for when I want the retro feel. Mostly they gather dust.


Note: that the items under Ports will not use the controller but the keyboard, so make sure you've configured that too. If you find a way to enable the controllers here, let me know as I've not had a look at doing that yet.

Note II: If you have everything setup as you like, now would be a good time to take a backup of your image using Win32diskimager again, but instead of using write, use read.


Media Centre time!

So now you've been playing games and you would like to do something else with your Pi to enhance the experience or save having to purchase a second for watching films, TV shows or listening to music.

This is where installing Kodi earlier comes into play.

You can find Kodi under the Ports, you'll need to use a keyboard and mouse here at first. We'll get onto changing that a in a bit.

So under Kodi you can go into System, file manager and add locations, such as a shared drive or NAS so it can see all your TV recordings, family photos and music.

But you can also add other peoples hard work to your Kodi install relatively easily, doing so is likely going to be different depending on how they setup their Kodi. I chose a particularly good custom build I was shown by a friend, its got a lot preconfigured and regularly updated.

Changing the function of the power button so it doesn't shutdown the Pi and rather gives me the options of what I want to do, so I can shutdown or reboot which will get me back to emulationstation. Unfortunately the Exit option doesn't return you to Emulationstation. 

So drop to command line again, you will need to reboot the Pi if you are in Kodi.
and type:
cd .kodi/userdata
sudo nano guisettings.xml
press control+w and type/paste in PowerBtnAction and hit enter
this will find what we need to modify, now delete the word Exit and type/paste the below where that word was between the > and <
ActivateWindow(shutdownmenu)


Using a PS3 controller in Kodi to Navigate your way around:
http://blog.petrockblock.com/forums/topic/kodi-tab-in-emulationstation/

The alternative is to find a Kodi remote on your smartphone's app store, there are  apps on Android, iOS and Windows Phone, and setup the remote control bit in Kodi, to do that go to System > Settings > Services > Web Server and enable the control of Kodi using remote http, set your port, username and password, load your app up and setup the connection using your Raspberry Pi's IP, the port, username and password you set. This won't let you make your way from emulatiuon station to Kodi, so you'll need to use your controller to get to Kodi then your smartphone app to control Kodi or just stick with the controller ...

Unfortunately at this time there is no way of getting Netflix working directly on the Pi, you'll need a subscription service called PlayOn and a Windows computer to install it on in addition to your Pi ... That's not an elegant solution. It's a shame as I enjoy Netflix's content.



Shutdown button

So I decided to do a shutdown button rather than use the reset header pins which cuts the power and juices it up when a momentary switch it used and pressed down and released and solder some pins in there with a switch. Doing this can corrupt your SD card or some files and cause things to go awry.

I won't go into to much detail as its well covered elsewhere on the interwebs, if you decide to go for one, use the Safe Shutdown switch in this link here.

If you want you can then also add the Reset Switch option to completely cut power  to the Pi using a rocker switch instead of a momentary switch.


Well, I hope you've found this helpful and if you have any comments or suggestions just shout! 

Windows 7 x64 slow computers, no free ram available!

A few months ago a client reported that all their computers started  becoming slow at times, my first thought was a virus on the network because it was ALL the computers and they had been infected with CryptoWall previously, however a lot of scanning with their AV and additional programs found nothing.

When ever we got control of their computers we couldn't find any viruses, we couldn't see the problem, so I started looking elsewhere.

All  their profile data is redirected to the servers, so I started looking at the network.
They're in a nice serviced building who take care of the cabling and a bunch of the sockets will only connect at 100Mb, they also have old Netgear switches. I'm no fan of Netgear,we've had a tonne of problems with Netgear equipment over the years and much prefer Cisco gear.

Unfortunately replacing the switches wasn't an option without sufficient proof they were at fault so the search continued. Whilst I was attending a scheduled visit I took a look at the computers and found that one instance of svchost.exe was using over 1Gb of RAM!

Loading up Process Explorer from Sysinternals, I managed to track down what was consuming the RAM. Surprisingly it was Windows Update! Windows freaking UPDATE!

Why the heck would it consume all that memory, almost constantly? A bit of digging and it seems like late last year/early this year others started having the same problems with Windows Update with no real reason found, no update in particular causing this.

The only thing I could find was to change the default Windows Update GPO's to tell Windows to check for updates less often than once an hour which is the default, I set it to 22hours which was the max allowed as there is also a ± 2hour variance in the schedule. (When I remember the exact setting I'll update this post, sorry).

Within a day they reported their computers were noticeably better! Well except one PC which even after removing from any OU's to prevent the GPO's from applying and rebooting many times, made no difference to it, disabling Windows Updates on that machine does help, but its no fix, its a bodge. Removing it from the domain, readding, reinitializing Windows Updates, etc still no joy, so this sucker is being reinstalled soon, for now its a scheduled task to start Windows Updates overnight and stopping it during working hours. Horrible non-standard fix (read as bodge), but sometimes a tech's gotta do what a tech's gotta do.

Pinning Hyper-V to Taskbar in Windows 10

So I've installed Windows 10 Enterprise at work, added the Hyper-V feature, great? I can now run my VM's again right? Yes, I can, if I could find Hyper-V !

It doesn't show up in the Start Menu, it doesn't show up in the search.

I thought sod opening MMC and adding it in and saving it.

Go to C:\Windows\system32 and find virtmgmt.msc, right click it and choose pin to Start, go to your Start menu, right click Hyper-V Manager and choose pin to taskbar.

You can't just drag it to the taskbar currently and have to first pin it to Start, I hope this is a bug Microsoft fix. This 'fix' will probably also work for other MMC Snap-in's that Windows 10 doesn't find after installing their features.

Monday 27 July 2015

OpenBSD router - BT Home hub 5 replacement

As we deploy OpenBSD based routers at work I decided to pimp up my home broadband in the same style! At work we use purpose built, low power devices, at home, I'm using an old Dell Optiplex 760 Core2Duo with a second NIC.

The onboard NIC is an Intel 1Gbps unit and the secondary is a Broadcom 1Gbps unit I salvaged.
Fortunately both have good support with OpenBSD.

At home I have Fibre to the Premise/Home from BT, currently on the mid level package 160Mbps - I'm not paying over the odds for the current 300Mbps package which is £50 at present. I am looking forward to BT's rollout of G.FAST which they're  about to trial out in Swansea which still uses copper to the distribution point, the new product will be called FTTdp. What exactly this means for those of us with FTTP, I'm not sure, but I hope we get matching speeds or more than FTTdp.

So I installed OpenBSD 5.7 on  the Dell, it's pretty easy, but not covered here. Once installed I wrote the following files to get the box 'dialled' up to BT, they're the generic BT credentials anyone with a non-BT Home Hub can use, they are the authname bthomehub@btinternet.com and password is BT, you apparently use your BT account login credentials.

Thus far I'm getting slightly faster speeds than I was with the BT Home Hub 5 in operation.

If you're not using a home brew OpenBSD router and some other router, then skip on down a bit to find out how to use the BT Hub's as a wireless access point to extend your wifi, you can use this with BT Hub 3,4 and 5's - so if you've got your old ones laying around you can use those to give you better wifi in areas where you may have sketchy wifi in your home.

OpenBSD router config, edit the files as below replacing em0 and bge0 for your own NIC identifiers.


File: /etc/hostname.em0   (em0 is a Intel NIC)
    descr "WAN"
    up

File: /etc/hostname.bge0   (bge0 is a Broadcom NIC, this is a PCIe card)
    descr "Internal Network"
    inet 192.168.1.254 255.255.255.0 NONE

File: /etc/hostname.pppoe0   (ppp "dial up" interface)
    inet 0.0.0.0 255.255.255.255 NONE \
        pppoedev em0 authproto chap \
        authname bthomehub@btinternet.com authkey BT up
    dest 0.0.0.1
    !/sbin/route add default -ifp pppoe0 0.0.0.1

File: /etc/dhcpd.conf
    subnet 192.168.1.0 netmask 255.255.255.0 {
        option routers 192.168.1.254;
        option dhcp-lease-time 2592000;
        option domain-name-servers 194.74.65.69, 194.74.65.68, 194.72.0.114, 8.8.8.8, 8.8.4.4;
        range 192.168.1.10 192.168.1.200;
    }

File: /etc/myname   (Setting the hostname of the router, this can be just about anything you want)
    Router

File: /etc/resolve.conf   (DNS resolvers in here, without configuring this you have no DNS)
nameserver 194.72.0.114   #BT DNS server
nameserver 194.74.65.69  #BT DNS server
nameserver 8.8.8.8            #Google DNS server1
nameserver 8.8.4.4            #Google DNS server2

File: /etc/rc.conf.local  (Daemon's / Services here, you may not need all, depending if you want VPN access into the network or not)
sshd_flags=""                    #Enables SSH access
dhcpd_flags=""                 #Enables DHCP Daemon/service
ntpd_flags="-s"                 #Enables Network Time Protocol server
isakmpd_flags="-K"         #Enables VPN Daemon, I'll be using this!
ipsec=YES                         #Enables IPSEC
ftpproxy_flags=""             #Enables the FTP Proxy service used in pf.conf

File: /etc/pf.conf    (This is the firewall config file)
    IF_WAN = "pppoe0"        #creates a variable for WAN, quicker updating by changing this only
    IF_LAN = "bge0"             #Creates a variable for LAN, in case I change the card/type later on

    set skip on { lo, enc }        #skips filtering loop back interface
    set block-policy drop        #drops any packets not dealt with below

    block in        #Blocks all incoming packets
    pass out       #Passes out any packets

    #FTP Proxy to allow FTP traffic correctly, this needs to be before your NAT rules
    anchor "ftp-proxy/*"         #Proxies all FTP traffic
    pass in log on $IF_LAN inet proto tcp from $IF_LAN:network to !$IF_LAN  \ port ftp flags S/SAFR modulate state divert-to 127.0.0.1 port 8021

    #NAT's the LAN traffic to WAN, which is pppoe0 NOT em0 as its the dialling interface, also alters the MTU to 1440 as I had issues with a higher MTU
    match out on $IF_WAN from $IF_LAN:network nat-to ($IF_WAN:0) scrub (no-df max-mss 1492)


    #Allows Anything on the LAN to talk to the router
    pass in on $IF_LAN from $IF_LAN:network

    #Allows response to pings
    pass in inet proto icmp to $IF_WAN icmp-type { echoreq, unreach }

File: /etc/sysctl.conf   (enabling different system calls)
    net.inet.ip.forwarding=1 # 1=Permit forwarding (routing) of IPv4 packets
    net.inet.ip.mforwarding=1 # 1=Permit forwarding (routing) of IPv4 multicast packets
    net.inet6.icmp6.rediraccept=1 # 1=Accept IPv6 ICMP redirects (for hosts)
    net.inet6.ip6.accept_rtadv=1 # 1=Permit IPv6 autoconf (forwarding must be 0)


Once you've edited your config files, connect the BT Openreach modem to your WAN port and reboot, looking for errors during boot.
Check ifconfig for pppoe0 to make sure its 'dialled up'.
do pings to external IP's and hostnames to check connectivitiy, maybe ever hook up  a machine to the LAN port and see if it can connect to the internet.

The pppoe0 section of ifconfig will look like this, this can be checked with the command:
 ifconfig pppoe0

    pppoe0: flags=8851 mtu 1492
        priority: 0
        dev: em0 state: session
        sid: 0xf PADI retries: 4 PADR retries: 0 time: 02:51:40
        sppp: phase network authproto chap authname "bthomehub@btinternet.com"
        groups: pppoe egress
        status: active
        inet MY.BT.WAN.IP --> 172.16.12.23 netmask 0xffffffff



BT Homehub config


Now that I have the OpenBSD box connected to BT's network, I still need WIFI, dammit!!!

So to re-purpose the BT Hub's all you need to do is:
  1. connect a PC/Laptop to the hub via ethernet
  2. Set a static IP on your ethernet adapter on the PC itself, make sure its in the same range as your LAN.
  3. Goto its webpage http://192.168.1.254
  4. Goto Advanced settings and login
  5. Goto Firewall, configuration, and set to disabled.
  6. Goto Home Network, IP Addresses, turn off DHCP
  7. Change IP from .254 to say .200, conveniently out of the DHCP scope I set on the OpenBSD router
  8. Wait a couple minutes and try get to to http://192.168.1.200, if you can't then yank out the BT Hub's power and plug it back in and try again, if you've done the above correctly you'll get into the Hub.
  9. Connect the Hub to your LAN port on the OpenBSD router, and any other devices to your Hub.
  10. Confirm your network works and you can get online.
  11. Remove the static IP you set and confirm you get a DHCP lease and can still get online.
Providing I've not missed anything and you've followed my directions, you should now be up and running with a home brew OpenBSD router, using your BT Hub as a WIFI access point, if neither of those are at fault then the stars aren't aligned with the planets in our neighbouring galaxy Andromeda.

Installing extra software on your router


To install binary packages, the preferred method of installing software not in the base install you should use pkg_add, however to do this you need to tell it where to get the packages from and to do this you simply type:

echo "export PKG_PATH=ftp://mirror.exonetric.net/pub/OpenBSD/5.7/packages/amd64/" >> .profile

You will probably want to change the ftp server, I've tested the speeds of each UK server and found exonetric's to be the "closest" in terms of hops on the internet, yes, I live in Oxford but there are 7 more hops from myself to the Oxford Uni's mirror than there are from myself to exonetric's mirror, this is just how my ISP and no doubt most ISP's route traffic back to London first then back out to the internet.

Now to install software you just type the below with < and > and choose from the list if asked relating to the software you want to install:

pkg_add


PS: Thanks go to the guys at the BSD Now podcast for linking to my BSD blog articles :) Keep up the good work guys, I am an occasional listener!