How to Use GSM/3G/4G in Embedded Linux Systems

Wednesday, May 18, 2016

Introduction

Embedded devices connected to the internet are growing every day. In many cases, these devices will be installed somewhere without a wire or wireless network connection. A good way to keep the devices connected to the internet in these situations is by seeking mobile network coverage. Therefore, this blog post will present the necessary configurations to connect a device running Linux to the internet through the PPP (Point-to-Point Protocol) link.

Hardware Specification

In this article we will be using the Colibri iMX6S with the base board called “Colibri Evaluation Board”, both of which, are from Toradex. The Colibri Evaluation Board is recommended specifically for project evaluation and development. This product comes with a great number of interfaces like USB, Ethernet, I2C, SPI, RS242, RS485, CAN and many others. We can also find some multimedia interfaces like HDMI, LVDS, VGA and LCD already supported on the Evaluation Board.

To follow this tutorial, you can use any USB or Serial modem to connect to the internet. You just have to adapt the interface created at “/dev/” and check the “AT” commands supported and implemented for your modem because each modem has specific AT commands. In this case, we have used for this tutorial a Huawei E173s 3G modem with a compatible SIM Card with data plan activated.

To configure and connect the 3G modem to the internet, Linux uses, in addition to the kernel drivers, a software which manages the connection. This software is PPP. Usually Toradex images already have the PPP software installed by default.

In case you do not have PPP installed, just add the following to your local.conf file for build environments based on Yocto/OpenEmbedded:

IMAGE_INSTALL_append =" ppp"

Testing Communication with the Modem

After all the necessary drivers and software are installed, it is possible to check if the module recognizes the 3G modem. The modem interface should appear in /dev. Basically the driver simulates a USB connection as many serial connections.

Execute the following command to search for the interfaces /dev/ttyUSB

$ ls -l /dev/ttyUSB*

If the files ttyUSB0, ttyUSB1 … e ttyUSB2 were found, the modem was succesfully recognized by the system.

In case you want to conduct in-depth testing, it is possible to use a serial terminal like “minicom” to start communicating with the modem and check if it is working well. In my case the modem had a baudrate of 115200.

$ minicom -D /dev/ttyACM0
___________________________
AT
OK

Configuring the PPP Files

After validating the communication with the modem, it is time to create the configuration files responsible for connecting to the internet.

You should be able to find a folder inside /etc/ called ppp. Some files need to be created inside this folder.

To configure DNS, we can use the following command and then ping a URL, for example:

$ echo nameserver 8.8.8.8 >/etc/resolv.conf
$ ping google.com

Image 1: A running web browser using 3G connection.

We can avoid the file being rewritten when system is restarted with the following command:

$ chattr +i /etc/resolv.conf

Now it is possible to connect using SSH from an external computer:

$ ssh root@179.133.47.109

Configuring a Host Name for our IP Address

Another thing we can do is to associate our IP address to a host name. For this purpose, we used the NoIP services (www.noip.com). We then created a free account and added a Host. In our case, we chose toradex.noip.me.

Image 2: NoIP Add Host page.

After the Host name is created we can again connect to the module using SSH:

$ ssh root@toradex.noip.me

In our case, when the 3G modem connection is restarted, we always receive a new IP adress. If we try to reconnect to the module, we no longer have access. A workaround was to use a method called Dynamic DNS(Dynamic Domain Name System). NoIP also offers Dynamic DNS services. Further information for using this method can be found here. Basically we send an http request to NoIP with information like IP, hostname, username and password.

To accomplish this request, a small Python program was made. It gets executed when we start the 3G modem connection and follows the sequence as listed below:

After creating the Python program and making it executable with chmod +x, we can move it to /etc/ppp/ip-up.d/, which is a folder where scripts are executed after ppp connection or create a small script which calls our Python program.

Inside /etc/ppp/ we have the files that we created at the beginning of the article as well as other files like ip-down, ip-up, ppp_on_boot. Feel free to explore each one of these files.

ip-up is the script which calls another script or program found inside ip-up.d folder.

In this case, we created a small script inside ip-up.d

#!/bin/bash
python /home/root/noipReq.py>/home/root/noipLog.log

Note: the script is not terminated with .sh

Note that all the print commands inside our Python program can be seen in the log file:

If everything is configured correctly, when we receive a new IP from our network provider, the system automatically updates our host created at NoIP, so it will be possible to connect to the module despite the IP address being changed.

Based on all the information provided in this article, it is possible to achieve many other network activities. One of them is to share the 3G modem network with another computer via Ethernet. For this we configured a basic LAN network between two machines and ran the following command:

iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

Apart from internet sharing, it is also possible to conduct port forwarding and many other network features.

Final Thoughts

As seen in this article, a Linux system has many network functionalities. We just need to properly configure them and have access to ppp0 network. Another important point, is that regardless of the used interface, the programming method remains the same. The task of choosing the interface to use is made automatically by Linux depending on the routes. For those who require network connection in their projects and also need system flexibility, the use of Linux in embedded systems is the way to go.

3 comments

I like this article alot as it provided the essential points in setting up a 3g/4g modem beacuse generally people will mess it up or look for a tutorial up on youtube never to give a thought what they were doing wrong.

Each module has its own configuration. As far as I remember from the modules I worked on in the past, if you use UART modules, you can just use internet or call/sms.

For the USB modules, I remember it emulates more than one UART channel (ttyACM0,1,2...). In that case, it was possible to use one UART to the ppp and the other to send some AT commands. Using the correct AT Commands you can accept calls, receive, and send SMS.

Again, it really depends more on what GSM module you are using.
If you need more help, please feel free to contact us: support.arm@toradex.com

Each module has its own configuration. As far as I remember from the modules I worked on in the past, if you use UART modules, you can just use internet or call/sms.

For the USB modules, I remember it emulates more than one UART channel (ttyACM0,1,2...). In that case, it was possible to use one UART to the ppp and the other to send some AT commands. Using the correct AT Commands you can accept calls, receive, and send SMS.

Again, it really depends more on what GSM module you are using.
If you need more help, please feel free to contact us: support.arm@toradex.com

Thank you,
Raul

Leave a comment

Your email ID will be kept confidential. Required fields are marked *

Reply for:

Author

Email

Comment

Verify Code

Please enter the letters as they are shown in the image above. Letters are not case-sensitive.