Wednesday, July 16, 2014

Consistency has been Achieved

OK, so I have been buggering around with my Raspberry Pi over the last few weeks. I have now been able to time after time follow these here destructions and get a working JMRI out of it and control my trains on track connected to my NCE PowerCAB.

There is only one more step alluding me, and that is how to get the R-Pi to autostart JMRI and thereby allowing me to run "faceless".

But these instructions so far as I have gotten, may be of help to those out there wanting to put the latest version of JMRI 3.8 onto their Raspberry Pi. (the hardware is as per my previous post):

1. Purchase your bits and pieces. Here are the parts I purchased to use for myself and this project:

2. Next I pulled down a small piece of software to format the SD Card I will use with this Raspberry Pi. The software is called “SD Formatter 4.0 for SD/SDHC/SDXC” and is available from:

So I followed the relevant instructions as per their website and formatted my SD Card.

3. Obtain the latest Operating System for the Raspberry Pi. You can get it from:

For my current build, the download I used was “NOOBS version 1.3.9”. This will get me the “Raspbian Debian Wheezy” Operating System that was release on 20 June 2014.

4. Follow the instructions to get the NOOBS copied to your SD Card and the to install the Operating System on your Pi:

5. Make a few minor configuration changes like:

            Password for your Pi
Locale:  select “en_AU.UTF-8”  and deselect the default “en_GB.UTF-8
Timezone: I selected “Australia”, then “Brisbane”
Keyboard Layout
Name you want to give your Pi

6. Reboot the Pi and login to it. The default user is “pi” with a password of “raspberry

7. I ensured I had my Ethernet cable plugged into the Pi and connected up to my hub/switch and router to my home internet feed. This way my Pi got an IP address and was able to connect to the internet to get all the software needed to continue with this build.

8. Check that the system is all up to date with its software. So issue the following command at the prompt. The prompt is that blue coloured dollar sign “$” on your screen. So here is the command:

                sudo apt-get update

9. Now to install the remote control software so you can get into your Raspberry Pi from your desktop computer, because as we know that has the most comfortable chair in the house and a nice big screen. But if you want, you really don’t need the remote software. So if you do, here we go. We want to install “TightVNCServer”. So type in the following command at the prompt:

sudo apt-get install tightvncserver

Once it has completed its install, you can start it up (note, there is a space before the colon):

vncserver :1

You will be asked to give it a password. Please note it only accepts a maximum of 8 characters. If asked for a read only password say no to it.

10. Install a VNC Client on your desktop PC. There are many, but I used the one from RealVNC at:

11. You can now connect remotely to your Raspberry Pi (let’s start shortening the name to R-Pi shall we) and be comfortable. As an example, my R-Pi when connected to my Ethernet at home, grabbed an IP Address of . Hence to connect via VNC to it from my desktop PC, I just fired up the RealVNC software and put this IP Address in for the remote connection I was going to (ie the R-Pi) as well as adding in a colon and a one at the end ie -

To find out your IP Address, at the prompt on you R-Pi, just type in:


This is part of what gets spat out. The important bits we want are the four numbers after the “inet:addr” in the section of “eth0”:

eth0    Link encap:Ethernet  HWaddr b8:27:eb:30:de:32 
                       inet addr:  Bcast:  Mask:
          RX packets:804 errors:0 dropped:0 overruns:0 frame:0
          TX packets:943 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:42747 (41.7 KiB)  TX bytes:220723 (215.5 KiB)

The following 2 steps would seem out of order, but it has a reason – it saves typing because you can do the second step much easier this way – trust me.

12. One problem I had with the TightVNC was that copy/paste between my Raspberry Pi and Desktop PC did not work. So I found a solution here:

So you pull down and install “autocutsel”:

sudo apt-get install autocutsel

Then you need to open this file /home/pi/.vnc/xstartup and edit it. So at the command prompt type this:

nano /home/pi/.vnc/xstartup

By the way, nano is a small editor that is in your Pi by default.

The edit is to insert this line   autocutsel -fork   into the existing file and thereby give a file that looks like the following:

xrdb $HOME/.Xresources
xsetroot -solid grey
autocutsel -fork
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work

So save the file and with a reboot of your Pi, the copy and paste will now work with your VNC.

13. We now need to make sure the TightVNC starts up automatically each time the Pi starts up. I tried a number of recommended ways, but ended up following the “Adding Tightvnc to the startup“ section of this one which worked fine for me:

So here we go – go to the command prompt (that little blue dollar sign) and we will make a new file to autostart the TightVNC. So start typing:

cd /etc/init.d
                sudo nano tightvncserver

Add all these lines into this file we have just created and opened up:

# Provides:          tightvncserver
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop tightvncserver
# More details see:
### Customize this entry
# Set the USER variable to the name of the user to start tightvncserver under
export USER='pi'
### End customization required
eval cd ~$USER
case "$1" in
    su $USER -c '/usr/bin/tightvncserver :1'
    echo "Starting TightVNC server for $USER "
    pkill Xtightvnc
    echo "Tightvncserver stopped"
    echo "Usage: /etc/init.d/tightvncserver {start|stop}"
    exit 1
exit 0

Now to set the file up to run. Type the following at the command prompt:

sudo chown root:root /etc/init.d/tightvncserver
sudo chmod 755 /etc/init.d/tightvncserver
sudo update-rc.d tightvncserver defaults

Reboot the Pi and voila, the TightVNC has now started.

14. Let us now install and configure the software to turn our Raspberry Pi into a wireless Access Point. Depending on the Wireless Dongle you have purchased, these steps may change. But for the one I have used (the Nano 150 from Jaycar), the best method was to follow one I found on the internet. This instruction set suits all dongles that have the Realtek 8188CUS chipset in them:

So here are the key strokes to follow at the command prompt:

tar -zxvf v1.1.tar.gz
cd RTL8188-hostapd-1.1/hostapd
sudo make

After a while, you should be given control back to the terminal. So then do these instructions:

sudo make install

So now we have the “hostapd” program installed – this is part of our Wireless Access Point. Now we need this program to also autostart every time we boot up the Raspberry Pi. So type this at the command prompt:

sudo ln -s /etc/init.d/hostapd /etc/rc2.d/S02hostapd
sudo update-rc.d hostapd enable

This is the output that will come too your screen – don’t worry about the two warnings:

update-rc.d: using dependency based boot sequencing
insserv: warning: current start runlevel(s) (2) of script `hostapd' overrides LSB defaults (2 3 4 5).
insserv: warning: current stop runlevel(s) (empty) of script `hostapd' overrides LSB defaults (0 1 6).

This should ensure that the “hostapd” application starts after a reboot.

15. Now we will install “dnsmasq” to use as the DHCP Server:

sudo apt-get install dnsmasq

16. Now you have to configure the “/etc/network/interfaces” file. So let’s edit:

sudo nano /etc/network/interfaces

Comment out the line “allow-hotplug wlan0” ie put a “#” in front of the line so it looks like:

# allow-hotplug wlan0

Add the following lines to setup the IP addressing. Now this can be all sorts of addresses, I’ll give an example here:

#Setup the device with an static IP
iface wlan0 inet static
# Static IP Address
# Network mask

Comment out the line with “wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf”
Comment out the “iface default inet dhcp” line
Save the file and exit the editor.

17. Now to edit the /etc/hostapd/hostapd.conf file:

sudo nano /etc/hostapd/hostapd.conf

Edit it replace it with the following lines. You just need to pick your own passphrase and ssid:

# Basic configuration

# WPA and WPA2 configuration
# passphrase has to be 8-63 characters in length

# Hardware configuration

18. Next we edit the file to configure the DNS mask. So edit:

sudo nano /etc/dnsmasq.conf

Now add the following two lines to the very bottom of the file:


18. So now that the Raspberry Pi is acting as a proper Access Point for us, it is time to turn our thoughts to installing the JMRI. So let’s start typing on the little blue dollar sign of our Pi.

First let’s check that the Pi is up to date:

sudo apt-get update

Download JMRI 3.8, which is the most current at the time of writing this article:

sudo tar -zxvf JMRI.3.8-r26527.tgz

19. Now to start JMRI and configure it. Now connect your DCC system interface (in my case an NCE USB adapter and my PowerCAB) to the USB Hub before starting up JMRI. Fire up a desktop environment:

cd /home/pi

Open File Manager and go to the directory  /home/pi/JMRI

Start JMRI by clicking on the DecoderPro (not DecoderPro3) start up file. This will take a while for the first time it starts.
When asked, use “Execute” and don’t use “Execute in a Terminal”.

Once the JMRI has started, change some preferences. These are found under Edit then Preferences in the menu bar of JMRI.

Once this Preferences window is displayed, enter the Connection information of your DCC system you wish JMRI to connect to, making sure you have connected your DCC system’s USB interface first to the USB hub. So I selected NCE PowerCAB.

Next select /dev/ttyUSB0 in the Serial port dropdown menu.
Save your settings and restart JMRI when asked to.

20. Once JMRI appears to be responding, activate the JMRI ‘WiThrottle’ (wifi) server. Do this by going to the menu bar and selecting Tools, then Throttle, then Start WiThrottle Server.

If this worked it should advise the static IP Address of the Pi (e.g. with a random port number (e.g. 49876) in a new window. Eg

21. To test connectivity, grab your smartphone or tablet and connect to the WiFi that your Raspberry Pi has been set to advertise. In this document we used MyWFiName and we used a password of MYSPECIALPASSWORD.

Let’s assume you have already gone and installed the Engine Driver app on your Android or WiThrottle app on your iPhone. If JMRI is working correctly, you should see the IP address and port of the JMRI ‘WiThrottle’ server on your app. So connect to it.

If this worked, the JMRI ‘WiThrottle ’server’ window on the Raspberry Pi should list the relevant smartphone / tablet device name (configured in the app) connecting to JMRI.

If connected to a layout, try acquiring a train and running the engine remotely.


By the way - I see there is a new Raspberry Pi available. It is called a Model B+. Same processor and memory, but a smaller form factor and a few other changes like now it uses a MicroSD card and has 4 USB ports and more GPIO pins.

No comments:

Post a Comment