Light information:

The class API Key:

1IVlFMzSgaLyWNiF8RAiI4bVyHwJgMBReCz0DkQ5

The API: https://developers.meethue.com/

The hub:  http://172.22.151.183/debug/clip.html

Function:

Arduino Nano 33 IoT Motherboard with Built-in WiFi VL53L0X ToF Sensor Measures distance from user to device Potentiometer Analog input for brightness adjustment(A3) Pushbutton Controls switch status(Pin 2)

Set up VL530X

VL53L0X pin Connect to Arduino
VIN / VCC 3.3V
GND GND
SDA A4/I2C SDA
SCL A5/ I2C SCL
XSHUT Optional
GPIO1 Optional

Control Light Group

/* Hue Group state control example for ArduinoHttpClient library

  Uses ArduinoHttpClient library to control Philips Hue
  Uses Hue API v.1
  For more on Hue developer API see <http://developer.meethue.com>

  To control a group, the Hue expects a HTTP PUT request to:
  <http://hue.hub.address/api/hueUserName/groups/groupNumber/action>

  The body of the PUT request looks like this:
   {
		"on": true,  // true of false
		"bri": 254,  // 0-254
		"ct": 346,   // 153 -500 in mireds which is 1000000/degrees Kelvin
		"alert": "select",
		"colormode": "ct"
	}
  This example  shows how to use a JSONVAr object from the 
  Arduino_JSON library to assemble the
  PUT request and the body of the request.

  Uses the following libraries: 
  WiFiNINA, WiFi101, WiFi, WiFi@3 or whatever works with your Arduino model
  ArduinoHttpClient, Arduino_JSON, EncoderStepCounter.h

  External hardware: A rotary encoder is connected to pins 5 and 6.
  Its pushbutton is connected to pin 2, and its other side is connected
  to ground, hence the use of INPUT_PULLUP below. 

  note: WiFi SSID and password and Hue app key are stored 
  in arduino_secrets.h file.
  If it is not present, add a new tab, call it "arduino_secrets.h" 
  and add the following defines, and change to your own values:

  #define SECRET_SSID "ssid"    
  #define SECRET_PASS "password"
  #define SECRET_HUBAPPKEY "hue app key"

   Created 23 Feb 2025
   by Tom Igoe 
*/

#include <Wire.h>
#include <Adafruit_VL53L0X.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include <Arduino_JSON.h>
#include "arduino_secrets.h"

char hueHubIP[] = "172.22.151.183";  // IP address of the HUE bridge

WiFiClient wifi;
HttpClient httpClient = HttpClient(wifi, hueHubIP);

JSONVar groupState;
int groupNum = 81;

Adafruit_VL53L0X sensor = Adafruit_VL53L0X();

#define BUTTON_PIN 2
#define POTENTIOMETER_PIN A3

bool lightOn = true;  // 记录灯光状态
int lastButtonState = HIGH;
const int debounceDelay = 10;
long lastSendTime = 0;
const int sendInterval = 1000;

void setup() {
  Serial.begin(9600);
  while (!Serial) delay(100);

  // VL53L0X
  Wire.begin();//启动 I2C 通信(使用默认 I2C 引脚A4A5)
  if (!sensor.begin()) {
    Serial.println("Failed to initialize VL53L0X sensor!");
    while (1);
  }

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(POTENTIOMETER_PIN, INPUT);

  // 连接 WiFi
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to SSID: ");
    Serial.println(SECRET_SSID);
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);
  }
  Serial.println("Connected! IP: " + String(WiFi.localIP()));

  // 初始化灯光状态
  groupState["on"] = true;
  groupState["bri"] = 127;
  groupState["hue"] = 0;
}

void loop() {
  // **读取按钮状态**
  int buttonState = digitalRead(BUTTON_PIN);
  if (buttonState != lastButtonState) {
    delay(debounceDelay);
    if (buttonState == LOW) {
      lightOn = !lightOn;
      groupState["on"] = lightOn;
      Serial.println(lightOn ? "Turn ON" : "Turn OFF");
    }
    lastButtonState = buttonState;
  }

  // **读取 VL53L0X 距离并映射到 Hue 颜色**
  int distance = sensor.readRange();
  if (!sensor.timeoutOccurred()) {
    int hueValue = map(distance, 0, 250, 0, 65535);
    hueValue = constrain(hueValue, 0, 65535);
    groupState["hue"] = hueValue;
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" mm, Hue: ");
    Serial.println(hueValue);
  } else {
    Serial.println("VL53L0X Timeout!");
  }

  // **读取电位器并映射到亮度**
  int potValue = analogRead(POTENTIOMETER_PIN);
  int brightness = map(potValue, 0, 1023, 0, 254);
  groupState["bri"] = brightness;
  Serial.print("Potentiometer: ");
  Serial.print(potValue);
  Serial.print(" -> Brightness: ");
  Serial.println(brightness);

  // send data
  if (millis() - lastSendTime > sendInterval) {
    Serial.println("Updating Hue light...");
    sendRequest(groupNum, groupState);
    lastSendTime = millis();
  }

  delay(100);
}

void sendRequest(int group, JSONVar myState) {
  String request = "/api/" + String(SECRET_HUBAPPKEY) + "/groups/" + group + "/action/";
  String contentType = "application/json";
  String body = JSON.stringify(groupState);

  Serial.print("PUT request: ");
  Serial.println(request);
  Serial.print("JSON Data: ");
  Serial.println(body);

  httpClient.put(request, contentType, body);
  int statusCode = httpClient.responseStatusCode();
  String response = httpClient.responseBody();

  Serial.print("Response Code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
}

录屏2025-04-07 20.57.21.mov

截屏2025-04-07 21.00.48.png

Control light strip (single)

#include <Wire.h>
#include <Adafruit_VL53L0X.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include <Arduino_JSON.h>
#include "arduino_secrets.h"

// ip
char hueHubIP[] = "172.22.151.183";
WiFiClient wifi;
HttpClient httpClient = HttpClient(wifi, hueHubIP);

// light number ID
const int lightId = 14;

//  JSON
JSONVar lightState;

// VL53L0X
Adafruit_VL53L0X sensor = Adafruit_VL53L0X();
#define BUTTON_PIN 2
#define POTENTIOMETER_PIN A3

bool lightOn = true;
int lastButtonState = HIGH;
const int debounceDelay = 10;
long lastSendTime = 0;
const int sendInterval = 1000;

void setup() {
  Serial.begin(9600);
  while (!Serial) delay(100);

  // 初始化 I2C 和传感器
  Wire.begin();
  if (!sensor.begin()) {
    Serial.println("Failed to initialize VL53L0X sensor!");
    while (1);
  }

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(POTENTIOMETER_PIN, INPUT);

  // 连接 WiFi
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to SSID: ");
    Serial.println(SECRET_SSID);
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);
  }
  Serial.println("Connected! IP: " + String(WiFi.localIP()));

  // 初始化灯光状态
  lightState["on"] = true;
  lightState["bri"] = 127;
  lightState["hue"] = 0;
  lightState["sat"] = 254;
  lightState["colormode"] = "hs";  // 强制使用色相模式
}

void loop() {
  // 读取按钮状态
  int buttonState = digitalRead(BUTTON_PIN);
  if (buttonState != lastButtonState) {
    delay(debounceDelay);
    if (buttonState == LOW) {
      lightOn = !lightOn;
      lightState["on"] = lightOn;
      Serial.println(lightOn ? "灯已打开" : "灯已关闭");
    }
    lastButtonState = buttonState;
  }

  // 距离 → 颜色 hue
  int distance = sensor.readRange();
  if (!sensor.timeoutOccurred()) {
    int hueValue = map(distance, 0, 250, 0, 65535);
    hueValue = constrain(hueValue, 0, 65535);
    lightState["hue"] = hueValue;
    Serial.print("距离: ");
    Serial.print(distance);
    Serial.print(" mm → 色调: ");
    Serial.println(hueValue);
  } else {
    Serial.println("VL53L0X 超时!");
  }

  // 电位器 → 亮度 bri
  int potValue = analogRead(POTENTIOMETER_PIN);
  int brightness = map(potValue, 0, 1023, 0, 254);
  lightState["bri"] = brightness;
  Serial.print("电位器: ");
  Serial.print(potValue);
  Serial.print(" → 亮度: ");
  Serial.println(brightness);

  // 定期发送请求
  if (millis() - lastSendTime > sendInterval) {
    sendRequestToLight(lightId, lightState);
    lastSendTime = millis();
  }

  delay(100);
}

void sendRequestToLight(int lightID, JSONVar myState) {
  String path = "/api/" + String(SECRET_HUBAPPKEY) + "/lights/" + String(lightID) + "/state/";
  String contentType = "application/json";
  String body = JSON.stringify(myState);

  Serial.println("发送 Hue 灯控制请求...");
  Serial.println("路径: " + path);
  Serial.println("数据: " + body);

  httpClient.put(path, contentType, body);
  int statusCode = httpClient.responseStatusCode();
  String response = httpClient.responseBody();

  Serial.print("响应码: ");
  Serial.println(statusCode);
  Serial.print("响应内容: ");
  Serial.println(response);
}

IMG_7241.MOV