Philips hue lighting system - a JSON device

My son got a Philips Hue bridge (network device) and some wi-fi bulbs. He wants to have his computer control the light bulbs. Has anyone played around with this Philips "personal wireless lighting system"? There is a Developers site and there are SDKs in a wide variety of languages but sadly none in MATLAB. So I guess one approach is to use the Java SDK and talk to that from MATLAB. I could probably figure it out but I was just hoping that if someone has done it already, I could start with a leg up. I already searched the entire Mathworks website and there is nothing on it (so far).

7 Commenti

I searched for ‘Philips Hue’ on the Arduino and Raspberry Pi sites (that MATLAB supports) and couldn’t find anything there. From what I’ve read about them, they could have the hardware to communicate with the lights. You would have to search them to find out.
Mark Hayworth
Mark Hayworth il 17 Gen 2016
Modificato: Mark Hayworth il 17 Gen 2016
Star, thank for your reply. I've been playing around with it for many hours this weekend. Evidently this thing uses JSON Script Object Notation, which is a communication protocol that seems like it aims to be like a simpler less verbose protocol than XML. I've never heard of it but apparently it's rapidly on it's way to becoming hugely popular so I'm sure we'll hear more and more about it. There is a small number of JSON posts on the Answers forum but none seemed to help me. Basically you communicate with JSON devices using the webread(), weboptions(), and webwrite() functions. When I query the device with webread(), that seems to work fine. However the webwrite is not working. It seems like there is some slight difference between the PUT method and the POST method and, unfortunately, it seems like webwrite() only can use the POST method. However when I use the POST method, I get an error. Even if I send the command using the manufacturers utility, PUT works while POST does not. So it seems like if I could just get webwrite() to use the PUT method, it might work, but unfortunately that does not seem like an option, at least not that I can figure out.
For what it's worth, I'm attaching my script - what I have so far. Like I said, I can ask the device for things and get answers, but I can't send it any data. So the first part works, but the second part doesn't (yet).
For your information, JSON has been around for over a decade and is already hugely popular for data serialisation (particularly with javascript). It's certainly much easier to process and less heavy than XML.
Not that it helps with your question.
Guillaume — Your Comment quite definitely could help if you could attach or link to any free references, ideally with code examples, on how to interface the requisite javascript functions with core MATLAB. A suite of such functions, if they don’t already exist, would be worthy of an undergraduate or Masters thesis project, uploaded to the File Exchange.
Yes, there are a few, not many, things in Answers and the File Exchange on JSON. I think it is a much easier to use protocol than XML and I have it working pretty well now, at least for the few commands that I need to send to the lamp. And I did upload code to a (now collapsed) comment to Stephen below.
The current problem now is figuring out the IP address of the lamp because my router changes it every few days. I don't think JSON can help with that, but I don't know what can. I'm still hoping for a solution on my Windows 10 system.
Finding the IP address was not too hard, as follows:
url = 'https://www.meethue.com/api/nupnp';
data = webread(url);
Hue_ID = data.id;
Hue_IP = data.internalipaddress;
url = sprintf('http://%s/api/invalidUserID1234/', Hue_IP)
response = webread(url);
response.error
type: 1
address: '/'
description: 'unauthorized user'
Now I have to figure out how to use POST without using webwrite (I use ML2014b) to send messages to it such as..
body = '{"devicetype":"my_hue_app#_appname"}'
The next step would be to get a valid user name after pairing with the bridge (pressing the button on it), as described in
https://www.developers.meethue.com/documentation/getting-started
Nice trick. I've oncorporated it into my code. But I wonder how meethue.com knows the IP address of my lamp. Does the lamp "phone home" to Philips?
I'll post my code, which does lots of fancy things with the Hue lamp below as an answer sometime today. Will probably upload it to my File Exchange this weekend.

Accedi per commentare.

 Risposta accettata

Stephen Doe
Stephen Doe il 19 Gen 2016
The webwrite function supports GET and POST methods only.
You might try using urlread2 to communicate with the device. It's a user submission that might help you.

6 Commenti

Stephen, thanks a bunch -- that did the trick. I don't know why, but it did. Right now I can set the state of the light based on a bunch of things, like volume of music in an audio file. I'll fancy it up some and post it here in a few days.
It was working great until today, when the router decided to renew the DHCP leases. It reordered/renumbered all the IP Addresses of all the devices on my home network. The Philips Hue used to have an IP Address of 192.168.1.120, but now today, it has an IP Address of 192.168.1.100.
When I changed it to 192.168.1.100, it worked fine again. This number is hard coded into the code.
From the router setup, which you can get to by entering the IP Address of 192.168.1.1 into the Firefox Browser address field, I can check the router DHCP Reservations to discover the IP address of the Hue. If you have a LinkSys router, then go to Setup->Basic Setup->DHCP Reservation. You get a list of all device names that are attached to the network along with their IP Address. That's how I discovered that it had moved my devices, including the Hue, to new IP addresses.
My question is this: Is it possible, from MATLAB, to query the router, or the operating system, to get a list of device names at each IP Address (like I can from the router)?
I have not been able to find anyplace that says how to get the DHCP reservations from MATLAB.
If I can, then I can initialize the program so that it uses the proper IP Address right from the start and it will immediately work. Otherwise I need to hard code in the IP address after going into the router setup to learn what it is.
Gosh, the router interface software on my Cisco Linksys has been completely updated since I last looked at it, replaced with Linksys software written by Belkin. The former software had its MAC table accessible via a URL, but this newer software is Javascript up the euphemism, significantly more difficult to figure out what would be needed.
Thanks Walter. The workaround that I've thought of, though not tried yet, is to just start sending out "Philips Hue friendly" signals to each IP address in sequence, looking at responses (if any) until I get the response I'm expecting from the hue, and not garbage, no response, or an error response. When I get the expected response, it must be my Hue, and then I could quit knowing that I've found it.
I'm a little unsure though how the other devices would react if I sent them a signal that they aren't expecting and/or is the wrong "language" or characters in that signal. I wouldn't want my mobile phone, DVR, or iPad to completely freak out. Would they be fine and just harmlessly ignore or handle it?
I would think finding out what devices lived at each IP Address so that you can find the one you're interested in must be a common thing to do with wireless apps. Like if you made an app that views an IP Camera, how can you look at it if it's changing its IP address all the time? Can I tell my router not to renew leases and hand out new IP Addresses (I think, probably) periodically? Is there any downside to doing that? How do wireless apps handle it if people didn't do that?
On the previous firmware on my Cisco/Linksys it would have been no problem to add a fixed IP address for a MAC address. With the version I seem to have now, I don't see a way to do it.
You can use a tool such as nmap to find out what devices are present on your local network.
If you were using a Unix type system then you would be able to use netstat to read the table of known MAC addresses off of your PC. I think there might be an equivalent for MS Windows, but do not have time to check.
Looking again on my Linksys router, if I connect to the router's web page from the lan, log in, select "Connectivity", and click on the "Local Network" tab, then there is a DHCP Server section and a DHCP Reservations button there. clicking on that gets me to another page that lists the current reservations and has a "Manually add device reservation" button that can be used to associate an internal IP address with a particular MAC address.

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 1 Lug 2017
Modificato: Image Analyst il 2 Lug 2017
Mark, I have a Philips Hue and have made a nice program that will let you send any color to your lamp. Plus it will let you browse to a folder with music files and it will let you change the color of the lamp in time with the music as it plays. I've also measured the spectrum of the lamp with my Ocean Optics spectrometer, and the spectrum of the emitted light is displayed as the light color changes. So the spectrum is accurate.
The code is attached, and in my File Exchange. The comments are below:
% Program to manipulate the colors by the Philip Hue wireless LED lighting system.
% Program plays music files and the brightness of the lamp is controlled by the volume of the song.
% Communicate with a Philips Hue bridge to talk to the Hue personal wireless LED lights.
% Ref: http://www.developers.meethue.com/documentation/getting-started
% Make sure you do everything on that page to find out the IP Address of the bridge (something like '192.168.1.120'),
% and to set yourself up as an authorized user (with a string something like 1028d66426293e821ecfd9ef1a0731df that identifies you).
% Uses function urlread2() from the File Exchange. http://www.mathworks.com/matlabcentral/fileexchange/35693-urlread2
% Although the program will work with any music file, these are
% recommended music files to effectively demonstrate the capability of the program and lamp.
% Note: classical music pieces seem best because they vary the volume slowly and have a great dynamic range in volume.
% Fanfare for the Common Man, by Aaron Copland
% Symphony #5 by Beethoven
% Symphony #9 "New World" by Antonin Dvorak (one of the best symphonies ever written)
% O'Fortuna from Carmina Burana by Carl Orff (powerful, spine tingling music I'm sure you've heard before)
% Toccata in D minor by Bach (haunting, creepy funeral dirge organ music)
% Also Sprach Zarathustra by Richard Strauss (2001 a Space Oddysey movie theme)
% The Four Seasons by Vivaldi
% Anvil Chorus by Giuseppe Verdi
% Palladio Allegretto by Karl Jenkins (deBeers "A Diamond is Forever" theme music)
% Note: Some music that you might think would be good, such as Ode To Joy by Beethoven, which have a good beat,
% unfortunately change too rapidly for the lamp to follow. The lamp can only change colors about 10 times a second, but it seems
% even slower than that.
% Some rock songs that play well because of large but slow volume oscillations are
% Good Times Bad Times by Led Zeppelin
% Dazed and Confused by Led Zeppelin
% Stairway to Heaven by Led Zeppelin (actually almost any Led Zeppelin song!)
% We will Rock You by Queen
% Good Riddance (Time of Your Life) by Green Day
% Uprising by Muse
% Viva la Vida by Coldplay

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by