Hi Zeal!
This follows instructions from Tom's Hue Control github
I had success with this lab and was able to connect a light to my thesis project! Tom's idea to add a light was actually really smart, because I wanted to have a visual feedback whenever someone voted. Originally I had the Arduinos sending data to TouchDesigner but the visuals were difficult to perceive. I think the light will be much more successful, I can even change the color for each answer!

0. Do you have a light? I borrowed one from the ER and also borrowed a clip lamp
1. First step is to get a user key from the hub you want to use. There are 4 Phillips Hubs on the floor, located on columns 3, 6, 9, and 12. They're 5" square white hubs (I thought they were bigger haha) You can find the IP addresses here
Tom made this really nifty dashboard to grab a user key from Phillips. However, you have to download the source code and run it locally to actually get the user key. So once you download it, open the html file and run the instructions to get a new user key. Start by filling in the IP address for the hub you're using.
Then follow these instructions:

2. Now you should have a user key! The next step is to run to the Phillips CLIP API which you can find by adding the hub's IP address to this URL:
https://[your hub IP address here]/debug/clip.html
3. Add your user key to this line, and press 'GET' to get a list of all the devices on the hub. Cool!
4. Now you need to figure out which light is yours... If your light is powered, it should be "reachable" on the hub. You can do a command+F and look up the string "reachable": true to see which lights are available. You'll find some lights that have integer identities, like '23' for example. Test each light identity by sending a command using the PUT command to turn the light on or off, for example. The URL would look something like this:
/api/[your user key]/lights/23/state
Then in the message body, write the value in JSON format, such as:
{"on": true} to turn the light on, or
{"on": false} to turn the light off
Did it work?
If no, try another reachable light on the hub
If still no, it might be connected to another hub. Try resetting the light with a Phillips remote by holding the side buttons for 10 seconds next to the light.
Hopefully by now it's successful. If not, try another light or another hub.
5. To connect the Arduino to the light, I used this Arduino example sketch from Tom's github. It uses the Arduino HTTPClient library to send messages to the lights through the Philips hub.
I tested the sketch which starts with a simple blinking light sketch before integrating it into my project. It was a success!
Then I combined the light with my thesis project that has a sound sensor and Arduino nano already set up. I modified the code to work with my sensor so that whenever someone votes in my project, a light turns on, and then immediately turns off. I made the delay really short - 10 ms - because I knew there would be a data transmission delay. I actually timed this against the blink sketch and even though there was a 4 second delay in software, in real life it was 8 seconds with the transmission delay.
#include <SPI.h>
//#include <WiFi101.h> // for MKR1000
#include <WiFiNINA.h> // for Nano 33 IoT, MKR1010
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"
int status = WL_IDLE_STATUS; // the Wifi radio's status
char hueHubIP[] = "172.22.151.226"; // IP address of the HUE bridge
String hueUserName = "wk3l8Rpb6Xkw7ooBVBev9bBzIXbWvTND5Vb7P6vB"; // hue bridge username
// make a wifi instance and a HttpClient instance:
WiFiClient wifi;
HttpClient httpClient = HttpClient(wifi, hueHubIP);
// change the values of these two in the arduino_serets.h file:
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
//double sounddiff
int sampleWindow = 50;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect.
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network IP = ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
}
void loop() {
double sound = soundDiff();
//this is the actual reading you want
double soundCalc = (sound * 3.3)/1024; // debug: Serial.println(soundCalc);
if(soundCalc > 0.99){
sendRequest(25, "on", "true");
delay(10);
sendRequest(25,"on","false");
}
}
void sendRequest(int light, String cmd, String value) {
// make a String for the HTTP request path:
String request = "/api/" + hueUserName;
request += "/lights/";
request += light;
request += "/state/";
String contentType = "application/json";
// make a string for the JSON command:
String hueCmd = "{\"" + cmd;
hueCmd += "\":";
hueCmd += value;
hueCmd += "}";
// see what you assembled to send:
Serial.print("PUT request to server: ");
Serial.println(request);
Serial.print("JSON command to server: ");
// make the PUT request to the hub:
httpClient.put(request, contentType, hueCmd);
// read the status code and body of the response
int statusCode = httpClient.responseStatusCode();
String response = httpClient.responseBody();
Serial.println(hueCmd);
Serial.print("Status code from server: ");
Serial.println(statusCode);
Serial.print("Server response: ");
Serial.println(response);
Serial.println();
}
double soundDiff(){
//record start time
double startMillis = millis();
//high peak, top range
int signalMax = 0;
//low peack, bottom range
int signalMin = 1024;
//sample period
int sample;
//collect data for sample length
while( (millis() - startMillis) < sampleWindow){
sample = analogRead(A0);
if (sample < 1024){
//if sample is larger than 0
if (sample > signalMax){
//this is the new sample max
signalMax = sample;
}
else if (sample < signalMin){
signalMin = sample;
}
}
}
int peakDiff = signalMax - signalMin;
return peakDiff;
}