3gdongle-connect.sh

3gdongle-connect.sh

This script allows to connect a Raspeberry-based-HotSpot (wireless router) to the internet using a USB 3G Modem Dongle. Modem brands which have been tested are Dlink, Huawei and Qualcomm.
For DLink and Qualcomm modems, we use wvdial with the parameters located in /etc/wvdial.conf:

wvdial.conf:

[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = Analog Modem
; Phone = <Target Phone Number>
ISDN = 0
; Password = <Your Password>
; Username = <Your Login Name>
#Modem = /dev/ttyUSB1
#Baud = 9600

[Dialer dlink]
# Dialer for D-Link dongle
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CFUN=1
# For prepaid card
#Init4 = AT+CGDCONT=1,"IP","prepaidbb"
# For postpaid card
#Init4 = AT+CGDCONT=1,"IP","sunsurf"
# In Bali, comment the line above and uncomment the line below
Init4 = AT+CGDCONT=1,"IP","internet"
Init5 = AT+COPS=0
Password = toto
Phone = *99#
Modem Type = Analog Modem
Stupid Mode = 1
Baud = 1440000  #460800
New PPPD = yes
Dial Command = ATD
Modem = /dev/ttyUSB0
ISDN = 0
Username = toto
Carrier Check = no

[Dialer Huawei]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CFUN=1
#Init4 = AT+CGDCONT=1,"IP","prepaidbb"
Init4 = AT+CGDCONT=1,"IP","sunsurf"
# In Bali, comment the line above and uncomment the line below
#Init4 = AT+CGDCONT=1,"IP","internet"
Init5 = AT+COPS=0
Password = "M1"
Phone = *99#
Modem Type = Analog Modem
Stupid Mode = 1
Baud = 1440000   #460800
New PPPD = yes
Dial Command = ATD
Modem = /dev/ttyUSB0
ISDN = 0
Username = "M1"
Carrier Check = no

[Dialer qualcomm]
# Dialer for Qualcomm dongle
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CFUN=1
# For prepaid card
#Init4 = AT+CGDCONT=1,"IP","prepaidbb"
# For postpaid card
Init4 = AT+CGDCONT=1,"IP","sunsurf"
# In Bali, comment the line above and uncomment the line below
#Init4 = AT+CGDCONT=1,"IP","internet"
#Init5 = AT+COPS=0
Password = M1
Phone = *99#
Modem Type = USB Modem
Stupid Mode = 1
Baud = 115200  #1440000  #460800
New PPPD = yes
Dial Command = ATD
Modem = /dev/ttyUSB0
ISDN = 0
Username = M1 
Carrier Check = no

For the Huawei modem, we use the sakis3g package which can be downloaded with the following command:For the Huawei modem, we use the sakis3g package which can be downloaded with the following command:

wget "http://raspberry-at-home.com/files/sakis3g.tar.gz"
#!/bin/bash
# When used for BALI VILLAS HotSpots
#BROKER=bubuoat.mine.nu
#VILLA=main

# removes the 3gdongle.log file
sudo rm /root/3gdongle.log

my_date=`/bin/date "+%Y.%m.%d at %H.%M.%S"`
echo "###################################" >> /root/3gdongle.log
echo "[3GDONGLE]   Starting 3gdongle    #" >> /root/3gdongle.log
echo "[3GDONGLE] $my_date #" >> /root/3gdongle.log
echo "###################################" >> /root/3gdongle.log
echo "[3GDONGLE] Removing previous files " >> /root/3gdongle.log
sudo rm /root/dlink.txt
sudo rm /root/huawei.txt
sudo rm /root/qualcomm.txt
echo "[3GDONGLE] Detecting modem brand " >> /root/3gdongle.log
dongle=0
brand="Unknown"
dmesg | grep "D-Link" >>  /root/dlink.txt
dmesg | grep "HUAWEI" >>  /root/huawei.txt
dmesg | grep "Qualcomm" >>  /root/qualcomm.txt

if [[ -s /root/dlink.txt ]]; then
	brand="D-Link"
	dongle=1
fi
if [[ -s /root/huawei.txt ]]; then
	brand="Huawei"
	dongle=1
fi
if [[ -s /root/qualcomm.txt ]]; then
        brand="Qualcomm"
        dongle=1
fi
if [ $dongle -eq 1 ]; then
	echo "[3GDONGLE] The modem brand is: " $brand >> /root/3gdongle.log
else
	echo "[3GDONGLE] There is no dongle!" >> /root/3gdongle.log
exit 0
fi

echo "################################" >> /root/ping.log
echo "#   HotSpot has been rebooted  #" >> /root/ping.log
echo "#     $my_date   #" >> /root/ping.log
echo "################################" >> /root/ping.log

echo "#[3GDONGLE] Using "$brand modem #" >> /root/ping.log
 
# Test if the Lan connection is up. If it is up, exit
echo "#####################################################" >> /root/3gdongle.log
echo "[3GDONGLE] Testing whether the LAN connection is Up #" >> /root/3gdongle.log
echo "#####################################################" >> /root/3gdongle.log
eth0up=`dmesg | sed '/eth.*link /h;${x;s/^.*link //;p};d' | awk '{print $1}'`
#echo "LAN Connection is: "$eth0up
if [ "up," == "$eth0up" ]; then
	echo "LAN Connection is: "$eth0up
        echo "# The LAN is active! There is no need to activate the dongle #" >> /root/3gdongle.log
  	exit 0
fi
echo "[3GDONGLE] The LAN is not connected. Start 3gdongle connection" >> /root/3gdongle.log
#####################################################
# Test which dongle we are using (Huawei or D-Link) #
#####################################################
# If dlink.txt size is different from 0, this is a Dlink modem, else this is a Huawei modem
# dlink.txt and huawei.txt are created under the script test.sh, launched inside /etc/rc.local
if [ -s /root/dlink.txt ]; then
        brand="D-Link"
fi
if [ -s /root/huawei.txt ]; then
        brand="Huawei"
fi
if [ -s /root/qualcomm.txt ]; then
        brand="Qualcomm"
fi
#echo "[3GDONGLE] Brand is: "$brand >> /root/3gdongle.log

#Starts usb_modeswitch to ensure the dongle is in Modem mode (not disk storage)
echo "[3GDONGLE] Switch the "$brand "Modem" >> /root/3gdongle.log
if [[ $brand = "D-Link" ]];
then
        sudo modprobe usbserial vendor=0x2001 product=0x7d10   # these parameters are for D-Link dongle
fi
if [[ $brand = "Huawei" ]];
then
	# for this to work, it is mandatory to have the file "12d1/14dc" in /etc/usb_modeswitch.d folder
        sudo modprobe usbserial vendor=0x12d1 product=0x1001   # these parameters are for Huawei dongle
fi
if [[ $brand = "Qualcomm" ]];
then
        sudo modprobe usbserial vendor=0x5c6 product=0x6000   # these parameters are for Qualcomm dongle
fi

#lists the available ports for modem connection
ls /dev | grep ttyUSB #>> /root/3gdongle.log

#Triggers the Full Functionality mode
if [[ $brand = "D-Link" ]];
then
        sudo printf "ATZ^M" >&/dev/ttyUSB0
        sudo printf "AT+CFUN=1^M" >&/dev/ttyUSB0
fi
if [[ $brand = "Huawei" ]];
then
        sudo printf "ATZ^M" >&/dev/ttyUSB0
        sudo printf "AT+CFUN=1^M" >&/dev/ttyUSB0
fi
if [[ $brand = "Qualcomm" ]];
then
        sudo printf "ATZ^M" >&/dev/ttyUSB0
        sudo printf "AT+CFUN=1^M" >&/dev/ttyUSB0
fi

# triggers the hotplug for the modem (to allow 3g connection when the modem is plugged-in after boot - NOT TESTED)
#sleep 1
#sudo udevadm trigger --subsystem-match=usb

# Launches Modem connection to network
# dlink is the configuration for D-Link dongle
if [[ $brand = "D-Link" ]];
then
        echo "[3GDONGLE] Calling Wvdial with "$brand "dialer" >> /root/3gdongle.log
	# starts the script to ping 8.8.8.8
#        sudo /root/ping_test.sh &
        # launches the connection to Internet through wvdial with "dlink" profile
	sudo wvdial dlink &
#        sleep 10
#        echo "" >> /root/3gdongle.log
#        echo "############################### IFCONFIG #####################################" >> /root/3gdongle.log
#        ifconfig >> /root/3gdongle.log
#        echo "##############################################################################" >> /root/3gdongle.log
fi
if [[ $brand = "Huawei" ]];
then
        echo "[3GDONGLE] Calling Sakis3g with "$brand "dongle" >> /root/3gdongle.log
#        sudo /root/ping_test.sh &
#        sudo /usr/bin/modem3g/sakis3g USBMODEM="12d1:14dc" USBINTERFACE="0" APN="sunsurf" APN_USER="M1" APN_PASS="M1" start
        sudo /usr/bin/modem3g/sakis3g USBMODEM="12d1:155e" USBINTERFACE="0" APN="sunsurf" APN_USER="M1" APN_PASS="M1" start
#        sleep 10
#        echo "" >> /root/3gdongle.log
#        echo "############################### IFCONFIG #####################################" >> /root/3gdongle.log
#        ifconfig >> /root/3gdongle.log
#        echo "##############################################################################" >> /root/3gdongle.log
fi
if [[ $brand = "Qualcomm" ]];
then
        echo "[3GDONGLE] Calling Wvdial with "$brand "dialer" >> /root/3gdongle.log
        # starts the script to ping 8.8.8.8
#	sudo /root/ping_test.sh &
        # launches the connection to Internet through wvdial with "qualcomm" profile
	sudo wvdial qualcomm &
#        sleep 10
#        echo "" >> /root/3gdongle.log
#        echo "############################### IFCONFIG #####################################" >> /root/3gdongle.log
#        ifconfig >> /root/3gdongle.log
#        echo "##############################################################################" >> /root/3gdongle.log
fi
        sudo /root/ping_test.sh &
        sleep 10
        echo "" >> /root/3gdongle.log
        echo "############################### IFCONFIG #####################################" >> /root/3gdongle.log
        ifconfig >> /root/3gdongle.log
        echo "##############################################################################" >> /root/3gdongle.log

sleep 20
echo "[3GDONGLE] ######################## IPTABLES ##############################" >> /root/3gdongle.log
iptables -S >> /root/3gdongle.log
echo "[3GDONGLE] ################################################################" >> /root/3gdongle.log

echo "[3GDONGLE] End Of connection process for "$brand "dongle." >> /root/3gdongle.log
echo "" >> /root/3gdongle.log

# When used for BALI VILLAS HotSpot
# Sends MQTT message to indicate device is rebooted
#mosquitto_pub -h $BROKER -t "bali/$VILLA/internet" -m "Rebooted"
# Sends an MQTT message to report the status of the internet connection
#sleep 10
#sudo mosquitto_pub -h $BROKER  -p 1883 -t "bali/$VILLA/internet" -m "Up"

exit 0