SpaceHack/New Server

From York Hackspace
(Redirected from Spacehack server 2)
Jump to: navigation, search

Spacehack server version 2 enclosure

The new server box contains;

  • a Raspberry pi 3 (user:pi pass:raspberry as per default)
  • a 5v power supply (from the old box)
  • a network switch with 9v supply (switch from old box, power supply is new)
  • and a 7" touchscreen.

Software

The pi is running raspbian lite with lots of extra packages installed. This is the list of extra packages, some of these are names that are apt installable, some are pip things, some may require googling:

  • various xorg things needed to get an x environment
  • xinit
  • xscreensaver (so we don't get burn-in)
  • python-pip
  • pygame
  • mosquitto (just the broker, SpacehackServer uses the paho library now, not the mosquitto one.)
  • paho (a library for doing mqtt in python)
  • lazarus and fpc (Pascal IDE and compiler, for building the monitor (servergui) see below)
  • vim, of course
  • fish, which is set to be the default shell for the user 'pi', of course
  • matchbox (window manager), because we just want one window, full screen, all the time
  • gtk2-engines and gtk-theme-switch so that it's easy to select 'clearlooks' which is the nicest looking theme that's easy to install.
  • (Many other packages that I forgot I had installed)

Note: when selecting a theme with gtk-theme-switch2 you probably need to do it for both root and pi if you want consistency. This is because the instance of the server gui which starts on boot starts as root. So if needed, do both:

gtk-theme-switch2
sudo gtk-theme-switch2 

Networking

This is all configured the same as the previous instance. In /etc/network/interfaces the eth0 interface is configured as follows:

auto eth0
iface eth0 inet static
    address 192.168.1.30
    netmask 255.255.255.0
    gateway 192.168.1.29

Notice that the gateway is set to 192.168.1.29. This is so that you can connect your laptop, configure the laptop for a static IP of 192.168.1.29 on the interface that connects to the pi (netmask same as pi, gateway can be blank). Then set a nameserver on the pi with the following:

sudo bash -c "echo \"nameserver 8.8.8.8\" > /etc/resolv.conf"

and then allow the pi on to the internet by running this on the laptop:

sudo bash -c "echo 1 > /proc/sys/net/ipv4/ip_forward; iptables -A POSTROUTING -t nat -j MASQUERADE"

Just note that sometimes after doing this you can't resolve raspberrypi.local for some reason, so just connect with the IP address if you need to reconnect after that. The iptables rules on the laptop will go away after rebooting, or you can just obliterate all nat rules without rebooting by using this command:

sudo iptables -F -t nat

xinit

TL;DR: When the pi starts up, an x session is started using xinit, the xinitrc starts the window manager, xscreensaver and the spacehack monitor

In detail:

An extra line has been added to /etc/rc.local:

xinit /home/pi/.xinitrc &

This is so that xinit runs after everything has booted. This command runs as root, so it would normally look for /home/root/.xinitrc. For convenience, I have set it to load the xinitrc from pi's home dir.

The .xinitrc in /home/pi looks something like this:

#!/bin/bash
# Load the window manager and the screensaver daemon
exec /etc/alternatives/x-window-manager &
sudo su pi -c xscreensaver & # explicitly run as pi, otherwise it ends up running as 'nobody'
unclutter -idle 1 & # get rid of mouse pointer
# The server gui starts first and prevents this script continuing
/home/pi/servergui/servergui
# If the server gui crashes, then an xterm window is started to let you fix it.
xterm

The window manager is matchbox, see below, but can be changed with update-alternatives if needed in the future. the xscreensaver daemon is started here, then the server gui, then xterm if the server gui crashes. This is just so that if it does crash, you don't go all the way back to having no x session, you can recover by fixing the issue and then running the gui again.

matchbox

Matchbox is set to be the window manager with update-alternatives. Matchbox is a window manager designed for embedded applications requiring only one window at a time.

Out of the box, it's not perfect for spacehack because there is a close button you can accidentally press and the window switcher button (which won't get used much) is so small that it's difficult to tap it.

This is why the theme has been tweaked. The menubutton.xpm and theme.xml files in /usr/share/themes/Default/matchbox/ have been replaced.

See File:Matchbox-theme-tweaks.tar.gz

Spacehack monitor 'servergui' (touchscreen GUI)

This is written in pascal, because when Dan asked if anyone wanted to write a gui, nobody said yes, so he wrote the gui in pascal.

The gui doesn't do anything special, it just connects to mqtt and listens for status updates and can post commands. All of the talking to game code is done through MQTT.

The server gui depends on mqtt-free-pascal, which is in the pi's home dir, it came from github. To recompile the mqtt library...

cd /home/pi/mqtt-free-pascal/TMQTTClient
lazbuild tmqttclient.lpk

This makes some magic happen! (You have to do this before you can compile the server gui.)

Now you can compile the gui by doing the following:

cd /home/pi/servergui
lazbuild servergui.lpr

Gotcha: Notice that the mqtt library is "dot el pee kay" but the servergui program is "dot el pee are". (Lazarus PacKage vs Lazarus PRoject)

Gotcha two: you might need to run lazbuild with the -B option. Sometimes it doesn't rebuild things that it should rebuild, -B fixes this.

Just be careful with this, the gui starts on boot.