see section: Smart ZC4BT system
(behind photo gallery)
2020/8/15
2022/11/09
ZC4BT: Z-Control for Brick Trains
🚂 Smart ZC4BT system: Pure Wi-Fi network with Z21 emulation and ROCRAIL ( iTrain, Train Controller, JMRI).
This project represents a revolutionary leap in L-gauge model railway control. It combines the mechanical genius of LEGO bricks with the modern Internet of Things (IoT). Traditional train control systems hit their limits when it comes to plastic and bricks: original infrared (IR) or Bluetooth controllers have a short range, offer no feedback, and classic DCC model control requires expensive, conductive metal rails. The Smart ZC4BT system breaks down these barriers. It transforms the entire track into a wireless local area network (WLAN), where communication takes place over the air, but according to the strict rules of the recognized Roco Z21 model standard.
🎛️ 1. System architecture and hardware
The heart and brain of the entire track is a software simulation of the Z21 control center, which runs on the popular ESP32-CYD (Cheap Yellow Display) development board.
Central unit (ESP32-CYD) All-in-One node: The board combines a dual-core ESP32 processor with Wi-Fi and Bluetooth connectivity and an integrated 2.8" TFT touch display.
Physical dispatcher desk: The display serves as a desktop control panel. It displays the emergency stop status (E-Stop), active IP addresses of connected trains and block occupancy statuses.
No power supply to the track: The central unit does not generate any DCC voltage to the rails. The plastic rails remain 100% non-conductive. There is no need to clean the rails and solve short circuits on the switch cocks. The central unit can be powered from a regular 5V USB source or power bank.
End elements (Locomotives, Turnouts, Traffic Lights): Each train, turnout or traffic light has an integrated low-cost Wi-Fi module (e.g. ESP8266 or ESP32) with its own IP address. The trains carry their own drive battery (Li-Po/Li-Ion) directly on board. Turnouts and traffic lights are powered locally (e.g. by a hidden 5V USB distribution under the landscape).
🌐 2. Z21-LAN network protocol as a universal bus
The key benefit of this architecture is the End-to-End deployment of the Z21-LAN network protocol via UDP (port 21105). Unlike conventional modeling, where the Z21 protocol ends at the headquarters and is converted into a DCC signal on the rail, in this system the same network packet travels from the control software to the locomotive itself or to the turnout.
[ Control PC / Rocrail ]
│
▼ (Z21-LAN UDP packets via Wi-Fi router)
┌────────────────┐
│ ESP32-CYD HUB │ <── (Monitors operation and controls logic)
└──────────-─────┘
│
┌──────────────────────────┐
▼ (Z21-LAN UDP) ▼ (Z21-LAN UDP)
[ Train (ESP) ] [ Turnouts & Traffic Lights (ESP) ]
- Locomotive Address - Accessory Addresses
- Packet: LAN_X_LOCO_INFO - Packet: LAN_X_SET_TURNOUT
How the Commands Are Transmitted:
Train Control: When the user changes the train speed, the ESP32-CYD central unit receives the UDP packet and sends it to the network. The train with the corresponding decoder address (e.g. address 24) intercepts the packet, unpacks the NbrProp_Lok_Drive command and changes the PWM power supply for the motor.
Accessory Control: Turnouts and traffic lights listen to the LAN_X_SET_TURNOUT command packets. The module hidden in the building recognizes the assigned address and immediately turns micro-servo that mechanically adjusts the switch. The main advantages of the protocol: Extremely low latency (responses in milliseconds), unified code for the entire hardware and theoretically unlimited scalability limited only by the capacity of your home Wi-Fi router.
💻 3. Software control via Rocrail
The top open-source software Rocrail was chosen for the full automation of this system. Thanks to the emulation of the Z21 protocol on the ESP32-CYD control unit, the software has no idea that it is controlling a plastic toy track - it sees it as a standard digital model.
Seamless integration: In the Rocrail settings, just add a new control unit, select the Z21 type, enter the IP address of your ESP32-CYD and communication is established immediately.
Complete automation (Grafikon): Rocrail can completely control dozens of trains at once based on feedback. It monitors the occupancy of blocks, builds train paths, blocks single-track lines and controls traffic lights, thus preventing train collisions 100% of the time.
Low hardware requirements: Rocrail works on a client-server architecture. The entire control can run on a cheap Raspberry Pi mini-computer hidden under the track, while you can use a laptop, tablet or smartphone application to control and monitor the layout itself.
🧲 4. Feedback modules (Block occupancy sensors)
In order for the Rocrail software to safely control automatic operation (graph), it must know exactly where each train is at all times. In a system with plastic rails, it is not possible to use classic model current sensors for section occupancy. The system therefore uses point detection using reed contacts (Reed relays) and ESP8266 modules.
[ Train with magnet ]
│ (Passage over sensor)
▼
[ Reed Relay ] ───(GPIO)───> [ ESP8266 Module ]
│
▼ (Z21-LAN: LAN_RBUS_DAT Package ACHANGED via Wi-Fi)
┌──────────────────┐
│ ESP32-CYD HUB │
└──────────────────┘
│
▼ (Z21 Protocol)
[ Control PC / Rocrail ]
Detection principle:
Physical layer (Magnet and Relay): A small, strong neodymium magnet is attached to the lower part of the chassis of each locomotive (or the last wagon). Miniature glass Reed relays are placed between the rail sleepers at critical points (entrances and exits from blocks, stations or hidden stations).
Processing by the ESP8266 module: When the train passes over the sensor, the magnetic field closes the reed contact. This impulse is captured by the digital pin (GPIO) of the ESP8266 module, which is hidden in the nearby scenery (e.g. under the track, in a gate or station building).
Sending a network packet (Z21-LAN): The ESP8266 immediately wraps the state change information into a UDP packet according to the Z21 protocol specification for feedback. It uses the native R-Bus message structure (LAN_RBUS_DATACHANGED packet). This packet contains information about the module index and the specific number of the connected pin (sensor address).
Processing at the central office and Rocrail: ESP32-CYD receives a UDP packet from ESP8266, updates the status on its local 2.8" TFT display (for visual control by the dispatcher) and immediately forwards it to the control computer. Rocrail processes the message in a flash. On the track screen, the relevant block lights up red. The program knows that the train has successfully entered the block (Enter sensor), starts to slow it down, or that it has reached the end of the block (In sensor) and the train stops safely in front of the red traffic light.
Main advantages of this feedback solution:
Contactless and reliable: Reed relays operate on the magnetic principle, so they do not suffer from mechanical wear and are immune to dust or ambient light conditions (unlike optical sensors).
Zero cabling to the PC: ESP8266 modules send data wirelessly via Wi-Fi. Several can be connected to one ESP8266 module reed contacts from the nearby area (e.g. from the entire railway station). This eliminates the need to pull dozens of cables across the entire room.
Simple calibration in Rocrail: Each sensor in Rocrail is simply assigned an address corresponding to the settings in ESP8266. The software then handles the advanced braking distance logic itself.
🌟 5. Why is this system groundbreaking for the community?
Traditional solutions (LEGO / DCC) -- SmartZC4BT system (ESP32-CYD + Rocrail)
High price: Original hubs and expensive DCC centrals -- low costs: ESP32 boards and display cost only a few euros.
Difficult maintenance: Need to clean metal rails from dust -- maintenance-free: Plastic rails, data and power transmission is independent.
Poor range: IR controllers require direct visibility -- range throughout the house: The only limit is the coverage of your Wi-Fi network.
None logic: Trains run blind, collisions are a risk -- full automation: Rocrail takes care of safety and graphics.
This system transforms MOC trains from a limited children's toy into a full-fledged, professionally controlled model system. Thanks to the use of Rocrail's open-source software and inexpensive ESP32 hardware, it opens the door to massive automation of tracks on carpets and large club exhibitions, while maintaining minimal financial costs.