Created
September 10, 2015 14:10
-
-
Save hrbrmstr/2da2312170a84340c14f to your computer and use it in GitHub Desktop.
working with the Nest API in R - code for http://rud.is/b/2015/09/10/a-better-way-to-read-nest-data-into-r/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(httr) | |
# Go to https://developer.nest.com/clients to register a new Nest client | |
# Nest OAuth 2.0 endpoints | |
nest <- oauth_endpoint( | |
request=NULL, | |
authorize="https://home.nest.com/login/oauth2?state=login", | |
access="https://api.home.nest.com/oauth2/access_token" | |
) | |
# Store your secret in NEST_CONSUMER_SECRET in .Renviron | |
nest_app <- oauth_app("nest", key="a8bf6e0c-89a0-40ae-869a-943e928316f5") | |
# Configure your app to require a PIN. The user will have to cut/paste | |
# the URL in the R Console into a browser then cut/paste the PIN | |
# back into the R Console | |
nest_token <- oauth2.0_token(nest, nest_app, use_oob=TRUE, cache=TRUE) | |
# Nest API usage examples | |
# Get structures | |
req <- GET("https://developer-api.nest.com", | |
path="structures", | |
query=list(auth=nest_token$credentials$access_token)) | |
stop_for_status(req) | |
structures <-fromJSON(content(req, as="text") | |
# Get devices | |
req <- GET("https://developer-api.nest.com", | |
path="devices", | |
query=list(auth=nest_token$credentials$access_token)) | |
stop_for_status(req) | |
devices <- fromJSON(content(req, as=text)) | |
# Get 1st thermostat readings | |
first_thermostat <- names(devices$thermostats)[1] | |
req <- GET("https://developer-api.nest.com/", | |
path=sprintf("devices/thermostats/%s", first_thermostat), | |
query=list(auth=nest_token$credentials$access_token)) | |
stop_for_status(req) | |
thermo <- data.frame(fromJSON(content(req, as="text")), | |
stringsAsFactors=FALSE) | |
cat(thermo$ambient_temperature_f, "F / ", thermo$humidity, "%", sep="") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Super minor, and know this is old (but I think it's still the best example in R!):
I think
library(rjsion)
needs to be added to the top of this script.