View Single Post
JohnLF's Avatar
Posts: 551 | Thanked: 507 times | Joined on Feb 2010 @ North West England
#1
Hi guys, after a couple of months browsing these forums and learning all about my new device I've decided to share a little shell script with you that has extended my phone's running time.
It's nothing new, simply the putting together of snippets of information gleaned from various threads. Took me a few days to gather it all, write it, test it and get it to a point where I was happy it works properly. I've been running it for a while with no problems. This goes to show how open this platform is and the endless possibilities it could provide (and to anyone leaving the party early, you are missing out, I really believe that).

Anyway, the script simply checks for an active WiFi connection, and if one is found, it changes the cellular mode from 3G to 2G. It can be made to automatically revert to 3G upon loss of WiFi if required. A little popup notification briefly appears to indicate a change.

Here it is (there's a file to download at the bottom of this post - Note if you download this file you will need to run a chmod 755 auto2g.sh on it in Xterminal to make it an executable file.)
Code:
#!/bin/sh
#
# auto2g by J.LeFebvre
#
# Automatically sets 2G cellular mode when a valid WiFi connection is running
# This helps conserve battery life
#
# If an automatic connection to 3G is required when not on WiFi then uncomment the two dbus commands under "set to 3G" line
#

if `/sbin/ifconfig wlan0 2>/dev/null | grep -q RUNNING`; then
   if `dbus-send --system --print-reply --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_selected_radio_access_technology | grep -q 'byte 2'`; then
      if (/bin/ping -c 1 www.google.com > /dev/null); then
         # set to 2G
         dbus-send --system --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:1
         dbus-send --system --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:'2G (GSM) cellular mode set'
      fi
   fi;
else
   if `dbus-send --system --print-reply --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_selected_radio_access_technology | grep -q 'byte 1'`; then
      # set to 3G
      # dbus-send --system --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:2
      # dbus-send --system --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:'3G cellular mode set'
      echo '' > /dev/null
   fi
fi

The reason for the PING is that for some WiFi connections, such as public hotspots, the WiFi can be running but you don't get access until you click an agreement or pay a fee. That line checks the connection is actually working before disabling 3G.

The second IF was required as without it, the N900 set 2G every minute and popped up the notification, which was quite annoying after a while!

The echo near the end is only required if the dbus commands are commented out as there needs to be something between the else and the fi.

I used this with Fcron and scheduled it to run every minute. Fcron instructions can be found here. Note Fcron is in extras-devel only, and you will need to know how to use the Vi editor. Usual warnings apply.

On my N900, the auto2g.sh file is in /home/user. My cron job was set up like so

Code:
0-59 * * * * /home/user/auto2g.sh
Now I don't have to remember to change to 2G when I connect to WiFi as the N900 does it for me!

Thanks to everyone on these forums for the excellent help received, and to Rob1n who showed me the difference between backticks and brackets in shell scripts!

One point to note: This script only deals with 2G and 3G modes, it does not deal with Dual mode as I don't use that myself - I will leave that as an exercise for the reader

Enjoy, and please post back any improvements you make!
Attached Files
File Type: zip auto2g.zip (638 Bytes, 163 views)

Last edited by JohnLF; 2010-03-17 at 22:59. Reason: add chmod note
 

The Following 19 Users Say Thank You to JohnLF For This Useful Post: