Trybotics Logo

WiFi DCC Command Station for Model Railway

DESCRIPTION

Update 16 Feb 2020: Amended sketch for Arduino to meet Hornby decoder cv read requirements - please use 'command_station_wifi_v1_2.ino'

Update 1st Oct 2019:
New version of App released to include full CV read/write routines 'LocoMotive WiFi DCC'
New sketch for DCC_WiFI (On ESP 32S) and Command Station (On Arduino Pro Mini)

Further to my Instructable on a Bluetooth / Android App version of a DCC Command Station, I have developed the system to be controlled by WiFi.

This WiFi version can be run on more than one Android device giving the user multiple DCC throttles.
Up to 50 locos may be listed within the App menu.

The ESP32s (Node MCU) module creates the local WiFI router and communicates with the App to receive DCC packets. The packets are sent to an Arduino Pro Mini and output to a LMD18200T h-bridge.

Firstly, you must set up the Arduino IDE to run the code for the ESP32 node Node32S (or NodeMCU-32S) device. The following links gives full instructions for PC and Mac setups :

'Last minute Engineers' or Randon tutorials or GitHub link

The main components of the system are :

ESP32S (eBay link)
Arduino Pro Mini
LMD18200T h-bridge

Power supply:For OO/HO layouts use a 14v DC power supply (e.g. laptop charger unit from eBay).
14V 4 Amp version with a 2.5 x 5.5 mm plug, search for eBay item # 401748337196

15V 2 Amp version with a 2.1 x 5.5 mm plug, search for eBay item # 401871382681

Do NOT use a DC train controller as these do not provide a true DC voltage.

All other components are available on eBay.

1 off LMD18200T H-bridge
1 off 0.1 ohm 2W Metal Film Resistor (11.5 mm x 4.5 mm)
3 off Capacitor 0.1uf
2 off 10kΩ Resistor
1 off Capacitor 10uf 25v;
1 off Capacitor 220uf 16v;
1 off Phoenix Contact MKDS 1/ 2-3,5 2 Way Screw PCB Terminal Block 13.5A 200V 3.5mm
1 off 4.7kΩ Resistor
1 off L7805 CV Positive Voltage Regulator IC
1 off Heatsink TO 220 style for L7805
2 off 15 pin Female Header Edge Pins Strip 0.1" 2.54mm
2 off 12 pin Female Header Edge Pins Strip 0.1" 2.54mm
1 off 6 pin 2.54mm PCB Universal Screw Terminal Block
1 off Zener Diode 4.7V 0.5 watt or 3.6v 0.5 watt

Description:

Few years back, ESP8266 took the embedded IoT world by storm. For less than $3, you could get a programmable, WiFi-enabled micro-controller being able to monitor and control things from anywhere in the world. Now Espressif (The semiconductor company behind the ESP8266) has released a perfect super-charged upgrade: the ESP32. Being successor to ESP8266; not only does it have a WiFi support, but it also features Bluetooth 4.0 (BLE/Bluetooth Smart) – perfect for just about any IoT project.

The ESP32 Integrates 802.11b/g/n HT40 Wi-Fi transceiver, so it can not only connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. The ESP32 supports WiFi Direct as well, which is a good option for peer-to-peer connection without the need of an access point. The WiFi Direct is easier to setup and the data transfer speeds are much better than Bluetooth.
The chip also has dual mode Bluetooth capabilities, meaning it supports both Bluetooth 4.0 (BLE/Bluetooth Smart) and Bluetooth Classic (BT), making it even more versatile.

In this project, I only use the WiFi capability to create a local server for the DCC command station to communicate with an Android App.

In theory, it is possible to only use the ESP module, however the clock generation code required is completely different from the AVR clock code use in the Arduino Pro Mini. I leave this task to another reader out there!

The connections between ESP32 and Arduino are really simple - see circuit diagram. The RX,TX from Pro Mini connect to the Rx2,Tx2 of the ESP device. Note the use of resistors to step down the signal level to the ESP32 as it can only use 3.3v levels.

Description:

The Arduino circuit is the same as that used in the Bluetooth version. I have added sockets to mount the ESP32 in place of the BT module. This PCB is now available for sale on eBay here.
The Arduino must be a Pro Mini ATmega 328 16MHz 5v version

The ESP32 acts as a WiFi server, receiving data from the WiFi_DCC App and transmitting this to the Arduino via TX2 pin. Any data going back to the App will be sent via the RX2 pin.

A current sense resistor 0.1 ohm detects overload and short circuit conditions which then sets down the system until a reset signal is received.

The LMD18200T h-bridge converts the DCC packet into a AC waveform that supplies the track with power and data.

Note: The 5 volt regulator in a TO-220 package gets hot when powering the ESP32 module (up to 200 mA) therefore a heatsink must be used.

Description:

Updated 1/10/2019 - please use new sketch attached 'DCC_WiFi_v1.ino'

This sketch sets up your local server and receives updates from the App on your android device.
The communication is 2-way to allow data on current drawn by the system to be reported back to the App.

Go to GitHub link to obtain required library files here.

ESP32S must be programmed via the Arduino IDE.
Go to Tools, Board, and select Node32S or NodeMCU-32S from list.

Go to Tools, Port and select /dev/cu.SLAB_USBtoUART

That is the option on my Apple MacBook Air - something similar on PC I would imagine.

The Arduino sketch 'DCC_WiFi_v1.ino' requires these library files :

// for App 'LocoMotive WiFi Controller'

// creates a WiFi access point and provides a web server on it

#include "WiFi.h"
#include "WiFiClient.h"
#include "WiFiAP.h"

const char *ssid = "DCC_WiFi"; // must be matched in Android device settings
const char *password = "123456789"; // must be entered when above ssid is selected

WiFiServer server(80);

Attachments

Description:

Updated 16/2/2020 - please use new sketch attached 'command_station_wifi_v1_2.ino'

#include "SoftwareSerial.h"

SoftwareSerial wifi_dcc(8,9); // RX TX connects to TX2 RX2 on ESP32

The following arrays hold the DCC packet values for repeated broadcasting within the sketch
// increased from 20 locos to 50

int sp[51]; // speed data for each loco 1 to 50
int f0f4[51]; // function 0 to 4 for each loco 1 to 50
int f5f8[51]; // function 5 to 8
int f9f12[51]; // function 9 to 12
int f13f20[51]; // function 13 to 20
int f21f28[51]; // function 21 to 28

Current sense is monitored by measuring voltage dropped over 0.1 ohm resistor as read by the A0 input pin on the Arduino.

void current(){
int i;
int value = 0;
int numReadings = 5;
for (i = 0; i < numReadings; i++){ // Read sensor data.
value = value + analogRead(A0); // 1ms pause adds more stability between reads.
delay(1); }
sensorValue = value/numReadings;
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 1.1V) = 1.08mv per division
// 0.1 ohm resistor on current sense gives 200mv at 2 Amps or, 100mv per Amp
// 1.08 mv per div for Internal Ref of 1.1v : 100/1.08 = 92.6 divisions per 1000mA or 1 div = 10.8mA
C = 10.8 * sensorValue ; // mA
if(C >2000){ // 2 amps for LMD18200T
Serial.println("Over Current");
Short = true;
digitalWrite(6, LOW); // logic control of DCC signal to low (off)
digitalWrite(13, HIGH); // to brake, shorts out h-bridge o/p
} }

Attachments

Description:

The App is called 'LocoMotive WiFi DCC' available on Google Play Store here.

The App may be loaded onto more than one Android device to provide multiple DCC throttles.

Note: App works well on Android 7, however on Android 9 you must switch on GPS in the Location settings of your device - even then, you must click the Get WiFi button a couple of times to connect effectively.


YOU MIGHT ALSO LIKE