Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Control your Nest with Google App Engine PHP

Terrence Ryan
September 30, 2014

Control your Nest with Google App Engine PHP

An introduction to the promise and problems of working with Nest from PHP on Google App Engine

Terrence Ryan

September 30, 2014
Tweet

More Decks by Terrence Ryan

Other Decks in Technology

Transcript

  1. What you need • Nest device • Nest developer account

    • Nest developer client • Firebase client • Javascript • iOS • Android OR • REST API
  2. Read all the data $access_token = <OAUTH TOKEN>; $base_url =

    "https://developer-api.nest.com"; $path = ''; $auth = '/?auth=' . $access_token; ! $api_url = $base_url . $path . $auth; $results = file_get_contents($api_url, false);
  3. Read all the data (results) { "devices": { "thermostats": {

    "peyiJNo0IldT2YlIVtYaGQ": { "device_id": "peyiJNo0IldT2YlIVtYaGQ" , ... } } , "smoke_co_alarms": { "RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs": { "device_id": "RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs" , ... } } } , "structures": { "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw": { "structure_id": "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw" , "thermostats": [ "peyiJNo0IldT2YlIVtYaGQ", ... ] , "smoke_co_alarms": [ "RTMTKxsQTCxzVcsySOHPxKoF4OyCifrs", ... ] , ... } } } }
  4. Read all the thermostats $access_token = <OAUTH TOKEN>; $base_url =

    "https://developer-api.nest.com"; $path = '/devices/thermostats'; $auth = '/?auth=' . $access_token; ! $api_url = $base_url . $path . $auth; $results = file_get_contents($api_url, false); ! ! ! !
  5. Read all the thermostats (results) { "peyiJNo0IldT2YlIVtYaGQ": { "device_id": "peyiJNo0IldT2YlIVtYaGQ"

    , "locale": "en-US" , "software_version": "4.0" , "structure_id": "VqFabWH21nwVyd4RWgJgNb292wa7hG_dUwo2i2SG7j3-BOLY0BA4sw" , "name": "Hallway (upstairs)" , "name_long": "Hallway Thermostat (upstairs)" , "last_connection": "2014-03-02T23:20:19+00:00" , "is_online": true , "can_cool": true , "can_heat": true , "is_using_emergency_heat": true , "has_fan": true , "fan_timer_active": true , "fan_timer_timeout": "2014-03-02T23:20:19+00:00" , "has_leaf": true , "temperature_scale": "C" , "target_temperature_f": 72 , "target_temperature_c": 21.5 , "target_temperature_high_f": 72 , "target_temperature_high_c": 21.5 , "target_temperature_low_f": 64 , "target_temperature_low_c": 17.5 , "away_temperature_high_f": 72 , "away_temperature_high_c": 21.5 , "away_temperature_low_f": 64 , "away_temperature_low_c": 17.5 , "hvac_mode": "heat" , "ambient_temperature_f": 72 , "ambient_temperature_c": 21.5 } }
  6. How you would really use it. $access_token = <OAUTH TOKEN>;

    ! $device_id = <UNIQUE ID>; ! $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; ! $api_url = $base_url . $path . $auth; $results = file_get_contents($api_url, false); ! ! ! ! ! !
  7. Set the temperature $access_token = <OAUTH TOKEN>; ! $device_id =

    <UNIQUE ID>; ! $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; $url = $base_url . $path . $auth; ! ! $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, '70'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_exec($ch); ! Doesn’t Work
  8. $access_token = <OAUTH TOKEN>; ! $device_id = <UNIQUE ID>; !

    $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; $url = $base_url . $path . $auth; ! $context = [ 'http' => [ 'method' => 'PUT', 'follow_location' => true, 'content' => '70' ] ]; ! $context = stream_context_create($context); $result = file_get_contents($url, false, $context); Set the temperature $access_token = <OAUTH TOKEN>; ! $device_id = <UNIQUE ID>; ! $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; $url = $base_url . $path . $auth; ! $context = [ 'http' => [ 'method' => 'PUT', 'follow_location' => true, 'content' => '70' ] ]; ! $context = stream_context_create($context); $result = file_get_contents($url, false, $context); Doesn’t Work
  9. Set the temperature $access_token = <OAUTH TOKEN>; $device_id = <UNIQUE

    ID>; ! $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; $url = $base_url . $path . $auth; ! $context = [ 'http' => [ 'method' => 'PUT', 'follow_location' => false, 'content' => '73' ] ]; ! $status_code = 0; $attempts = 0; $context = stream_context_create($context); ! while (($status_code != 200) && $attempts <= 3 ){ $attempts++; $result =file_get_contents($url, false, $context); $response_headers = processHeaders($http_response_header); $status_code = $response_headers['status_code']; $url = $response_headers['location']; } Sorta Works
  10. Set the temperature $access_token = <OAUTH TOKEN>; $device_id = <UNIQUE

    ID>; ! $base_url = "https://developer-api.nest.com"; $path = '/devices/thermostats/'.$device_id.'/target_temperature_f'; $auth = '/?auth=' . $access_token; $url = $base_url . $path . $auth; ! $context = [ 'http' => [ 'method' => 'PUT', 'follow_location' => false, 'ignore_errors' => true, 'content' => '73' ] ]; ! $status_code = 0; $attempts = 0; $context = stream_context_create($context); ! while (($status_code != 200) && $attempts <= 3 ){ $attempts++; $result = json_decode(@file_get_contents($url, false, $context)); $response_headers = processHeaders($http_response_header); $status_code = $response_headers['status_code']; $url = $response_headers['location']; if ($status_code == 400){ break; } }
  11. What can you set? Thermostat • fan_timer_active • target_temperature_f •

    target_temperature_c • target_temperature_high_f • target_temperature_high_c • target_temperature_low_f • target_temperature_low_c • hvac_mode Structure • away • eta Smoke and CO Alarm • nothing
  12. Technical Limitations • REST calls are single shots • Streaming

    REST not a good match for PHP • REST API is severely rate limited
  13. Policy Limitations • Most attributes are read only • You

    many not retain data for more than 10 days
  14. Result • Nest apps are about: • Real time •

    Responding to Nest • Not making Nest respond
  15. Javascript Client function initNest(){ var nest_token = getCookie('nest_token'); dataRef =

    new Firebase('wss://developer-api.nest.com'); dataRef.auth(nest_token); dataRef.on("value", readThermostatTemp); } ! function processTempChange(e){ var device_id = e.target.name; var target_value = parseInt(e.target.value); var path = "devices/thermostats/" + device_id + "/target_temperature_f"; dataRef.child(path).set(target_value); } ! function processNestTempChange(e){ var device = e.val(); var device_id = device.device_id; var newvalue = device.target_temperature_f; var el = document.querySelector("#" + device_id); el.value = newvalue; }