Controlling from Python

Using the juiceboss module

The juiceboss module provides a simple way to control the juiceboss (http://juiceboss.com) web power controller:

>>> from juiceboss import Juiceboss
>>> jb = Juiceboss("555-0001")
>>> print jb.unit_id, jb.switch
555-0001 0
>>> jb.switch = 1
>>> print jb.switch
1

To install it, you can use easy_install:

easy_install -U juiceboss

Or download it from PyPI at http://pypi.python.org/pypi/juiceboss/1.0

class juiceboss.Juiceboss(unit_id, ip=None)

The Juiceboss class provides a simple way to monitor and control a juiceboss device on the local network.

Parameters:
  • unit_id (string) – unit id, e.g. “555-0001”
  • ip (string) – IP address of unit on network

If ip is not specified, the Juiceboss object uses the central server to discover the IP address of the juiceboss device (see Finding the address of the juiceboss unit).

switch

state of the device switch, integer 0 or 1. This attribute is writable: assigning to it controls the device’s switch:

>>> j = Juiceboss("555-0001")
>>> j.switch = 0    # turn it off
>>> j.switch = 1    # turn it on
update()
Connect to the device and update attributes
uptime

Seconds since last restart of the device; int, read-only.

>>> j = Juiceboss("555-0001")
>>> print j.uptime
79218
version
firmware version of the device; int, read-only.
voltage

Voltage of the device’s DC supply, in volts; float, read-only.

>>> j = Juiceboss("555-0001")
>>> print j.voltage
7.8

Using urllib2

You can find the local IP address of the juiceboss device (see Finding the address of the juiceboss unit) like this:

>>> import urllib2
>>> urllib2.urlopen('http://juiceboss.com/ip/555-0001').read()
'192.168.0.101'

Basic authentication using urllib2 takes a few lines of code:

import urllib2

ip = urllib2.urlopen('http://juiceboss.com/ip/555-0001').read()
password = "555-0001"

top_level_url =  "http://%s/" % ip
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, top_level_url, "admin", password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
print opener.open(top_level_url).read()

Table Of Contents

Previous topic

juiceboss for programmers

Next topic

Controlling from the command line

This Page