Thursday, July 2, 2015

LittleBits CloudBit with Python Requests - Part 1 Getting Started

Hi, I found the LittleBits Cloudbit really interesting and did the following screencast showing the few lines of Python code to query the LittleBitsCloud.CC to integrate some programming logic to it.

https://vimeo.com/132463662


CloudBit 1 from Eric Chou on Vimeo.
- Here are the actual outputs that I blurred out in the video (sorry I am a bit new to the screencast software):

$curl -i -XGET -H "Authorization: Bearer <your token>" -H "Accept: application/vnd.littlebits.v2+json" https://api-http.littlebitscloud.cc/devices/<your device ID>
HTTP/1.1 200 OK
accept-ranges: bytes
access-control-allow-headers: Authorization, Content-Type, If-None-Match
access-control-allow-methods: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS
access-control-allow-origin: *
access-control-expose-headers: WWW-Authenticate, Server-Authorization
access-control-max-age: 86400
cache-control: no-cache
content-type: application/json; charset=utf-8
Date: Thu, 02 Jul 2015 16:48:44 GMT
Content-Length: 266
Connection: keep-alive

{"label":"<your device name>","id":"<your device id>","user_id":<user id>,"is_connected":true,"ap":{"ssid":"<ssid>","mac":"<mac>","strength":"66","server_id":"<serverid>","socket_id":"<socketid>","status":"<status>"},"subscriptions":[],"subscribers":[],"input_interval_ms":750}$
$
$

- Here is the Python code:

import json
import requests

deviceId = "<deviceID>"
littleBitsUrl = "https://api-http.littlebitscloud.cc/devices/"
authToken = "<token>"

headers = {"Authorization": "Bearer "+authToken, "Accept":"application/vnd.littlebits.v2+json"}

r = requests.get(littleBitsUrl+deviceId, headers=headers)
print(r.text)

- Here is the output: 

$python CloudBit_Request.py
{"label":"<your device name>","id":"<your device id>","user_id":<user id>,"is_connected":true,"ap":{"ssid":"<ssid>","mac":"<mac>","strength":"66","server_id":"<serverid>","socket_id":"<socketid>","status":"<status>"},"subscriptions":[],"subscribers":[],"input_interval_ms":750}$
$

Happy coding!

2 comments:

  1. I agree this is one of the more exciting possibilities with littlebits. Could you show us how to take this one useful step further? I'd like to be able to write a python module to read the value of the cloudbit input say every 10 seconds. In other words, record streaming data at certain intervals. I keep failing because either b/c I'm not understanding streaming json data... It seems like it should be easy but I haven't found anybody who has posted a simple solution to this problem. THanks, interesting site/blog!

    ReplyDelete
  2. Hi Jonathan, thanks for reading and commenting! Check out part 2 of the CloudBit with Python Request, http://blog.pythonicneteng.com/2015/07/littlebits-cloudbit-with-python.html. I figured it was too long to respond in the comment section. :)

    In short, I think we can use the pre-release v3 API https://github.com/littlebits/cloud-api-lessons#lesson-2-read-from-cloudbit and the iter_lines() method of the Request package for input. More example on the blog post. Hope it helps and let me know what you think. :)

    ReplyDelete