Controlling Märklin Digital Railroad with Raspberry Pi(Raspi) and Rocrail


Software part:

Setting up the Raspberry PI to act as Central Station 2

 

Downloading the Raspbian image on a PC:

This manual workes best with an older version: raspbian-2015-05-07/2015-05-05-raspbian-wheezy.zip
from http://downloads.raspberrypi.org/raspbian/images/
 

But for a new PI3 you better download: raspbian-2016-09-28/2016-09-23-raspbian-jessie.zip


Unzip the image and write 2015-05-05-raspbian-wheezy.img to SD Card with Win32 Disk Imager
https://sourceforge.net/projects/win32diskimager/

Put SD Card in Pi and start your PI.

Installing on the PI:

Raspi will start up with the Raspi-config.
You can now hit the enter key to Expand the filesytem, but this is optional.

But what you need to do is go to Advanced Options.
Move Cursor down to A6 SPI, and enable it.
Now you can finish the setup and reboot your PI.

You can also check this link for reference with some screenshots as guide: http://skpang.co.uk/blog/archives/1165

After Pi has rebooted, log in with:

user: pi
password: raspberry

Take attention that the reasperry is setup for english keyboard, and on german keyboards, the keys Z and Y are switched.
 

Now we have to add 2 lines to the file: /boot/config.txt
so type:

sudo nano /boot/config.txt

Scroll at the end of the file, and you should see this line:

dtparam=spi=on

If you don't see above line, add it, and right afterwards add this at the end:

For Jessie release before 2016-05-10, add this:

dtoverlay=mcp2515-can0-overlay,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

For Jessie release from 2016-05-10 and later, add this:

dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-bcm2835-overlay

User info for using a RPI 4 with Buster (2019-09-26 Kernel 4.19):
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25
dtoverlay=spi-1cs
bcm2835 Info: Use version bcm2835-1.62. Version 42 did not work.
(Thanks to Florian for this info!)


Save and exit with:

Ctrl+o -> Enter -> Ctrl+x


Now reboot your Pi with

sudo reboot


After the reboot, and after you logged in again, type following line to test your Board:

sudo ip link set can0 up type can bitrate 250000


You should get no error message.
If you get the error message: Can't find CAN0 device, then your board is probably faulty.

to ensure your CAN Baord is mounted, type:

ifconfig

and you should be able to see your mounted board as can0 like this:

can0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
UP RUNNING NOARP MTU:16 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:10
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

eth0 Link encap:Ethernet HWaddr b8:27:eb:97:f1:68
inet addr:192.168.1.18 Bcast:192.168.188.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:125 errors:0 dropped:0 overruns:0 frame:0
TX packets:149 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12903 (12.6 KiB) TX bytes:22629 (22.0 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:40 errors:0 dropped:0 overruns:0 frame:0
TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3856 (3.7 KiB) TX bytes:3856 (3.7 KiB)

wlan0 Link encap:Ethernet HWaddr 74:da:38:1a:c0:2f
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:2 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)



At this point you know that your first chip the MCP2515 is working properly and communicating with the PI.
Now lets see if the second chip the MCP2551, is also working and is able to listen on the CAN Bus.
For this, lets install some CAN Helper tools, so type this into your PI:


To avoid getting this error: ./autogen.sh: line 20: autoreconf: command not found
lets do this first:


sudo apt-get install autoconf
sudo apt-get install libtool


Now install the CAN Helper tools:
git clone https://github.com/linux-can/can-utils.git
cd can-utils
./autogen.sh
./configure
make
sudo make install


Now we can test our board. Connect the Märklin Gleisbox with your Raspberry, and with a Mobile Station 2. and power it up.

Now type following command to listen on the CAN Bus and dump it on the screen:
sudo ./candump can0

When you press now the STOP Button on your Mobile Station 2, you should get following lines:

pi@raspberrypi ~/can-utils $ sudo ./candump can0
can0 00002F17 [7] 00 00 00 00 09 00 04
can0 00002F17 [6] 00 00 00 00 08 07
can0 00017B1F [7] 00 00 00 00 09 00 04
can0 00002F17 [5] 00 00 00 00 01
can0 00017B1F [6] 00 00 00 00 08 07
can0 00017B1F [5] 00 00 00 00 01



This is my output after pressing the STOP Button a few times:

can0 00002F17 [7] 00 00 00 00 09 00 04
can0 00002F17 [6] 00 00 00 00 08 07
can0 00017B1F [7] 00 00 00 00 09 00 04
can0 00002F17 [5] 00 00 00 00 01
can0 00017B1F [6] 00 00 00 00 08 07
can0 00017B1F [5] 00 00 00 00 01
can0 00002F17 [5] 00 00 00 00 00
can0 00017B1F [5] 00 00 00 00 00
can0 00002F17 [7] 00 00 00 00 09 00 04
can0 00002F17 [6] 00 00 00 00 08 07
can0 00017B1F [7] 00 00 00 00 09 00 04
can0 00002F17 [5] 00 00 00 00 01
can0 00017B1F [6] 00 00 00 00 08 07
can0 00017B1F [5] 00 00 00 00 01
can0 00002F17 [5] 00 00 00 00 00
can0 00017B1F [5] 00 00 00 00 00



Awesome, at this point, the board is working perfectly. Time to setup my tool which will communicate with Rocrail Server to control our railroad.

Go back to home directory first:


cd ..


First we need a tool to access the GPIO ports of the Pi:

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.42.tar.gz
tar zxvf bcm2835-1.42.tar.gz
cd bcm2835-1.42
./configure
make
sudo make check
sudo make install


Now we can do the main program:

cd ..
wget http://www.ifoedit.com/PiCanS88.tar
tar xvf PiCanS88.tar
cd PiCanS88
make

Now you can start it with (where the IP would be the IP of your Rocrailserver):
sudo ./PiCanS88 -f -b 192.168.2.255 -v -d 192.168.2.96 -m 1

Explanation of possible options:

-f (stay in forground and show details on screen)
-v (show details on screen)
-b 192.168.2.255 (broadcast address of your local home network)
-d 192.168.2.96 (IP address of the Computer where your Rocrail server is running)
-m 1 (number of connected S88 modules with 16 signals each) - (If you have modules with only 8 signals, using 2 of such in a row will be seen as one module!)

Your Pi is now fully setup to act as a Central Station and to work with Rocrail. So lets create a quick script, that starts everything up, once we boot our Pi.
I will name my script: startcs2, so type this line:

sudo nano startcs2.sh

and add following lines into it:


sudo ip link set can0 up type can bitrate 250000
sleep 2
ifconfig
sudo /home/pi/PiCanS88/PiCanS88 -f -b 192.168.2.255 -v -d 192.168.2.96 -m 1


You will have to adjust the two addresses to your home network.
For example, if you have Rocrail Server running on your PC, open a command prompt on your PC, and type:

ipconfig

Then you will retrieve the IP address of your PC and the IP address of the default Gateway. Example: your IP 192.168.1.47, Gateway: 192.168.1.1
So in above script you will enter following line:

sudo /home/pi/PiCanS88/PiCanS88 -f -b 192.168.1.255 -v -d 192.168.1.47 -m 1

If you have more then one S88 modules connected, you will have to adjust the option "-m" aswell with your number of connected modules.

Save and exit with:

Ctrl+o -> Enter -> Ctrl+x

Because this will be a script, we have to make this file executable with:

sudo chmod u+x,g+x,o+x startcs2.sh

Crosscheck with:

ls -ll

-rwxr-xr-x 1 root root 152 Apr 10 17:24 startcs2.sh

our script is now executable and could be started after a reboot with:

./startcs2.sh

I assume you will be using this Pi just for your railraod and want to have the script started automatically on power up, so lets edit the autostart by doing this:

sudo nano /etc/rc.local

You will see this:


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

exit 0






add your script to start, before the exit line like this:




#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi


/home/pi/startcs2.sh

exit 0



Save and exit with:

Ctrl+o -> Enter -> Ctrl+x
 


You Pi is now all setup to act as a almost full Central Station 2 for your Märklin Railroad. Enjoy.

 

Solution for watching the output in ssh session:

Big time gratitude goes to Christian Karrié for providing this solution!

If you are using the autostart script, and wonder how to see the output of my program, here is the solution provided by Chris:

We'll need a tool called screen:

sudo apt-get install screen

now change your autostart script to this:


sudo ip link set can0 up type can bitrate 250000
sleep 2
ifconfig
screen -dmS cs2 bash -c 'sudo /home/pi/PiCanS88/PiCanS88 -f -v -b 192.168.2.255 -d 192.168.2.96 -m 1'

 

In your ssh session you can link to the running process at any time with:

sudo screen -r cs2

and exit with:

Ctrl+A -> d
 

(Thanx to Christian Karrié for this solution)

Fixing the Wifi-Disconnet problem:

I was facing a problem, when I started using a PI with an Edimax USB Wifi stick.
After a few minutes I couldn't reach my Pi inside the network.
Reason was an energy saving "feature", which disconnected the Pi off the network.
Here's the solution to avoid this:

1 - Network settings:


Check your network settings, if wireless-power management is off.
Type:

iwconfig

This is my output:

wlan0 IEEE 802.11bgn ESSID:"IWD546312" Nickname:"<WIFI@REALTEK>"
Mode:Managed Frequency:2.437 GHz Access Point: D4:0E:11:2F:FD:AB
Bit Rate:72.2 Mb/s Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality=99/100 Signal level=94/100 Noise level=0/100
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

Power Management is already off. If yours should be on, do this:

sudo nano /etc/network/interfaces

Add this at the end:

wireless-power off

Save and exit with:

Ctrl+o -> Enter -> Ctrl+x

and reboot:

sudo reboot

2 - Turn off Edimax Powersaving mode:

Check if you have a Realtek device by typing this:

lsmod

This is my output:

Module Size Used by
can_raw 6736 0
can 27502 1 can_raw
snd_bcm2835 18365 0
snd_pcm 73475 1 snd_bcm2835
snd_seq 53078 0
snd_seq_device 5628 1 snd_seq
snd_timer 17784 2 snd_pcm,snd_seq
snd 51038 5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
mcp251x 8640 0
can_dev 9227 1 mcp251x
8192cu 528365 0
spi_bcm2835 4061 0
evdev 9950 3
joydev 8879 0
uio_pdrv_genirq 2958 0
uio 8119 1 uio_pdrv_genirq

If you can find the module: '8192cu' like in the output above, then create a new config file:

sudo nano /etc/modprobe.d/8192cu.conf

And add this line:

options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

Save and exit with:

Ctrl+o -> Enter -> Ctrl+x

and after you reboot, this annyoing disconnect problem will be gone:

sudo reboot