How do I connect ZigBee to Arduino

How to Control Philips Hue Lights from an Arduino (and add a motion sensor)

The Philips Hue range of lights certainly isn't cheap (there are some alternatives that are really worth it.) Philips Hue alternatives that are really worth The Philips Hue system leads the way when it comes to smart lighting, but there are some worthwhile alternatives. We picked three that all share the same basic features and hacks. More info), but I appreciate the well-documented API What are APIs and how are open APIs changing the internet, and how are open APIs changing on the internet? Have you ever wondered how programs on your computer "talk" to each other and the websites you visit? Learn more about creating your own Hue apps. Today I'll show you how to control your Hue lights from an Arduino - then add a simple motion sensor.

Why are we doing this Because home automation systems can be quite rigid and expensive. By learning how to control the hue from within Arduino, you open the door to a wide variety of bespoke home automation projects that simply cannot be beaten with off-the-shelf components. If you're just hacking things together, check out these 8 awesome Hue apps. 8 Fabulous Apps for Philips Hue Lights 8 Fabulous Apps for Philips Hue Lights The Philips Hue system has its own app, but why settle for the standard? cooler options out there? These 8 apps have fantastic options for some really cool effects. Instead, read More

The Hue system

Let's get a little technical so that you know the underlying systems that you are working with. Hue lights form a mesh network Mesh Networks: The Future of Communication Mesh Networks: The Future of Communication Mesh networks are almost invulnerable. In a mesh network there are no throttling points through which all traffic flows. Instead, information is passed from one device to the next until it reaches its destination. Read More In particular, they are ZigBee Light Link certified. That means that other ZLL products besides the Hue should also work (in theory).

Although the range is technically very short, the ZigBee networking function means that each new light bulb expands the network and forwards messages to other light bulbs. So if you're having trouble controlling a light on the other side of the house, put another light between the two.

ZigBee is a great protocol, but it's very different from WiFi or a wired computer network. So we need the Philips Hue Bridge to connect the two together. The Hue Bridge runs modified open source Linux firmware that sends a basic web server.

By interacting with this local web server, you can determine and control the current status of the luminaires.

It's a beautifully simple system and ready for home improvement projects. Kudos to Philips for making this thing so hackable.

Starting

Before you can access the API documentation, you must register as a developer. It's free, but you have to accept the terms and conditions. Do this now.

Anyone who is familiar with standard web services or Javascript should be able to work with the Hue: All data is passed and received as JSON packets. What is JSON? Layperson Overview What is JSON? Layperson Overview Whether you are a web developer or not, it is a good idea to at least know what JSON is, why it matters, and why it is used all over the web. Continue reading .

To see this in action, you need to know the IP address of your Hue bridge. There are several possibilities for that:

  • Take a look at the DHCP address assignment table in your router's admin interface
  • Run a network mapping program such as IP Scanner Home
  • Try the Philips UPnP broker tool
  • Ringing “philips-hue.home”

When you're done, enter it in your browser's address bar debug / clip.html appended to the URL. In my case this was:

This is a debugging tool that allows you to send and receive the JSON packets through a simple web interface. The first step is to activate the developer profile on the Hue Bridge yourself - this is deactivated by default for security reasons. Paste the following into the BODY box and leave the URL as / api /and send a POST request by clicking Post button:

If this is your first time doing this, you will see a “shortcut key not pressed” somewhere in the answer. This is a security feature that requires that any newly used application be physically authorized. Find your bridge, press the button and send the same request again within 30 seconds. This time you will get a different answer and the user will be authorized. If you'd like to use a different username, see the API documents on creating users. That's enough for the moment.

Once your user is set up, it becomes the base URL that you should interact with / api / new developer / . You can send a GET request to find out what your bridge currently knows about connected lights, alarms, scenes and a short log of the apps used. Here is an example of some of the status information that is included for a single light bulb.

Note that the “On”: true State doesn't really tell you whether the bulb is on or not; only that according to the bridge settings should to be up. “Reachable”: wrong can indicate a bulb that is too far away or simply switch it off at the power switch

One final example before we integrate this into the Arduino: make sure one of your lights is visible and on and you know what number it is. Change the url to / api / new developer / lights / 1 / state (Change the number in your light) and send a PUT request with the following data:

You should see your light react like this:

What you did is give the light bulb a new condition. You can also add “Transition Time”, which is a primitive animation method that tells how many seconds you want the status change to take.

There are several ways to set the color, but unfortunately there is no easy way to send an RGB value. In the example above we have sent hue, saturation and briHealth Change the hue value and send the PUT request again.

Working from Arduino

There is an existing Hue / Arduino library called ArduinoHue that Philips links itself to in the API documentation. The problem with this library, however, is that it uses the USB connection to communicate with your PC, which also has a Python application running all the time. Eugh. You could just as easily run your computer at this point and cut out the Arduino entirely.

Instead, I'll show you how to control the hue from the Arduino with an ethernet shield. Your Arduino doesn't need to be connected to a computer, so it can operate regardless of where you can plug an ethernet cable. In fact, it should work with a Wi-Fi shield too, but I don't have one to play with.

If you'd like to go further, the full sample code is embedded below or available here. I adapted it from an example provided by Gilson Oguime. If you've never played with your ethernet shield before, you might want to take a quick walk through the tutorial for the web client. I am assuming that you have some level of familiarity with it and that you are not describing the code used to build a network, IP etc. We also showed you how to create a mini web server using the ethernet shield. Give your Arduino project its own mini web server with an ethernet shield. Give your Arduino project its own mini web server with an ethernet shield A. I showed you how to set up an internet control system for your Arduino a while ago - but it had to stay connected to a computer via USB in order to connect to the internet to maintain.

Create state changes

Creating a new state to press on the light bulbs is an easy case when creating a new one String variable and with all quotation marks. I've added one random () Function there too to create a somewhat dynamic animation. Try to randomize the hue variable for different colors as well.

Sending the command

To actually send the command there is a helper function called setHue () that takes the light number and command string as arguments:

Then it just connects to the bridge, spits out the command as a PUT request, along with all the other nonsense that comes with making an HTTP request.

Add motion sensor

Finally, I connected a simple HC-SR501 motion sensor to digital I / O pin 2. When motion is detected, a series of 2 lamp states is pushed to the bridge to create a slow, dynamic animation. When the motion sensor is deactivated, a single switch-off command is sent to both. Ideally, they would be restored to the state they were in before motion was detected, but the logic isn't that smart - we're just going to toggle it on and off.

restrictions

While not optimized code, it takes almost a second for the Arduino's network interface to send a single command request. I tested the same command from a Mac on the same Ethernet connection and was able to get it ten to twenty times the speed (here the AppleScript, in case you want to test). As a result, any kind of fast animation (I was trying to create a flickering candle effect) is just not possible from an Arduino. This shouldn't be a problem for most projects, just high-speed animation, but it's good to be aware of the limitations.

It's also difficult to fully parse JSON responses you get from the bridge. There isn't enough RAM on the Arduino to store all of the raw data. For this reason, you should mainly limit yourself to sending.

Hack the shade

Now that you are armed with the knowledge of how to control Hue completely independently, a whole world of Hue home automation hacks opens up to you. The real question is: what are you going to do?

Learn more about: Arduino, Philips Hue, Smart Lighting.