Use Bluetooth to control Märklin Z Model Train
In the last blog, I succeed in connecting the Train to an Arduino and running it automatically. Obviously, the next task is to connect it to my mobile and steer it via Bluetooth. So instead of recreating a transformer, I created a remote control.
Bluetooth and Arduino
To connect my Moile to the Arduino to Bluetooth, I will use an HC-05 module. If you are new to Bluetooth with Arduino, here is an easy-to-follow blog to help you get started. It’s incredibly straightforward and should work without too much effort. However, I encountered the problem that the L298N 5V supply was insufficient to power itself, the Arduino and the HC-05 module. The solution was relatively easy, I just connected an external 5v power supply such as a USB Charger, USB Port or Powerbank to the Arduino. To make sure that nothing gets damaged in the L298N the Jumper (5VEN) has to be removed from the L298N.
The HC-05 modules are preconfigured and work right out of the box and should be detectable from the mobile directly after power is supplied. The default password for the HC-05 modules is either 0000 or 1234.
The HC-05 modules can be reconfigured by using AT commands sent to them. I choose to change the name of the device (AT+NAME=bluething), the baud rate (+UART=38400) and the password/pin (AT+PSWD=4321).
The ESP32 has Bluetooth inbuilt and works fine with the code for the HC-05 directly out of the box.
Setting up the communication
The communications between the Mobile App and the Arduino works by transferring data in form of bytes (Characters) between the two Bluetooth devices, one by one. The main challenge with Bluetooth communication is how to package the information that needs transporting. In my Mobile App, I want the ability to:
start the train up slowly with one click
stop the train slowly with one click
use a slider to directly control the speed and direction of the train.
change the parameter (int delayTimes) that defines how slow the train starts and stops
Therefore the data package for Bluetooth communication needs to fulfil the following requirements:
It needs to contain the action that needs to be performed
needs to contain any additional data such as manual speed or the delayTime value
needs to be expendable for further controls such as Switch Points
needs to be short to increase the communication speed between mobile and Arduino
should be human readable
A character that signals the end of the data package
I settled on a 4 character code that contains the action and the data as well as a carriage return \n to signal the end of the package.
To make the Arduino code reacts fast, I have it read each byte as it comes in and construct a String out of it. When the Arduino receives the carriage return \n it starts phrasing the received data package.
I reused the code from the last example, the only things I improved were the startUp and stopIt routines to guarantee a much smoother running. The only other major addition to the code is the manual section, where I wrote the output of the Slider (See yellow bar in the picture) directly to the L298N. The issue I faced here was that the C++ that is implemented in Arduino is not able to convert a String like “m123\n” to an integer with the value 123. The solution was not really satisfying and maybe someone has a better way, but it’s good enough for me at the moment. To solve the problem I used the function substring to cut the values (123) out of the String and then convert them to integers.
speed=(text.substring(2,text.length()-1)).toInt();
It worked and now we are ready to properly steer the model train…using a mobile App. Welcome to 2022.
Building a Mobile App User Interface
I needed a user interface on the Mobile that allowed me to interact with the Android. I wanted some sliders and buttons and some colours. For this purpose, I chose MIT’s App Inventor. It’s free, it’s easy and a lot of fun. The MIT App Inventor is a nifty little tool that uses a block-building programming language, where all programming steps and functions are combined using drag and drop.
These are the steps to follow to recreate my result:
Go to http://appinventor.mit.edu/ and sign up for free
Start a new Project
When you are inside the project, click on Projects (top menu) and then select Import project (.aia) from my computer
Select the file you downloaded in step 1
Pair your Mobile with the Arduino via Bluetooth
Install the MIT AI2 Companion on your Mobile
On the Website click on Connect (top menu) and then select AI Companion
On your Mobile open the MIT AI2 Companion App and either scan the QR code on the Computer screen or enter the code.
Your Mobile should now show the same layout as the computer screen.
Crank up the transformer to the right
Power up the Arduino using the USB on the Nano or the ESXP32
Tap on Connect on the Mobile app and select your Arduino
Tap on Forward. The train should now start running.
Tap on Stop, and the train will come to a stop.
Tap on Manual, and a slider bar will appear. Use your finger to move the slider slowly in one direction. The train should now accelerate. The number shown underneath shows values between -255 (full speed backwards), 0 (STOP) and 255 (full speed forward).
Tap back on Stop, and the train will come to a stop again.
When the Train is stopped you can change the following settings:
MaxSpeed: When you use Forward or Backward this will be the maximum speed used. (0 - 255, default 255 - Full speed)
Delay [in milliseconds] when the train speeds up. The higher the setting the slower the train will speed up or stop.
Clicking on Disconnect will disconnect the App from the Arduino.