Skip to content

Instantly share code, notes, and snippets.

@JohnReeves
Forked from daniellevass/littlebits.md
Last active January 1, 2018 16:06
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 JohnReeves/d753d00077ac96c3b202238dca4e7c33 to your computer and use it in GitHub Desktop.
Save JohnReeves/d753d00077ac96c3b202238dca4e7c33 to your computer and use it in GitHub Desktop.
littlebits hack day @ bristol

1 starting

Firstly, using terminal install YQL and request if you don't have it already:

npm install request
npm install yql

Create a new .js file - make sure you can navigate to it with the terminal prompt to run.

Make sure you have at the top:

//includes
var YQL = require('yql');
var request = require('request');

2 Cloud Control

go to control.littlebitscloud.cc - username and password will be somewhere in real life I have indicated!

Find the pokemon name that matches yours - test it to make sure you can light it up.

Also make a note of the device ID access token 😄

3 Sending Data

Here is an example request (you'll want to change which cloudbit your using)

function sendRequestToCloudBit(percent){

  //my cloudbit
  var pokemon = "243c200c0659";

  //set up request to send.
  var options = {
    method: "POST",
    url: 'https://api-http.littlebitscloud.cc/v2/devices/'+pokemon+'/output',
    body: JSON.stringify({
      "percent" : percent,
      "duration_ms" : 1000 }),
      headers: {
        'Content-Type' : 'application/json',
        'Authorization' : 'Bearer xxx'
      }
    };

    //completion function
    function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(JSON.stringify(body));
      }
    }

    //send request.
    request(options, callback);

}

Don't forget to call it like :

sendRequestToCloudBit(30);

4 Getting data from YQL

Have a play around on the YQL console to familiarise yourself with the data you'll be getting - https://developer.yahoo.com/yql/console

here's an example query:

select item.forecast 
  from weather.forecast 
  where woeid in 
    (select woeid 
      from geo.places 
      where text="bath, england") 
  AND u='c'

5 Putting that into JS

Next, we want to put that into our JS script:

function getDataFromYahoo(){

  var query = new YQL('select item.forecast from weather.forecast where woeid in '
                      +'(select woeid from geo.places '
                      +'where text="bath, england") '
                      +'AND u=\'c\'');

  query.exec(function (error, response) {

    if(error){
      
      console.log("error!\n" + JSON.stringify(error));
      
    } else {

      console.log(JSON.stringify(response));

    }//end if error

  });

}

6. Next steps

Maybe you could expand your function to get data from YQL to allow a person to chose where? Could you allow them to type that in via the terminal prompt?

What if it doesn't rain a lot where you are? You could have a sunglasses checker? coat checker etc?

Weather isn't for everyone, you could play around with any other API's using request's GET instead of POST - my favourite API is the USGS earthquake API - I could build an earthquake notifier. What could you do instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment