You are not logged in.

rcmodeler

Schrankenwärter

(21)

  • "rcmodeler" started this thread

Posts: 37

Location: Sweden

  • Send private message

1

Friday, November 1st 2019, 1:51pm

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…..

6 registered users thanked already.

Users who thanked for this post:

barilla73 (01.11.2019), AliSpur0 (01.11.2019), werkzeugmacher (02.11.2019), Altenauer (02.11.2019), derfranze (06.11.2019), Udom (10.11.2019)

rcmodeler

Schrankenwärter

(21)

  • "rcmodeler" started this thread

Posts: 37

Location: Sweden

  • Send private message

2

Monday, November 4th 2019, 5:02pm

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 :)

Zoltan

Bremser

(21)

Posts: 45

Location: Wien

Occupation: Dampflokmechaniker in Pension

  • Send private message

3

Monday, November 4th 2019, 6:09pm

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
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 :)

rcmodeler

Schrankenwärter

(21)

  • "rcmodeler" started this thread

Posts: 37

Location: Sweden

  • Send private message

4

Friday, November 8th 2019, 10:10pm

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....

5 registered users thanked already.

Users who thanked for this post:

barilla73 (08.11.2019), jbs (09.11.2019), KBS 249 (09.11.2019), Zoltan (09.11.2019), Udom (10.11.2019)

Zoltan

Bremser

(21)

Posts: 45

Location: Wien

Occupation: Dampflokmechaniker in Pension

  • Send private message

5

Saturday, November 9th 2019, 9:11am

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
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 :)

This post has been edited 2 times, last edit by "Zoltan" (Nov 15th 2019, 5:04pm)


rcmodeler

Schrankenwärter

(21)

  • "rcmodeler" started this thread

Posts: 37

Location: Sweden

  • Send private message

6

Friday, November 15th 2019, 5:02pm

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/documentatio…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/introduction-to-…ng-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…..

Zoltan

Bremser

(21)

Posts: 45

Location: Wien

Occupation: Dampflokmechaniker in Pension

  • Send private message

7

Friday, November 15th 2019, 5:17pm

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
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 :)