Creating a 3G hotspot with the Raspberry Pi
Automatic
An entry in the /etc/network/interfaces
configuration file (Listing 3, lines 4-8) also perform an automatic start with a PPP dial-in connection. If you only want to configure the network device without an automatic connection, you should deactivate line 4, auto ppp0
, by prefixing it with a hash mark (#
). Line 7 executes the setpin
script before the connection is established (Listing 4). This small program waits up to 30 seconds for the modem and sets PIN
as soon as it finds the modem. When the connection has been made, the command in line 8 loads a series of firewall rules. (More on this topic later.)
Listing 3
/etc/network/interfaces
01 auto lo 02 iface lo inet loopback 03 04 auto ppp0 05 iface ppp0 inet wvdial 06 provider umts 07 pre-up /usr/local/sbin/setpin 08 up iptables-restore < /etc/iptables.3ghotspot 09 10 auto br0 11 iface br0 inet static 12 address 192.168.100.250 13 netmask 255.255.255.0 14 network 192.168.100.0 15 broadcast 192.168.100.255 16 bridge-ports eth0 wlan0 17 bridge_maxwait 0
Listing 4
/usr/local/sbin/setpin
01 #!/bin/bash 02 # in case /dev/ttyUSB0 does not exist, wait for it at most 30 seconds 03 let i=1 04 while ! test -c /dev/ttyUSB0; do 05 let i+=1 06 if [ $i -gt 10 ]; then 07 logger -s -t setpin "/dev/ttyUSB0 does not exist" 08 exit 3 09 fi 10 logger -s -t setpin "waiting 3 seconds for /dev/ttyUSB0" 11 sleep 3 12 done 13 14 # check for pin and set it if necessary 15 wvdial pinstatus 2>&1 | grep -q '^+CPIN: READY' 16 if [ $? -eq 0 ]; then 17 logger -s -t setpin "SIM card is ready to use :-)" 18 else 19 logger -s -t setpin "setting PIN" 20 wvdial pin 2>/dev/null 21 fi 22 23 echo -en "sleeping for 3 seconds ..." 24 sleep 3 25 echo " done" 26 exit 0
The connection should be made during system boot once this preparatory work is complete. One way to test the UMTS line is with:
ping -c 3 google.com
The commands
sudo ifdown ppp0 sudo ifup ppp0
let you terminate the connection and dial back in, respectively. The ifup
command is also used if you have deactivated the automatic creation of a connection. Reports on establishing and terminating the connection are always found in the /var/log/messages
logfile. However, details on the creation of connections during system starts are not found in the log, because Linux boots the system long before the system logger starts running.
Monitoring
Typically, a service provider slows data throughput significantly once a predefined volume of data has been transmitted, so it is a good idea to keep an eye on this number, because some mobile phone service contracts might charge for each additional kilobyte that goes beyond that level. The program vnStat [4] monitors data volumes.
To use vnStat, you enter the command
sudo vnstat -u -i ppp0
once the WAN is established by the UMTS stick. This command initiates the process of logging data flow at the UMTS interface. Entering
vnstat -i ppp0
gets current flow statistics; however, this process is somewhat slow. Installing a web front end for vnStat means you do not have to open a terminal window each time you want to check on usage, something I discuss later in the article. It is not a good idea to rely only on the information provided by vnStat, because the daemon only updates the database at scheduled intervals and the Rasp Pi does not have a real-time clock. The nanoPC updates its system time only when it has established a network connection; therefore, the assignment of data volume usage to individual days is not correct when the network connection is initially established.
Buy this article as PDF
(incl. VAT)