Control model railroad with Wifi and Mqtt

  • What is that?
    Well, Wifi communicates without wiring and Mqtt is the way things talk to each other.
    It is a smart, flexible and cheap way of controlling almost everything on a model railway.
    Most known in the popular home automation systems but it can of course be used anywhere.
    It can be connected to internet, but it can also be used in a closed, local network.
    A wireless router is needed to provide wifi and if there isn't any wifi where the railroad is,
    or if it not a smart idea to interfere with the rest of the family's web activities,
    a separate router doesn't cost much.


    There is a lot of Youtube videos on how to use this on a model railroad and I just want to show
    how I do to make things work the way we want to.


    I don't have any model railroad myself but my brother is building a new layout with Lenz track
    and Lenz DCC turnout motors, and I have been given the task to get everything running.
    As we don't live in the same town, I am building the electric parts in my home and visit him
    once a week for testing and mounting.


    The room he is building his layout in is about 7 x 3,5 meters, with a track in to another room where there is a small yard and a workshop.
    There will be four separate trackplans and we will not sit and drive, we will walk around with wireless remotecontrols.
    There will not be any automated driving, everything is done manually but with wireless remote control.
    This is the way we want to do it.


    We use the existing wifi in the house, with a range extender in the room where the railroad is.
    The base in Mqtt is something called a "broker", everything goes thru that broker.
    The "broker" is a computer program and for that we need some kind of computer where this can be installed.
    It is possible to use a standard PC, but that is not a good idea. It can be used for testing.
    We use a Raspberry Pi 3+ with built in wifi. It cost about €40 (+ €20 for power adapter if you don't have one) and
    that is the most expensive part in this system. A small SD-card is also needed.



    There is many installation guides on how to install the operating system on the Raspberry, so I wont go in to that,
    but it is quite easy to follow the instructions on Raspberrys website, Raspberry Pi .
    Make sure to make the right wifi-connections.


    Then we need an Mqtt-broker, that is the only program (apart from the operating system) that we need to install.
    We use a program called Mosquitto, it's free and it is probably the most popular, but there is other choises.
    After installing Mosquitto that part is done and we don't have to touch that anymore.


    As I have a couple of 3D-printers I printed out a small case, there is many to choose from on Thingiverse.



    Then there is just one important thing we have to do, the Raspberry must have a static/fixed adress (ip-number) in the router.
    So we are logging in to the router and in the LAN-settings we choose DHCP and assign a static adress.
    How this is done can vary in different routers but this must be done.
    It is also a good idea to write this IP-adress down, because it will be needed later.


    And it is ready to run.


    To be continued…..

  • Got some interesting information from a member at another forum.
    He said it is possible to use the Raspberry Pi for creating a local network, then you don't need another router.
    I have not tested that method but I might do it later. Raspberry Pi and Linux is new to me.


    Well, then there is two ways of fixing the wifi stuff :)

  • I use an ESP (vemos d1 mini) and a H-Bridge, simulating a Z21, for full DCC control and steering via Roco Z21 App. The whole costs under 5 € and is 2,5 *2,5 * 3 cm. You can even build it into a loco with accus, and can thus be full independent of the rail voltage.


    BR Zoltan

    LG Zoltan von der StEAG
    (Somestaler Eisenbahn-Aktiengesellschaft), BW Traktion Langkatzenhofen
    SzVVT (Szamosvölgyi Vaspálya Társaság), Hosszúmacskási Vontatási Telep
    Schaut bei meiner "echten" Dampflok 52.4984 vorbei!
    Und hier ist mein YouTube Kanal mit meinen (Bahn)filmen
    :)

  • Interesting thing, Zoltan. Can you tell us more about that?


    Anyway, we are using DCC++ to get the DCC-signals out on the track. It's a cheap and clever
    open source project based on Arduino. I put some links here if you feel like diving in to it.
    DCC++ on trainboard
    DCC++ on Youtube


    We use an Arduino Uno as BaseStation Master to create DCC-signals.


    On top of that we put an Arduino Motorcontroller to get the power to the rail.


    And on top of that is a prototype board with a step down regulator and a Wemos D1 mini for handling Wifi and Mqtt.


    The step down regulator takes the input voltage (16V DC) from the power source and feeds all electronic
    on this stack with 5V. Thus we just need one power source.


    The Wemos D1 mini subscribes to all Mqtt topic/messages related to DCC (that is Track power, Cab (Throttle and F-functions) and Turnouts)
    translate and send them via serial command (TX) to the Arduino Uno/BaseStation Master (RX), which creates and sends out DCC signals to the rail via the Motorcontroller.


    This is probably not for anyone to understand, but if you have been playing around with Arduino
    then this is really something to look into. Take me, for exampel, I have never worked with It and programming.
    It is just a hobby.


    To be continued....

  • Thanks for the links.
    I am not really deeply interested, for I do not have an own track but only a single loco to drive as a guest somewhere.
    But who knows.
    For my "thing" see this forum where it goes all about that.


    BR Zoltan

    LG Zoltan von der StEAG
    (Somestaler Eisenbahn-Aktiengesellschaft), BW Traktion Langkatzenhofen
    SzVVT (Szamosvölgyi Vaspálya Társaság), Hosszúmacskási Vontatási Telep
    Schaut bei meiner "echten" Dampflok 52.4984 vorbei!
    Und hier ist mein YouTube Kanal mit meinen (Bahn)filmen
    :)

    Edited 2 times, last by Zoltan ().

  • Thank's for the link, Zoltan. My german is unfortunally not good enough to understand all that, but
    I think that it is something else then what we are building her.



    As I mentioned in post nr 2 I got a tip from a member at another forum that a Raspberry Pi could create a local network by itself.
    It sounds interesting so I thought I must try that. Ordered a new Raspberry Pi Zero W, it cost around €12 and an original case €6.
    You also need a micro SD-card and a power source. Thats all.



    It is really small and the original case has no screws.



    When it's all done it look like this, network and Mqtt broker in one.




    Then I used the following steps.
    First of all we need to download an Operating System.
    I chose Raspbian Buster with desktop, it is easier and we don't need the recommended software:
    https://www.raspberrypi.org/downloads/raspbian/


    To get the Operating system on to the SD-card I used a program called balenaEtcher:
    https://www.balena.io/etcher/


    Put the SD-card into the card slot on the raspberry Pi and connect monitor, keyboard and power.
    When everything has started we just need to choose country and connect to a network.
    Let the Pi update itself.


    Then comes the fun (not) part, you have to manually write a lot of stuff in the command line to set up a local network.
    Just follow these instructions down to "Start it up" just over "Add routing and masquerade":
    https://www.raspberrypi.org/do…/wireless/access-point.md


    When everything is working and checked we just need to install Mosquitto, but that is a piece of cake.
    I used this link, but there are many others:
    https://appcodelabs.com/introd…server-using-raspberry-pi


    Scroll down to "1. Install the mosquitto MQTT Broker" and write the text in the black box.
    Then the same with "2. Enable the mosquitto broker" and it's done.



    If you, like me, is not familiar with Raspberry Pi and Linux it is a pain in the ass to do all this.
    But when it is done and it works, it is worth it. Then we don't need to trouble ourself with
    the existing wifi network, we have a dedicated network just for our model railroad. :thumbup:



    To be continued…..

  • Hi rcmodeler, the pointed forum talks also english, if you would have any question.


    As for the ESP: yes, my thing is a bit different. I drive a single loco.


    The ESP does not need an Arduino or RPI to throw up an own WLAN AP, with Z21 simulation, where you directly can connect to with the Roco Z21 App and drive your loco.


    I build the D1 mini and a miniature H-Bridge (1" x 1" x 1" complete, alltogehter under 5 €) into the loco together with a battery, the output of the D1 goes via the H-Bridge direct to the decoder and so I am independent from the track voltage (loco does not even have contact to the rails). Thus I can drive it with the smartphone app (even on the floor w/o rails...:)).


    I needed this for I hate rail voltage problems (and I also do not have such for I am driving live steam except my NOHAB Loco who but goes direct via D1 WiFi DCC, so w/o rail power).


    Best wishes for your project!


    (BTW I am on Linux and for me Windows would be a (double) pain in the Ass... :D)


    BR Zoltan

    LG Zoltan von der StEAG
    (Somestaler Eisenbahn-Aktiengesellschaft), BW Traktion Langkatzenhofen
    SzVVT (Szamosvölgyi Vaspálya Társaság), Hosszúmacskási Vontatási Telep
    Schaut bei meiner "echten" Dampflok 52.4984 vorbei!
    Und hier ist mein YouTube Kanal mit meinen (Bahn)filmen
    :)

  • Now that we have a DCC++ Base Station Master that can generate DCC-signals to the track and the little device on top that can communicate via wifi and mqtt,
    we need something that can send mqtt-messages so we can get things working. I made a wireless remote control about a year ago that i have showed in another thread.

    It is basicelly a (little different) rc-transmitter that I use for cars and trucks, but it works fine for trains to. So we are going to use this until I get the
    time to build a new one, that is wifi-mqtt ready.


    To be able to use this old remote control I built a "translater" to the new system, RC - mqtt. Here is a little picture that shows how it is connected.

    I used the old code in the Arduino Nano, that was connected to DCC++ base station. That is why it sends DCC-commands to the ESP32.
    So the only new code is in the ESP32 that converts DCC-commands to mqtt messages.


    This is, of course, not a smart soloution. Rather a quick fix to get things running.
    And, beleive it or not, it works just fine :)


    To be continued….

  • There is going to be a lot of signals on the layout and they will be controlled by the same type of Micro controller (Wemos D1 Mini).
    As we live in Sweden we try to make the layout as Swedish as possible, but there is not much Swedish things to buy in this scale.
    So we try to make things our self. I made a cuople of signals in Fusion 360 and printed them in the resin printer, Anycubic Photon.
    How the Swedish signals look like and work can be found here (in Swedish) :D
    http://www.jarnvag.net/banguide/signaler
    And here is my first attempts as signal-maker, a 4-led signal:



    and a small shunting(?) signal:




    They will be controlled wireless via Mqtt as the rest of the layout, and will only need power, 5V.
    And this is the Little thing that make it happen:


    It controls the two signals above, each Led separately with PWM and flashing if needed.
    Might do a little video of working signals and perhaps a schematic of the controller.


    To be continued….

  • Made a schematic in Fritzing so that it is a little easier to understand.
    There is not many components on this small board but it can do a lot of things.
    This is for the four-led main signal (Signal A / S10) and the shunting signal (Signal B / S11) from the post above.



    And when it is mounted under the layout it looks like this



    Going to re-route some wiring later to tidy things up.



    To be continued…..

  • Almost all turnouts on the layout (around 50) is equipped with Lenz DCC-motors, and is controlled by the Mqtt-DCC that is shown in post nr 4.
    But for double crossings Lenz motors are not ideal, since the stroke/travel distance seems to be to short.
    Then it is better to use a servo and there is plenty of those to buy. They are often mounted under the layout but I took another approach.
    Using a small, linear servo it is possible to put it beside the turnout/csossing. That is an advantage if there are several levels on the layout.


    I ordered a bunch of small 1,5g linear servos for micro rc-planes for less then €5 each.
    Then I draw some mounting parts in Fusion 360 and 3D-printed them.


    The first version came out well and look like this:


    Mounting is the same as Lenz motors, here tested on a turnout.


    Made a new version that look like this:







    For double crossing we had to cut off the original "switch box" and replace the sleeve that goes under the switch blade (?).
    Here is a part of the original and the new one I printed:


    Original:


    New version:



    And then we can mount the servo in the same way as a turnout:


    And is, of course, controlled by a Wemos D1 mini. It can Control up to 8 servos, or 4 servos and 4 relays
    to control electrofrog, if you are using that.


    Time and stroke/travel can be set individually for each turnout.
    Everything goes via wifi/mqtt so the operator doesn't need to now which turnout
    is equipped with DCC-motors and which is servo.


    Made a short video of it in function:
    Youtube video



    to be continued…..

  • When I'm building and programming the pcb's with the microcontrollers it is nice to have something to test that they work.
    As I don't have a layout at my place, just 1 meter test track, I made a small PC-program in Visual Studio.
    I could not get mqtt to work directly in the pc-program so I connected a Wemos D1 Mini to a USB-port, to handle mqtt communication.
    This is, of course, not the best solution but this program is just for troubleshooting, testing and development.
    It is not ment to be used driving on the finished layout.


    This is the nifty little thing that translates serial data from my PC-program to wireless mqtt messages:




    The pc-program subscribes to/reads all messages and can send all types of messages. It lookes like this:



    connected to mqtt broker (raspberry pi) and Track power on:


    if I want to put turnout 10 in to straight position the program sends a message "ft/turnout/10;1",
    and turnout nr 10 is subscribing to messages with topic "ft/turnout/10". It then sets in position 1, straight.
    Then it sends a reply "ft/re/turnout/10;1" so that, for example, that can be seen on a track plan, or something.


    Same thing with signals, house and street lights, etc.
    Send a message what you want it to do and it answers when it is done.
    In this case I don't have signal 10 connected to the system and then I don't get any answer.



    I can decide what to name everything, "ft" is just short for "flextrain".
    I am also testing driving multipel locos as you maybe guessed by the "Locomotive" pictures.
    It seemes to be working fine. I have upgraded the pc-program a bit, but not made any new screenshots.


    Next week I'm hoping to make some videos, if everything works.

  • Moving on with ir-detectors. We now have a couple of them mounted and the first thing we are going to use them with is a road crossing.
    I've made a test setup for a light and sound crossing just to se how it works. The test setup looks like this:


    It is my favourite microcontroller Wemos D1 mini, Mp3-player with SD-card and speaker, one white LED and two red LED.
    The soundfile on the SD-card is downloaded and probably american. Going to record one in Sweden in a couple of weeks.
    The soundfile is trimmed in start and end, and goes in a loop.


    The ir-detectors messages can of course be used in many applications, this is a great advantage with mqtt.


    A small video of this test drive can be seen here:
    Road crossing test

  • As I mentioned earlier, the pc-program can control multipel drive locomotives.
    It is just a test, I don't like driving with a laptop.
    But I need to figure out a smart way to get the principal into a handheld, wireless controller.
    Anyway, made a little video on the first multipel testdrive.
    Multipel drive

  • Upgraded the wireless sender with new microcontroller.
    The old one was an Arduino Nano (blue) and the new one is ESP32 (black)


    ESP32 has a lot more memory, is much faster, has built in wifi and bluetooth and sd-card reader



    The only extra thing in the box is a voltage converter to feed the microcontroller and display from the DeWalt battery


    USB-port for uploading new program and SD-card (for future functions) is accessible from outside


    And as I was at it, I made three :D


    They are identical except for their "device name", sender 1, 2 and 3


    and can be used independent or all at the same time, there is no master or slave.
    When startpicture shows it is connected to the mqtt broker


    Everything else works the same, for now. There will be more functions later on.


    Tested and working fine :)


    Now summer is knocking on my door and a lot of work is waiting outdoors ;(

  • As I don't have any stationary layout at home I've started to build some modules in scale 0,
    and maybe attend to some meetings in the future. At those meetings they have another system
    for dcc control but at home I would like to use my wireless system.

    I have a lot of battery powered machines and thus I also have a lot of batteries,
    so I made a battery powered DCC++ master station with wifi and mqtt so
    I can use my wireless controls.

    Made a short (bad) video on how it works:

    youtube

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!