2018-01-27 23:28:20 +01:00
|
|
|
/*
|
|
|
|
* Sync to Philips hue lights
|
|
|
|
*/
|
2018-11-01 15:36:13 +01:00
|
|
|
#ifndef WLED_DISABLE_HUESYNC
|
2018-02-28 00:27:10 +01:00
|
|
|
void handleHue()
|
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
if (hueClient != nullptr && millis() - hueLastRequestSent > huePollIntervalMs && WiFi.status() == WL_CONNECTED)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
hueLastRequestSent = millis();
|
|
|
|
if (huePollingEnabled)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
reconnectHue();
|
|
|
|
} else {
|
|
|
|
hueClient->close();
|
|
|
|
if (hueError[0] == 'A') strcpy(hueError,"Inactive");
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
if (hueReceived)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
colorUpdated(7); hueReceived = false;
|
|
|
|
if (hueStoreAllowed && hueNewKey)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
saveSettingsToEEPROM(); //save api key
|
|
|
|
hueStoreAllowed = false;
|
|
|
|
hueNewKey = false;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 22:34:21 +01:00
|
|
|
void reconnectHue()
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-02-18 22:34:21 +01:00
|
|
|
if (WiFi.status() != WL_CONNECTED || !huePollingEnabled) return;
|
|
|
|
DEBUG_PRINTLN("Hue reconnect");
|
|
|
|
if (hueClient == nullptr) {
|
|
|
|
hueClient = new AsyncClient();
|
|
|
|
hueClient->onConnect(&onHueConnect, hueClient);
|
|
|
|
hueClient->onData(&onHueData, hueClient);
|
|
|
|
hueClient->onError(&onHueError, hueClient);
|
|
|
|
hueAuthRequired = (strlen(hueApiKey)<20);
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
hueClient->connect(hueIP, 80);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onHueError(void* arg, AsyncClient* client, int8_t error)
|
|
|
|
{
|
|
|
|
DEBUG_PRINTLN("Hue err");
|
|
|
|
strcpy(hueError,"Request timeout");
|
|
|
|
}
|
|
|
|
|
|
|
|
void onHueConnect(void* arg, AsyncClient* client)
|
|
|
|
{
|
|
|
|
DEBUG_PRINTLN("Hue connect");
|
|
|
|
sendHuePoll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendHuePoll()
|
|
|
|
{
|
|
|
|
if (hueClient == nullptr || !hueClient->connected()) return;
|
|
|
|
String req = "";
|
|
|
|
if (hueAuthRequired)
|
|
|
|
{
|
|
|
|
req += "POST /api HTTP/1.1\r\nHost: ";
|
|
|
|
req += hueIP.toString();
|
|
|
|
req += "\r\nContent-Length: 25\r\n\r\n{\"devicetype\":\"wled#esp\"}";
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
req += "GET /api/";
|
|
|
|
req += hueApiKey;
|
|
|
|
req += "/lights/" + String(huePollLightId);
|
|
|
|
req += " HTTP/1.1\r\nHost: ";
|
|
|
|
req += hueIP.toString();
|
|
|
|
req += "\r\n\r\n";
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
hueClient->add(req.c_str(), req.length());
|
|
|
|
hueClient->send();
|
2018-02-28 00:27:10 +01:00
|
|
|
hueLastRequestSent = millis();
|
|
|
|
}
|
|
|
|
|
2019-03-03 23:27:52 +01:00
|
|
|
void onHueData(void* arg, AsyncClient* client, void *data, size_t len)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
if (!len) return;
|
|
|
|
char* str = (char*)data;
|
2018-02-28 00:27:10 +01:00
|
|
|
DEBUG_PRINTLN(hueApiKey);
|
2019-03-03 23:27:52 +01:00
|
|
|
DEBUG_PRINTLN(str);
|
|
|
|
//only get response body
|
|
|
|
str = strstr(str,"\r\n\r\n");
|
|
|
|
if (str == nullptr) return;
|
|
|
|
str += 4;
|
|
|
|
|
|
|
|
StaticJsonBuffer<512> jb;
|
|
|
|
if (str[0] == '[') //is JSON array
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
JsonArray& root = jb.parseArray(str);
|
|
|
|
if (!root.success())
|
|
|
|
{
|
|
|
|
strcpy(hueError,"JSON parsing error"); return;
|
|
|
|
}
|
|
|
|
int hueErrorCode = root[0]["error"]["type"];
|
|
|
|
|
|
|
|
if (hueErrorCode)//hue bridge returned error
|
|
|
|
{
|
|
|
|
switch (hueErrorCode)
|
|
|
|
{
|
|
|
|
case 1: strcpy(hueError,"Unauthorized"); hueAuthRequired = true; break;
|
|
|
|
case 3: strcpy(hueError,"Invalid light ID"); huePollingEnabled = false; break;
|
|
|
|
case 101: strcpy(hueError,"Link button not pressed"); hueAuthRequired = true; break;
|
|
|
|
default:
|
|
|
|
char coerr[18];
|
|
|
|
sprintf(coerr,"Bridge Error %i",hueErrorCode);
|
|
|
|
strcpy(hueError,coerr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hueAuthRequired)
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
const char* apikey = root[0]["success"]["username"];
|
|
|
|
if (apikey != nullptr)
|
|
|
|
{
|
|
|
|
strlcpy(hueApiKey, apikey, sizeof(hueApiKey));
|
|
|
|
hueAuthRequired = false;
|
|
|
|
hueNewKey = true;
|
|
|
|
}
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
return;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
|
2019-03-03 23:27:52 +01:00
|
|
|
//else, assume it is JSON object, look for state and only parse that
|
|
|
|
str = strstr(str,"state");
|
|
|
|
if (str == nullptr) return;
|
|
|
|
str = strstr(str,"{");
|
|
|
|
|
|
|
|
JsonObject& root = jb.parseObject(str);
|
|
|
|
if (!root.success())
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
strcpy(hueError,"JSON parsing error"); return;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
float hueX=0, hueY=0;
|
|
|
|
uint16_t hueHue=0, hueCt=0;
|
2018-03-14 13:16:28 +01:00
|
|
|
byte hueBri=0, hueSat=0, hueColormode=0;
|
2019-03-03 23:27:52 +01:00
|
|
|
|
|
|
|
if (root["on"]) {
|
|
|
|
if (root.containsKey("bri")) //Dimmable device
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
hueBri = root["bri"];
|
|
|
|
hueBri++;
|
|
|
|
const char* cm =root["colormode"];
|
|
|
|
if (cm != nullptr) //Color device
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
if (strstr(cm,"ct") != nullptr) //ct mode
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
hueCt = root["ct"];
|
|
|
|
hueColormode = 3;
|
|
|
|
} else if (strstr(cm,"xy") != nullptr) //xy mode
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
hueX = root["xy"][0]; // 0.5051
|
|
|
|
hueY = root["xy"][1]; // 0.4151
|
|
|
|
hueColormode = 1;
|
|
|
|
} else //hs mode
|
2018-02-28 00:27:10 +01:00
|
|
|
{
|
2019-03-03 23:27:52 +01:00
|
|
|
hueHue = root["hue"];
|
|
|
|
hueSat = root["sat"];
|
|
|
|
hueColormode = 2;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else //On/Off device
|
|
|
|
{
|
2018-03-14 13:16:28 +01:00
|
|
|
hueBri = briLast;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
hueBri = 0;
|
|
|
|
}
|
2019-03-03 23:27:52 +01:00
|
|
|
|
2019-02-18 22:34:21 +01:00
|
|
|
strcpy(hueError,"Active");
|
2019-03-03 23:27:52 +01:00
|
|
|
|
|
|
|
//apply vals
|
2018-02-28 00:27:10 +01:00
|
|
|
if (hueBri != hueBriLast)
|
|
|
|
{
|
|
|
|
if (hueApplyOnOff)
|
|
|
|
{
|
|
|
|
if (hueBri==0) {bri = 0;}
|
2018-03-14 13:16:28 +01:00
|
|
|
else if (bri==0 && hueBri>0) bri = briLast;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
if (hueApplyBri)
|
|
|
|
{
|
|
|
|
if (hueBri>0) bri = hueBri;
|
|
|
|
}
|
|
|
|
hueBriLast = hueBri;
|
|
|
|
}
|
|
|
|
if (hueApplyColor)
|
|
|
|
{
|
|
|
|
switch(hueColormode)
|
|
|
|
{
|
|
|
|
case 1: if (hueX != hueXLast || hueY != hueYLast) colorXYtoRGB(hueX,hueY,col); hueXLast = hueX; hueYLast = hueY; break;
|
|
|
|
case 2: if (hueHue != hueHueLast || hueSat != hueSatLast) colorHStoRGB(hueHue,hueSat,col); hueHueLast = hueHue; hueSatLast = hueSat; break;
|
|
|
|
case 3: if (hueCt != hueCtLast) colorCTtoRGB(hueCt,col); hueCtLast = hueCt; break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 22:34:21 +01:00
|
|
|
hueReceived = true;
|
2018-02-28 00:27:10 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:36:13 +01:00
|
|
|
#else
|
|
|
|
void handleHue(){}
|
2019-02-18 22:34:21 +01:00
|
|
|
bool reconnectHue(){}
|
2018-11-01 15:36:13 +01:00
|
|
|
#endif
|