On 26 Jan 2011, at 15:41, Chris G wrote:
I'm looking for the simplest possible way for a script (or any sort of program, can be C, Python, whatever you like within reason) to check whether mains power is on.
The computer which is going to check this is running off batteries so will still be running (I'm not quite that stupid!).
The easiest way I can think of at the moment is to have some sort of hardware with a presence on the LAN powered by mains so that one can ping it to check that mains power is there. However the 'simplest' device I have at the moment is an old router which seems rather overkill for such a simple requirement.
Can anyone think of any better way of doing this for which I am likely to have the hardware available or which will cost very little? I'm quite happy to build simple bits of electronics. The computer running the check is an old eeePc so has only USB and ehternet connections as far as I remember.
Find a USB device that is powered by an external supply, plug that supply into the mains and check for it from the command line lsusb. E.g. an external hard disk, or an old hub, or some arduino-style project, or rip the guts out of a dumpster-dived usb printer.
For a more over-engineerd electronics solution I'm sure you can throw together some usb-bus-powered arduino with an optocoupler that checks the (down-transformed) supply explicitly, then communicate that back over usb to a custom program on your computer. Complex, and requires knowledge, skill, and money, but would be fun.
I'm not sure how the running costs compare, but pinging your router sounds like a fine idea. If you think that using your router is overkill, buy a cheap second hand one off ebay. You can probably find a faulty one where the adsl modem part has blown up, but the rest may still work well enough to ping.
-- Martijn