maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Nokia N900 (https://talk.maemo.org/forumdisplay.php?f=44)
-   -   [Announce] Auto2G: A battery saver script for the N900 (https://talk.maemo.org/showthread.php?t=47601)

JohnLF 2010-03-17 13:00

[Announce] Auto2G: A battery saver script for the N900
 
1 Attachment(s)
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 :D

Enjoy, and please post back any improvements you make!

F2thaK 2010-03-17 13:09

Re: [Announce] Auto2G: A battery saver script for the N900
 
Id love this as an app with GUI, nice work

maguitodelrock 2010-03-17 13:19

Re: [Announce] Auto2G: A battery saver script for the N900
 
hey!! good job!

rolan900d 2010-03-17 13:23

Re: [Announce] Auto2G: A battery saver script for the N900
 
@f2thak

There is an applet app that gives you the opportunity to change 3G in2 2G

Jayelzibub 2010-03-17 13:26

Re: [Announce] Auto2G: A battery saver script for the N900
 
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.

JohnLF 2010-03-17 13:33

Re: [Announce] Auto2G: A battery saver script for the N900
 
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 :D

cashclientel 2010-03-17 13:39

Re: [Announce] Auto2G: A battery saver script for the N900
 
Would be good to bundle into tweakr.

@JohnLF - Any estimations on battery life saved with this? Does the cronjob itself have much overhead?

JohnLF 2010-03-17 15:57

Re: [Announce] Auto2G: A battery saver script for the N900
 
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.

nidO 2010-03-17 16:12

Re: [Announce] Auto2G: A battery saver script for the N900
 
Potentially handy script, thanks.

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

Code:

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

MohammadAG 2010-03-17 16:20

Re: [Announce] Auto2G: A battery saver script for the N900
 
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.

JohnLF 2010-03-17 16:26

Re: [Announce] Auto2G: A battery saver script for the N900
 
@MohammadAG
Did consider that originally but wasn't sure if the scripts would ever end and so would nest forever eventually running out of memory. Big lack of Linux knowledge on my part there, hence the cron method.

@nidO, thanks, that's much neater!

Aydan 2010-03-17 17:35

Re: [Announce] Auto2G: A battery saver script for the N900
 
If you could make that into a demon with dbus messaging it would be perfect. :)
It might even be more energy efficient because you wouldn't need polling. You could just listen for the "wifi (dis)connect event" from dbus.

JohnLF 2010-03-17 17:41

Re: [Announce] Auto2G: A battery saver script for the N900
 
I didn't do that because of the hotspot issue. You could automatically connect to a hotspot with a valid IP address, but it wouldn't necessarily be active unless you clicked agree or put your card details in to buy time or logged in with an account.
It would be very annoying if you didn't want to use WiFi in a hotspot area and the phone kept changing your 3G connection to a 2G !

There could be any amount of time between connecting to a hotspot and getting a working connection, hence the polling.

gregc2009 2010-03-17 17:58

Re: [Announce] Auto2G: A battery saver script for the N900
 
Excellent idea JohnLF! This should help me a ton.

What CPU meter are you using that is shown in the status area?

joshv06 2010-03-17 18:09

Re: [Announce] Auto2G: A battery saver script for the N900
 
Would it be possible to switch to 3g when the phone is charging?

dsawhney 2010-03-17 18:14

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by gregc2009 (Post 570806)

What CPU meter are you using that is shown in the status area?

cpumem applet - http://maemo.org/packages/view/cpumem-applet/

kiralema 2010-03-17 20:14

Re: [Announce] Auto2G: A battery saver script for the N900
 
Very nice idea indeed.
However, just a little warning for those who started using their N900 on WindMobile in Canada. As I had a chance to confirm, N900 relies on the 3G based data connection for voice as well. Switching to 2G turns off the cellular connection on Wind automatically. :(

gregc2009 2010-03-17 20:38

Re: [Announce] Auto2G: A battery saver script for the N900
 
Works like a champ. The only thing I'd add to your first post is the need to do a chown and chmod to auto2g.sh after you get it where you need it.

Aydan 2010-03-17 22:09

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by JohnLF (Post 570784)
I didn't do that because of the hotspot issue. You could automatically connect to a hotspot with a valid IP address, but it wouldn't necessarily be active unless you clicked agree or put your card details in to buy time or logged in with an account.
It would be very annoying if you didn't want to use WiFi in a hotspot area and the phone kept changing your 3G connection to a 2G !

There could be any amount of time between connecting to a hotspot and getting a working connection, hence the polling.

In that case you could start polling only if wifi is connected but not usable due to it being blocked.
Even if you want it to keep polling I think it would be nicer as a properly packaged demon, so the noobs can use it too.

JohnLF 2010-03-17 22:53

Re: [Announce] Auto2G: A battery saver script for the N900
 
Good idea, I'll look into it but out of my level of knowledge here. Any takers on moving this forward to a packaged daemon?

I did think about a GUI for it, that allowed you to select
  • change back to 3G or Dual mode
  • change the messages shown (no need for localisation issues)
  • switching 3G / 2G when charger connected / disconnected
etc. etc.

I may have to have a crash course in Python / Qt :D

nbc 2010-03-17 23:42

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by Aydan (Post 570779)
If you could make that into a demon with dbus messaging it would be perfect. :)
It might even be more energy efficient because you wouldn't need polling. You could just listen for the "wifi (dis)connect event" from dbus.

I've played a bit with your idea, I like it far more than the cron way and it's not so hard (even if harder than the cron script I think).

I use dbus-scripts, it's a daemon a bit like cron but for dbus. So I add a rule to dbus-scripts, that rule watch for a wifi connection or deconnection signal.

Just follow the instructions of the install/configuration doc at the beginning of the following script and copy the script in a file /home/user/bin/w32g :

Code:

#!/usr/bin/perl

#
# You need to install dbus-scripts (BE CAREFUL, it's a devel package)
#
# as root you need to create a file /etc/sudoers.d/w32g
# with this line :
#  user ALL = NOPASSWD: /bin/ping
# and run the command update-sudoers because only root can use ping
#
# always as root and you need to create another file
# /etc/dbus-scripts/w32g with one of the following line :
#
#  to only change to 2g on connection (on one line) :
#  /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA CONNECTED
#
#  to change to 2g on connection and 3g on deconnection
#  /home/user/bin/w32g * * com.nokia.icd status_changed * WLAN_INFRA *
#
# if you want to change to 2g only with some wifi networks, put their maemo wifi id separated by |
# my $wifi = '91f493fb-7c89-4fc6-ac2c-b822923dde45|...';
# you can find the wifi id with the command gconftool-2 -R /system/osso/connectivity/IAP
my $wifi = '';


# if you don't want to test connection, change the ping string by "/bin/true"
my $connection_test = "sudo /bin/ping -c 1 www.google.com";

### end of documentation/configuration

my ($wifi_id,$state) = @ARGV[4,6];

# if we are not on the good wifi network we do nothing and exit
exit if $wifi and not $wifi eq $wifi_id;

my $dbus_wifi = "dbus-send --system --type=method_call --print-reply " .
    "--dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology";

my $dbus_notif = "dbus-send --system --type=method_call --print-reply " .
    "--dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint";

if ($state eq 'CONNECTED') {
    # we verified that the connection works
    my $ret = system($connection_test);
    if ($ret == 0) {
        system("$dbus_wifi byte:1");
        system("$dbus_notif string:'2G (GSM) cellular mode set'")
    }
} elsif ($state eq 'IDLE') {
    # to change to 3g instead of dual, use 2 instead of 0 in the line below
    system("$dbus_wifi byte:0");
    system("$dbus_notif string:'3G cellular mode set'")
}


nbc 2010-03-17 23:49

Re: [Announce] Auto2G: A battery saver script for the N900
 
@JohnLF

Hum yes, I'm more a perl guy but that's a good idea, I could try to do that in python. Could be fun.
And to add a GUI, you can just create a conf file and the script called by dbus-scripts can read that file

And this is very easy to package.

nbc 2010-03-18 02:17

Re: [Announce] Auto2G: A battery saver script for the N900
 
2 Attachment(s)
Quote:

Originally Posted by nbc (Post 571281)
@JohnLF

Hum yes, I'm more a perl guy but that's a good idea, I could try to do that in python. Could be fun.
And to add a GUI, you can just create a conf file and the script called by dbus-scripts can read that file

Ok, I've done a python script that use a config file so it's easy to add a GUI. If I have time and some people ask for it I can try to build a package and why not a GUI.

You must copy the file w32g.py.txt (see below attached files) in /home/user/bin/w32g and follow the instruction at the beginning of this file to install it.

If you want a different configuration you can copy the file w32g.conf.txt to /home/user/.w32g.conf. If you don't want a different configuration there's no need to copy it now.

For information, the default configuration is :

Code:

[w32g]

### the comment reflect the default configuration
### true is true (case insensitive), everything else is false

### the command used to test the wifi connection before downgrading to 2g
### if you don't want to do test just delete after =
connection_test = sudo /bin/ping -c 1 www.google.com

### notification message when going to 3g
message_on_idle = 3G cellular mode set
### notification message when going to 2g
message_on_connected = 2G (GSM) cellular mode set

### shall we go back to 3g when wlan disconnect ?
#### if you want to have false just delete after =
change_on_idle = true
### shall we go back to dual or 3g ?
change_to_dual = true

### if you want go to 2g only when connected to some wlan you can add their wlan id here
### you can find the wlan id using the command
### gconftool-2 -R /system/osso/connectivity/IAP
### For example :
### wifi = 91f493fb-7c89-4fc6-ac2c-b822923dde45 9ee5dd55-9a32-4ee9-9131-c464ad31d907
# wifi =


baksiidaa 2010-03-18 23:14

Re: [Announce] Auto2G: A battery saver script for the N900
 
An alternative to fcron and dbus would be to put a script in /etc/network/if-up.d (and in if-down.d). If you want to keep doing the polling, put a script there that enables fcron-managed polling.

nbc 2010-03-19 00:22

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by baksiidaa (Post 572599)
An alternative to fcron and dbus would be to put a script in /etc/network/if-up.d (and in if-down.d). If you want to keep doing the polling, put a script there that enables fcron-managed polling.

It's doable but using ifup to enable fcron pulling seems to me like using a sledgehammer to crack a nut while dbus signal is like a groom knocking on your door to wake you up :D

Using if-up.d/if-down.d is quite easy to use but limited to network automation. dbus allow me to automatize many things. I like dbus :)

gregc2009 2010-03-19 15:39

Re: [Announce] Auto2G: A battery saver script for the N900
 
Ran into a problem with it last night. I was on a work call and when I got home, it was dumped as the phone switched to the home wifi and then dropped to 2G. Called him back and then I was leaving home and after I lost my wifi, the script brought me back to 3G and dumped the call. Anyway to add a provision for it not to do anything if there is an active call?

nbc 2010-03-23 21:11

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by gregc2009 (Post 573486)
Ran into a problem with it last night. I was on a work call and when I got home, it was dumped as the phone switched to the home wifi and then dropped to 2G. Called him back and then I was leaving home and after I lost my wifi, the script brought me back to 3G and dumped the call. Anyway to add a provision for it not to do anything if there is an active call?

Ok, I've searched a bit and found a solution. You can check if there's a call with this code :
Code:

dbus-send --system --dest=com.nokia.csd.Call --print-reply=literal /com/nokia/csd/call/1 com.nokia.csd.Call.Instance.GetStatus
It will return "uint32 0" if no call

So I've modified the script to test this before . You can find it on this wiki page User:Nbc/W32g

JohnLF 2010-04-07 22:21

Re: [Announce] Auto2G: A battery saver script for the N900
 
Update on this: found a problem. For the first time I'm using Joikuspot in a hotel to get my laptop online. Whenever I connect, because Joikuspot is using WiFi to share the 3G signal, it disconnects and drops down to 2G. Very annoying - when I eventually realised what was happening I had to rename the script temporarily to stop it running!

If anyone packages this, there will have to be way to stop this or perhaps just have a way to turn it off manually.

rooted 2010-04-07 22:27

Re: [Announce] Auto2G: A battery saver script for the N900
 
if [`ps | grep joiku` = ...]
exit

else
-script-

It's a very simple addition.

Patola 2010-04-07 23:33

joikuspot and call during network connect/disconnect
 
A truly complete program, besides checking for these two conditions (ongoing call and joikuspot sharing), should also wait for these two events to finish before switching to 2G/3G, and when doing that, first check if the condition hasn't changed again.

The logic starts to become complicated here. We would have to use some threads for that?

BTW, if a packaged script with some user interface is done, I'd recommend using the regular if-up.d and if-down.d networkmanager scripts, because it is not very good to depend on dbus-scripts because it's still in extras-devel, not extras. This way you get only the interface being connected/disconnected, but you can get the remaining details with iwconfig and the like.

HtheB 2010-04-08 00:39

Re: [Announce] Auto2G: A battery saver script for the N900
 
Hope this will be in the repos (with a gui) soon! :)


Very handy script!!! :)

boudjere 2010-04-15 19:57

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by nbc (Post 571374)
Ok, I've done a python script that use a config file so it's easy to add a GUI. If I have time and some people ask for it I can try to build a package and why not a GUI.

Very nice script indeed.. It just does not work for me.. Followed all the instructions in the script, restarted dbus-scripts, but it does not trigger the switch to 2G when entering a wlan network..

Any ideas how to fix this? I have the 3G 2G status bar applet installed, may this interfere with the dbus-script?

Thanks a lot in advance for your help!

Update:
Using the script from http://wiki.maemo.org/User:Nbc/W32g I got it to work, however I had to disable the connection test although w32g is in sudoers.. Strange.

mthmob 2010-04-26 20:01

Re: [Announce] Auto2G: A battery saver script for the N900
 
Can anyone help out with an easy script that switches too 3G (if available) when i open a data connection (tethering), and switches back to GSM when i close the connection?

Tried messing with dbus-scripts-settings a little, but i got no response on the n900.

vkthor 2010-08-24 20:55

Re: [Announce] Auto2G: A battery saver script for the N900
 
I was just looking for this.
Would love to see it with a GUI, though. Any intentions on programming it?

HtheB 2010-08-24 22:07

Re: [Announce] Auto2G: A battery saver script for the N900
 
Quote:

Originally Posted by vkthor (Post 797225)
I was just looking for this.
Would love to see it with a GUI, though. Any intentions on programming it?

Try out "Autodisconnect" it's in the repos

vkthor 2010-08-24 23:15

Re: [Announce] Auto2G: A battery saver script for the N900
 
Well, it's not exactly the same thing, but it'll do. Thank you HtheB :)


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

vBulletin® Version 3.8.8