Thursday, May 25, 2017

Low Cost Wireless JMRI Signal boards with the ESP8266 and MQTT

I want to use this space to present some work I have been doing, along with Gert "Speed" Muller, on using low cost ESP8266 WiFi microcontrollers to control signaling.

In February on the Arduini Yahoo group I got a little carried away when someone suggested that the European MERG boards were the best solution for CAN based signaling. My experience with MERG 15+ years ago was not great and after seeing a couple consecutive posts where buying boards from MERG was the solution given, I challenged the well meant suggestions in a less than tactful manner. I only mention this because that flame-war was the starting point of this project.

John Plocher mentioned using MQTT with a WeMos D2 Mini R2 board to control signals over WiFi using tried and true TCP protocols. While I had heard of affordable WiFi chips becoming popular recently, I never considered using MQTT to control signals, so there was a big learning curve.

MQTT is an Internet of Things (IoT) connectivity protocol. The IoT buzzword is super hot right now and there is a lot of interest in anything that has to do with connecting things to the Internet. Any time there is a lot of excitement around some technology, there is usually some way to apply it to model railroading.

This isn't meant to be an introduction to MQTT, there are thousands of resources where you can get up to speed on that. I suggest going to Youtube and searching for MQTT Tutorial.

The first thing I had to do was determine if JMRI can be made to understand the MQTT protocol. I knew JMRI has the ability to run scripts and a number of examples are included in every version. Because JMRI is written in Java, the creators cleverly decided to include the Jython interpreter with JMRI. Jython allows Python scripts to be run natively in the Java environment and use the JMRI libraries written in Java. This is amazingly powerful and useful in a way that is probably not obvious at first glance. Some basics of Python scripting in JMRI can be found at JMRI: The Python/Jython language.

The MQTT library for Python is called Paho and is from Eclipse. I set an initial goal of getting the library to load without errors, which turned out to be harder than I expected. Here are the steps I had to take to get there.

Many of these steps involve the command line and editing files. I will add a prompt before the command and use a Courier font. $ for MacOS, > for Windows and % for Linux. If text is added or edited, I will use blue.

Starting with MacOS.
  1. Download latest jython installer from
  2. $ java -jar jython-installer-2.7.0.jar to install jython in ~/jython2.7.0
  3. $ ~/jython2.7.0/bin/pip install paho-mqtt
  4. Copy /Volumes/Macintosh\HD/Applications/JMRI/ to ~/Library/Preferences/JMRI
  5. Add the following to ~/Library/Preferences/JMRI/ python.path=~/jython2.7.0/Lib/site-packages                                                            python.startup =
  6. Change lines in ~/jython2.7.0/Lib/site-packages/paho/mqtt/ file from listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) to listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) as discussed here.
  7. Add import paho.mqtt.client as mqtt to the top of a generic python script
  8. Run script and look for errors in console
  9. After script runs, check that the python.path is correct under Help -> Context...
In Windows.

  1. Download latest jython installer from
  2. > java -jar jython-installer-2.7.0.jar to install jython in C:\jython2.7.0
  3. > C:\jython2.7.0/bin/pip.exe install paho-mqtt
  4. Copy C:\Program Files (X86)\JMRI\ to C:\Users\username\JMRI\
  5. Add the following to C:\Users\username\JMRI\ (had to escape out backslashes and dash with JMRI version 4.7.2 )
  6. Change lines in C:\jython2.7.0\Lib\site-packages\paho\mqtt\ file from listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) to listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) as discussed here.
  7. Add import paho.mqtt.client as mqtt to the top of a generic python script
  8. Run script and look for errors in console
  9. After script runs, check that the python.path is correct under Help -> Context...
In Ubuntu 16.04.
  1. Download jython-installer-2.7.0.jar
  2. % java -jar jython-installer-2.7.0.jar and install jython in ~/jython2.7.0
  3. % ~/jython2.7.0/bin/pip install paho-mqtt
  4. Copy <JMRI_FOLDER>/ to ~/.jmri/
  5. Add the following to ~/.jmri/ python.path=~/jython2.7.0/Lib/site-packages                                                            python.startup =
  6. Add import paho.mqtt.client as mqtt to the top of a generic python script
  7. Run script and look for errors in console
  8. After script runs, check that the python.path is correct under Help -> Context...
So, the key to these instructions is that you have to install Jython. then you have to install Paho so Jython can find it. The reason for editing the is described here, at first it wasn't right, but I posted a note to the JMRI Yahoo group and it was updated. Thank you to the editor. Basically, you have to install the full version of Jython that can have libraries added to it. The standalone version that is shipped with JMRI can't be extended like this.

I found a bug pop up in the paho library, luckily some Google foo found a solution as noted above. We'll get more into the Python code for handlers, etc., later but any of the examples in the Python Examples directory can be used to test that you have installed everything correctly.

Next time we'll talk about the ESP8266.

Wednesday, March 1, 2017

A New Year, a New Post

It seems like most of my model railroad projects haven't really been out in the section house. After the Plano train show in January, I traded some unsold locomotives and freight cars for some Walthers, Rapido and Balboa passenger cars. I am starting to build a pretty decent North Coast Limited and Mainstreeter, but I need dome cars for the NCL and more baggage car or mail storage cars for the Mainstreeter.

My next big project is to add benchwork along the southeast wall. This will allow me to have train continue past Billings and add the Sugar Factory on the lower deck and add Parkman, the top of the grade, on the upper deck. This is what it looked like at the beginning of February.

The blue backdrop is all that remained of Shane's layout, I took it down and salvaged what I could.

You might remember the trouble I had finding studs with the stud finder. At some point over the winter I removed a chunk of drywall and discovered the reason is because the 5/8" sheetrock is offset from the studs by 1x4 material, adding 3/4" of air in most places. My solution was to buy some 5/8" plywood, screw it to the studs with more 1x4s and attach brackets to the plywood where needed. Removing the 9" wide strip of drywall with a circular saw was a horrible, messy ordeal, but it made for nice clean edges. Oh, and did I mention the mud daubers in the wall? Luckily it was winter, but not a very cold one.

With the first strip of plywood up, I was ready to add brackets and fascia.

I started by adding heavy duty shelf brackets upside down and attaching pieces of 1/2" plywood underneath with short screws that still poke through, but they can be ground off.

By the end of February I had the fascia extended around the new section and the bottom painted white. I could finally stack some junk on top of it and get it up off the floor.

Saturday, December 31, 2016

Holiday update

I took a couple weeks off after the DFW Interchange but one thing that was nagging at me was the lack of staging. I remember reading that my SP&S buddy Bob Willer in Kansas City had written a short article in Model Railroad Planning a while back describing his battleship staging yard. This is portable staging yard that can be moved around as needed. I decided something similar was in order.

Starting with the 4 dominoes from the switching layout that Matt Latham and I built for the 2013 LSR convention in Irving, I started working on a staging yard ladder that takes off from the end of the layout near the Yellowstone River bridge. The staging yard track is also salvaged. I used Altas #6 switches from the old layout and Micro Engineering code 83 flex track. The old switching layout had 1/2" Homasote covering it.

A couple weeks later, another domino and more track. I wasn't working on the layout ever night like I was leading up to the DFW Interchange.

By the second week of December I had use all four dominoes and all the track from the switching layout, but a test showed is wasn't quite long enough. Oh, and I had wired the lead so that trains didn't stall out.

By Christmas, I was able to show my mom my layout progress. A lot of changes since she had seen it.

By the end of December I had added a 5th domino and had all the track in, making the staging yard 20 feet long. It will be interesting to see how I move it.

Monday, October 31, 2016

October fun

In October I had all the switch machine controllers installed and was ready to put fascia on the lower lever. This is necessary mainly to keep things from falling on the floor. I had already lost a brand new sound-equipped Rivarossi U25C, which wasn't pretty.

I cut slots in the masonite in where gaps between the buildings will be. I will eventually add brick to the track side to represent buildings. These will really only be seen if you look over the fascia, but it will give the effect of looking thru a canyon of buildings that you get when you are in Billings.

A little paint gives the facia a clean look and ties the fascia together with the valance.

We cut some vinyl signs on the Cricut.

The first operators on the layout were Peter Bryant from Houston and John Emerson from Amarillo. They were two of the 35 or so out of town guests to come up for the 2016 DFW Interchange operating weekend. Speed Muller is on the other side of the peninsula, but he didn't count since he was a local. Actually, Speed was a big help getting the switches plugged into JMRI.

The next day David Perkins and his girlfriend Pheriche from Baton Rouge operated the last session of the DFW Interchange. The night before, the third operator decided he couldn't read HO scale car numbers and went home. Pheriche has a Kindle Fire tablet in her hand throwing the switches. The tablets will eventually be mounted on the fascia at eye level.

Saturday, October 1, 2016

September Update

In September I was still preparing for the DFW Interchange, but that didn't keep me from taking a trip to Washington, Idaho, Utah, Wyoming and Montana.

I started by flying into Spokane and picking up a Wyoming economy car. So much for a Toyota. At least it had Oregon plates... the only Northwestern state I wasn't driving thru.

First stop out of Spokane was Salt Lake City, UT for Great Basin Getaway. Yes, there is a plan to my madness. I knew it was going to be a good weekend as I was following the Northern Pacific and Milwaukee Road Mainlines thru Montana and these double rainbows ended on the double tunnels at Revenna.

I've attended 5 Great Basin Getaways over the lasts 14 years and each time I have operated on Ted York's Cajon Pass and Gary Peterson's Great Salt Lake.

All 5 times on Ted's layout I have been in the yard, although I've never had the yardmaster's job.

I worked this town on Gary Peterson's layout. This didn't even exist the first couple times I operated with him He did a massive reconstruction 10 years or so ago and this area is all new.

This is the first Great Basin Getaway that I didn't get to operate on Lee Nicholas's fantastic Utah Colorado Western, but we did go out and see Lee's expanded layout where he moved his mole operation and added this scene. I fell in love with his sugar factory.

The reason I didn't get to operate on Lee's layout is that I had requested to operate on rob Spangler's Western Pacific and it didn't disappoint.

Next day, on to Wyoming. I didn't see one damn train thru the canyon while in Utah, but once I crossed into Wyoming I did catch some traffic. Here we see the east main above the west main. I thought about stopping here to pick up a used car but I had a schedule to keep, I was headed for Sheridan.

They have done a great job turning the roundhouse in Evanston into an event center.

Every since operating on Michael Borkon's in Kanas City I've been excited to get back to Green River. It is an amazing place to rail fan. Too mad I'm only about 70 years too late for the big steam.

I planned on spending a whole day photographing around Sheridan and Gillette and another day between Sheridan and Billings. Of course, not only was the weather crappy, but trains weren't running.

The yard at Donkey Creek near Gillette had hundreds of locomotives stored because of the slow down in coal. I couldn't get an angle that showed them all at once, but here is a bunch next to the highway.

What would a trip to Wyoming or Montana be without catching some 737s heading to Seattle. These were sitting outside Laurel waiting to get into the yard.

I got up early the next day to see what was going on before heading to Wallace, ID for the Northern pacific Historical Association's annual convention. The weather still sucked and it was raining, but I did see more 737s, this time in Billings next to the old coach yard.

The sun started shining some by the time I made it to Garrison, MT where the BNSF still operates from here to Butte while Montana Rail Link has a lease on the NP main from Billings into nearly Sandpoint, ID.

The Wallace convention was a lot of fun and I really enjoyed the week in Northern Idaho. A couple highlights were a trip up the canyon to Burke, which is just a shadow of its former glory.

We also had a guided tour over Lookout pass between Idaho and Montana including this spot where famous S bridge trestle was located. The trestle was famously taken out by a slide with a train on it, which was well documented in photographs.

Saturday, September 3, 2016

August update

August was mostly wiring, which doesn't make for very great blog entries. Katy and I did move all the cars that I had gotten out and move them to the yard.

One of the more interesting events to happen in August was a train derailed on the tracks that go by our house. It was crossing Denton creek north of Roanoke on 8/21/16 when it happened. It just so happened that I was at Home Depot in Roanoke picking up something for the layout, wire I think, when it happened. When I started north out of Roanoke headed home, I saw a bunch of police cars, etc. on on the highway where it crosses Lake Grapevine.

It wound up taking two spans of the bridge out, which are blocked by those trees. The trees were all removed during the cleanup. Now there are concrete sections in their place.

Nothing ran for a couple weeks. On 8/23 this went by while I was at the post office in Argyle.

One of the projects I tackled was adding a booster. Until then I had just been using the command station.

In the mean time, I worked my way around the layout adding drops and connecting them to the sub-busses. Once all the track had power, I moved on to hooking up the Tortoises. I used NCE SwitchIt circuits to power the Tortoises. 

Monday, August 1, 2016

July Progress

July was a very busy month. After the open house in June the layout room was clean and ready to be torn up again. Of course, the bad thing is that when the room is clean I can never find my tools, so I spend most of my time hunting.

After the convention, this is what the Billings yard looked like. I needed to figure out a way to get the turntable pit installed close to the backdrop.

Yard looked like a stub yard at the beginning of July.

I decided to cut a template with a big plunge router and then use a trim router to cut the actual hole. 

Here is the crappy trim router from Harbor Freight. It died about 3/4 of the way through and I had to finish with a rotozip.

This is what the hole looked like before I removed the template.

And with the pit dropped in. I still need to get the correct screws to hold it firmly in place.

It didn't take long to lay the rest of the yard tracks.

A closer view of the west end of the yard ladder.

I had to insert ties in the gaps where the flex track comes together.

At the end of July I went to Durango, Colorado with Speed to take the Soundtraxx Academy. It was a lot of fun and one of the highlights was seeing their surface mount assembly floor. Here is a production worker testing decoders using a functional tester that they have special built to emulate a locomotive.  

We rode the Durango and Silverton on Saturday with the Soundtraxx group.

On Sunday we went over to Chama and rode the Cumbres and Toltec. It was a spectacular weekend and I highly recommend it to anyone.