Skip to content

Instantly share code, notes, and snippets.

@renoeno
Created March 30, 2016 15:49
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 renoeno/2c2bdd81995275d0dce228afc043abc0 to your computer and use it in GitHub Desktop.
Save renoeno/2c2bdd81995275d0dce228afc043abc0 to your computer and use it in GitHub Desktop.
// API to acess weather information
var apikey = "28167fb511851659e54ae51574586b15";
// defining current city and below data, sunrise time, sunset time, current time and quantity of clouds,
//information that is gonna be retrieved using Open Weather Map API.
var currentCity = "Toronto";
var data;
var sunrise;
var sunset;
var currentTime;
var clouds;
/*
The data is retrieved using a jQuery function, right below. If the request is successful, the page with information
is received, and then we get informations of Toronto weather, specified above. Sunrise time is in Time Unix UTC
format, so is necessary a convertion to know it in a more concrete form. Those informations are printed in console
just to know if they were really received.
*/
var $ = require('jQuery');
console.log(currentCity);
/*
var url = "http://api.openweathermap.org/data/2.5/weather?q="+currentCity+"&APPID="+apikey;
$.getJSON( url, function(jsonData) {
data = jsonData;
var sec = data.sys.sunset;
var date = new Date(sec * 1000);
var n = new Date().getHours();
var timestr = date.toLocaleTimeString();
document.write("Current sunset time at " + currentCity +" is "+ timestr);
document.write("<br>");
document.write("Current time at " + currentCity +" is "+ n);
document.write("<br>");
document.write("Current clouds at " + currentCity +" is "+ data.clouds.all);
// getSunriseTime(currentCity);
// getSunsetTime ();
// updateHTML();
// sendToParticle (sunrise, sunset);
});
*/
var request = require("request")
var url = "http://api.openweathermap.org/data/2.5/weather?q="+currentCity+"&APPID="+apikey;
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
//console.log(body) // Print the json response
data = body;
var sec = data.sys.sunset;
var date = new Date(sec * 1000);
currentTime = new Date().getHours();
sunset = date.toLocaleTimeString('en-US', { hour12: false }).split(":");
sunrise = new Date(data.sys.sunrise * 1000).toLocaleTimeString('en-US', { hour12: false }).split(":");
clouds = data.clouds.all;
console.log("Current sunset time at " + currentCity +" is "+ sunset[0]);
console.log("Current sunrise time at " + currentCity +" is "+ sunrise[0]);
console.log("Current time at " + currentCity +" is "+ currentTime);
console.log("Current clouds at " + currentCity +" is "+ clouds);
}
})
//creating particle object and token variable.
var Particle = require('particle-api-js');
var particle = new Particle();
var token;
//logging in with photon. Password changed for obvious motives.
particle.login({username: 'renoalm@gmail.com', password: 'xxxxxxx'})
.then(function(response) {
//token variable receive access token of reponse.
token = response.body.access_token;
return particle.listDevices({ auth: token });
})
.then(function(devices) {
// `devices` is an array of resolved Particle#getDevice responses
console.log(devices);
})
.then(function(data){
//each function needs to be called individually, otherwise only the first one of the row (in case they all are in the same statement)
//is gonna be called.
//function that sends sunset time to photon.
return particle.callFunction({ deviceId: '3e003b000f47343432313031', name: 'sunset', argument: sunset.toString(), auth: token });
})
.then(function(data){
//function that sends sunrise time to photon.
return particle.callFunction({ deviceId: '3e003b000f47343432313031', name: 'sunrise', argument: sunrise.toString(), auth: token });
})
.then(function(data){
//function that sends current time to photon.
return particle.callFunction({ deviceId: '3e003b000f47343432313031', name: 'time', argument: currentTime.toString(), auth: token });
})
.then(function(data){
//function that sends percentage of cloudness to photon.
return particle.callFunction({ deviceId: '3e003b000f47343432313031', name: 'clouds', argument: clouds.toString(), auth: token });
})
.then(function(data){
//function that light neopixel ring.
return particle.callFunction({ deviceId: '3e003b000f47343432313031', name: 'shine', argument: 'nana', auth: token });
})
.catch(function(error) {
// Any API client error will get caught down here
console.error(error);
});
/*
var jq = document.createElement('script');
jq.src = "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js";
document.querySelector('head').appendChild(jq);
jq.onload = procede;//DON'T TYPE PARENTHESIS
//i.e. 'procede()' runs instantly and assigns return value to jq.onload
// 'procede' gives it a function to run when it's ready (what you want)
function procede()
{
//jQuery commands are loaded (do your magic)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment