I am using the node-hue-api package on an Node.js/Express server to work with the Hue API. I've built an admin section of a website that only I can access that I'd like to use to control my Hue lights. This works fine in my dev environment as my localhost is obviously running on the same network/IP as the Bridge.
I think the problem is that when I push my changes to my production environment -- which is running on a Digital Ocean droplet of a different IP that the Bridge isn't connected to or aware of, it obviously can't find the Bridge -- returns empty array as response:
Hue Bridges Found: []
hue = require("node-hue-api");
//===== Hue =====
var HueApi = require("node-hue-api").HueApi,
lightState = hue.lightState,
timeout = 5000;
var displayResult = function(result) {
console.log(JSON.stringify(result, null, 2));
};
var displayError = function(err) {
console.error(err);
};
var displayBridges = function(bridge) {
console.log("Hue Bridges Found: " + JSON.stringify(bridge));
};
hue.nupnpSearch().then(displayBridges).done();
hue.upnpSearch(timeout).then(displayBridges).done();
var hostname = "my-ip-address",
username = "my-registered-user-hash",
api = new HueApi(hostname, username),
state = lightState.create(),
lightsObject;
//Get all lights attached to the bridge
api.lights(function(err, lights) {
if (err) throw err;
lightsObject = lights;
displayResult(lightsObject);
console.log(lightsObject);
});
onchange
app.put
app.put('/lighton', function(req, res) {
//Set the state of the light
api.setLightState(req.body.lightId, state.on())
.fail(displayError)
.done();
});
app.put('/lightoff', function(req, res) {
//Set the state of the light
api.setLightState(req.body.lightId, state.off())
.fail(displayError)
.done();
});
According to the FAQs in the Phillips Hue API documentation pages:
Where is the API located?
The hue API in the bridge is accessible on your local network. That is to say, it is accessible on your local WiFi or wired network and cannot be accessed directly outside that network. This also makes your hue system secure. The hue bridge, once installed on your network, will have its own IP address set up by your local router. This is the IP address you will use when sending commands to the RESTful API.
...
Can I get remote access to hue (e.g. other than IFTTT)?
It is planned that we will have a remote API for accessing hue across the Internet. For now the only option available is to use the IFTTT interface.
(emphasis added)
So you'll either need to expose some custom API of your creation on your home server, or use IFTTT (I believe a maker channel could do what you want here, but I've not worked with one before so am not really sure), if this FAQ is to be believed.