Configuration of an LTE modem on the Raspberry Pi
-------------------------------------------------

[HUAWEI 4G Dongle E3372](https://consumer.huawei.com/za/routers/e3372/specs/) is an USB 2.0 device with a microSD card reader and a slot for a standard SIM Card

![Huawei-e3372-rpi-image](Huawei_e3372_rpi.jpg)

After plugging in the USB dongle check if Linux recognized it. See `Huawei Technologies Co., Ltd. Modem/Networkcard`

`lsusb`
```
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 04d9:0007 Holtek Semiconductor, Inc. 
Bus 001 Device 005: ID 12d1:1506 Huawei Technologies Co., Ltd. Modem/Networkcard
Bus 001 Device 003: ID 2516:012f Cooler Master Co., Ltd. 
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
```
Check if the driver created the modem device. You should see the `/dev/cdc-wdm0`

`ls /dev/cdc*`
```
/dev/cdc-wdm0
```
Install `NetworkManager`

`sudo apt install network-manager`

Check modems avalable to the NetworkManager

`mmcli --list-modems`
```
    /org/freedesktop/ModemManager1/Modem/0 [huawei] E3372
```
Check status of the modem

`mmcli --modem=/org/freedesktop/ModemManager1/Modem/0`
```
  --------------------------------
  General  |            dbus path: /org/freedesktop/ModemManager1/Modem/0
           |            device id: 46c19d81068f7f5001140bf211de0b717b981698
  --------------------------------
  Hardware |         manufacturer: huawei
           |                model: E3372
           |             revision: 21.200.07.00.55
           |            supported: gsm-umts
           |              current: gsm-umts
           |         equipment id: 861821033381192
  --------------------------------
  System   |               device: /sys/devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.3
           |              drivers: option1, huawei_cdc_ncm
           |               plugin: Huawei
           |         primary port: ttyUSB1
           |                ports: ttyUSB0 (at), ttyUSB1 (at), cdc-wdm0 (at), wwan0 (net)
  --------------------------------
  Status   |       unlock retries: sim-pin (3), sim-pin2 (3), sim-puk (10), sim-puk2 (10)
           |                state: connected

           |          power state: on
           |          access tech: lte
           |       signal quality: 70% (recent)
  --------------------------------
  Modes    |            supported: allowed: 2g; preferred: none
           |                       allowed: 3g; preferred: none
           |                       allowed: 4g; preferred: none
           |                       allowed: 2g, 3g, 4g; preferred: none
           |              current: allowed: 2g, 3g, 4g; preferred: none
  --------------------------------
  IP       |            supported: ipv4
  --------------------------------
  3GPP     |                 imei: 861821033381192
           |          operator id: 23101
           |        operator name: Orange
           |         registration: home
  --------------------------------
  3GPP EPS | ue mode of operation: csps-2
  --------------------------------
  SIM      |            dbus path: /org/freedesktop/ModemManager1/SIM/0
  --------------------------------
  Bearer   |            dbus path: /org/freedesktop/ModemManager1/Bearer/0
```
Check the SIM card status

`mmcli --sim=/org/freedesktop/ModemManager1/SIM/0`
```
  ---------------------------
  General    |     dbus path: /org/freedesktop/ModemManager1/SIM/0
  ---------------------------
  Properties |          imsi: 231014462647671
             |         iccid: 8942101180024842166
             |   operator id: 23101
             | operator name: Orange
```
First check if the NetworkManager is running

`sudo systemctl status NetworkManager`  

If not, start it

`sudo systemctl start NetworkManager`

Create configuration for the modem. Set `con-name 'Orange-LTE'` to any name and `apn 'internet'` to the APN name of your mobile provider

`sudo nmcli connection add type gsm ifname '*' con-name 'Orange-LTE' apn 'internet' connection.autoconnect yes`

The configuration file is created with the con-name as filename and nmconnection as the suffix. Edit if neccessary

`sudo cat /etc/NetworkManager/system-connections/Orange-LTE.nmconnection`
```
[connection]
id=Orange-LTE
uuid=8dc159fe-e0ed-4a56-9b7f-fb813e2c4753
type=gsm
permissions=

[gsm]
apn=internet
number=*99#

[ipv4]
dns-search=
method=auto

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto
```
Check status of the network device `wwan0`. It should have an IP address assigned by your mobile provider (e.g. 10.121.79.142)

`ip addr show wwan0`

```
4: wwan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:1e:10:1f:00:00 brd ff:ff:ff:ff:ff:ff
    inet 10.121.79.142/30 brd 10.121.79.143 scope global noprefixroute wwan0
       valid_lft forever preferred_lft forever
    inet 169.254.62.145/16 brd 169.254.255.255 scope global noprefixroute wwan0
       valid_lft forever preferred_lft forever
```
Check if the internet is reachable

`ping -I wwan0 www.google.com -c 5`
```
PING www.google.com (142.251.37.100) from 10.121.79.142 wwan0: 56(84) bytes of data.
64 bytes from prg03s13-in-f4.1e100.net (142.251.37.100): icmp_seq=1 ttl=117 time=23.5 ms
64 bytes from prg03s13-in-f4.1e100.net (142.251.37.100): icmp_seq=2 ttl=117 time=36.9 ms
64 bytes from prg03s13-in-f4.1e100.net (142.251.37.100): icmp_seq=3 ttl=117 time=32.7 ms
64 bytes from prg03s13-in-f4.1e100.net (142.251.37.100): icmp_seq=4 ttl=117 time=43.7 ms
64 bytes from prg03s13-in-f4.1e100.net (142.251.37.100): icmp_seq=5 ttl=117 time=29.7 ms

--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 11ms
rtt min/avg/max/mdev = 23.539/33.298/43.704/6.781 ms
```
Make the NetworkManager start automaticaly after the boot

`sudo systemctl enable NetworkManager`

Monitor data usage
------------------

Install `vnstat`. Gathered statistics persists through system reboots

`sudo apt install vnstat`

Check monthly data usage

`vnstat --iface wwan0 --months`
```
wwan0  /  monthly

        month        rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       2022-11       1.05 GB |     4.32 GB |     5.37 GB |   17.78 kbit/s
       2022-12     893.89 MB |     3.68 GB |     4.55 GB |   14.60 kbit/s
       2023-01       1.14 GB |     5.10 GB |     6.24 GB |   20.01 kbit/s
       2023-02       1.21 GB |     4.93 GB |     6.14 GB |   21.80 kbit/s
       2023-03       1.23 GB |     5.64 GB |     6.87 GB |   22.03 kbit/s
       2023-04       1.15 GB |     5.22 GB |     6.37 GB |   21.11 kbit/s
       2023-05       1.23 GB |     4.39 GB |     5.62 GB |   18.04 kbit/s
       2023-06       2.68 GB |     7.01 GB |     9.69 GB |   32.10 kbit/s
       2023-07       1.61 GB |     6.94 GB |     8.55 GB |   27.41 kbit/s
       2023-08       1.76 GB |     7.65 GB |     9.41 GB |   30.19 kbit/s
       2023-09       1.82 GB |     7.99 GB |     9.81 GB |   32.52 kbit/s
       2023-10       1.71 GB |     7.64 GB |     9.35 GB |   44.80 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated       2.56 GB |    11.41 GB |    13.97 GB |
```
See realtime traffic

`vnstat --iface wwan0 --live`
```
Monitoring wwan0...    (press CTRL-C to stop)

   rx:        0 kbit/s     1 p/s          tx:        1 kbit/s     1 p/s
   rx:        2 kbit/s     4 p/s          tx:        3 kbit/s     3 p/s
   rx:        1 kbit/s     1 p/s          tx:        1 kbit/s     1 p/s^C


 wwan0  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                        3 KiB  |           6 KiB
--------------------------------------+------------------
          max               2 kbit/s  |        3 kbit/s
      average            0.76 kbit/s  |     1.51 kbit/s
          min               0 kbit/s  |        1 kbit/s
--------------------------------------+------------------
  packets                         49  |              47
--------------------------------------+------------------
          max                  4 p/s  |           3 p/s
      average                  1 p/s  |           1 p/s
          min                  0 p/s  |           0 p/s
--------------------------------------+------------------
  time                    30 seconds
```

