Reply
Thread Tools
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, 147 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:
F2thaK's Avatar
Posts: 4,365 | Thanked: 2,467 times | Joined on Jan 2010 @ Australia Mate
#2
Id love this as an app with GUI, nice work
 
Posts: 99 | Thanked: 26 times | Joined on Jan 2010 @ Ecuador
#3
hey!! good job!
 
Posts: 527 | Thanked: 121 times | Joined on Feb 2010
#4
@f2thak

There is an applet app that gives you the opportunity to change 3G in2 2G
 
Posts: 49 | Thanked: 6 times | Joined on Jan 2010
#5
Really like the idea but could do with some spacing in your code and a few more comments to get a really good idea of what it is doing.
__________________
There are 10 kinds of people in the world:
Those who understand binary and those who don't.
 
JohnLF's Avatar
Posts: 551 | Thanked: 507 times | Joined on Feb 2010 @ North West England
#6
Look at the attached zip file, it will be clearer - the text wrapping on the code above makes it look awful

Pseudo code: -

Code:
if wlan0 running
   if mode is 3G
      if can ping google
          set to 2G
          Notify message
      endif
   endif
else
   if mode is 2G
      set to 3G
      notify message
   endif
endif
That's how I wrote it to begin with, then filled in the pseudo-code with real code

Last edited by JohnLF; 2010-03-17 at 22:56. Reason: typo
 

The Following User Says Thank You to JohnLF For This Useful Post:
cashclientel's Avatar
Posts: 663 | Thanked: 282 times | Joined on Nov 2009 @ London, UK
#7
Would be good to bundle into tweakr.

@JohnLF - Any estimations on battery life saved with this? Does the cronjob itself have much overhead?
__________________
Nokia are a business and have chosen a path of using the OSS community phenomenon to reduce their overheads specifically after sales support and development. Unlike Apple who do the opposite and make a killing from their Applications store.
 
JohnLF's Avatar
Posts: 551 | Thanked: 507 times | Joined on Feb 2010 @ North West England
#8
Fcron is a proper implementation of cron - i.e. it puts itself to sleep when not in use. I've not done any scientific testing, but if it actually switches modes I see three bars on the CPU meter in the staus area for about a second, if it doesn't switch, I see a single bar for about a second. I figure that will be a lot less power use than having 3G, but again, nothing to back that up, other than the fact that I don't have to charge my N900 more than once a day now.

If you think it is too much, you could change the cron job to run every 5 minutes instead of every minute. To do that, use

Code:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/user/auto2g.sh
- although be aware I haven't tested that myself.
 
Posts: 278 | Thanked: 303 times | Joined on Feb 2010 @ Norwich, UK
#9
Potentially handy script, thanks.

A slightly neater job for the 5-minutely cron though would be:

Code:
*/5 * * * * /home/user/auto2g.sh
 

The Following User Says Thank You to nidO For This Useful Post:
MohammadAG's Avatar
Posts: 2,473 | Thanked: 12,265 times | Joined on Oct 2009 @ Jerusalem, PS/IL
#10
Just had an idea, not sure if it's practical or better than the above method BUT
You could add sleep 300 (5 mins) and then run-standalone.sh <path to script>.
Add a .desktop icon in /usr/share/applications/hildon
Run it, and it should keep looping.
 
Reply


 
Forum Jump


All times are GMT. The time now is 09:50.