Mark DiVecchio's O-Gauge Train Layouts

1992 Layout Page

2006 Layout Page

2009 Layout Page

P&LE Postcards by Howard Fogg 

Plasticville Buildings

Portable Layout Page

Train Clubs

Bing Track

Remote Train Control Program

YouTube Channel

OOK Radio Support

Technical Videos

3D Prints for my Layout

More RTC Videos

ADPCM - Playing clips from .mth sound files

P&LE McKees Rocks Locomotive Shop
3D Printer Project

White Tower Restaurant
3D Printer Project

RFID Train Detection

Engine and Car Operation
Hints and Tricks

RFID Tag Programmer using PN532

RTC Control Language - Scripting

RTCAdapter  ESP8266 Sketch

More RFID Tag Videos

RTC Control Language - Signaling


RTC WTIU Emulator

This Page last updated on .

I started development of this web page in January 2025. It will be under construction for a while.

On this page, I describe the ESP8266 code for  RTC WTIU Emulator (in sketch RTCWTIU.ino using only built-in WiFI) .

Click here for the copyright notice.

 Background

I've been working on ways to control my MTH trains from my PC for many years. About 8 years ago, I released the first version of my Remote Train Control (RTC) program for the PC. The very first version was for a TIU tethered between the Remote jack on the TIU to a serial port on the PC (or a USB serial port). Next came a radio which enables RTC communicate to over a wireless radio connection to a TIU. After that came the ability for RTC to communicate over WiFi with both the WIU/TIU and the WTIU.

I've written several utility sketches to help me learn and understand how the trains are controlled. For example:
With a lot of effort, I was able to understand almost all of the communications between the Remote and TIU. I am now able to create packets to control the engines. The packets are complete with correct addressing, command syntax and CRC.

I figured this out by examination of the packets that I could sniff using Mike Hewett's original RS-232 interface design and the port settings that he found. Without Mike's insights into the RS-232 data stream, I don't think that I would have been able to get a foothold into this protocol.

So again, I figured this out just by looking at the RS-232 stream over the tether cable. No code disassembly, no logic analyzers, no opening up of Remotes or TIU's.

The sketches originally used USB based serial communications to the RTC program using the Infiduino. Then they moved to the ESP8266 using WiFi commications with the RTC program.
An ESP8266 sketch that emulates a WTIU. This sketch can be controlled by either the App or the RTC program (RTCWTIU)

 The ESP8266

Mike Hewett always seems to be acknowledged on my web pages and he deserves it. Mike learned about the ESP8266 based NodeMCU board and started to use it in his projects. After I saw his success and how easy it was to use, I started to rewrite my TIU Emulator (RTCEngine) sketch to use that device.

The ESP8266 is a small powerful computer connected to a WiFi Radio. They are available from many sources on the Internet. Watch out for knock-offs.


My Ideas

Here is a photo of an ESP8266. I've been using the LoLin version with the CH340 USB chip and one built-in LED on D4. This is all that is needed for the RTC WTIU Emulator sketch.


The ESP8266 that I have all have the LED on pin D4. Some versions of the ESP8266 have the LED on pin D0. This can be powered with a USB cable plugged into a USB port or directly from a 5 volt wall wart.


Parts Sources

Two sources for the ESP8266:

NodeMcu Lua ESP8266 CH340G ESP-12E Wireless WIFI Internet Development Board:  https://www.ebay.com

NodeMCU ESP8266 ESP-12 WiFi Development Board Module ESP 8266 ESP8266MOD w/CH340:  https://www.ebay.com


RTC WTIU Emulator (sketch in file RTCWTIU.ino)

This sketch is similar to the original Radio based RTC TIU Emulator (RTCEngine) except all communications is done over WiFi. This sketch does not need a radio - All communication is over WiFi which is built into the ESP8266 board.

It is a simple WTIU Emulator which responds to the RTC program or the App as if it were a WiFi based WTIU connected to a layout. It is pretty simple so don't expect to always get responses that make sense.

Out of the box, it emulates WTIU #4 with 3 AIU attached. It knows 18 engines (taken from my layout). If you press the [Read] button on RTC or the App, the sketch should respond as if it found 18 engines.

The sketch can be recompiled to respond as any WTIU number with up to 5 AIU attached. Also can be recomplied to support only 4 engines and to support 20 engines.

The sketch named "RTCWTIU.ino" compiles into one runtime module which can be uploaded into the ESP8266. At the bottom of this page are instructions for loading a precompiled binary into an ESP8266 and instructions for compiling the source code yourself.

There is some debug output that the sketch sends out the USB port. You can use a terminal emulator (I like 'Termite") set at 115200 baud to watch this debug output.

What works and what doesn't

I've only run the sketch on a network with IP addresses of 192.168.1.XXX . Most home router based networks default to this IP address range but it should work on any other typical range like 192.168.0.XXX .
This sketch will respond in a reasonable way to most commands from RTC or from the App.
I've not tested any of the really esoteric things like Protocast, ProtoDispatch, Track and Trolley modes(and maybe others).
I've not tested TMCC mode as I don't have any TMCC hardware.
Responses to commands to show times and mileage are made up by me and don't represent reality.
Sound commands, of course, don't make any sounds.
RTCWTIU always responds to the '!' command with version v1.04-0-g63ff412.

WiFi Setup

When you first load the ESP8266 with the compiled sketch, it must connect up to the WiFi router (so we need the SSID of the router and the password) and you must tell it the WTIU number and AIU count.

The first time it runs, it will automatically connect in this "setup" mode. After that, it will reenter "setup" if the SSID router is not found or if you perform two presses of the ESP8266 reset button [RST] a few seconds apart.

"Setup" is performed by connecting to the ESP8266 from your phone or a WiFi enabled computer. Tap on your WiFi icon and follow along (using the cell phone as the example):


After you upload the RTCWTIU sketch into the ESP8266, it will start an Access Point. If you look on your phone, you will see the new WiFi AP listed as "mthdcs-XXXX".

Touch on that AP to connect to it.

 The phone should connect up and you should see "mthdcs-XXXX Connected without internet".








. Then startup a web browser such as Firefox or Chrome. Enter this IP address into the Address Bar:  192.168.4.1 . Press GO. You should see this screen which lists all of the accessibleWiFi routers in the area. Here is were we enter the information that the RTCWTIU sketch needs to communicate with your computer via the WiFi router.

Enter:
The SSID of your router
The WiFi password of your router
The WTIU number to be emulated (1-5)
The number of emulated AIU connected (0-5)

Then click on [Setup RTCWTIU WiFi].
You should see this screen indicating that the device is rebooting and will connect up the WiFi Router.

The ESP9266 will reboot and start. If the LED on the board stays on, the board is having trouble connecting to the router.

If you need to change these setting after first setting them, press the ESP8266 reset button twice a few seconds apart. The ESP8266 will then let you go through these setup steps again.
The RTCWTIU will reboot.

The sketch first tries to connect to the router. The LED on the ESP8266 will stay solid blue until it does that. If the LED stays on solid, it indicates the sketch can't connect to the WiFi router. Probably the wrong SSID and/or password were entered.

After the connection, the sketch fast blinks the LED to indicate the WTIU number being emulated. Be sure that you don't duplicate numbers with actual WTIU on your layout.

Once everything is running, the LED will blink each time the sketch responds to a command from RTC or the App or each time it answers an mDNS request.

Getting the sketches into an ESP8266

There are two ways to get the program installed into the ESP8266

1. Use the pre-compiled binary file that I supply and upload it into the ESP8266.

2. Install the complete development environment, compile the sketch youself and load it into the ESP8266

Here are (probably incomplete) instructions for each method.

RTCWTIU : loading compiled code using RTCWTIU-X.XX.zip.

RTCWTIU            This is an ESP8266 NodeMCU based sketch using WiFi  only

If you want to work with this, contact me for the zip file. I will have it on-line once it is stable.

1. install Python 3.12 or newer from:

https://www.python.org/downloads/release/python-3128/

I used the "Windows installer (64-bit)".

On the first screen of the install, note the location of  where python is going to be installed. Something like "C:\Users\XXXX\AppData\Local\Programs\Python\Python312\python". Note the username "XXXX" for step 6.

Image loading....


Installing Python


2. Make a folder somewhere

3. unzip the RTCWTIU.zip file into that folder

4. connect your ESP8266/CC1101E to a USB port. You will need to know the COMx port that Windows assigns to the ESP8266. You can use the "Device Manager" to get that. Search for that program and run it. Click on "Ports (COM & LPT)" and note the COMx port assigned.

5. It appears that some (maybe all) ESP8266 are not supported by the default WIN10 and WIN11 CH340 driver. If you get a permission error on the COMx port when uploading, you will have to replace the current CH340 driver with an older version. On my computer, I replaced the current version 3.9.2024.9 with version 3.5.2019.1. I've run the following steps on both WIN10 and WIN11 machines:

    NOTE: the CH340 will be phased into the main ZIP file at the next release. If you have a ZIP file which does not have the CH340 driver, you can get it here. Make a folder named "CH340" somewhere and extract the contents of the ZIP file into that folder. Then continue on with step (a) below.

    a. Go to the folder "CH340" which was installed as part of the main RTCWTIU.ZIP.
    b. Double click on "setup.exe". Click the [Yes] button at the UAC prompt.
    c. Click on [INSTALL] (for version 3.5.2019.1) and in a minute or so, you should see the "Driver install success!" message.

    If you ever need to return to the default CH340 driver, on the Properties window in Device Manager, click on [Update Driver].

6. edit the file "upload.bat" with notepad

    edit the path to python:

        C:\Users\XXXX\AppData\Local\Programs\Python\Python312\python

    change "XXXX" to your user name - the one you noted in step 1.
    change the python version number if you downloaded a newer version.

    edit the port number for the USB port connected to the ESP8266

    change "COM4" to the USB serial port.

    save the file

6. double click on "upload.bat" and the upload into the ESP8266 should occur.

Image loading....

Loading Compiled Binary into the ESP8266 (this is for RTCAdapter but it should look very similar for the RTCWTIU)


7. Here is a transcript of a successful upload:

E:\Projects\ESP8266\RTCWTIU>rem

E:\Projects\ESP8266\RTCWTIU>rem USB

E:\Projects\ESP8266\RTCWTIU>"C:\Users\markd\AppData\Local\Programs\Python\Python312\python" -I "upload.py" --chip esp8266 --port "COM4" --baud "115200" ""  --before default_reset --after hard_reset write_flash 0x0 "build\esp8266.esp8266.nodemcuv2/RTCWTIU.ino.bin"
esptool.py v3.0
Serial port COM4
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: ac:0b:fb:ce:d6:2a
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 367792 bytes to 261823...
Wrote 367792 bytes (261823 compressed) at 0x00000000 in 23.1 seconds (effective 127.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

E:\Projects\ESP8266\RTCWTIU>rem

E:\Projects\ESP8266\RTCWTIU>pause
Press any key to continue . . .


Then follow the instructions above to enter the SSID and password of your router into the ESP8266.

RTCWTIU : compiling source code using RTCWTIU-X.XX.zip

RTCWTIU            This is an ESP8266 NodeMCU based sketch using WiFi  only

If you want to work with this, contact me for the zip file. I will have it on-line once it is stable.

To compile these, I used the Arduino IDE version 2.3.4. You will have to download the IDE, install it and then install the ESP8266 board.

1. Make a folder somewhere

2. unzip the RTCWTIU.zip file into that folder

3. connect your ESP8266/CC1101E to a USB port

4. It appears that some (maybe all) ESP8266 are not supported by the default WIN10 and WIN11 CH340 driver. If you get a permission error on the COMx port when uploading, you will have to replace the current CH340 driver with an older version. On my computer, I replaced the current version 3.9.2024.9 with version 3.5.2019.1. I've run the following steps on both WIN10 and WIN11 machines:

    NOTE: the CH340 will be phased into the main ZIP file at the next release. If you have a ZIP file which does not have the CH340 driver, you can get it here. Make a folder named "CH340" somewhere and extract the contents of the ZIP file into that folder. Then continue on with step (a) below.

    a. Go to the folder "CH340" which was installed as part of the main RTCTIU.ZIP.
    b. Double click on "setup.exe". Click the [Yes] button at the UAC prompt.
    c. Click on [INSTALL] (for version 3.5.2019.1) and in a minute or so, you should see the "Driver install success!" message.

    If you ever need to return to the default CH340 driver, on the Properties window in Device Manager, click on [Update Driver].

5. Download and install the IDE from the Arduino download page.

    Click on menu item  "Tools->Board->Boards Manager", select "esp8266 Community version 3.1.2" and click on [Install]. There may be a newer version.

    Before compiling, use the "Tools->Board->esp8266" menu to select board "NodeMCU 1.0 (ESP-12E Module)".

    From the Arduino IDE, use the "Sketch->Include Library->Manage Libraries", find these library and click [Install]. There may be a newer versions.
   
ESP_DoubleReset_Detector v1.3.2 by Khoi Hoang
ESP8266TimerInterrupt v1.6.0 by Khoi Hoang
Time  version 1.6.1 by Paul Stoffregen
  
    Compile the code. It should be error free and if you set the IDE preference for verbose output during compile, you should see these libraries being used (they might not be in the same folders on your computer):

Using library ESP8266WiFi at version 1.0 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi
Using library ESP8266HTTPClient at version 1.2 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266HTTPClient
Using library ESP8266WebServer at version 1.0 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WebServer
Using library ArduinoOTA at version 1.0 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ArduinoOTA
Using library ESP8266mDNS at version 1.2 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266mDNS
Using library EEPROM at version 1.0 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\EEPROM
Using library Time at version 1.6.1 in folder: C:\Users\markd\Documents\Arduino\libraries\Time
Using library ESP_DoubleResetDetector at version 1.3.2 in folder: C:\Users\markd\Documents\Arduino\libraries\ESP_DoubleResetDetector
Using library LittleFS at version 0.1.0 in folder: C:\Users\markd\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\LittleFS
Using library ESP8266TimerInterrupt at version 1.6.0 in folder: C:\Users\markd\Documents\Arduino\libraries\ESP8266TimerInterrupt 


6. Before uploading the compiled sketch into the ESP8266, from the "Tools->Port" menu to select the USB COM port connected to the ESP8266.

Then follow the instructions above to enter the SSID and password of your router into the ESP8266.


For some tools to help you get RTCWTIU on-line, click here. They center around tools to let you know if RTCWTIU has successfully connected to your router. They work as well for the WIU/TIU and WTIU.


Copyright Notice

As I mentioned in the posts on the forum, I am releasing these programs and documentation under the  GNU GENERAL PUBLIC LICENSE Version 3 and GNU Free Documentation License Version 1.3. These licenses permit anyone to use the program and the documentation to produce follow on programs as long as those programs remain covered by the GNU licenses.

The Free Software Foundation web page talks about what "free software" means.

For more information, look on the Open Source Initiative web site which includes a description of what Open Source means and their certification of the GPL version 3.

Here is the copyright notice for those two licenses:

GNU GPL

This is part of the Remote Train Control Program for Windows
© Copyright 2025 by Mark DiVecchio

Remote Train Control is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Remote Train Control is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

GNU FDL

This is part of the Remote Train Control Manual.
© Copyright 2025 by Mark DiVecchio

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. You should have received a copy of the GNU Free Documentation License along with Remote Train Control. If not, see <http://www.gnu.org/licenses/>.



This site prepared and maintained by Mark DiVecchio

email :  markd@silogic.com

SD&A HOME
 
 Mark's Home Page

The DiVecchio genealogy home page
The Frazzini genealogy home page

This site will be under construction for a while.