Saturday, September 23, 2017

Standalone Timesaver puzzle using Raspberry Pi3 + JMRI + Engine Driver


The Timesaver module forms the heart of the Greenly Northern, my office layout. Today I pulled it from storage where it had been since a desk move left me with no space to set up the trains to much sadness in the team. My current desk location has sufficient space along the window sill to do something with trains again. I cleaned the tracks, rearranged electrical hook-ups a bit, and a short while later the little GE 70-toner was up and running again.

For the puzzle web page to work, I need a computer nearby. Everyone has a smart phone, but I can't guarantee Internet connectivity to my server at home. Also, many switching moves take time, so the screen saver turns on while we run the train and having to unlock the phone all the time to plan the next move is annoying.

What if I used the phone to operate the train? Been there, done that. JMRI  and Engine Driver are well-known solutions for that. I already have the NCE USB adapter for use with my NCE PowerCab. Connect that to a computer and Wifi, and we're done. However, I can't just connect anything I want to the office network at work.

So, the whole thing needs to be stand-alone. The Raspberry Pi 3 with built-in Wifi came out last year and fills that gap nicely.


Here's a quick overview of what I did. If you are unsure how to do any of the steps: I generally followed instructions I found on the Web as needed.

I started with the Raspberry Pi 3, installed Raspian and ran through the usual system upgrades. Next I installed the hostapd and dnsmasq packages. I switched wlan0 to static configuration, and configured hostapd to use wlan0 for the Access Point and serve up the SSID "GreenlyNorthern". I configured dnsmasq to serve DHCP on wlan0. It's a good idea to use a private IP range, e.g. 192.168.0.0/24 for wlan0. After a reboot I had my own private Wifi network hosted on the Raspberry Pi 3.

Moving on to JMRI. I downloaded the lastest production version of JMRI, ran PanelPro and configured it to use the NCE USB interface, as well as start both the Web Server and WiThrottle Server automatically. I added the switcher engine to the JMRI engine roster, taking care to make the function button assignments reflect reality. I added JMRI/PanelPro to the program list in /home/pi/.config/lxsession/autostart, so that JMRI starts as soon as the user pi logs in after boot, which is the default behavior for a Raspberry Pi setup.

On the next reboot I could take my smart phone, connect to the GreenlyNorthern wifi network, start Engine Driver, select the Greenly Northern layout, select the switcher engine from the roster, and control the switcher from the smart phone.

The last piece of the puzzle was adding the Timesaver puzzle web page to the Engine Driver Web pane. I installed lighttpd on the Raspberry Pi 3 and configured it to run the little Python script that produces the puzzle Web page. That required adding a scriptalias entry to map /cgi-bin/ to /usr/lib/cgi-bin where I copied my script, and assign /usr/bin/python to run .py scripts. You can enable the debian documentation config snippet in lighttpd to get the settings, or just add them to the cgi config snippet under /etc/lighttpd/.

Finally, I added a link to the puzzle script (e.g. http://192.168.0.1/cgi-bin/puzzle.py , there's no DNS in this wifi network) to the main web page served up by the JMRI Web Server.

Next I changed the preferences for Engine Driver on my phone to show the Web pane on the throttle window and only use one throttle. Engine Driver loads the JMRI web page by default, so a tap on the puzzle link is all that's needed to get the puzzle shown on the lower half of the Engine Driver screen. The upper half has the throttle.


I'm using an old Android phone as throttle in this picture. I don't know whether a similar trick can be done with the WiThrottle app on the iPhone.
The general approach works just as well with JMRI's web throttle from any Web-capable device, by embedding the puzzle on a web throttle page. Use e.g. an iframe, but pretty much anything goes, including creating the puzzle generator in Javascript.

No comments: