Twine and Pi

Here is a little script to get data from your Twine using a Raspberry Pi (or any other computer that runs Python ….).

The script can be split into 4 parts:

  1. Setup
  2. Login
  3. Retrieve data
  4. Parse and display data

1. Setup

# Fill in your config below
EMAIL = '[email protected]'
PASSWORD = 'secret'
TWINE_ID = 'blahblah'

USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' # fake a user agent, some websites (like google) don't like automated exploration
import os.path
import urllib
import urllib2
import json
import cookielib
from StringIO import StringIO

# set some global variables
urlopen = urllib2.urlopen
Request = urllib2.Request

# setup cookie handling
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

Nothing special here, just some imports and some initialisation. Please note that cj (the cookie jar) will be where cookies will be stored.

2. Login

Login is doen through HTTP. A cookies is return which we need to retrieve the data in the next step.

# prepare to login
theurl = 'https://twine.supermechanical.com/login'
txdata = urllib.urlencode({'email': EMAIL, 'password': PASSWORD})
txheaders =  {'User-agent' : USER_AGENT}

# do the login
try:
    req = Request(theurl, txdata, txheaders)
    handle = urlopen(req)
except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    exit()

3. Retrieve data

Login is doen through HTTP. A cookies is return which we need to retrieve the data in the next step.

# prepare to get the data
theurl = 'https://twine.supermechanical.com/rt/' + TWINE_ID + '?cached=1'
txdata = None

# get the data
result = None
try:
    req = Request(theurl, txdata, txheaders)
    cj.add_cookie_header(req) # add the received cookie(s) to this request
    handle = urlopen(req)
except IOError, e:
    print 'We failed to open "%s".' % theurl
    if hasattr(e, 'code'):
        print 'We failed with error code - %s.' % e.code
    exit()
else:
    io = StringIO(handle.read())
    result = json.load(io)

 4. Retrieve data

Login is doen through HTTP. A cookies is return which we need to retrieve the data in the next step.

# handle the result
if result != None:
    for index, value in result['values']:
        if index == '0000d47c6a53066a01':
            print 'Temperature:', round(5.0/9.0 * ( value/100 - 32), 2), unichr(176) + 'C' # Celsius = 5/9 (Fahrenheit - 32)
        if index == '0000d47c6a53066a03':
            print 'Orientation:', value
        if index == '0000d47c6a53066a05':
            print 'Voltage:', round(value/1000000.0, 2)
        if index == '01a5474016000049':
            if value == 0:
                print 'Swith: open'
            if value == 1:
                print 'Swith: closed'
        if index == '01046a8a150000f0':
            if value == 0:
                print 'Moisture: dry'
            if value == 1:
                print 'Moisture: wet'
        if index == '018d0e4016000044':
            print 'Breakout:', value
    print 'Signal strength', result['rssi'] # is this correct, rssi = Received Signal Strength Indication?

 

Thanks to Jay Erickson (a Twine community member) for the explanation on how to access Twine data!

Leave a Reply

Your email address will not be published. Required fields are marked *