Friday, July 24, 2015

How can profile (log the charging and discharging current for some time period) my battery?


As the question reads, I want to log the current in milliAmpere or Ampere (both charging and discharging) over a period of time.


Please note I am not asking about charging or discharging rate, but rather on the current. I am aware of the app called Ampere which displays the current, which is good but I want them logged over a period of time. I am also aware of the app Battery Log, which logs the details but not the current.



Device info: Nexus 5 rooted, running stock 5.1.1. Ready to install custom kernel but stock ROM only.


Edit: Based on a comment from Firelord, I could see a file in /sys/class/power_supply/battery/current_now which at the moment of writing had a number 835910 and after a second it became 695229, then 673163. All these values were during the discharge phase.


When plugged in, it showed 412190 and the Ampere app showed 160mA. It seems this is the current, but I guess I have to average it out to compare it with Ampere's output.


Interestingly, there is another file named status which has values charging and discharging.


I also happen to know another question in this site.



Answer



Ampere shows us the value of current made available to the system by the kernel. I used three devices with different Android versions to test my answer. Here we go!




Test 1 & 2: Two MTK devices running stock Android 4.2.1 and 4.4.2


Since MTK is involved, thing would behave a bit differently. In both the devices Ampere had trouble measuring the current not because it couldn't, but two files were available to measure the current, and the user had to choose one from its Settings → Basic settings → Measurement interface. (Rant: A design flaw I tend to think since I wasn't aware of that before digging it up).



The two files for my Android 4.2.1:


/sys/device/platform/mt6320-battery/power_supply/battery/BatteryAverageCurrent
/sys/class/power_supply/battery/BatteryAverageCurrent

The two files for my Android 4.4.2:


/sys/device/platform/battery/power_supply/battery/BatteryAverageCurrent
/sys/class/power_supply/battery/BatteryAverageCurrent

The file BatteryAverageCurrent would show a numeric value with no mention of any unit (be it A or mA).


In my devices, both the files (second was the symlink of the first) had the same value (dynamic, Current varies of course) like 440 when connected to PC, and 880 when connected to charger. Correspondingly, Ampere showed the similar value with a slight mismatch of +- 1-10 mA. // Somebody on XDA said that this app doesn't show true values, but that's none of my concern honestly.



Test 3: A Qualcomm device running Android 5.0.2 (CyanogenOS 12)


I read that Google made changes in Lollipop related to measurement of current, and among other things, BatteryAverageCurrent is nowhere to be found in the system, at least not in my system. Don't blame me since I didn't verify whether the changes were introduce in Lollipop or before, and it's not really a concern to test the things here.


Before I tell the tale further I should mention that Ampere had no trouble in this device even though there were two files to measure current:


/sys/devices/00-qcom,charger/power_supply/battery/current_now
/sys/class/power_supply/battery/current_now

Not to mention that the second one is the symlink of the first one, and 00-qcom,charger may differ in your device.


Anyhow, when it comes to the content of the current_now you can consider everything same as in BatteryAverageCurrent except that the value was in negative like -180 or -840 (don't ask me why?)


There's more


The last directory named battery has some other cool stuff which might interest some people. Checkout them like Battery temperature, Voltage, capacity, among other things.



If you want a summary of them, look at uevent file whose output would be like:


POWER_SUPPLY_NAME=battery
POWER_SUPPLY_CHARGING_ENABLED=1
POWER_SUPPLY_STATUS=Charging
POWER_SUPPLY_CHARGE_TYPE=Unknown
POWER_SUPPLY_HEALTH=Good
POWER_SUPPLY_PRESENT=1
POWER_SUPPLY_ONLINE=1
POWER_SUPPLY_TECHNOLOGY=Li-ion
POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4350000

POWER_SUPPLY_VOLTAGE_MIN_DESIGN=4400000
POWER_SUPPLY_VOLTAGE_NOW=4316000
POWER_SUPPLY_CAPACITY=100
POWER_SUPPLY_CURRENT_NOW=0
POWER_SUPPLY_INPUT_CURRENT_MAX=100000
POWER_SUPPLY_INPUT_CURRENT_TRIM=33
POWER_SUPPLY_INPUT_CURRENT_SETTLED=0
POWER_SUPPLY_VOLTAGE_MIN=4300000
POWER_SUPPLY_INPUT_VOLTAGE_REGULATION=1
POWER_SUPPLY_CHARGE_FULL_DESIGN=0

POWER_SUPPLY_CHARGE_FULL=0
POWER_SUPPLY_TEMP=311
POWER_SUPPLY_TEMP_COOL=0
POWER_SUPPLY_TEMP_WARM=0
POWER_SUPPLY_SYSTEM_TEMP_LEVEL=0
POWER_SUPPLY_CYCLE_COUNT=0
POWER_SUPPLY_CHARGE_NOW=5048
POWER_SUPPLY_FASTCHARGER=0
POWER_SUPPLY_CHARGE_TIMEOUT=1


And if (somehow) you're wondering about 0 in POWER_SUPPLY_CURRENT_NOW=0 then know that POWER_SUPPLY_CAPACITY=100 i.e. charging was complete and even Ampere was showing 0mA.




As for Logging, sorry! since I've no detailed solution as I didn't bother because the logging can be done using an automation tool like Tasker.


But a brief usage of Tasker must be told:



  • Create a profile based on time or the type that suits your goal.

  • There are two file actions under the action File namely Read File and Write File. If it doesn't work, then you can always do cat >> using the action Code → Run Shell and you don't need root access.


We're good to go now!


No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...