Note: Please contact us to confirm instructor availability and for remote lesson ordering instructions.
We offer remote lessons on basic Raspberry Pi gadget building. Suppose you have a Raspberry Pi device, some other sensors and parts to connect to it, and need help to get started. This is where remote Raspberry Pi tutoring may help.
In this example lesson we show how to connect a cellular module to a Pi and make it connect wirelessly over cellular network. This rig could later be used as a foundation for IOT gadgets.
We will use the following parts for this project:
We'll first try to get Raspberry Pi up and running. This should verify that that we have a working board and that the OS is properly installed. We do it by doing the following:
Now let's connect our cellular module to Raspberry Pi. Our example setup occurs in Vancouver, British Columbia, Canada on Shaw Mobile network. For this to work we need have the following.
Name: Internet APN: wsp.shaw.ca Proxy: Not Set Username: Not Set Password: Not Set Server: Not Set MMSC: Not Set MMS Proxy: Not Set MMS Port: Not Set MCC: 302 MNC: 490 Authentication type: Not Set Name: MMS APN: mms.shaw.ca Proxy: Not Set Username: Not Set Password: Not Set Server: Not Set MMSC: http://mms.shaw.ca (iPhone Only) MMS Proxy: 18.104.22.168 MMS Port: 8080 MCC: 302 MNC: 490 Authentication type: Not Set APN Type: mms
Some resources on the Internet mention that you might need a 2A power supply for Pi and this hat to work together nicely. In this lesson we used a more powerful supply with connected USB breakout. Our power supply can display currently consumed current as displayed on the picture below.
In our experiments we never saw consumed current to go over 1A. However, the Pi itself still occasionally showed us a low voltage warning when 4G hat was connected via a single USB cable (no connection over GPIO pins). PS displayed a solid 5 volt output all the time. Why this happens and how to fix this is beyond the scope of this lesson.
Below is a picture of our rig with 2A USB power supply, which works just as good.
Make sure to connect the antenna to your 4G hat! Otherwise you may not be able to connect to your network. It is as simple as that. Save yourself hours of troubleshooting by connecting the antenna properly in the first place.
For our cellular connectivity to work we have to insert a SIM card into a slot on the back of the cellular module. Here is how it looks with inserted SIM card:
Now it's time to connect everything together. We need 2 USB to Micro USB cables. One goes from Raspberry Pi to our power supply breakout (black one on the pictures below). Another goes from a Raspberry Pi USB port to 4G hat (white one). Note that we don't use GPIO pins.
There is a power on button on 4G hat identified as PWRKEY. Press, hold it for approximately 1 second, then release to turn the hat on. The NET LED comes on. If all is good it should become flashing after approximately 10 seconds.
Some documentation for the cellular module is here. Among other things, it says that if you want to let the module power on automatically after booting, you can connect PWR pin to GND and connect P7 pin to GND as illustrated by the following picture.
Waveshare documentation also has the following on the NET LED status:
Always on: seacrhing network, call connect. 200 ms on, 200 ms off: data transmit, 4G registered. 800 ms on, 800 ms off: 2G/3G registered network. Off: power off, sleep.
Which means we can determine if we are connected to 4G (faster) network rather than 2G/3G (slow) by how fast the LED is blinking, if at all.
We used what is excellently covered in this resource to set up our hat. Detailed steps are below.
We first update our Raspberry Pi with the following commands:
sudo apt update -y sudo apt dist-upgrade -y sudo rpi-update
Reboot after all updates are applied.
We have to enable serial port hardware for the Pi to communicate with our cellular module. Start raspi-config as so:
Then select Interface Options followed by P6 Serial Port. Make sure that serial port hardware is enabled. Reboot if required.
I made a mistake of not enabling it in my initial experiments. I was able to establish connections as described below. However Internet connectivity was quickly being lost (within minutes). Serial port communication is required to keep it up. Do not repeat my mistake, enable serial port hardware.
We will need libqmi-utils to communicate with a cellular module and udhcpc to acquire an IP address for Raspberry Pi from mobile network. We can install these packages using the following command:
sudo apt install libqmi-utils udhcpc
We can now run qmicli commands to talk with our 4G hat.
pi@raspberrypi:~ $ sudo qmicli -d /dev/cdc-wdm0 --nas-get-home-network [/dev/cdc-wdm0] Successfully got home network: Home network: MCC: '302' MNC: '490' Description: 'Freedom'
The response above indicates a successful detection of a home network. We see that MCC and MNC values match with what Shaw Mobile provided us with.
There are many qmicli commands available. You can run this to see them all:
We can start using raw_ip protocol with the module by issuing the following commands:
sudo ip link set wwan0 down echo 'Y' | sudo tee /sys/class/net/wwan0/qmi/raw_ip sudo ip link set wwan0 up
We will now use the APN provided by ISP to connect to cellular network as so:
sudo qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='wsp.shaw.ca',ip-type=4" --client-no-release-cid
If all goes well, the result could be something like below:
pi@raspberrypi:~ $ sudo qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='wsp.shaw.ca',ip-type=4" --client-no-release-cid [/dev/cdc-wdm0] Network started Packet data handle: '3785466208' [/dev/cdc-wdm0] Client ID not released: Service: 'wds' CID: '20'
We can use udhcpc to lease an IP address for our Raspberry Pi as so:
sudo udhcpc -i wwan0
A successful lease may look like the following:
pi@raspberrypi:~ $ sudo udhcpc -i wwan0 udhcpc: started, v1.30.1 No resolv.conf for interface wwan0.udhcpc udhcpc: sending discover udhcpc: sending select for 10.187.89.70 udhcpc: lease of 10.187.89.70 obtained, lease time 7200 Too few arguments. Too few arguments.
We have now connected our Raspberry Pi to the Inernet using cellular network. We can ping outside hosts, browse the Internet, check emails, etc.
We can use the following shell script to automate bringing up cellular network after Raspberry Pi boot:
#!/bin/bash echo 'starting...' ip link set wwan0 down echo 'Y' | tee /sys/class/net/wwan0/qmi/raw_ip ip link set wwan0 up echo 'trying to start network...' qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='wsp.shaw.ca',ip-type=4" --client-no-release-cid echo 'sleeping 10 seconds' sleep 10 echo 'about to call udhcpc' udhcpc -i wwan0 -f -n -q echo 'after a call to udhcpc' ip a s wwan0 echo 'ending...'
And executing it 2 minutes after boot with the following crontab entry. This command assumes our is /home/pi/start_network.sh:
@reboot sleep 120 && sudo /home/pi/start_network.sh > /home/pi/start_network.log
Long term (overnight) experiments with this rig show us that cellular Internet connectivity occasionally disappears. Perhaps due to inactivity on sockets, or because of some other reason. Also, sometimes the qmicli command to start network fails for unknown reasons. We can fix this by rebooting our Pi every hour like so in crontab:
55 * * * * sudo reboot
The above cron entry reboots our Pi at 55 minutes every hour, therefore forcing it to reconnect. This means that if / when things go haywire, we may have a chance to recover connectivity in an hour. Here is how our crontab file looks like with both lines in:
@reboot sleep 120 && sudo /home/pi/start_network.sh > /home/pi/start_network.log 55 * * * * sudo reboot
Please contact us to order a remote tutoring lesson.