Dual SIM Application

From MikroTik Wiki
Revision as of 12:21, 19 July 2019 by Martinc (talk | contribs)
Jump to navigation Jump to search


This script example shows how to switch between SIM slots in case mobile roaming is detected for LtAP mini devices. This could be useful for mobile vehicle applications, where cars, buses or trains could drive abroad and should use two SIM cards (one for a home network, other for a roaming network). Since RouterOS version 6.43 a roaming status for info command is added (displayed only when roaming) so we can use this in RouterOS scripts to change SIM cards accordingly.


Note: Keep in mind that this is just an example how to utilize dual SIM slots. For real-life production environments, a proper testing should be carried out, so try to optimize it and add new features according to your needs.

Initial settings

First, make sure you have correctly set up LTE network parameters (provided by the mobile network operator) for each SIM card. You can use default APN profile or create two separate ones, follow this link - Quick setup example. This example uses default APN profile.

After that, enable data roaming for connecting to other countries data-providers with following command. This allows to keep track of roaming status.

/interface lte set [find name=lte1] allow-roaming=yes

Then, choose which SIM slots will be used for home and roaming networks. In this example, we use slot "down" for home and slot "up" for roaming network. Use the following command to switch between active slots.


Note: command for sim slot selection changes in v6.45.1

Command for pre 6.45.1

/system routerboard sim set sim-slot=down

Command after 6.45.1

/system routerboard modem set sim-slot=down

After changing SIM slots, LTE modem will be restarted. It can take some time (depending on modem and board around 30 seconds) to fully initialize it, so make sure you test your modem.

Creating a script

Now create a script that will run with a scheduler. This script example is going through few key points:

  • Check if LTE interface is initialized (shows in /interface lte list), otherwise try a power reset
  • Check if LTE connection is established (interface is in "running" state), otherwise create a log entry and simply wait for next scheduler
  • Read currently used LTE slot and make a decision whether to change SIM slots based on roaming status

Let's call this script "roamingScript", and see below the source:

# Setup and read current values, "up" SIM slot will be used for roaming, "down" for home network
:global simSlot [/system routerboard sim get sim-slot]
:global timeoutLTE 60
:global timeoutConnect 60

# Wait for LTE to initialize for maximum "timeoutLTE" seconds
:local i 0
:local isLTEinit false
:while ($i<$timeoutLTE) do={
    :foreach n in=[/interface lte find] do={:set $isLTEinit true}
    :if ($isLTEinit=true) do={
        :set $i $timeoutLTE
    :set $i ($i+1)
    :delay 1s

# Check if LTE is initialized, or try power-reset the modem
:if ($isLTEinit=true) do={
    # Wait for LTE interface to connect to mobile network for maximum "timeoutConnet" seconds
    :local isConnected false
    :set $i 0
    :while ($i<$timeoutConnect) do={
        :if ([/interface lte get [find name="lte1"] running]=true) do={
            :set $isConnected true
            :set $i $timeoutConnect
        :set $i ($i+1)
        :delay 1s
    # Check if LTE is connected
    if ($isConnected=true) do={
        :local Info [/interface lte info [find name="lte1"] once as-value]
        :local isRoaming ($Info->"roaming")
        # Check which SIM slot is used
        :if ($simSlot="down") do={
            # If "down" (home) slot, check roaming status
            :if ($isRoaming=true) do={
                :log info message="Roaming detected, switching to SIM UP (Roaming)"
                /system routerboard sim set sim-slot=up
        } else={
            # Else "up" (roaming) slot, check roaming status
            :if (!$isRoaming=true) do={
                :log info message="Not roaming, switching to SIM DOWN (Home)"
                /system routerboard sim set sim-slot=down
    } else={
        :log info message="LTE interface did not connect to network, wait for next scheduler"
} else={
    :log info message="LTE modem did not appear, trying power-reset"
    /system routerboard usb power-reset duration=5s

Setting up scheduler

Last, create your scheduler that will run the previously created script. Choose a proper scheduler interval, so two or more events do not overlap with each other. For this example above, 3 minutes will be enough.

/system scheduler add interval=3m on-event=roamingScript name=Roaming

Keep in mind that "home" SIM card will consume some roaming data because changing SIM slots do not happen instantly.