Skip to content

Instantly share code, notes, and snippets.

@aniline
Created October 22, 2016 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aniline/68ab3a870a1a7d50c08149e8131aa205 to your computer and use it in GitHub Desktop.
Save aniline/68ab3a870a1a7d50c08149e8131aa205 to your computer and use it in GitHub Desktop.
Collect some stuff form apcupsd.
#!/usr/bin/env python
from subprocess import Popen, PIPE
import urllib2
from time import sleep
p = Popen(['/sbin/apcaccess'], stdout=PIPE, stderr=PIPE)
(o, e) = p.communicate()
status = {}
for l in o.splitlines():
pm = l.split(':')
status[pm[0].strip()] = (':'.join(pm[1:])).strip()
voltage = status['LINEV'].split()[0]
loadpct = status['LOADPCT'].split()[0]
timeproj = status['TIMELEFT'].split()[0]
battchg = status['BCHARGE'].split()[0]
base_url='https://api.thingspeak.com/update?api_key=APIKEY'
update_url=base_url+'&field1='+voltage+'&field2='+loadpct+'&field3='+timeproj+'&field4='+battchg
try:
urllib2.urlopen(update_url, timeout = 10)
except urllib2.URLError, e:
pass
sleep(15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment