Configuring a Mobile Network Connection
To set up the connection with the mobile network, make sure that you have set up the 4G module as described in Hardware Setup and the red LED attached to the 4G module is on. Apart from that, if your SIM Card is protected by a pin or your provider requires you to set the APN manually, refer to the subsection Additional Steps for Some SIM Cards or Providers below.
Start off by installing minicom, which provides a terminal to control the 4G hat using AT-commands, which we will use to define a PDP context and check if your modem can connect to a mobile network.
sudo apt update
sudo apt install minicom -y
Naturally, any other software that accomplishes the same suffices.
Next, try to open a connection to the modem, which should be attached to ttyUSB2:
minicom -D /dev/ttyUSB2
Occasionally, the ModemManager service blocks the channel to ttyUSB2, which results in the error message
minicom: cannot open /dev/ttyUSB2: Device or resource busy
when trying to open a connection through minicom. You can verify that this is the case (and additionally check for any other processes blocking ttyUSB2) by running
sudo lsof /dev/ttyUSB2
In the output of which, ModemMana should be listed somewhere. This issue did not seem to always arise, but as we do not require ModemManager for our weather station’s software, we recommend simply disabling the service, as leaving it enabled sometimes caused trouble, even if /dev/ttyUSB2 was not initially blocked by ModemManager. Do this by executing
sudo systemctl stop ModemManager.service
sudo systemctl disable ModemManager.service
After a succesfully running minicom on ttyUSB2, a console should be opened in which you can enter AT-commands. By default, this console does not echo back the commands you typed, which can be inconvenient for typing in messages. Though, this can be enabled by typing in ATE1 and then pressing Enter. In the minicom console, type in
AT+CGDCONT=1,"IP","super"
to define the PDP context. We are assuming here that you are using IPv4 and your provider does not require you to set the APN yourself. Otherwise, replace "super" by the name of your APN in quotation marks. Wait until you receive an OK as answer and then type in
AT+COPS?
to show the network operator you are registered to. This should result in an answer that looks like
+COPS: 0,0,"name of your network operator",7
Also make sure that your modem is attached to a packet service by running
AT+CGATT?
which should return
+CGATT: 1
If any of these differ, there might be a problem that prevents a successful connection, see the troubleshooting section below for tips from our side. Nonetheless, you may want to try if the steps described below work anyways.
As a next step, try to connect your Raspi to the internet through PPP. To do so, first make sure that ppp is installed. Normally, it should come installed in the OS specified in OS Installation, though you can make sure by executing
sudo apt install ppp -y
Configure your dialup connection in the file /etc/ppp/peers/provider:
sudo nano /etc/ppp/peers/provider
and toggle to the section that contains something like
# MUST CHANGE: replace ******** with the phone number of your provider.
# The /etc/chatscripts/pap chat script may be modified to change the
# modem initialization string.
connect "/usr/sbin/chat -v -f /etc/chatscripts/pap -T ********"
and replace the last line by
connect "/usr/sbin/chat -v -f /etc/chatscripts/gprs"
Below this section, you should find that the serial connection to which the modem shall be connected to defaults to /dev/modem:
# Serial device to which the modem is connected.
/dev/modem
Replace this by
/dev/ttyUSB2
to which your 4G module should be connected to. Lastly, add
nocrtscts
debug
nodetach
ipcp-accept-local
ipcp-accept-remote
to the end of the configuration file (or add all of the lines that are missing). Finally, connect to the internet by executing
sudo pon provider
and set ppp0 as default gateway when the chat script was executed:
sudo route add default ppp0
Finally, verify that everything has worked by disabling Wifi connections through executing
sudo ifconfig wlan0 down
or simply switching Wifi off in the GUI of the Raspi, and trying to ping Google’s DNS server
ping 8.8.8.8
If you are successfully connected through the mobile network, you should not experience a large percentage of package losses.
Lastly, verify that everything works the way it should by turning off the connection
sudo poff provider
and executing the steps from above again. Repeat this a few times and check if everything still works after rebooting the Raspi. Pinging Google’s DNS should always succeed upon successful setup. If you are encountering any difficulties, see the troubleshooting steps from below.
Additional Steps for Some SIM Cards or Providers
In some special cases, your provider may not support that your network automatically supplies a default APN, so you have to set it manually. To do so, open your chat script
sudo nano /etc/chatscripts/gprs
and replace the line
OK AT+CGDCONT=1,"IP","","",0,0
with
OK AT+CGDCONT=1,"IP","your APN name","",0,0
In the above subsection, you also have to enter your APN name when you execute the command AT+CGDCONT=1,"IP","","",0,0 during the minicom session.
Apart from that, if your SIM card is protected by a pin, open sudo nano /etc/chatscripts/gprs and uncomment the line
#OK "AT+CPIN=1234"
and replace 1234 with your pin.
Make sure to execute AT+CPIN=YOURPIN in the minicom terminal before executing the commands from the above subsection.
