Skip to content

Instantly share code, notes, and snippets.

@kylejcarlton
Last active June 29, 2017 17:07
Show Gist options
  • Save kylejcarlton/12a85c4a5b375eaff62ee509d76a6720 to your computer and use it in GitHub Desktop.
Save kylejcarlton/12a85c4a5b375eaff62ee509d76a6720 to your computer and use it in GitHub Desktop.
Google Apps Script that records temperature values from TMP36 sensor connected to Particle Photon, Nest thermostat and WeatherUnderground API into Sheet. More @ http://techiys.com/tagged/WiFiTemp.
//PARTICLE DEVICE
//Define the Particle Photon's Device ID and Access Token. Then use those variables to create the URL to access JSON output from board.
var deviceID = "3e002..................."; //Set to specific device ID.
var accessToken = "6dae...................................."; //Set to specific device's access token.
var particleUrl = 'https://api.particle.io/v1/devices/' + deviceID + '/analogvalue?access_token=' + accessToken;
//WEATHERUNDERGROUND
//Define WeatherUnderground API key, weather station location and request type. Then create URL to access JSON results.
var apiKey = "d1792..........."; //Set to your API Key.
var requestType = "conditions";
var wStation = "KILCHICA310"; //Set to desired weather station location.
var weatherUrl = 'http://api.wunderground.com/api/' + apiKey + '/' + requestType + '/q/pws:' + wStation + '.json';
//NEST THERMOSTAT
//Define Nest URL, device ID and OAuth v2.0 credentials
var nestID = "N4ndF..........................."
var nestAuth = "c.PNi.............................................................................................................................................";
var nestUrl = "https://developer-api.nest.com";
//Obtain active spreadsheet and set Results sheet to write results in.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var wsheet = ss.getSheetByName("Temperature_History");
function readTemperatures() {
// Create Array to store Date, Temperature from Particle device, WeatherUnderground API results and Nest Thermostat results.
var results = new Array(4);
results[0] = new Date();
//Parse JSON output from particleURL into array set photonValue to Voltage reading from Photon JSON output convert to C then F and write in results array.
var response = UrlFetchApp.fetch(particleUrl);
var json = response.getContentText();
var data = JSON.parse(json);
var photonValue = data.result;
var tempC = 100 * (photonValue*0.0008) - 50;
var tempF = tempC*1.8+32;
results[1] = tempF;
//Parse JSON output from weatherURL into array, set currentTemp and then write to results array.
var response = UrlFetchApp.fetch(weatherUrl);
var json = response.getContentText();
var data = JSON.parse(json);
var currentTemp = data.current_observation.temp_f;
results[2] = currentTemp;
//Parse JSON output from nestURL into array, set nestTemp and then write to results array.
var response = UrlFetchApp.fetch(nestUrl, {
headers: {
Authorization: 'Bearer ' + nestAuth
}
});
var json = response.getContentText();
var data = JSON.parse(json);
var nestTemp = data.devices.thermostats[nestID].ambient_temperature_f;
results[3] = nestTemp;
//var response = UrlFetchApp.fetch(nestUrl, nestOptions);
//Logger.log(response.getContentText());
wsheet.appendRow(results);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment