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: []
I can't be the first to have this problem, but the documentation for Hue in general is sparse. I'm already doing both UPnP and N-UPnP searches, but I've seen mentions of doing an IP scan where you can set a specific IP to look for (I know the IP), but documentation for that is practically non-existent. Any ideas?
Since docs are sparse and this is close to working, here's the useful parts of my code in case it helps others or shows I'm doing something wrong.
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);
});
I then pass the lightsObject to my admin page through the render function, do a for loop to loop through each light in the returned object, then show some toggle switches based on the state in the object. Then onchange
, I run a jQuery AJAX call to the app.put method here, which runs the node-hue-api code to set the light's state, passing the lightId from the value attribute of the toggle from the admin page. Here's a pic to show that working.
And the app.put
code. Could probably combine these, but I wanted separate calls in case I want to do something more creative with the on state.
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.