Examples


Here is a list of simple code examples in different languages to get you started.


SSDP Example

Discovery Example using Node.js

var dgram = require('dgram')
, socket = dgram.createSocket('udp4')
, interfaces = require('os').networkInterfaces()
, multicastAddress = '239.255.255.250'
, multicastPort = 1900;

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

//wait for incoming messages and print ip address
socket.on('message', function ( 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.
var 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)
								

Gist available here.

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: colortouch:ecp
Location: http://192.168.1.104/
USN: 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: colortouch:ecp
Location: http://192.168.1.102/
USN: ecp:78:c4:0e:01:67:c7:name:TEST2:type:commercial
								

Note the USN attribute identifying, the mac address, equipment name, and type.


NodeJS

This example uses the request module.

GET Request
var request = require('request');
request('http://192.168.1.104/query/info', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var 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
  }
}, function(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()