Connect raspberry pi to a 3g network automatically during its boot

This article is for those who want to connected their Raspberry pi with debian OS, automatically to 3g network without any human interaction. This is mostly useful if the pi is left alone at certain location and should connect itself to 3g network at boot. In my case I wanted to use the pi as a 3g to wan converter(3G router), to replace the faulty dlink dwr 113, so autoconnect to network is a default property of a 3g router.
This article is divided into 5 simple steps. Taking into consideration that you already have a raspbian installed pi at hand(if not go to http://goo.gl/4bNkyW for directions on how to setup raspbian using ubuntu PC ), and a working 3g usb data card, Lan cable.This entire article was done using ubuntu pc to ssh into the pi, and i didnt require any display or seperate keyboard and mouse( accessed the pi headlessly)

To do this I first powerup the pi & ssh into the pi’s terminal using putty(windows/ubuntu) or terminal(ubuntu). For direction to use the pi headlessly visithttp://goo.gl/9XK12Q.
Step1: INSTALL LATEST VERSION OF USB-MODESWITCH PROGRAM
Once ssh into the pi is successful, search if the usb-modeswitch program is installed into the pi using the command
usb-modeswitch –help
auto1

Incase it is not installed, then install it by typing the following command onto the the terminal
sudo apt-get install usb-modeswitch
auto2

usb-modeswitch program works automatically for most of huawei devices, where as for all other devices a specialized script needs to be created. For more info about usb_modeswitch have a look at http://goo.gl/QQdMjr. This website contains direction to create specialized script for diffrent manufactures of Usb modem.In my case no script was needed as my huawei modem E162G is automatically switched while plugged in. For other modems a script might be required for automatically mode-switching it.

Step2: INSTALL SAKIS 3G PROGRAM AND MAKE IT EXECUTABLE
Sakis3g is a out-of-the-box working script for most of huawei devices. All needed is to use the interactive gui to connect to the pirticular 3g usb modem.
sakis3g cannot be downloaded from the debian libraries using the apt-get install command. Its an application that allows to conenct the pi to internet using various 3g modems.

To install sakis3g first download the program on to the default location of the pi using the command

wget "http://darknet.co.za/wiki/uploads/Posts/sakis3g.tar.gz"

auto 3

The downloaded package is a compessed package and needs to be extracted.
To extract the sakis3g.tar.gz file use the command tar -xzvf sakis3g.tar.gz
auto4
The ls command displays all files and folder in current location.
Once extracted the sakis3g script file is present.
auto 5
Now the sakis3g script is extracted and needs to be converted to a executable file. Usually it is set by default but incase it is not just type the following command on terminal.
chmod +x sakis3g.
For making the script automatic and not ask for admin password each time it is used, we can change the script to a system default app and giving super user rights. To do so move the sakis3g file to /opt.
Enter the following script on terminal
sudo mkdir -p /opt/sakis3g/
sudo mv sakis3g /opt/sakis3g
sudo chown root:root /opt/sakis3g/sakis3g
sudo ln -s /opt/sakis3g/sakis3g /usr/bin


auto 6
Now sudo owns the script.
run sudo visudo
auto7
add the following line at the last line.
your-username ALL = NOPASSWD: /opt/sakis3g/sakis3g
auto8
For basic details of sakis3g go to http://goo.gl/NeBgP2

Step3: INSTALL ppp PROGRAM
Type the following command on pi’s terminal sudo apt-get install ppp
auto8.1

Step4: CONFIGURE THE SAKIS3G PROGRAM WITH THE sakis3g.conf FILE.
To configure the sakis3g.conf file, insert the 3g modem on to the pi. Type the lsusbcommand and confirm that the usb 3g modem is dectected.
auto9
Now we need to get 2 parameters that are specific for the 3g usb modem. these parameters are usbdriver, and usbinterface. These 2 parameters are needed to create a working sakis3g.conf file.
Type sudo sakis3g –interactive
auto10

select the first option
auto11

If the modem is automatically switched(happens for most of huawei devices),the following parametes, locationg device,locating tty,preparing modem,resolving connection details occours automatically, then the sakis3g program asks for ‘APN’
auto12

Once the ‘APN’ is entered it asks for userid and password of the 3g network. In my case there is no id and password required so type (blank empty space) and enter.Do not leave the userid and password option empty as this will abort the connection. The sakis3g then displays connecting, fixing connection and then it displays connected.
auto14

Once connected go to ‘more option’–’generate success report’
auto15

From the picture it can be obseved that the USBDRIVER is ‘option’ and USBINTERFACE is not mentioned or not needed, MODEM is “12d1:1001″, APN is “bsnlnet”
Now create a file with all the above parameters.
Type the command sudo nano /etc/sakis3g.conf
auto16

Type the following details onto the file and save it using “CTRL+X”

USBDRIVER="option"
#USBINTERFACE="3"
APN="bsnlnet"
APN_USER=" "
APN_PASS=" "
MODEM="12d1:1001"

Modify the setting as per your connection success report of sakis3g
auto17

Once the sakis3g.conf file have been saved type the command

/opt/sakis3g/sakis3g --sudo "connect" 

and the modem connects itself after approx 15 seconds.
auto18

Type the command

/opt/sakis3g/sakis3g --sudo "disconnect"

and the modem disconnects itself after approx 15 seconds.
auto19

For advanced direction on how to use sakis3g program, go to http://goo.gl/b9QrN9. I have refered to this blog for directions on sakis3g.

Step5: CREATE AN AUTOMATIC SCRIPT THAT RUNS AT BOOT/POWEROFF TO CONNECT/DISCONNECT AUTOMATICALLY.
When you create a boot script this way, you must understand its harmfull effects as well. if the modem fails to connect at certain times, the boot will fail and ssh into the pi cant be established successfully. To avoid this problem I have added a sleep timer for this script so this script sleeps while the booting function and executes once the timer expires or boot is complete, whichever comes first.

To create this script file I have refered to http://goo.gl/eD9eOl. For advanced user, do visit this website for doubts or suggestions.

create a script file in etc/init.d . All script files inside init.d are started on boot and stopped on poweroff. Hence this is the right place for our script.
Type the command
sudo nano /etc/init.d/autoconnectnet
auto19

and paste the following code in the new opened file .

#***************************************************
#! /bin/sh
# /etc/init.d/autoconnectnet

### BEGIN INIT INFO
# Provides:          noip
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to start a program at boot
# Description:       A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO


case "$1" in
  start)
    sleep 10
    echo "connecting via sakis3g"
    # run application you want to start
    /opt/sakis3g/sakis3g --sudo  "connect"
    ;;
  stop)
    echo "dissconnecting via sakis3g"
    # kill application you want to stop
    /opt/sakis3g/sakis3g --sudo  "disconnect"
    ;;
  *)
    echo "Usage: /etc/init.d/autoconnectnet {start|stop}"
    exit 1
    ;;
esac

exit 0
#*********************************************************

auto19
and CTRL+X to save it.

The saved is is not yet and boot executable file. To make it executable type the command
sudo chmod 755 /etc/init.d/autoconnectnet
auto20

Once the script has been modified as executable, type the command
sudo /etc/init.d/autoconnectnet start
The pi get connected to network after the sleep 10 timer is set.
sudo /etc/init.d/autoconnectnet stop

auto21
and confirm that they execute without any user interaction. As user interactions might halt the boot process and hang the system.

Once it is successfull, register the script to run at startup and shutdown by entering the following command.
sudo update-rc.d autoconnectnet defaults
auto21

If the script needs to be removed from startup, in case it is causing the system to halt etc: run the following script.
sudo update-rc.d -f autoconnectnet remove
auto22

Thats all.. Restart the pi with the usb 3G modem connected and checkout if it works successfully. Incase of any errors, remove the usb modem and boot and then remove the script with startup and trouble-shoot.
I have refered to http://goo.gl/eD9eOl for direction to create a auto run script that executes itself on boot. For advanced users go to that website for doubts and suggestions…
Incase you face any touble with this setup, reply here so some one might help to solve the issue..