API Basics - Ruxiio/EzHue GitHub Wiki
To start with the EzHue API, you will want to download the latest version of the API and add it to your project.
After you have the API installed, you will want to include it into your HTML file like so.
<!-- Link to EzHue API -->
<script type="text/javascript" src=".../EzHue.js"></script>
Now we are ready to communicate with your bridge! To do this we first need to instantiate the API into our javascript project.
//Creates a reference to the API
var hueApi = new EzHue();
Before you go any further - If you are not familiar with what "callback functions" are, I suggest you go to the "Callbacks, What and Why" section of the Wiki. Callback functions are used quite heavily in this API to allow you to have more control over what is going on between the API and the Hue Bridge
Next, we need to get connected to the bridge by running the createBridge
function.
This function requires three callback functions to work correctly. This is because the bridge requires the user to press the link button on the bridge to acquire a username.
- The first callback function is to alert the user that they need to press the link button to proceed.
- The second callback function is ran if the user failed to press the link button in time.
- The last callback function is ran if the bridge has successfully been created and is passed the bridge data for quick access.
TIP - The API waits 10 seconds before checking if the link button has been pressed
//Finds and gains access to bridge
hueApi.createBridge(function(){
//The alert callback
console.log("Press the link button on the bridge within 10 seconds!");
}, function(){
//The failure callback
console.log("Link button was not pressed within 10 seconds, please try again");
}, function(res){
//The success callback
console.log("Bridge created successfully! Bridge IP : " + res.ip);
});
Assuming the success callback function is run after running the createBridge()
function, you are set and ready to use your bridge!
If you were not successful in creating the bridge and the "Failure callback" ran,but you are sure you have pressed the link button in the required time, please refer to the EzHue.createBridge() section for more in depth details of what may be going wrong.