Tuesday, January 28, 2014

Getting my USB modem to startup automatically

Working on getting the USB modem working on boot up with the beaglebone black.  Initially setup was blogged about here.  To accomplish this I created cellmodem.sh (with lots of online help) and put it in /etc/init.d.  Here is the code:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          cellmodem
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start cell modem if ethernet is not connected
# Description:       Start cell modem if ethernet is not connected
### END INIT INFO

#
# Function that starts the daemon/service
#
do_start()
{
        ipAddress=`/sbin/ifconfig eth0 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1`
        echo "$ipAddress"
        ipLength=`expr length "$ipAddress"`
        echo $ipLength
        if [ $ipLength -gt 1 ]; then
                echo "Ethernet Running, exiting..."
                exit 3
        else
                modem=$( /etc/udev/nvtl_modem.sh 2>&1 )
                echo "$modem"
                modemLength="${#modem}"
                echo $modemLength
                if [ "$modemLength" = "0" ]; then
                        echo "USB Cell Modem Connected"
                        diffcheck=$( diff /etc/default/wvdial.conf /etc/wvdial.conf 2>&1 )
                        echo "$diffcheck"
                        outLength="${#diffcheck}"
                        echo "$outLength"
                        if [ "$outLength" = "0" ]; then
                                echo "run wvdial"
                        else
                                echo "copy wvdial.conf"
                                cp /etc/default/wvdial.conf /etc/wvdial.conf
                        fi
                        wvdial &
                else
                        echo "No USB Cell Modem Connected"
                        exit 3
                fi
        fi
        
        
}

#
# Function that stops the daemon/service
#
do_stop()
{
        pkill wvdial
}

do_status()
{
        ipAddress=`/sbin/ifconfig ppp0 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1`
        echo "$ipAddress"
        ipLength=`expr length "$ipAddress"`
        echo $ipLength
        if [ $ipLength -gt 1 ]; then
                echo "USB Cell Modem Up and Running"
        else
                echo "USB Cell Modem Not Up"
        fi
}


case "$1" in
  start)
        do_start
        ;;
  stop)
        do_stop
        ;;
  status)
        do_status
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status}" >&2
        exit 3
        ;;
esac


:

I also copy wvdial.conf into /etc/default in case it gets corrupted or changed.

I then run the following command:

insserv cellmodem.sh

and that sets it up.

The cell modem and ethernet don't like working at the same time so the script checks to see if eth0 has an IP address and if so it doesn't setup the cell modem.  Otherwise it does setup the cell modem.  It still needs some work but a good initial run.

Side note: I notice that on occasion the cell modem doesn't turn on even though your plug it into the BBB.  I power down and try again and eventually the LED on the cell modem will turn on and then everything powers up.

No comments:

Post a Comment