Examples

We have created open source example applications using popular programming languages that demonstrate how to build direct integrations on top of the Venstar Thermostat Local API.

SSDP Example

Discovery Example using Node.js:
const dgram = require('dgram');
const socket = dgram.createSocket('udp4');
const interfaces = require('os').networkInterfaces();
const multicastAddress = '239.255.255.250';
const multicastPort = 1900;

//bind to multicast address & port using our ip address
socket.bind(multicastPort, () => {
  socket.addMembership(multicastAddress, '192.168.1.111'); //my ip address
};

//wait for incoming messages and print ip address
socket.on('message', ( data, rinfo ) => {
  console.log(new Date() + ' RECEIVER received from ', rinfo.address, ':');
  console.log(data.toString());
});

//Set up discovery message. Make sure to leave out any extra space in the message.
const discover_message = new Buffer('M-SEARCH * HTTP/1.1\r\nHost: 239.255.255.250:1900\r\nMan: ssdp:discover\r\nST: colortouch:ecp\r\n');
socket.send(discover_message, 0, discover_message.length, multicastPort, multicastAddress);
Example Response
Thu Apr 24 2014 12:22:12 GMT-0700 (PDT) RECEIVER received from 192.168.1.104 :
HTTP/1.1 200 OK
Cache-Control: max-age=300
ST: venstar:thermostat:ecp
Location: http://192.168.1.104/
USN: colortouch:ecp:78:c4:0e:01:66:58:name:TEST1:type:residential

Thu Apr 24 2014 12:22:12 GMT-0700 (PDT) RECEIVER received from 192.168.1.102 :
HTTP/1.1 200 OK
Cache-Control: max-age=300
ST: venstar:thermostat:ecp
Location: http://192.168.1.102/
USN: colortouch:ecp:78:c4:0e:01:67:c7:name:TEST2:type:commercial

NodeJS

This example uses the request module
GET Request
const request = require('request');

request('http://192.168.1.104/query/info', (error, response, body) => {
  if (!error && response.statusCode == 200) {
    const stat = JSON.parse(body);
    console.log(stat.name);
  }
});
POST Request
request.post({
  url:'http://192.168.1.104/control',
  form:{
  mode:3, //setting mode to AUTO
  fan:0, //fan to AUTO
  heattemp: 75,
  cooltemp: 78
}, (e,r, body) => {
  console.log(body);
});

PHP

This example uses the libcurl, a library created by Daniel Stenberg, support for PHP that allows you to
connect and communicate to many different types of servers with many different types of protocols.
GET Request
$url = 'http://192.168.1.104/query/info';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);

//exec call
$result = curl_exec($ch);
$obj = json_decode($result);
echo 'Thermostat Name: ' . $obj->name;

//close connection
curl_close($ch);
POST Request
$url = 'http://192.168.1.104/control';

$fields = array(
  'mode' => 3, //setting thermostat mode to AUTO
  'fan' => 0,
  'heattemp' => 75,
  'cooltemp' => 78
);

//pretifying fields for curl
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//exec call
$result = curl_exec($ch);

var_export($result);

//close connection
curl_close($ch);

Python

This example uses urllib, a built-in Python module, that opens arbitrary resources by URL.
GET Request
import json,urllib

url = 'http://192.168.1.104/query/info'
u = urllib.urlopen(url)

# u is a file-like object
jsonStr = u.read()

# parse json string to dictionary
data = json.loads(jsonStr)
print data['name'] # print thermostat name
POST Request
import urllib

url = 'http://192.168.1.104/control'

params = urllib.urlencode({
  'mode': 0,
  'fan': 0,
  'heattemp': 75,
  'cooltemp': 78
})
data = urllib.urlopen(url, params).read()