A Home Wi-Fi Connected Garden Railway

What is your latest project?
Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Fri Mar 09, 2018 2:39 pm

The following posts detail my winter project, updating my Garden Railway locos with the latest single board microcontrollers, all connected and controlled via a local wi-fi network.

As the "Why Live Steam?" thread shows, we are all different in our approach to this hobby. I came into Garden Railway with an electronics background. I have tried modelling, but my kit built coach and station proved less than satisfactory, so I have therefore decided to stick to what I know and enjoy doing. This initially involved updating my electric and steam locos to battery remote control using a Spectrum controller and separate commercially available ESP/Radio Control units. This was updated to a system using the Arduino range of microcontrollers a few years ago. After recently completing a home automation project, I've started to update everything with the same technology.

Overview of System

The system relies on each component, locos, hand-held controller and in the future any lineside/signalling systems, being connected to a local wi-fi network. I'm using my home network, extended into the garden but it could just as easily operate on a separate network.

Each device uses an ESP8266 based microcontroller module, widely used by hobbyists in robotic and home automation systems. They are cheap, have built-in 2.4Ghz wi-fi support and are far more powerful and smaller than any of the Arduino range. I use the Wemos D1 mini pro, which costs a little more than the basic Wemos model but has an external aerial connection. This will be needed when fitted to locos manufactured in metal. It can be easily programmed using the Arduino IDE or other similar software and a basic program to control 2 servos on a steam train is just 60 lines of code. An important feature of the ESP8266 software library is an option for 'over the air programming' which means that once installed in a loco, it doesn't have to be taken apart when changes to the program are needed.

wemos2.jpg
wemos2.jpg (36.87 KiB) Viewed 8598 times

Picture shows the control board for a typical steam loco showing the Wemos D1 mini pro with a 5v step-up regulator, temperature sensor and connectors for two servos and a 3.7 volt lithium battery.

Once connected to the network, there are various ways of sending messages between all the devices. Based on my home automation experience, I'm using the messaging protocol MQTT. This requires an MQTT server which in my case is located on a Raspberry Pi Zero W. Unlike typical loco control systems, messages can also be sent from each device, and between each device, allowing useful information from the locos to be available to the user, like battery voltage, location, actual speed, etc.

An ESP8266 based hand-held controller with a tft colour display and a PC/tablet/phone program provides the control.

control.jpg
control.jpg (33.53 KiB) Viewed 8598 times

I'm not used to writing up things like this so comments and any questions are most welcome.

Michael

Next: Bachman Lyn converison

Big Jim
Moderator
Moderator
Posts: 2694
Joined: Sat Jul 06, 2013 5:28 pm
Location: Near Llanelli

Re: A Home Wi-Fi Connected Garden Railway

Post by Big Jim » Fri Mar 09, 2018 4:20 pm

Wow, thanks for this Micheal.
I confess this is far beyond my understanding at the moment, but I find it fascinating. I am glad that someone who knows what they are doing I able to explain it in, for what seems to me, simple terms.
I look forward to the next instalment.
If at first you don't succeed, use a bigger hammer!

User avatar
ge_rik
Administrator
Administrator
Posts: 6497
Joined: Sun Oct 25, 2009 10:20 pm
Location: Cheshire
Contact:

Re: A Home Wi-Fi Connected Garden Railway

Post by ge_rik » Fri Mar 09, 2018 6:01 pm

Sounds fascinating. Just wondering what sort of cost is each unit?

Rik
------------------------
Peckforton Light Railway - Blog Facebook Youtube

Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Fri Mar 09, 2018 7:18 pm

Thanks Jim and Rik for your replies. The cost of the printed circuit board above used to control my Accucraft Lyn loco was around £13 from UK suppliers, £7.30 for the microcontroller and £3.30 for the step-up regulator. Below £10 if not using the one with an external aerial socket and/or buying on Ebay from China/Hong Kong.

Michael

metalmuncher
Cleaner
Cleaner
Posts: 95
Joined: Sat Oct 20, 2012 4:15 pm

Re: A Home Wi-Fi Connected Garden Railway

Post by metalmuncher » Fri Mar 09, 2018 7:50 pm

Very interesting, thanks for sharing. That MQTT protocol is super easy to set up, got a Raspberry Pi and my machine talking to each other in a few minutes. Just ordered an ESP module now for further experimentation.

The messages seem to be just strings - I guess you have your own format that packs the signal strength, speed, temperature, etc into the message? I would also be interested to see how you have your 'topics' set up. Each loco has its own control topic, or all locos subscribe to a global control topic, with an address in the message that the loco can compare with its own in order to tell if the command is intended for itself?

Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Fri Mar 09, 2018 9:16 pm

Yes, MQTT is very simple to understand, to use, and to get working on a Raspberry Pi and ESP8266.

For simplicity, I use separate messages for each function and each loco only subscribes to it's own named topics. Topics are a header followed by the loco name followed by the message. For example, railway/lew/speed, railway/lew/temperature, railway/bachmannlyn/direction etc.

I use mqtt-spy on a PC for testing.

Michael

DG
Cleaner
Cleaner
Posts: 74
Joined: Thu Mar 23, 2017 7:02 pm

Re: A Home Wi-Fi Connected Garden Railway

Post by DG » Sat Mar 10, 2018 7:08 am

Michael can only agree with what Jim said "Wow" you have been very busy, it looks great.

I would be very interested in learning more how you use this infrastructure to actually run your trains. For instance do you have multiple trains on the line and use this to control them?

This infrastructure would be a great base for two other ideas I have been thinking about. Running trains on your own garden railway from an armchair when it is cold or raining, you could view and control on a PC and other screens. Opening up your garden railway to others to do same via the Internet. This may be another way for like minded people to meet.

Great stuff, you are breaking new ground and I would be very interested in reading more, have you considered doing an article for one of the mags?

Dave

User avatar
Peter Butler
Driver
Driver
Posts: 5219
Joined: Sun Sep 09, 2012 10:33 pm
Location: West Wales

Re: A Home Wi-Fi Connected Garden Railway

Post by Peter Butler » Sat Mar 10, 2018 10:31 am

DG wrote: Sat Mar 10, 2018 7:08 am Opening up your garden railway to others to do same via the Internet. This may be another way for like minded people to meet.
I hope this will not sound too pessimistic but it is the way I view the modern world.

Technology is moving forward rapidly and electronics in particular open up a whole new world to many. For dinosaurs like me it is a complete mystery and one I have no wish to become involved with. My only exception to this is the use of RC for battery powered stock for motion only!
My concern is the way Internet contact is now taking over from personal interaction and if this system becomes widely available there will be no need to 'meet' as mentioned in the above quote.
Already sports and hobbies are in decline by the younger generation who prefer to watch virtual reality on a screen than experience life in the great outdoors.
In a worst case scenario we modellers, already a secretive bunch, will stay at home behind closed doors (or gates!) with our own miniature world surrounding us, operating garden railways around the world without even knowing who owns them or who is behind the controls.
What opportunity, or need, would there be for 'like minded people' to meet then?
Of course this would not become the norm..... or would it?
The best things in life are free.... so why am I doing this?

User avatar
Soar Valley Light
Driver
Driver
Posts: 1451
Joined: Sun Dec 08, 2013 5:18 pm
Location: North West Leicestershire

Re: A Home Wi-Fi Connected Garden Railway

Post by Soar Valley Light » Sat Mar 10, 2018 8:12 pm

I'm with you all the way Peter :thumbup: .

Dinosaur I may be but basic skills and personal interaction with other dinosaurs is what I enjoy.

That's not to detract for the more technically gifted, I'm genuinely in awe of what is achievable (and achieved) by more technically minded souls. :shock: However, for someone who can't change channel on the telly if his partner is out :oops: , the technicalities of it all are a mystery! :scratch:

Andrew
"Smith! Why do you only come to work four days a week?
"'cause I can't manage on three gaffer!"

User avatar
Peter Butler
Driver
Driver
Posts: 5219
Joined: Sun Sep 09, 2012 10:33 pm
Location: West Wales

Re: A Home Wi-Fi Connected Garden Railway

Post by Peter Butler » Sat Mar 10, 2018 8:38 pm

Thank you for your support Andrew, for a while I thought I might be out of step with progress but clearly there is a small group of dinosaurs still on the planet!
Our local model club is made up of approx. 30 members, about 25 of whom are senior or retired, the remainder still working (50+) and only one junior! I fear this will increasingly be the pattern for the future if more gadgets are introduced to encourage youngsters to stay at home watching TV.
Personal interaction is an incredibly important life skill, one which is sadly becoming redundant.
The best things in life are free.... so why am I doing this?

User avatar
philipy
Moderator
Moderator
Posts: 5033
Joined: Sun Jan 30, 2011 3:00 pm
Location: South Northants

Re: A Home Wi-Fi Connected Garden Railway

Post by philipy » Sun Mar 11, 2018 6:06 am

Peter Butler wrote: Sat Mar 10, 2018 8:38 pm Thank you for your support Andrew, for a while I thought I might be out of step with progress but clearly there is a small group of dinosaurs still on the planet!
Hi Peter, Philiposaurus speaking!!! :D
Seriously though, I can totally appreciate the mental satisfaction of getting this type of system designed, built, installed and operational, although it's beyond me ( I've had several serious attempts to get to grips with Arduino, but a flashing LED is as far as I've managed to get before ripping my own head off!) . However having done all that I'm left wondeing "Why bother?". If you need complicated electronics to tell you where the loco is and what speed its doing, presumably that means you can't see it, in which case you are more or less just playing Pacman!
Philip

DG
Cleaner
Cleaner
Posts: 74
Joined: Thu Mar 23, 2017 7:02 pm

Re: A Home Wi-Fi Connected Garden Railway

Post by DG » Sun Mar 11, 2018 6:30 am

Peter I totally agree that personal interaction is important. I think this is a great area of debate and I could start a thread in the off topic section if there is an appetite for it. As a quick reply though, I think that there is room for everyone and developments like this may be a way to inspire and attract the younger folk and others into the hobby and keep them interested in the long run in the same way that viewing a layout and getting hands on at a show might.

Michael apologies my reply sent your thread off course and I am sure that many others will be like me, keen to know more about your system.

Dave

User avatar
LNR
Driver
Driver
Posts: 1533
Joined: Sat Feb 27, 2016 5:26 am
Location: Australia

Re: A Home Wi-Fi Connected Garden Railway

Post by LNR » Sun Mar 11, 2018 7:46 am

Well I'll stick myself in the dinosaur category as well, and while there is perhaps some relevance in attracting younger people to the hobby, I would query which hobby your attracting them to, electronics/computers or building a model railway. Perhaps it is an entirely age related thing.
Grant.

Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Sun Mar 11, 2018 8:44 am

No problem Dave, I knew there would be some lively interesting debate at some point! Luckily we can pick and choose which technologies we choose to use.

I'm away for a long weekend visiting two very elderly Mums so will replay to the above comments and continue the article when I'm back home.

Michael

User avatar
tom_tom_go
Driver
Driver
Posts: 4824
Joined: Wed Feb 23, 2011 3:08 am
Location: Kent, UK
Contact:

Re: A Home Wi-Fi Connected Garden Railway

Post by tom_tom_go » Sun Mar 11, 2018 9:36 am

Can we keep this thread on topic and let Michael go about explaining what he enjoys doing with his railway.

We are a broad church here and welcome all types.

We have an off topic section for debating, please use it.

Mod hat off, now back to the trains.

User avatar
Soar Valley Light
Driver
Driver
Posts: 1451
Joined: Sun Dec 08, 2013 5:18 pm
Location: North West Leicestershire

Re: A Home Wi-Fi Connected Garden Railway

Post by Soar Valley Light » Sun Mar 11, 2018 5:49 pm

tom_tom_go wrote: Sun Mar 11, 2018 9:36 am Can we keep this thread on topic and let Michael go about explaining what he enjoys doing with his railway.

We are a broad church here and welcome all types.

We have an off topic section for debating, please use it.

Mod hat off, now back to the trains.
You're quite right Tom, it has been a bit of a hi-jack! Apologies to Michael.

Andrew(osaurus!)
"Smith! Why do you only come to work four days a week?
"'cause I can't manage on three gaffer!"

User avatar
RylstonLight
Trainee Fireman
Trainee Fireman
Posts: 209
Joined: Fri Oct 26, 2012 3:10 pm
Location: Pontefract West Yorkshire

Re: A Home Wi-Fi Connected Garden Railway

Post by RylstonLight » Mon Mar 12, 2018 1:45 pm

Hopefully more or less on topic, in the original post Micheal, you refer to controlling signals etc.

I have dabbled in microprocessor control (albeit Picaxe and arduino) and tested them outside. The ultimate plan was to control lighting, points and signals.I am worried about the longevity of the microprocessors (any any servos) outside. I tested one in what I thought was a sealed environment. All it controlled was a simple LED but after a few months it failed, I assume due to water ingress. Have you any ideas on this or do your plans for infrastructure control avoid getting the microprocessors damp in any way?

Andy
Andy S. at the Rylston Light Railway

User avatar
tom_tom_go
Driver
Driver
Posts: 4824
Joined: Wed Feb 23, 2011 3:08 am
Location: Kent, UK
Contact:

Re: A Home Wi-Fi Connected Garden Railway

Post by tom_tom_go » Mon Mar 12, 2018 2:07 pm

Hi,

I use an Arduino outside for R/C point and signal control and not had any problems with the weather.

The Arduino lives in an ice cream box inside a Plastikard building. I run it off batteries at the moment but would like to run a 'bus' power cable along the line to power it and any future servos or lights.

The servos have no special housing and simply screwed to the track base and protected from the elements that way.

It's all in my RWLR thread to read about.

Michael's stuff is far better than my efforts so interested in hearing more.

Tom

Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Mon Mar 12, 2018 4:09 pm

My initial plan was to update my locos and get them working with the hand-held controller (one train at a time) similar to the way any other remote control system operates. Whilst writing the software I realised the many other things I could do with this system and my plans were continuingly being updated. So the controller can now handle more than one train at once (ideal for my long squashed oval shaped layout) and the trains can run in automatic mode (stopping for a set period at stations, speed changes, reversing, details in forthcoming post).

Location detection and automatic mode can mean more realism, not taking away the fun of manual control. One train on the main loop in auto mode, one doing auto shunting and you manually control the third train.

The PC controller, along with live video feed(s) could if enabled, allow anyone, anywhere to control the trains (like home automation systems) but that is not something I plan to do. Sitting in the lounge operating a train is something I have done but just to prove I could.

As for signalling, this is something that could be added, though I have no plans to do that at present. A couple of signals randomly going red might prove challenging for the driver.

Sorry, I can't comment on weather proofing as I haven't reached that stage yet. I do plan to have my main station "connected", lights, sounds, points but that should have a leakproof roof as I'm not building it :)

Michael

Southern188
Cleaner
Cleaner
Posts: 40
Joined: Thu Jan 11, 2018 8:37 am
Location: Dorset

Re: A Home Wi-Fi Connected Garden Railway

Post by Southern188 » Tue Mar 13, 2018 8:01 pm

Bachmann Lyn Conversion

Whilst the Bachmann Lyn is G scale (1:22.5) and therefore slightly undersized for the rest of my 16mm railway, it's a nice little loco, good value for the money and runs flawlessly on my less than perfectly laid 45mm track. A minor niggle is that the cab and bunker are too high. I tend to use this first to ensure the track is ok and when I'm in the garden, working, eating or drinking wine, and just want a train to run with minimal control. A secondhand model purchased from Ebay was my first loco, later to replaced by a new one, when Bachmann bought out a much improved version a few years later.

lyn_4.jpg
lyn_4.jpg (258.87 KiB) Viewed 9263 times

It is easy to take apart (4 screws) and contains a DCC ready control PCB that sits on top of the motor drive assembly. There are two switches accessible when the front of the smoke box is opened. The original PCB has been replaced with one containing the microcontroller and associated components. Two 18650 lithium batteries are fitted in each side tank along with a 4 cell charger/protection circuit.

lyn_2.jpg
lyn_2.jpg (90.06 KiB) Viewed 9263 times

The component cost of the PCB, which is a typical of that needed for an electric loco, is £12-£18 depending on source of components. Details are:

1) The 3 connectors to the top left are the electric pick up points for the front, centre and rear wheels. These are used to charge the battery either off the track or from a section of the track that is powered, so it can be recharged during use. This also saves having a separate charger socket. The bridge rectifier ensures the polarity is irrelevant.

2) The lower left comprises a 2 way connector to the motor, power MOS FET and relay used to drive the motor. I use this rather than a separate motor driver as from experience these typically (especially type L298) lose to many volts (reduces speed), get hot and wastes battery capacity. However, I haven't looked lately so there maybe a better alternative now.

3) A 4 way connector for power from the batteries in the side tanks and the original on/off switch located in the smoke box.

4) A 5 volt step-down switched mode power supply. I love Pololu products.

5) An 8 way connector and ULN2003 driver for the relay, front lamp and smoke generator, leaving 4 outputs available for future use.

6) A Wemos D1 mini pro microcontroller. There are cheaper alternatives and clones but I have standardised on this one.

Pictures showing the assembly of the original control board and the new one.

lyn_3.jpg
lyn_3.jpg (104.07 KiB) Viewed 9263 times

lyn_1.jpg
lyn_1.jpg (139.68 KiB) Viewed 9263 times

A simplified descriptive flowchart of the software is as follows:

 Power on
 Initialisation
 Front lamp on
 Connect to wi-fi
 Connect to MQTT server
 Subscribe to messages that are applicable (speed, direction etc.)
 Download schedule (for automatic mode)
 Front lamp off (gives visual indication that everything has been setup correctly)
 Start main loop
 Check for incoming messages
   If speed, set loco speed
   If direction, set direction
   If lamp, turn lamp on or off
 If 30 second timer occurs
    Send status, speed, battery voltage, temperature and wi-fi signal strength messages
 Return to start of loop

Between the recent bad weather, I've been testing the Bachmann Lyn on my newly extended track and have been really pleased with the results, sufficiently to start this thread and share my experiences.

Michael

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests