Skip to content

Instantly share code, notes, and snippets.

@daniellevass
Last active August 29, 2015 14:18
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 daniellevass/4950c4dc427d242029d3 to your computer and use it in GitHub Desktop.
Save daniellevass/4950c4dc427d242029d3 to your computer and use it in GitHub Desktop.
IoT Hack day

#littleBits hack day

##1. Tools to install!

  1. Node.js = https://nodejs.org/
  2. A text editor e.g. Atom.io = https://atom.io/

2. Checking node is installed

  • Open terminal
  • type node
    • if it doesn't complain your probably fine 😄
    • if it complains, ask me for some help!

#7. Getting data (b)

  • Back in Atom or our text editor
  • Find where we included request
  • Include yql below:
var YQL = require('yql');

Create a new function below the final } for sendRequestToCloudBit :

function getDataFromYQL()
{

}

If your using Atom, you can "collapse" and hide all your littleBit function, so we don't get totally confused, by pressing a little triangle next to the start of the function.

Imgur

#7. Getting data (c)

Next, we want to send a query to Yahoo to get us the weather for Bath. Copy the following and place it inside your getDataFromYQL() function:

//1. Set up query
  var query = new YQL('select item.forecast from weather.forecast '
                        +'where woeid in '
                        +'(select woeid from geo.places(1) where text="Bath, England")'
                        +'AND u=\'c\'');

note the where text = "Bath, England", we can change this to look at the weather for anywhere else in the world!

Then, like we had in our littleBits request, we can send our request and listen back for the results (or a failure code!). Paste this below your query:

//2. Run it and set up what happens when it completes
  query.exec(function (error, response) {

      if(error){
        console.log("error!");
      } else {
        console.log(JSON.stringify(response));
      }

  });

This will log to our console the entire response (unformatted)

Next, don't forget you need to call your function to make it run. Outside of the last } bracket for it, call:

getDataFromYQL();

Imgur

Finally, run your project in terminal or cmd.

node littlebits.js

If your request was successfully sent, you should see a bunch of text and numbers all smooshed together. Don't panic! Try looking out for the days of the week in the text, and it should tell you some additional weather information!

Imgur

#8. Reading our Data

So now we have a bunch of data - what're we going to do with it!

To make your data look like it makes some sense, copy the entire result:

Imgur

Then we can paste it into a tool like http://jsonlint.com/ - and click validate - to make it format it properly!

Imgur

So we can then see todays forecast text is inside an object called forecast which is an object called item which is an array called channels etc.

#9. Getting out the useful bits of our data

So now hopefully we have a bit of idea what the data we're getting back looks like, how do we get that out into code!

Firstly, we only really care about getting the array of individual week days. If we take a look at the data returned we can see that the individual day data is saved in a response - query - results - channel objects.

We can get an array of only those objects by creating our own variable and logging it out. You want to copy and paste the following into the success part of the if statement:

var results = response.query.results.channel;
console.log(JSON.stringify(results));

Try running that and test what happens.

Imgur

We can then refine that even further by getting a specific day in the array. Remember arrays start at 0 so if we want today:

var today = results[0];

If we then just wanted the weather text for today we could do :

var todayText = today.item.forecast.text;
console.log(todayText);

Imgur

Again, run your program and make sure it just displays the weather for today.

Try making it show you the weather for tomorrow instead!

#10. Final Logic!

So now we know what the weather forecast is going to be like for today. We can do a final "if" - if today is supposed to rain, tell our cloudbit to light up max. e.g.

if(todayText == "Rain"){
  sendRequestToCloudBit(20);
}

If today isn't going to rain, you could check instead if it's Sunny and light up differently!

My final code looks like this :

Imgur

#2. Cloud Control Test

  • Make sure your cloudBit is powered and has a green light to tell you it's connected to the Internet
  • Open a web browser (e.g. Chrome)
  • Go to http://control.littlebitscloud.cc/
  • Logon with the information on the card.
  • Select on the left the pokemon that matches your cloudBit
  • Make sure when you push the button, the light

Imgur

#3. Hello World

  • Create a folder on your desktop called hackday
  • Open up Atom (or your text editor)
  • Save the empty file as littlebits.js inside your hackday folder
  • write :
console.log("hello");

Mac / Ubuntu

  • open terminal
  • we're going to navigate into our Desktop / hackday folder
    • Use ls -l to list all the files and folders in the current directory
    • Use cd folder to enter in folders e.g. cd Desktop

Windows

  • open cmd
  • we're going to navigate into our Desktop / hackday folder
    • Use dir to list all the files and folders in the current directory
    • Use cd folder to enter in folders e.g. cd Desktop

To run your project type:

node littlebits.js

Imgur

#4. Sending data to a littleBit (a)

Downloading libraries

  • Open Terminal / cmd
  • We want to install a library called request so type the following into terminal / cmd:
npm install request
  • When they're downloading it might look like nothings happened, but wait a moment and you should see something like this:

Imgur

#4. Sending data to a littleBit (b)

Variables!

  • Open back the cloudbit control website (http://control.littlebitscloud.cc/)
  • Click on your pokemon name
  • Click on the settings tab at the bottom
  • Scroll down to check out the Device ID and AccessToken
  • Don't close this page!

Imgur

My cloudBit - they change regularly so make sure you check your own!

  • Open Atom or your text editor with littlebits.js
  • Add a deviceId and accessToken variable with your numbers e.g.
var deviceId = 243c200c0659;
var accessToken = db1a5b0a254dc532fd2d7265b8d758c8c4c03ed62220ab5d1daec47fdd9b9057;

#4. Sending data to a littleBit (c)

Actually sending data

  • Still in Atom or your text editor
  • We need to tell our file to "include" the library we installed earlier (do this above where you declare your two variables).
/set up request to send.
//includes
var request = require('request');
  • Request Options - copy the following after your two declerations
//1. set up request to send.
var options = {
  method: "POST",
  url: 'https://api-http.littlebitscloud.cc/v2/devices/'+deviceID+'/output',
  body: JSON.stringify({
    "percent":50,
    "duration_ms":1000 }),
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + accessToken
    }
  };

Here the only thing you need to pay attention to is the "body" tag - the percent and duration is the data that'll actually get sent to our bit!

  • Next we need to set up something to watch when our request completes (or fails)
  • Copy the following code after where you set up your options
 //2. what happens when it completes
  function callback(error, response, body) {
  
    if (!error && response.statusCode == 200) {
      console.log(JSON.stringify(body));
    } else {
      console.log(JSON.stringify(error));
    }
    
  }

Here, we're just checking if there were any errors, and logging if there were!

  • Now to send the actual request
  • Copy this after your callback code
//3. send request.
  request(options, callback);

We're just sending a request with the options, and callback we just created :-)

Imgur

  • Running our code
  • In your open Terminal / cmd
  • Type node littlebits.js again to run your project
  • Wait a second and you should see your bit light up for 1 second.

Try changing the percent and duration numbers and see what effect that has on your cloudBit

#5. Congratulate Yourself

If you've made it this far your deserve a high five around your team!

#6. Functions

Next want to make good reusable code!

We can encapsulate all our of our request code inside a function called sendRequestToCloudBit() e.g.

function sendRequestToCloudBit() {
  //all your request code in here
}

Imgur

If we want to make it even better, we can give it a parameter to set the bargraph e.g.

function sendRequestToCloudBit(percent) {
//code 
}

Don't forget inside the options section, to set the percent number to be the input percent e.g.

"percent":percent,

Finally, we need to call our function, e.g.:

sendRequestToCloudBit(100);

Imgur

my code

#7. Getting data (a)

Next, we need to install yql to access Yahoo's Weatherforecast.

  • Open terminal
  • type:
npm install yql
  • Again, you'll have to wait for it to install.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment