top of page

Local Network Philips Hue with Arduino JSON Control

  1. Plug router, Philips Bridge, lights into power.

  2. Connect ethernet cable to bridge and laptop

  3. Discover router IP on Network settings

  4. Discover bridge IP on Terminal: ping -c 5 XXX.XXX.255, then arp -a. Look for the bridge's MAC address which you can find on the bottom on a sticker

  5. Make sure you have a working hue key, or set one up

  6. Use the Philips debug site to set up lights

  7. To reset a light; unplug and plug 4-6x for 3 seconds each state

  8. On Arduino:


#include <SPI.h>
#include <WiFiNINA.h>  // for Nano 33 IoT, MKR1010
#include <ArduinoHttpClient.h>
#include <Arduino_JSON.h>
#include "arduino_secrets.h"

int status = WL_IDLE_STATUS;  // the Wifi radio's status
char hueHubIP[] = HUBIP;      // IP address of the HUE bridge
String hueUserName = HUBKEY;  // hue bridge username

int lightOne = 7;
int lightTwo = 8;
JSONVar lightState;  //this is the compiled JSON message you're sending to the bridge

// make a wifi instance and a HttpClient instance:
WiFiClient wifi;
HttpClient httpClient = HttpClient(wifi, hueHubIP);

//double sounddiff
int sampleWindow = 50;

// boolean as to whether you should be requesting:
bool okToSend = true;      // whether periodic requests are being made
long lastRequestTime = 0;  // timestamp of last request
int interval = 60 * 1000;  // 60 seconds in milliseconds

void setup() {
  //Initialize serial and wait for port to open:
  pinMode(LED_BUILTIN, OUTPUT);
  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(SECRET_SSID);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(SECRET_SSID, SECRET_PASS);
  }

  connectToNetwork();

  // 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;

  // if (soundCalc > 1.6) {
  lightState["on"] = true;
  lightState["bri"] = 80;
  lightState["hue"] = 42000;  //this is blue
  lightState["hue"] = 0; //this is red
  lightState["transitiontime"] = 1; //.1 second i believe
  sendRequest(lightTwo, lightState);
  lightState["on"] = false;
  sendRequest(lightTwo, lightState);
  // }
}

void sendRequest(int light, JSONVar myState) {
  // make a String for the HTTP request path:
  String request = "/api/" + String(HUBKEY);
  request += "/lights/";
  request += light;
  request += "/state/";

  String contentType = "application/json";

  // make a string for the JSON command:
  String body = JSON.stringify(lightState);

  // see what you assembled to send:
  Serial.print("PUT request to server: ");
  Serial.println(request);
  Serial.print("JSON command to server: ");
  Serial.println(body);

  // make the PUT request to the hub:
  httpClient.put(request, contentType, body);

  // read the status code and body of the response
  int statusCode = httpClient.responseStatusCode();
  String response = httpClient.responseBody();

  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;
}

void connectToNetwork() {
  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED) {
    if (Serial) Serial.print("Attempting to connect to WPA SSID: ");
    if (Serial) Serial.println(SECRET_SSID);
    // Connect to WPA/WPA2 network:
    // WiFi.begin(SECRET_SSID);
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);
  }

  // you're connected now, so print out the data:
  if (Serial) Serial.print("You're connected to the network. IP: ");
  IPAddress ip = WiFi.localIP();
  if (Serial) Serial.println(ip);
  digitalWrite(LED_BUILTIN, HIGH);
}

3 views0 comments

Recent Posts

See All

Hmmmmmmm...... (Hrngh!!!!!!!!!!) Let's talk this through. I'm referencing Tom's Making Things Talk AQI Web Client sketch for the Arduino Nano IoT33 to connect to a weather API through an HTTP GET requ

bottom of page