Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
157 lines (134 sloc) 4.41 KB
//Code adapted from Tom Igoe's Hue Controller Code found here:
//https://github.com/tigoe/hue-control/tree/master/ArduinoExamples
#include <Arduino_JSON.h>
#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;
JSONVar lightState;
int on = true;
int difference = 15;
int brightness = 0;
int hue = 0;
//on off switch
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.
// attempt to connect to Wifi network:
while ( WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(SECRET_SSID);
// Connect to WPA/WPA2 network:
WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(2000);
}
// 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() {
lightState["on"] = true;
if (reading == HIGH){
lightState["on"] = false; // turn light on
delay(4000);
if (reading ==LOW){
lightState["on"] = true; // turn light off
delay(4000); // wait 4 seconds
}
sendRequest(1, lightState);
}
// fade light up and down
sensorValue = analogRead(sensorPin);
mappedValue = map(sensorValue,0,1023,0,60000);
Serial.println(mappedValue);
lightState["hue"] = mappedValue;
if (hue >= 255 || hue <= 0) {
difference = -difference;
}
hue += difference;
// keep it in range regardless of the difference value:
hue = constrain(hue, 0, 255);
// set the "hue" property:
lightState["hue"] = hue;
//if ("on) {
// lightState["on"] = true;
// } else {
// lightState["on"] = false;
// }
//sendRequest(1, lightState); // turn light on
//
delay(100); // wait 1 second
}
void sendRequest_hue(int light, JSONVar myState) {
// make a String for the HTTP request path:
String request = "/api/" + String(hueUserName);
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();
}
//void sendRequest_on(int light, JSONVar myState) {
// // make a String for the HTTP request path:
// String request = "/api/" + String(hueUserName);
// 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();
//}
You can’t perform that action at this time.