Battery Level Display on PirateBox Homepage

Posted by FunGlee 
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.
Now, this forum is in read-only mode. You find details Details hereContinue on /r/PirateBox
Battery Level Display on PirateBox Homepage
March 03, 2016 04:32PM
I have PirateBox running on a TP-Link MR3040 and was wondering if anyone figured out a way to read the battery level and then put it in an info box on the home page. Not sure if this is even possible, I'm assuming it is possible since there is an led indicator on the router that is probably controlled by the software. I'm not experienced enough to figure this out by myself but it sounds like a cool idea and would like some feedback if anyone has any tips.
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 05:24PM
Unfortunately often the power LED circuit is only in hardware and does not feed back to the OS - I think this might be the case with this device too, but I do not have one, so this is only speculation.
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 07:14PM
Is there some directory that holds the battery status in openwrt? I know of such directories in ubuntu but I'm not sure how that all carries over between linux derivatives
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 08:43PM
You could check the files in /sys/class/power_supply/
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 10:44PM
OK now this is pretty interesting, there isn't any power_supply-like file but there is a directory called leds (now I'm getting excited) and in that there's these files:

ath9k-phy0
tp-link:green:3g
tp-link:green:lan
tp-link:green:wlan

now the bottom three are obviously for the network activity, ath9k-phy0 must be the battery led status light, a quick web search gave many hits related to led's so I think this is a clue that the software may control that. at the very least this gives me confidence that if not a battery percentage we might be able to gleen a "discharging" "charging" and "charged" status from the status of this led. now the only question is how does one get any info from these? I tried opening them with vim like a noob and obviously that didn't work.
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 11:12PM
Nah, this is just for the status LED's, you can toggle them, but that is pretty much it.
ath9k is quite sure not responsible for the power LED's.

I am pretty sure that the power LED's are not controlled by software, it is much simpler to build this in hardware.

You can check what GPIO pins are used for which functionality here => [wiki.openwrt.org]

Technically you _could_ hack something together with the 3 available GPIO pins and some other components, but I don't know if it is worth it.
Re: Battery Level Display on PirateBox Homepage
March 07, 2016 11:58PM
yep I successfully toggled the network leds but I believe you are right about the power led. I'm looking at the images of the circuit board inside, from poking around the site you linked to I think a hardware mod to connect the green and red power leds to gpios r9 and r10 shouldn't be too big of a hassle. how do i link the gpio status's to a display on the hompage?
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:07AM
That would be a multi part process.
First make sure you have a script that can read the GPIO states.
Then take a look at the code that is updating the shoutbox. This is pulling information from a script, and this is basically the same thing you have to do, once you can read the GPIO states.

Technically you should be able to use GPIO 14, 15, 19 and 20.

Another thing you could try, is to check if the Box shutting down correctly if the battery gets empty, or if it simply switches off? If it simply switches off, there is definitely no feedback from the battery to the OS.
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:18AM
thats an interesting thought, how do i test if it shuts down? keep a ssh session running until the box turns off and see if there was a shutdown -h message?
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:35AM
Not sure if */var/log/messages* (or similar) is available and persistent between boots, if it is, you can evaluate that.

Otherwise:
On shutdown all running processes should be shut down cleanly, eg.: receive a SIGTERM/SIGKILL message.
So you could hack up a small app that touches/writes a file when it receives SIGTERM/SIGKILL messages.

I am just wondering, what is your use case? I'd imagine that it is faster to check the LED's directly than to check them via UI,...
I'd also accept: Because I can ;-)
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:52AM
The log directory has two empty logs and a minidlna log, so no luck for logs. I like the app idea, would a bash script do the trick? That's the only linux programming experience i have as of yet..
The few times I've used the box I put it in my backpack with a battery to keep it powered and I thought about this idea to see when the battery dies and the piratebox switches to its internal battery, not any real necessity for the function though. so I suppose it's along the lines of because I can haha
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 01:11AM
This is what I came up with, I think it should work
#!/bin/bash
#Make file to show program started
echo "No Shutdown" > MyOutput.txt

#if program is asked to stop remove the file and exit
trap "rm MyOutput.txt; exit" SIGHUP SIGINT SIGTERM

#do nothing while waiting
while true; do
	: # Do nothing
done
Let me know if I missed something, in the meantime I'll test it on my pc.



Edited 2 time(s). Last edit at 03/08/2016 01:25AM by FunGlee.
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:10PM
Ran the program and left the box on all night until it eventually died, looks like it never sent a terminate signal so probably didn't shutdown. These gpio pins are purely digital correct? It would be niffty if I could wire a voltage divider from the battery and get a percentage that way.
Re: Battery Level Display on PirateBox Homepage
March 08, 2016 12:19PM
Yes they are digital. But you could in theory build something with an AD converter, but you'll still only have a resolution of 3(4 if you remove the switch)Bit,...

Pretty sure the LED's are also just driven by a voltage divider, so taking the LED state as input should be enough.