littleBits remote temperature sensor

My friend gave me a littleBits Smart Home kit for Christmas. Fun electronics toy; you snap together a simple network of components like temperature sensors, LED displays, and a “cloud module” to build devices. I set up a simple thing that gets the room temperature, displays it on an LED, and also feeds it to the cloud device. It works!

Update: left it running a couple of days, 100% stable. A bit bandwidth hungry though; about a megabyte an hour uploads, or 300 bytes a second. And about half that download. I suspect it’s sending a single voltage value once a second and the rest is overhead.

Here’s a Python program I wrote to fetch the data from the device. The wrinkle here is that there’s no simple “get current number” polling interface; it’s all subscriptions and streams. That seems to be on purpose and while I can sort of see the rational, it does make the HTTP client 4 lines of code instead of 2.

#!/usr/bin/env python3
from pprint import pprint
import requests, json, sys, os
token = '27ebe6c681d8db547ff8ac02714e6c07ed08084cf844ffc247cc9f926f155b4'
device = '00e0547ff8ac'

headers = {
    'Accept': 'application/vnd.littlebits.v2+json',
    'Authorization': 'Bearer %s'; % token,
}

try:
    resp = requests.get('https://api-http.littlebitscloud.cc/devices/%s/input' % device,
                        headers=headers,
                        stream=True,
                        timeout=5)
    output = next(resp.iter_lines(decode_unicode='ascii'))
    resp.close()
    data = json.loads(output.lstrip('data:'))
except Exception as e:
    sys.exit(1)

out = os.path.dirname(os.path.realpath(__file__)) + "/data.tsv"
with open(out, 'a') as fp:
    fp.write('%d\t%d\n' % (data['timestamp'], data['percent']))
sys.exit(0)