Published Saturday 10 February 2018
Categories: Home Automation |

Share this Share on Facebook Share on Twitter Share on Google Plus
Post View Counter 177

This code example shows how you can use C# in either a Windows application or asp.net website to retrieve data from the Nest API to show information for your Nest Thermostat or Nest Smoke Alarms.

Before starting to write your code, you need to create a “product” on the Nest developer site at https://console.developers.nest.com/products

Create a new product and select the permissions for Smoke-CO Alarm and Thermostat. The fields for “Default OAuth Redirect URI” and “Additional OAuth Redirect URIs” can be left blank.

Once your product has been created you will see the following screen with your products information.

Nest Product overview page

You need to use the Product ID, Product Secret and Authorization URL with the code. Open the Authorization URL in a browser and you will be asked to authorise the product to access your Nest products. Confirm the authorisation and you will be given a code which you use to generate an access token.

Using a Linux terminal or Linux subsystem on Windows 10, enter the following to generate your access token.

curl -X POST -d "code=[code]&client_id=[Product ID]&client_secret=[Product Secret]&grant_type=authorization_code" https://api.home.nest.com/oauth2/access_token

This will return a JSON string which contains your access token for your account.

Entering the following url into your web browser will return the JSON data for your Nest devices:

https://firebase-apiserver10-tah01-iad01.dapi.production.nest.com:9553/?auth=[access  token]

Now you have the full JSON data you can view the data in a easy to view format using a site such as https://jsonformatter.curiousconcept.com/ or use your own JSON parser to obtain the data fields you require.

The basic JSON format has a top level “devices” node followed by “thermostats” and “smoke_co_alarms” in the second level. Each of these contains a node with a unique identifier for your device and these contain the information for each device.

Below are C# classes generated from a thermostat and smoke/co alarm:

public class Thermostat
    {
        public int humidity { get; set; }
        public string locale { get; set; }
        public string temperature_scale { get; set; }
        public bool is_using_emergency_heat { get; set; }
        public bool has_fan { get; set; }
        public string software_version { get; set; }
        public bool has_leaf { get; set; }
        public string where_id { get; set; }
        public string device_id { get; set; }
        public string name { get; set; }
        public bool can_heat { get; set; }
        public bool can_cool { get; set; }
        public double target_temperature_c { get; set; }
        public int target_temperature_f { get; set; }
        public double target_temperature_high_c { get; set; }
        public int target_temperature_high_f { get; set; }
        public double target_temperature_low_c { get; set; }
        public int target_temperature_low_f { get; set; }
        public double ambient_temperature_c { get; set; }
        public int ambient_temperature_f { get; set; }
        public double away_temperature_high_c { get; set; }
        public int away_temperature_high_f { get; set; }
        public double away_temperature_low_c { get; set; }
        public int away_temperature_low_f { get; set; }
        public double eco_temperature_high_c { get; set; }
        public int eco_temperature_high_f { get; set; }
        public double eco_temperature_low_c { get; set; }
        public int eco_temperature_low_f { get; set; }
        public bool is_locked { get; set; }
        public double locked_temp_min_c { get; set; }
        public int locked_temp_min_f { get; set; }
        public double locked_temp_max_c { get; set; }
        public int locked_temp_max_f { get; set; }
        public bool sunlight_correction_active { get; set; }
        public bool sunlight_correction_enabled { get; set; }
        public string structure_id { get; set; }
        public bool fan_timer_active { get; set; }
        public DateTime fan_timer_timeout { get; set; }
        public int fan_timer_duration { get; set; }
        public string previous_hvac_mode { get; set; }
        public string hvac_mode { get; set; }
        public string time_to_target { get; set; }
        public string time_to_target_training { get; set; }
        public string where_name { get; set; }
        public string label { get; set; }
        public string name_long { get; set; }
        public bool is_online { get; set; }
        public DateTime last_connection { get; set; }
        public string hvac_state { get; set; }
    }
    
    public class SmokeAlarm
    {
        public string name { get; set; }
        public string locale { get; set; }
        public string structure_id { get; set; }
        public string software_version { get; set; }
        public string where_id { get; set; }
        public string device_id { get; set; }
        public string where_name { get; set; }
        public string name_long { get; set; }
        public bool is_online { get; set; }
        public DateTime last_connection { get; set; }
        public string battery_health { get; set; }
        public string co_alarm_state { get; set; }
        public string smoke_alarm_state { get; set; }
        public string ui_color_state { get; set; }
        public bool is_manual_test_active { get; set; }
        public DateTime last_manual_test_time { get; set; }
    }

Using the access token you can retrieve the JSON data using a Windows application or ASP.Net web page with the following code which writes to two text boxes called tb1 and tb2:

string url = "https://firebase-apiserver10-tah01-iad01.dapi.production.nest.com:9553/?auth=[your access token]";
try
{
    var request = (HttpWebRequest)WebRequest.Create(url);
    request.KeepAlive = false;
    request.Timeout = 10000;
    
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        Stream dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
    
        dynamic data = JObject.Parse(responseFromServer);
        tb1.Text = data.ToString();
    
       tb2.Text = data.devices.thermostats.[your thermostats unique ID].hvac_state.ToString();
    }
}
catch (Exception ex)
	{
		tb1.Text = ex.ToString();
	}

Permalink


0 Comments



Post your comment