Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (79 sloc) 2.86 KB
//Code adapted from Tom Igoe's Hue Controller Code found here:
//https://github.com/tigoe/hue-control/tree/master/ArduinoExamples
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"
int status = WL_IDLE_STATUS; // the Wifi radio's status
char hueHubIP[] = "172.22.151.181"; // IP address of the HUE bridge
String hueUserName = "Bas855t7CuPNdy2Ja5gHtW3P0mZdoZjij7RK9D2O"; // 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;
int switchPin = 2;
int state = HIGH;
int reading;
int previous = LOW;
//Pot1
int sensorPin = A7; // select the input pin for the potentiometer // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int mappedValue = 0;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect.
pinMode(switchPin, INPUT);
// 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() {
reading = digitalRead(switchPin);
sensorValue = analogRead(sensorPin);
mappedValue = map(sensorValue,0,1023,0,60000);
if (reading == HIGH && previous == LOW && millis()){
sendRequest(1, "on", "true"); // turn light on
delay(4000); // wait 4 seconds
sendRequest(1, "on", "false"); // turn light off
delay(4000); // wait 4 seconds
}
}
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();
}
You can’t perform that action at this time.