#littleBits hack day
##1. Tools to install!
- Node.js = https://nodejs.org/
- A text editor e.g. Atom.io = https://atom.io/
- Open terminal
- type
node
- if it doesn't complain your probably fine 😄
- if it complains, ask me for some help!
#littleBits hack day
##1. Tools to install!
node
#7. Getting data (b)
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.
#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();
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!
#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:
Then we can paste it into a tool like http://jsonlint.com/ - and click validate - to make it format it properly!
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.
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);
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 :
#2. Cloud Control Test
#3. Hello World
littlebits.js
inside your hackday folderconsole.log("hello");
ls -l
to list all the files and folders in the current directorycd folder
to enter in folders e.g. cd Desktop
dir
to list all the files and folders in the current directorycd folder
to enter in folders e.g. cd Desktop
To run your project type:
node littlebits.js
#4. Sending data to a littleBit (a)
npm install request
#4. Sending data to a littleBit (b)
Device ID
and AccessToken
My cloudBit - they change regularly so make sure you check your own!
var deviceId = 243c200c0659;
var accessToken = db1a5b0a254dc532fd2d7265b8d758c8c4c03ed62220ab5d1daec47fdd9b9057;
#4. Sending data to a littleBit (c)
/set up request to send.
//includes
var request = require('request');
//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!
//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!
//3. send request.
request(options, callback);
We're just sending a request with the options, and callback we just created :-)
node littlebits.js
again to run your project#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
}
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);
my code