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 visit http://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 lsusb command 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..

Advertisements
This entry was posted in Debian, Electronics, Linux OS, Networking, Raspberry pi, Tech, Ubuntu, Uncategorized and tagged , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

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

  1. JL says:

    Hi! ….Is there any way to re-establish the connection automatically when it is down? Your procedure is ok, if I do not want to use the Sakis3g interface after booting. But, what if after some minutes the connection is down? I think I should restart my RPi and see if the connection works….

  2. Pingback: DevPi Changelog | I am the BFG

  3. Pingback: Some Electronics Stuff for young engineers !!! | Brewing Thoughts

  4. jsp.y0.pl says:

    Since NLP had no professional standing, thanks to Bandler’s anti-establishment
    stance, it was extremely vulnerable to attack to any new product
    that occupied its target market. Instead of openly saying “we want more money and this is an easy way to do it,” Google claims that it’s all
    done in order to improve user experiences. These blocks can be dirt,
    cobblestone, wood, or any other solid block.

  5. Pingback: Raspberry Pi + Huawai E156G 3G | RaspberryPi Greenhouse

  6. faaiz says:

    hi lorentz
    i just want to ask if you know how to make GPS to auto active like this 3G Modem during RPi boots? wait for your good news..tq

    • lorentz says:

      hi
      yes it is possible. pls use usb gps for that, as uart gps might have to be level converted before connecting to the gpio. once boot is complete a small code in python might be able to open the usb port and read the serial gps strings

  7. Saman shaikh says:

    The script works well with my ubuntu 13.10 when i run the “sudo /etc/init.d/autoconnectnet start” command in terminal… but i also get a gui at the end. Can you suggest some ways to get rid of the gui

  8. Anonymous says:

    hi, when i do the sudo sakis3g –interactive
    there is command not found error. Why does it be?

    • Anonymous says:

      try – – (with no space between the minus signs) rather than cut/paste the line above, which combines the two – symbols into a single character

  9. Neelesh says:

    thanks. with the help of above procedure (given step wise) I connected my USB Micromax 352 3G dongle to my Desktop with ubuntu 13.10. It was realy very helpfull
    thanks a lot again

  10. Abdu says:

    I got my E352 connected using sakis3g. but I have no internet. I can’t ping any address :(. the dongle is connected according to Sakis3g results. The peer to peer address is 10.64.64.64 and the ip address is 10.144.55.229. Any idea what could be the reason??

  11. sachin dawane says:

    I am unable to get connected using sakis3g on boot up on beaglebone black..
    and able to connect by typing following command in terminal
    sudo /etc/init.d/autoconnectnet start
    it works without any user input and get connected to internet via usb modem
    but when i am doing to do at boot up time i am not getting connected
    when i type this command
    sudo update-rc.d autoconnectnet defaults
    i m getting following in terminal
    perl: warning: Setting locale failed.
    perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = “en_IN”
    are supported and installed on your system.
    perl: warning: Falling back to the standard locale (“C”).
    update-rc.d: using dependency based boot sequencing
    plase help

  12. Alan says:

    Awesome!
    Now I can stream live audio with my pi anywhere were there is a mobile internet signal!!
    Will be conducting tests with a binaural dummy head microphone in the next few weeks.
    Hope to have a webserver up and running so that you can see if I am on-line over at http://lan2lan.no-ip.com – should have a link to the stream there when up and working – be sure to put your headphones on or you wont get the 3D effect.

    Thank you so much for your “how to” I would not have been able to do this without your help.
    Thank you!

    Alan

  13. nani says:

    i run sudo sakis3g — interactive and after connect and next select modem type in that next select HUAWEI MOBILE but running sakis3g for connect to tty an d last it gives error occured like failed to connect …help me what to do..

    • binauralhead says:

      What Huawei dongle are you using?
      I have found that the one’s which give you a private IP (192.168.1.*) are going through NAT and are not PPP! Tried a few flavors of Huawei dongles, and only found out why I was not getting drivers, or other errors with them.

      Might be of help I guess.

      Works great with a e156G – it connects well, but another thing to remember is the APN too, as you could get private IP (10.0.0.*) allocation, eg : an APN of “internet” on three.co.uk gets private, but an APN of “3internet” gets you a public IP. They will not tell you this!

      Good luck

      • lorentz says:

        i am not using huawei modem, i used dlink dwm 156 with hardware version a6, huawei modems are easy to connect but always prefer e153 or e1553 modules, latest modules have different command to mode switch between cd install mode and modem mode

    • lorentz says:

      You might have selected the wrong device. Check etc dev

  14. samrat says:

    We are using Huawei modem, and every time Rpi is restarted MODEM’s id got change.
    MODEM=”12d1:10xx” last two digit got changed

    • lorentz says:

      The last 2 digits will change accoding to modes of the modem #. 1st is software installer cd mode ## and 2nd is modem mode

      • Anonymous says:

        Thank you Lorentz for your help on this. I also have this issue and I cannot connect to it once I reboot the Raspberry Pi.
        How can I solve this issue? Is there anyway I can refer to the USB device differently or how can i fix the addess=”12d1:1014″ for example.

      • lorentz says:

        Could you send the address before and after mode switch.

      • Nico says:

        Thank you Lorentz,

        The usual addresses are “19d2:0124″ and “12d1:0149″. I have been able to make it work on reboot, but once i add or take away one USB device, the address changes and it doesn’t connect anymore. the thing is, that I have to have it solved regardless the number if USB devices connected.

        Best regards,
        Nico.

  15. Nico says:

    Thank you Lorentz,

    The usual addresses are “19d2:0124” and “12d1:0149”. I have been able to make it work on reboot, but once i add or take away one USB device, the address changes and it doesn’t connect anymore. the thing is, that I have to have it solved regardless the number if USB devices connected.

    Best regards,
    Nico.

  16. Amir says:

    Thank you Lawrence, with best regards from Vienna!

  17. Anonymous says:

    Thank you for the detailed instructions.
    I have succeed til the last step.
    But update-rc,d doesn’t seem to give this response “update-rc.d : using dependency based boot sequencing”. then the service failed when reboot.
    What should I do?

    Thankyouuuu.

    • Anonymous says:

      it has failed due to the issue with the script not able to run at boor, either the modem is connected to another port, or modem not visible to the rpi.

      • Ashish Mahendra says:

        How to resolve this issues, even am encountering te same issue. Tried lot of things, but was not useful.

  18. jerry says:

    sudo wget “http://darknet.com.za/wiki/uploads/Posts/sakis3g.tar.gz” says unable to resolve host adress
    i install sakis from sakis3g.com en go to the next step but then i want sudo mv sakis3g/opt/sakis3g
    mv missing destinatio file operand after sakis3g/opt/sakis3g

    help

  19. Anonymous says:

    Hi Thanks for this GREAT Topic 🙂
    This works for me but :
    Once connected, the DNS used by the PI in /etc/resolv.conf is the local default IP of the PI instead of my google dns IP or operator dns ip… Hence i can ping IPs but not nameservers like google.com
    2nd, I use also a USB Wifi dongle and from there the DNS from google configured in my DHCP of the PI is blank… so no browsing possible from a client either… Do you have a clue to put another DNS address each time sakis3g connects ?
    (on ethernet the dhcp uses correctly the DNS from google, locally and from client side through my AP)

    thanks a LOT

  20. Thomas Noret says:

    Hi Thanks for this GREAT Topic 🙂
    This works for me but :
    Once connected, the DNS used by the PI in /etc/resolv.conf is the local default IP of the PI instead of my google dns IP or operator dns ip… Hence i can ping IPs but not nameservers like google.com
    2nd, I use also a USB Wifi dongle and from there the DNS from google configured in my DHCP of the PI is blank… so no browsing possible from a client either… Do you have a clue to put another DNS address each time sakis3g connects ?
    (on ethernet the dhcp uses correctly the DNS from google, locally and from client side through my AP)

    thanks a LOT

  21. If you desire to grow your knowledge simply keep visiting this site and bbe updated with the latest news posted here.

  22. libas nw says:

    hi,
    i am not able to switch my huawei dongle E303f from “CDROM” mode to “MODEM” mode. wat might be the problem and any solution for that?. i have used the usb_modeswitch as well but i didnt Help !!

    thanks in advance!

    • binauralhead says:

      I guess the modem type you have is of the wrong type, some modems have a built in router feature, the E156G works fine as there is no router feature (you can’t login to it via a browser [ex: 192.168.1.1] to make changes). .

      • libas nw says:

        i also tried my E173 huawei dongle . i get the same problem..!!
        i want to connect my dongle to the pi and establish intenet connecion. i almost understood all the commands and the sakis3g script . but for worrking with sakis3g i must first convert the dongle from CDROM to MODEM mode. am unable to do that.

      • binauralhead says:

        Can you login to the E173, in a browser?
        I have tried a few different dongles (cant remember which ones they were) to see if they performed any better, but I found that the dongles that you can login to (on an ordinary OS like windows) via http://192.168.1.1 etc would not work, they would not switch at all. I found that using a USB powered hub for the dongle was more reliable too.
        Good luck, please post back if you sort it :o)

  23. Anonymous says:

    Hi Lawrence,
    thanks a lot for your article. It helps me a lot to get my Huawei E303 working on my Beaglebone black Rev C with Debian. Except for the download link (sudo wget “http://www.sakis3g.com/downloads/sakis3g.tar.gz” -O sakis3g.tar.gz works better) everything was fine and well-documented. So again, thanks a lot. Regards Marc

  24. libas nw says:

    hi i am using sakis3g to connect my pi . things work fine but i noticed that on my sakis3g doesnt accept anyother interface other than “0” or “1”. i.e suppose if i give USBINTERFACE=3 IT says like invalid interface. is it not possible to take any other interface or i have to do some tweaking work?
    any suggestions ?

    thanks in advance

  25. Drew Street says:

    Thanks for your effort with this tutorial…. I have followed it on my pi 3 B. everything works pretty well until I do the final [sudo update-rc.d autoconnectnet defaults] command. it doesn’t get added and I don’t get a confirmation or error report. Is there something obvious that I am missing?

  26. Wita says:

    Hi Lawrence, the script works well with my Raspbian Jessie. But, when i run the command: “sudo /etc/init.d/autoconnectnet start” in terminal… I get a gui at the end, i must fill APN and choose usb. Any suggestions?

    Thanks,
    Wita

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s