LocoNet to MQTT Gateway

Watch video here: https://www.youtube.com/watch?v=e6NgUr4GQrk&t=4s

Ever since Digital Command Control systems became available, manufacturers provided a way to hook them up with a computer. And over the last 40 years, the fundamental structure of this connection has always been a point-to-point connection to a single computer. 

Compared to technologies used in IoT, the Internet of Things, this point-to-point connection is like the Stone Age of Computer Control. 

Today, we are used to having all our devices connected to the Internet. Indeed, even middle school kids nowadays are carrying a device that really looks like a throttle. And they use them constantly to control pretty much everything.  

So I really think it is time to use some of this IoT stuff for model railroading. Let’s create the IoTT, the Internet of Toy Trains.  

In this video I am going to create a gateway from a Digitrax LocoNet Network to the Cloud. This will enable us to send and receive messages to and from as many devices we want to the layout control system. 

Hello YouTubers and Welcome to the Internet of Toy Trains. I am Hans Tanner and here is a new episode of IoTT with fresh ideas about how to use the Internet of Things along with sensors and micro controllers to control a model railroad layout. So, Get on board, the train is leaving the station. 

The big advantage of feeding data to the cloud is that it becomes available immediately to all devices that are connected to that cloud. So, connected devices like cell phones, tablets, smart watches or computers all of a sudden become universal control devices for the layout. 

In the last few years, there has been a tremendous development going on in the field of IoT and Home Automation. People are getting used to control appliances and lights or stereo systems via voice commands. Popular devices are SmartThings Hub, Amazon Alexa or similar. But no one is using this technology to say things like: OK Google, set switch 4 to thrown. Well, I do, and in this video I am going to show you how to start using these technologies on your layout. 

The concept to do so is rather simple and makes use of well-known building blocks from the IoT technology. The good thing is that all required tools, programs, and communication protocols are in the public domain with no license fees. Furthermore, there is a huge community and literally thousands of YouTube videos showing how these tools work, how to install and how to use them. 

So, here’s how it works: I am going to build a small WiFi interface that is connected to the Digitrax system’s LocoNet and listens to the messages. When a message is received, it is then sent via WiFi to a server or message broker. Devices that would like to receive the messages can subscribe to the server for the topics of interest. 

As always, the first step is to develop the schematics for the interface. I am using EasyEDA for my electronic design work. This is a free, web based tool that is quite powerful.  

The LocoNet Interface consists of two parts, a micro controller with WiFi support, and a few components to convert the LocoNet voltage levels to the 3.3V logic of the controller. Several schematics for the basic analog part can be found on the internet as other people have built LocoNet interfaces before, so I did not reinvent the wheel. I used an LM311 comparator for the Receiving signal and an 8050 transistor for Sending. As controller, I am using an ESP8266 mounted on a NodeMCU development board. This device has everything needed, including Wifi, and much more computing power than required, but as these modules are now available for about US$ 4.—I don’t complain about that. The nice thing is that this device can be programmed using the very popular Arduino IDE.  

Once the schematics were done, I built a first prototype on a bread board. Note that I used trimmers instead of individual resistors as voltage dividers. This allows me to easily play with various resistor values in case the circuit does not work as intended from the beginning. I also prefer to build the prototype with the components I will use on the final board, so even on the bread board I used an SMD LM311 mounted on a small adapter board.  

Of course, as you can imagine, the first version of the circuit did not work, so I had to go through two or three iteration cycles between drawing and bread board along with software development until I was seeing the messages flow in the terminal window. 

Having a functional prototype on the bread board, I used EasyEDA again to convert the schematics drawing into a 2-sided PCB. The software comes with an auto router that automatically creates all the copper tracks needed on the board. I also placed a copper shield on the back side of the PCB, except in the area of the WiFi antenna. 

This was really the first time I created a PCB myself and I found the process quite simple.  

After completing the PCB design, I was able to directly create a Gerber file and transfer it to the ordering page from JLCPCB, a Chinese PCB manufacturer. The minimum order for this board was 10 pieces, which cost $2.00 plus $18.00 for shipping. It took about 10 days and the actually quite high quality PCBs were sitting on my desk, ready for assembly. 

Soldering the components on the PCB was actually easier than expected. As this was also my first PCB using SMD components, I spent some time watching a few YouTube videos to bring me up to speed on the subject. Then I applied soldering paste to the pads and placed the components. The most important thing here is not to use too much paste. The placement of the components on the other hand allows for some tolerance as they will be pulled into the right place by the melting solder. 

As to be expected for a first prototype, I overlooked some errors when designing the PCB. Stuff happens, I guess.  

The first error was a pin mismatch in the component library of EasyEDA, causing a wrong connection to the base and emitter pins of the transistor. To fix this, I had to cut two copper traces and use two thin wires to fix the problem.  

The second error was that I miscounted the spacing between the two rows of pads for the processor socket. That I was able to fix by bending the pins of one socket and solder them on the top side.  

Finally, the RJ11 connectors I ordered were bottom latch instead of top latch, which means that the pinout was mirrored, so I had to mount them on the bottom side of the PCB. As a result of this, the total height of the PCB was much more than expected, but hey, that’s how we learn, right? 

Finally, it was time to connect the board to LocoNet and mount the processor board to the socket. Also no real surprise, it did not work the first time and I had to do some debugging. It turned out that I applied too much soldering paste and there was bridge between pins 7 and 8 of the comparator. A quick touch with the soldering iron to remove some solder fixed the problem and the board was working nicely, as indicated by the blue LED that is on when LocoNet is active. 

So, let’s have a quick look into the firmware sketch that makes everything work. Again, to keep the video short, I do not explain the sketch in detail, but I uploaded it with all needed libraries to my GitHub page, so that you can play with it. I also left a lot of comments in the software to make it easier to understand. 

One library to point out is LocoNetESPSerial. This is a modified version of the SoftwareSerial library for the ESP8266. I changed it so it works for half duplex communication, i.e. I added collision detection and CD backoff timeout as specified in the LocoNet PE document from Digitrax. 

Communication on the WiFi side makes use of ArduinoJSON to create the payload data and PubSubClient to send it to an MQTT server. 

I also included a web server including an AJAX mechanism to change the configuration of the Gateway. I plan to explain that further in one of the future videos, so stay tuned. 

Now you wonder where all the LocoNet messages are being sent to? Well, here at home I send them to my home automation server, which is a Raspberry Pi 3 that handles all messages from my home sensors, lights, etc. as well. The Raspberry Pi is a small computer for about US$ 50.00 that can run everything needed for our purposes, and more. It is connected to the home network either via WiFi or cable connection. I will talk more about that part in a future video. 

Since I know you really would like to see the LocoNet messages flowing from my test layout, I installed a second gateway. You remember I had 10 PCB’s made. The second gateway is sending the messages to a public MQTT broker. If you want, you can connect to that broker and subscribe for the messages from my layout. 

Here is how you do that. I explain it using an Android phone, but I’m sure it works in similar ways if you use an iPhone. 

First you go to the Google Play Store and install an app called MQTT Dashboard. Once installed, open it up and click the plus sign to install a server connection. Write something like LocoNetMQTT into the Client ID field. This is just a name and it does not matter what you put in. The Server field is next, and this is important. Enter broker.HiveMQ.com as name of the public server I am using. And in the Port field add 1883 as port number. 

When done, click Create and the new server connection will show in your display. 

Now you need to specify the message topics you are registering for. 

The first topic of interest is the ping topic. My gateway broadcasts a ping message every 30 seconds, with some internal status data which has nothing to do with LocoNet, but with the operation of the gateway. To register for that, click the plus sign, than add a friendly name, e.g. LN Ping or similar. Then enter lnPing in the Topic field. Note that this is case sensitive. Then click create and you are a subscriber of the lnPing topic. Now do the same for LocoNet In messages. This is the topic that is used to broadcast everything that is received from LocoNet. The topic name is lnIn. This is small l small n capital I small n. Again, click create. Now watch. It should not take too long until you see some messages appear. You will always see the last message coming through, and to see the history, you simply double click the topic name. 

Now, is that cool or what?