Merge branch 'development' into release

This commit is contained in:
Theo Arends 2020-03-10 12:33:41 +01:00
commit d5fa09f157
4 changed files with 347 additions and 328 deletions

View File

@ -16,6 +16,7 @@
- Add support for MaxBotix HRXL-MaxSonar ultrasonic range finders by Jon Little (#7814) - Add support for MaxBotix HRXL-MaxSonar ultrasonic range finders by Jon Little (#7814)
- Add support for Romanian language translations by Augustin Marti - Add support for Romanian language translations by Augustin Marti
- Add command ``SetOption89 0/1`` for Zigbee distinct MQTT topics per device for SENSOR, allowing retained messages (#7835) - Add command ``SetOption89 0/1`` for Zigbee distinct MQTT topics per device for SENSOR, allowing retained messages (#7835)
- Change Hue emulation code optimization
### 8.1.0.9 20200220 ### 8.1.0.9 20200220

View File

@ -55,7 +55,9 @@ String HueBridgeId(void)
{ {
String temp = WiFi.macAddress(); String temp = WiFi.macAddress();
temp.replace(":", ""); temp.replace(":", "");
String bridgeid = temp.substring(0, 6) + "FFFE" + temp.substring(6); String bridgeid = temp.substring(0, 6);
bridgeid += "FFFE";
bridgeid += temp.substring(6);
return bridgeid; // 5CCF7FFFFE139F3D return bridgeid; // 5CCF7FFFFE139F3D
} }
@ -83,16 +85,17 @@ void HueRespondToMSearch(void)
char response[320]; char response[320];
snprintf_P(response, sizeof(response), HUE_RESPONSE, WiFi.localIP().toString().c_str(), HueBridgeId().c_str()); snprintf_P(response, sizeof(response), HUE_RESPONSE, WiFi.localIP().toString().c_str(), HueBridgeId().c_str());
int len = strlen(response); int len = strlen(response);
String uuid = HueUuid();
snprintf_P(response + len, sizeof(response) - len, HUE_ST1, HueUuid().c_str()); snprintf_P(response + len, sizeof(response) - len, HUE_ST1, uuid.c_str());
PortUdp.write(response); PortUdp.write(response);
PortUdp.endPacket(); PortUdp.endPacket();
snprintf_P(response + len, sizeof(response) - len, HUE_ST2, HueUuid().c_str(), HueUuid().c_str()); snprintf_P(response + len, sizeof(response) - len, HUE_ST2, uuid.c_str(), uuid.c_str());
PortUdp.write(response); PortUdp.write(response);
PortUdp.endPacket(); PortUdp.endPacket();
snprintf_P(response + len, sizeof(response) - len, HUE_ST3, HueUuid().c_str()); snprintf_P(response + len, sizeof(response) - len, HUE_ST3, uuid.c_str());
PortUdp.write(response); PortUdp.write(response);
PortUdp.endPacket(); PortUdp.endPacket();
@ -134,17 +137,15 @@ const char HUE_DESCRIPTION_XML[] PROGMEM =
"</device>" "</device>"
"</root>\r\n" "</root>\r\n"
"\r\n"; "\r\n";
const char HUE_LIGHTS_STATUS_JSON1[] PROGMEM = const char HUE_LIGHTS_STATUS_JSON1_SUFFIX[] PROGMEM =
"{\"on\":{state}," "%s\"alert\":\"none\","
"{light_status}"
"\"alert\":\"none\","
"\"effect\":\"none\"," "\"effect\":\"none\","
"\"reachable\":true}"; "\"reachable\":true}";
const char HUE_LIGHTS_STATUS_JSON2[] PROGMEM = const char HUE_LIGHTS_STATUS_JSON2[] PROGMEM =
",\"type\":\"Extended color light\"," ",\"type\":\"Extended color light\","
"\"name\":\"{j1\"," "\"name\":\"%s\","
"\"modelid\":\"LCT007\"," "\"modelid\":\"LCT007\","
"\"uniqueid\":\"{j2\"," "\"uniqueid\":\"%s\","
"\"swversion\":\"5.50.1.19085\"}"; "\"swversion\":\"5.50.1.19085\"}";
const char HUE_GROUP0_STATUS_JSON[] PROGMEM = const char HUE_GROUP0_STATUS_JSON[] PROGMEM =
"{\"name\":\"Group 0\"," "{\"name\":\"Group 0\","
@ -173,8 +174,6 @@ const char HueConfigResponse_JSON[] PROGMEM =
"\"linkbutton\":false," "\"linkbutton\":false,"
"\"portalservices\":false" "\"portalservices\":false"
"}"; "}";
const char HUE_LIGHT_RESPONSE_JSON[] PROGMEM =
"{\"success\":{\"/lights/{id/state/{cm\":{re}}";
const char HUE_ERROR_JSON[] PROGMEM = const char HUE_ERROR_JSON[] PROGMEM =
"[{\"error\":{\"type\":901,\"address\":\"/\",\"description\":\"Internal Error\"}}]"; "[{\"error\":{\"type\":901,\"address\":\"/\",\"description\":\"Internal Error\"}}]";
@ -182,7 +181,9 @@ const char HUE_ERROR_JSON[] PROGMEM =
String GetHueDeviceId(uint8_t id) String GetHueDeviceId(uint8_t id)
{ {
String deviceid = WiFi.macAddress() + F(":00:11-") + String(id); String deviceid = WiFi.macAddress();
deviceid += F(":00:11-");
deviceid += String(id);
deviceid.toLowerCase(); deviceid.toLowerCase();
return deviceid; // 5c:cf:7f:13:9f:3d:00:11-1 return deviceid; // 5c:cf:7f:13:9f:3d:00:11-1
} }
@ -318,49 +319,35 @@ void HueLightStatus1(uint8_t device, String *response)
// hue, sat, bri, prev_hue, prev_sat, prev_bri); // hue, sat, bri, prev_hue, prev_sat, prev_bri);
} }
*response += FPSTR(HUE_LIGHTS_STATUS_JSON1); const size_t buf_size = 256;
response->replace("{state}", (power & (1 << (device-1))) ? "true" : "false"); char * buf = (char*) malloc(buf_size); // temp buffer for strings, avoid stack
//String resp;
snprintf_P(buf, buf_size, PSTR("{\"on\":%s,"), (power & (1 << (device-1))) ? "true" : "false");
// Brightness for all devices with PWM // Brightness for all devices with PWM
if ((1 == echo_gen) || (LST_SINGLE <= local_light_subtype)) { // force dimmer for 1st gen Echo if ((1 == echo_gen) || (LST_SINGLE <= local_light_subtype)) { // force dimmer for 1st gen Echo
light_status += "\"bri\":"; snprintf_P(buf, buf_size, PSTR("%s\"bri\":%d,"), buf, bri);
light_status += String(bri);
light_status += ",";
} }
if (LST_COLDWARM <= local_light_subtype) { if (LST_COLDWARM <= local_light_subtype) {
light_status += F("\"colormode\":\""); snprintf_P(buf, buf_size, PSTR("%s\"colormode\":\"%s\","), buf, g_gotct ? "ct" : "hs");
light_status += (g_gotct ? "ct" : "hs");
light_status += "\",";
} }
if (LST_RGB <= local_light_subtype) { // colors if (LST_RGB <= local_light_subtype) { // colors
if (prev_x_str[0] && prev_y_str[0]) { if (prev_x_str[0] && prev_y_str[0]) {
light_status += "\"xy\":["; snprintf_P(buf, buf_size, PSTR("%s\"xy\":[%s,%s],"), buf, prev_x_str, prev_y_str);
light_status += prev_x_str;
light_status += ",";
light_status += prev_y_str;
light_status += "],";
} else { } else {
float x, y; float x, y;
light_state.getXY(&x, &y); light_state.getXY(&x, &y);
light_status += "\"xy\":["; snprintf_P(buf, buf_size, PSTR("%s\"xy\":[%s,%s],"), buf, String(x, 5).c_str(), String(y, 5).c_str());
light_status += String(x, 5);
light_status += ",";
light_status += String(y, 5);
light_status += "],";
} }
light_status += "\"hue\":"; snprintf_P(buf, buf_size, PSTR("%s\"hue\":%d,\"sat\":%d,"), buf, hue, sat);
light_status += String(hue);
light_status += ",";
light_status += "\"sat\":";
light_status += String(sat);
light_status += ",";
} }
if (LST_COLDWARM == local_light_subtype || LST_RGBW <= local_light_subtype) { // white temp if (LST_COLDWARM == local_light_subtype || LST_RGBW <= local_light_subtype) { // white temp
light_status += "\"ct\":"; snprintf_P(buf, buf_size, PSTR("%s\"ct\":%d,"), buf, ct > 0 ? ct : 284);
light_status += String(ct > 0 ? ct : 284); // if no ct, default to medium white
light_status += ",";
} }
response->replace("{light_status}", light_status); snprintf_P(buf, buf_size, HUE_LIGHTS_STATUS_JSON1_SUFFIX, buf);
*response += buf;
free(buf);
} }
// Check whether this device should be reported to Alexa or considered hidden. // Check whether this device should be reported to Alexa or considered hidden.
@ -372,14 +359,16 @@ bool HueActive(uint8_t device) {
void HueLightStatus2(uint8_t device, String *response) void HueLightStatus2(uint8_t device, String *response)
{ {
*response += FPSTR(HUE_LIGHTS_STATUS_JSON2); const size_t buf_size = 192;
if (device <= MAX_FRIENDLYNAMES) { char * buf = (char*) malloc(buf_size);
response->replace("{j1", SettingsText(SET_FRIENDLYNAME1 +device -1)); const size_t max_name_len = 32;
} else { char fname[max_name_len + 1];
char fname[33];
strcpy(fname, SettingsText(SET_FRIENDLYNAME1 + MAX_FRIENDLYNAMES -1)); strlcpy(fname, SettingsText(device <= MAX_FRIENDLYNAMES ? SET_FRIENDLYNAME1 + device -1 : SET_FRIENDLYNAME1 + MAX_FRIENDLYNAMES -1), max_name_len + 1);
if (device > MAX_FRIENDLYNAMES) {
uint32_t fname_len = strlen(fname); uint32_t fname_len = strlen(fname);
if (fname_len > 30) { fname_len = 30; } if (fname_len > max_name_len - 2) { fname_len = max_name_len - 2; }
fname[fname_len++] = '-'; fname[fname_len++] = '-';
if (device - MAX_FRIENDLYNAMES < 10) { if (device - MAX_FRIENDLYNAMES < 10) {
fname[fname_len++] = '0' + device - MAX_FRIENDLYNAMES; fname[fname_len++] = '0' + device - MAX_FRIENDLYNAMES;
@ -387,35 +376,55 @@ void HueLightStatus2(uint8_t device, String *response)
fname[fname_len++] = 'A' + device - MAX_FRIENDLYNAMES - 10; fname[fname_len++] = 'A' + device - MAX_FRIENDLYNAMES - 10;
} }
fname[fname_len] = 0x00; fname[fname_len] = 0x00;
response->replace("{j1", fname);
} }
response->replace("{j2", GetHueDeviceId(device)); snprintf_P(buf, buf_size, HUE_LIGHTS_STATUS_JSON2, fname, GetHueDeviceId(device).c_str());
*response += buf;
free(buf);
} }
// generate a unique lightId mixing local IP address and device number // generate a unique lightId mixing local IP address and device number
// it is limited to 32 devices. // it is limited to 32 devices.
// last 24 bits of Mac address + 4 bits of local light + high bit for relays 16-31, relay 32 is mapped to 0 // last 24 bits of Mac address + 4 bits of local light + high bit for relays 16-31, relay 32 is mapped to 0
// Zigbee extension: bit 29 = 1, and last 16 bits = short address of Zigbee device
// #ifndef USE_ZIGBEE
uint32_t EncodeLightId(uint8_t relay_id) uint32_t EncodeLightId(uint8_t relay_id)
// #else
// uint32_t EncodeLightId(uint8_t relay_id, uint16_t z_shortaddr = 0)
// #endif
{ {
uint8_t mac[6]; uint8_t mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
uint32_t id = 0; uint32_t id = (mac[3] << 20) | (mac[4] << 12) | (mac[5] << 4);
if (relay_id >= 32) { // for Relay #32, we encode as 0 if (relay_id >= 32) { // for Relay #32, we encode as 0
relay_id = 0; relay_id = 0;
} }
if (relay_id > 15) { if (relay_id > 15) {
id = (1 << 28); id |= (1 << 28);
} }
id |= (relay_id & 0xF);
// #ifdef USE_ZIGBEE
// if ((z_shortaddr) && (!relay_id)) {
// // fror Zigbee devices, we have relay_id == 0 and shortaddr != 0
// id = (1 << 29) | z_shortaddr;
// }
// #endif
id |= (mac[3] << 20) | (mac[4] << 12) | (mac[5] << 4) | (relay_id & 0xF);
return id; return id;
} }
// get hue_id and decode the relay_id // get hue_id and decode the relay_id
// 4 LSB decode to 1-15, if bit 28 is set, it encodes 16-31, if 0 then 32 // 4 LSB decode to 1-15, if bit 28 is set, it encodes 16-31, if 0 then 32
uint32_t DecodeLightId(uint32_t hue_id) { // Zigbee:
// If the Id encodes a Zigbee device (meaning bit 29 is set)
// it returns 0 and sets the 'shortaddr' to the device short address
// #ifndef USE_ZIGBEE
uint32_t DecodeLightId(uint32_t hue_id)
// #else
// uint32_t DecodeLightId(uint32_t hue_id, uint16_t * shortaddr = nullptr)
// #endif
{
uint8_t relay_id = hue_id & 0xF; uint8_t relay_id = hue_id & 0xF;
if (hue_id & (1 << 28)) { // check if bit 25 is set, if so we have if (hue_id & (1 << 28)) { // check if bit 25 is set, if so we have
relay_id += 16; relay_id += 16;
@ -423,6 +432,13 @@ uint32_t DecodeLightId(uint32_t hue_id) {
if (0 == relay_id) { // special value 0 is actually relay #32 if (0 == relay_id) { // special value 0 is actually relay #32
relay_id = 32; relay_id = 32;
} }
// #ifdef USE_ZIGBEE
// if (hue_id & (1 << 29)) {
// // this is actually a Zigbee ID
// if (shortaddr) { *shortaddr = hue_id & 0xFFFF; }
// relay_id = 0;
// }
// #endif // USE_ZIGBEE
return relay_id; return relay_id;
} }
@ -453,22 +469,14 @@ uint32_t findEchoGeneration(void) {
void HueGlobalConfig(String *path) { void HueGlobalConfig(String *path) {
String response; String response;
uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present;
path->remove(0,1); // cut leading / to get <id> path->remove(0,1); // cut leading / to get <id>
response = F("{\"lights\":{"); response = F("{\"lights\":{");
bool appending = false; // do we need to add a comma to append bool appending = false; // do we need to add a comma to append
for (uint32_t i = 1; i <= maxhue; i++) { CheckHue(&response, appending);
if (HueActive(i)) { // #ifdef USE_ZIGBEE
if (appending) { response += ","; } // ZigbeeCheckHue(&response, appending);
response += "\""; // #endif // USE_ZIGBEE
response += EncodeLightId(i);
response += F("\":{\"state\":");
HueLightStatus1(i, &response);
HueLightStatus2(i, &response);
appending = true;
}
}
response += F("},\"groups\":{},\"schedules\":{},\"config\":"); response += F("},\"groups\":{},\"schedules\":{},\"config\":");
HueConfigResponse(&response); HueConfigResponse(&response);
response += "}"; response += "}";
@ -481,96 +489,70 @@ void HueAuthentication(String *path)
snprintf_P(response, sizeof(response), PSTR("[{\"success\":{\"username\":\"%s\"}}]"), GetHueUserId().c_str()); snprintf_P(response, sizeof(response), PSTR("[{\"success\":{\"username\":\"%s\"}}]"), GetHueUserId().c_str());
WSSend(200, CT_JSON, response); WSSend(200, CT_JSON, response);
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " Authentication Result (%s)"), response);
} }
void HueLights(String *path) // refactored to remove code duplicates
{ void CheckHue(String * response, bool &appending) {
/* uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present;
* http://tasmota/api/username/lights/1/state?1={"on":true,"hue":56100,"sat":254,"bri":254,"alert":"none","transitiontime":40} for (uint32_t i = 1; i <= maxhue; i++) {
*/ if (HueActive(i)) {
String response; if (appending) { *response += ","; }
int code = 200; *response += "\"";
*response += EncodeLightId(i);
*response += F("\":{\"state\":");
HueLightStatus1(i, response);
HueLightStatus2(i, response);
appending = true;
}
}
}
void HueLightsCommand(uint8_t device, uint32_t device_id, String &response) {
uint16_t tmp = 0; uint16_t tmp = 0;
uint16_t hue = 0; uint16_t hue = 0;
uint8_t sat = 0; uint8_t sat = 0;
uint8_t bri = 254; uint8_t bri = 254;
uint16_t ct = 0; uint16_t ct = 0;
bool resp = false; // is the response non null (add comma between parameters)
bool on = false; bool on = false;
bool resp = false; // is the response non null (add comma between parameters)
bool change = false; // need to change a parameter to the light bool change = false; // need to change a parameter to the light
uint8_t device = 1; uint8_t local_light_subtype = getLocalLightSubtype(device); // get the subtype for this device
uint8_t local_light_subtype = Light.subtype;
uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present;
path->remove(0,path->indexOf("/lights")); // Remove until /lights const size_t buf_size = 100;
if (path->endsWith("/lights")) { // Got /lights char * buf = (char*) malloc(buf_size);
response = "{";
bool appending = false;
for (uint32_t i = 1; i <= maxhue; i++) {
if (HueActive(i)) {
if (appending) { response += ","; }
response += "\"";
response += EncodeLightId(i);
response += F("\":{\"state\":");
HueLightStatus1(i, &response);
HueLightStatus2(i, &response);
appending = true;
}
}
#ifdef USE_SCRIPT_HUE
Script_Check_Hue(&response);
#endif
response += "}";
}
else if (path->endsWith("/state")) { // Got ID/state
path->remove(0,8); // Remove /lights/
path->remove(path->indexOf("/state")); // Remove /state
device = DecodeLightId(atoi(path->c_str()));
#ifdef USE_SCRIPT_HUE
if (device>devices_present) {
return Script_Handle_Hue(path);
}
#endif
if ((device < 1) || (device > maxhue)) {
device = 1;
}
local_light_subtype = getLocalLightSubtype(device); // get the subtype for this device
if (WebServer->args()) { if (WebServer->args()) {
response = "["; response = "[";
StaticJsonBuffer<400> jsonBuffer; StaticJsonBuffer<300> jsonBuffer;
JsonObject &hue_json = jsonBuffer.parseObject(WebServer->arg((WebServer->args())-1)); JsonObject &hue_json = jsonBuffer.parseObject(WebServer->arg((WebServer->args())-1));
if (hue_json.containsKey("on")) { if (hue_json.containsKey("on")) {
on = hue_json["on"];
response += FPSTR(HUE_LIGHT_RESPONSE_JSON); snprintf_P(buf, buf_size,
response.replace("{id", String(EncodeLightId(device))); PSTR("{\"success\":{\"/lights/%d/state/on\":%s}}"),
response.replace("{cm", "on"); device_id, on ? "true" : "false");
#ifdef USE_SHUTTER #ifdef USE_SHUTTER
if (ShutterState(device)) { if (ShutterState(device)) {
if (!change) { if (!change) {
on = hue_json["on"];
bri = on ? 1.0f : 0.0f; // when bri is not part of this request then calculate it bri = on ? 1.0f : 0.0f; // when bri is not part of this request then calculate it
change = true; change = true;
resp = true;
response += buf; // actually publish the state
} }
response.replace("{re", on ? "true" : "false");
} else { } else {
#endif #endif
on = hue_json["on"];
switch(on) switch(on)
{ {
case false : ExecuteCommandPower(device, POWER_OFF, SRC_HUE); case false : ExecuteCommandPower(device, POWER_OFF, SRC_HUE);
response.replace("{re", "false"); //response.replace("{re", "false");
break; break;
case true : ExecuteCommandPower(device, POWER_ON, SRC_HUE); case true : ExecuteCommandPower(device, POWER_ON, SRC_HUE);
response.replace("{re", "true"); //response.replace("{re", "true");
break;
default : response.replace("{re", (power & (1 << (device-1))) ? "true" : "false");
break; break;
} }
response += buf;
resp = true; resp = true;
#ifdef USE_SHUTTER #ifdef USE_SHUTTER
} }
@ -593,31 +575,29 @@ void HueLights(String *path)
prev_x_str[0] = prev_y_str[0] = 0; // reset xy string prev_x_str[0] = prev_y_str[0] = 0; // reset xy string
if (hue_json.containsKey("bri")) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off. if (hue_json.containsKey("bri")) { // Brightness is a scale from 1 (the minimum the light is capable of) to 254 (the maximum). Note: a brightness of 1 is not off.
tmp = hue_json["bri"]; bri = hue_json["bri"];
prev_bri = bri = tmp; // store command value prev_bri = bri; // store command value
if (resp) { response += ","; }
snprintf_P(buf, buf_size,
PSTR("{\"success\":{\"/lights/%d/state/%s\":%d}}"),
device_id, "bri", bri);
response += buf;
if (LST_SINGLE <= Light.subtype) {
// extend bri value if set to max // extend bri value if set to max
if (254 <= bri) { bri = 255; } if (254 <= bri) { bri = 255; }
if (resp) { response += ","; }
response += FPSTR(HUE_LIGHT_RESPONSE_JSON);
response.replace("{id", String(device));
response.replace("{cm", "bri");
response.replace("{re", String(tmp));
if (LST_SINGLE <= Light.subtype) {
change = true; change = true;
} }
resp = true; resp = true;
} }
// handle xy before Hue/Sat // handle xy before Hue/Sat
// If the request contains both XY and HS, we wan't to give priority to HS // If the request contains both XY and HS, we wan't to give priority to HS
if (hue_json.containsKey("xy")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). if (hue_json.containsKey("xy")) {
float x, y; float x = hue_json["xy"][0];
x = hue_json["xy"][0]; float y = hue_json["xy"][1];
y = hue_json["xy"][1];
const String &x_str = hue_json["xy"][0]; const String &x_str = hue_json["xy"][0];
const String &y_str = hue_json["xy"][1]; const String &y_str = hue_json["xy"][1];
x_str.toCharArray(prev_x_str, sizeof(prev_x_str)); x_str.toCharArray(prev_x_str, sizeof(prev_x_str));
y_str.toCharArray(prev_y_str, sizeof(prev_y_str)); y_str.toCharArray(prev_y_str, sizeof(prev_y_str));
//AddLog_P2(LOG_LEVEL_DEBUG_MORE, "XY (%s %s)", String(prev_x,5).c_str(), String(prev_y,5).c_str());
uint8_t rr,gg,bb; uint8_t rr,gg,bb;
LightStateClass::XyToRgb(x, y, &rr, &gg, &bb); LightStateClass::XyToRgb(x, y, &rr, &gg, &bb);
LightStateClass::RgbToHsb(rr, gg, bb, &hue, &sat, nullptr); LightStateClass::RgbToHsb(rr, gg, bb, &hue, &sat, nullptr);
@ -625,41 +605,41 @@ void HueLights(String *path)
prev_sat = (sat > 254 ? 254 : sat); prev_sat = (sat > 254 ? 254 : sat);
//AddLog_P2(LOG_LEVEL_DEBUG_MORE, "XY RGB (%d %d %d) HS (%d %d)", rr,gg,bb,hue,sat); //AddLog_P2(LOG_LEVEL_DEBUG_MORE, "XY RGB (%d %d %d) HS (%d %d)", rr,gg,bb,hue,sat);
if (resp) { response += ","; } if (resp) { response += ","; }
response += FPSTR(HUE_LIGHT_RESPONSE_JSON); snprintf_P(buf, buf_size,
response.replace("{id", String(device)); PSTR("{\"success\":{\"/lights/%d/state/xy\":[%s,%s]}}"),
response.replace("{cm", "xy"); device_id, prev_x_str, prev_y_str);
response.replace("{re", "[" + x_str + "," + y_str + "]"); response += buf;
g_gotct = false; g_gotct = false;
resp = true; resp = true;
change = true; change = true;
} }
if (hue_json.containsKey("hue")) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue. if (hue_json.containsKey("hue")) { // The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue.
tmp = hue_json["hue"]; hue = hue_json["hue"];
prev_hue = tmp; prev_hue = hue;
// change range from 0..65535 to 0..359
hue = changeUIntScale(tmp, 0, 65535, 0, 359);
if (resp) { response += ","; } if (resp) { response += ","; }
response += FPSTR(HUE_LIGHT_RESPONSE_JSON); snprintf_P(buf, buf_size,
response.replace("{id", String(device)); PSTR("{\"success\":{\"/lights/%d/state/%s\":%d}}"),
response.replace("{cm", "hue"); device_id, "hue", hue);
response.replace("{re", String(tmp)); response += buf;
if (LST_RGB <= Light.subtype) { if (LST_RGB <= Light.subtype) {
// change range from 0..65535 to 0..359
hue = changeUIntScale(hue, 0, 65535, 0, 359);
g_gotct = false; g_gotct = false;
change = true; change = true;
} }
resp = true; resp = true;
} }
if (hue_json.containsKey("sat")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white). if (hue_json.containsKey("sat")) { // Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white).
tmp = hue_json["sat"]; sat = hue_json["sat"];
prev_sat = sat = tmp; // store command value prev_sat = sat; // store command value
if (resp) { response += ","; }
snprintf_P(buf, buf_size,
PSTR("{\"success\":{\"/lights/%d/state/%s\":%d}}"),
device_id, "sat", sat);
response += buf;
if (LST_RGB <= Light.subtype) {
// extend sat value if set to max // extend sat value if set to max
if (254 <= sat) { sat = 255; } if (254 <= sat) { sat = 255; }
if (resp) { response += ","; }
response += FPSTR(HUE_LIGHT_RESPONSE_JSON);
response.replace("{id", String(device));
response.replace("{cm", "sat");
response.replace("{re", String(tmp));
if (LST_RGB <= Light.subtype) {
g_gotct = false; g_gotct = false;
change = true; change = true;
} }
@ -669,10 +649,10 @@ void HueLights(String *path)
ct = hue_json["ct"]; ct = hue_json["ct"];
prev_ct = ct; // store commande value prev_ct = ct; // store commande value
if (resp) { response += ","; } if (resp) { response += ","; }
response += FPSTR(HUE_LIGHT_RESPONSE_JSON); snprintf_P(buf, buf_size,
response.replace("{id", String(device)); PSTR("{\"success\":{\"/lights/%d/state/%s\":%d}}"),
response.replace("{cm", "ct"); device_id, "ct", ct);
response.replace("{re", String(ct)); response += buf;
if ((LST_COLDWARM == Light.subtype) || (LST_RGBW <= Light.subtype)) { if ((LST_COLDWARM == Light.subtype) || (LST_RGBW <= Light.subtype)) {
g_gotct = true; g_gotct = true;
change = true; change = true;
@ -714,11 +694,61 @@ void HueLights(String *path)
else { else {
response = FPSTR(HUE_ERROR_JSON); response = FPSTR(HUE_ERROR_JSON);
} }
free(buf);
}
void HueLights(String *path)
{
/*
* http://tasmota/api/username/lights/1/state?1={"on":true,"hue":56100,"sat":254,"bri":254,"alert":"none","transitiontime":40}
*/
String response;
int code = 200;
uint8_t device = 1;
uint32_t device_id; // the raw device_id used by Hue emulation
uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present;
path->remove(0,path->indexOf("/lights")); // Remove until /lights
if (path->endsWith("/lights")) { // Got /lights
response = "{";
bool appending = false;
CheckHue(&response, appending);
// #ifdef USE_ZIGBEE
// ZigbeeCheckHue(&response, appending);
// #endif // USE_ZIGBEE
#ifdef USE_SCRIPT_HUE
Script_Check_Hue(&response);
#endif
response += "}";
}
else if (path->endsWith("/state")) { // Got ID/state
path->remove(0,8); // Remove /lights/
path->remove(path->indexOf("/state")); // Remove /state
device_id = atoi(path->c_str());
device = DecodeLightId(device_id);
// #ifdef USE_ZIGBEE
// uint16_t shortaddr;
// device = DecodeLightId(device_id, &shortaddr);
// if (shortaddr) {
// return ZigbeeHandleHue(shortaddr, device_id, response);
// }
// #endif // USE_ZIGBEE
#ifdef USE_SCRIPT_HUE
if (device > devices_present) {
return Script_Handle_Hue(path);
}
#endif
if ((device >= 1) || (device <= maxhue)) {
HueLightsCommand(device, device_id, response);
}
} }
else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID else if(path->indexOf("/lights/") >= 0) { // Got /lights/ID
AddLog_P2(LOG_LEVEL_DEBUG_MORE, "/lights path=%s", path->c_str()); AddLog_P2(LOG_LEVEL_DEBUG_MORE, "/lights path=%s", path->c_str());
path->remove(0,8); // Remove /lights/ path->remove(0,8); // Remove /lights/
device = DecodeLightId(atoi(path->c_str())); device_id = atoi(path->c_str());
device = DecodeLightId(device_id);
#ifdef USE_SCRIPT_HUE #ifdef USE_SCRIPT_HUE
if (device > devices_present) { if (device > devices_present) {
@ -750,6 +780,7 @@ void HueGroups(String *path)
*/ */
String response = "{}"; String response = "{}";
uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present; uint8_t maxhue = (devices_present > MAX_HUE_DEVICES) ? MAX_HUE_DEVICES : devices_present;
//AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " HueGroups (%s)"), path->c_str());
if (path->endsWith("/0")) { if (path->endsWith("/0")) {
response = FPSTR(HUE_GROUP0_STATUS_JSON); response = FPSTR(HUE_GROUP0_STATUS_JSON);
@ -759,11 +790,16 @@ void HueGroups(String *path)
lights += EncodeLightId(i); lights += EncodeLightId(i);
lights += "\""; lights += "\"";
} }
// #ifdef USE_ZIGBEE
// ZigbeeHueGroups(&response);
// #endif // USE_ZIGBEE
response.replace("{l1", lights); response.replace("{l1", lights);
HueLightStatus1(1, &response); HueLightStatus1(1, &response);
response += F("}"); response += F("}");
} }
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE " HueGroups Result (%s)"), path->c_str());
WSSend(200, CT_JSON, response); WSSend(200, CT_JSON, response);
} }
@ -791,17 +827,17 @@ void HandleHueApi(String *path)
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE_POST_ARGS " (%s)"), json.c_str()); // HTP: Hue POST args ({"on":false}) AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_HTTP D_HUE_POST_ARGS " (%s)"), json.c_str()); // HTP: Hue POST args ({"on":false})
} }
if (path->endsWith("/invalid/")) {} // Just ignore if (path->endsWith(F("/invalid/"))) {} // Just ignore
else if (!apilen) HueAuthentication(path); // New HUE App setup else if (!apilen) HueAuthentication(path); // New HUE App setup
else if (path->endsWith("/")) HueAuthentication(path); // New HUE App setup else if (path->endsWith(F("/"))) HueAuthentication(path); // New HUE App setup
else if (path->endsWith("/config")) HueConfig(path); else if (path->endsWith(F("/config"))) HueConfig(path);
else if (path->indexOf("/lights") >= 0) HueLights(path); else if (path->indexOf(F("/lights")) >= 0) HueLights(path);
else if (path->indexOf("/groups") >= 0) HueGroups(path); else if (path->indexOf(F("/groups")) >= 0) HueGroups(path);
else if (path->endsWith("/schedules")) HueNotImplemented(path); else if (path->endsWith(F("/schedules"))) HueNotImplemented(path);
else if (path->endsWith("/sensors")) HueNotImplemented(path); else if (path->endsWith(F("/sensors"))) HueNotImplemented(path);
else if (path->endsWith("/scenes")) HueNotImplemented(path); else if (path->endsWith(F("/scenes"))) HueNotImplemented(path);
else if (path->endsWith("/rules")) HueNotImplemented(path); else if (path->endsWith(F("/rules"))) HueNotImplemented(path);
else if (path->endsWith("/resourcelinks")) HueNotImplemented(path); else if (path->endsWith(F("/resourcelinks"))) HueNotImplemented(path);
else HueGlobalConfig(path); else HueGlobalConfig(path);
} }
@ -813,14 +849,14 @@ bool Xdrv20(uint8_t function)
{ {
bool result = false; bool result = false;
#ifdef USE_SCRIPT_HUE #if defined(USE_SCRIPT_HUE) || defined(USE_ZIGBEE)
if ((EMUL_HUE == Settings.flag2.emulation)) { if ((EMUL_HUE == Settings.flag2.emulation)) {
#else #else
if (devices_present && (EMUL_HUE == Settings.flag2.emulation)) { if (devices_present && (EMUL_HUE == Settings.flag2.emulation)) {
#endif #endif
switch (function) { switch (function) {
case FUNC_WEB_ADD_HANDLER: case FUNC_WEB_ADD_HANDLER:
WebServer->on("/description.xml", HandleUpnpSetupHue); WebServer->on(F("/description.xml"), HandleUpnpSetupHue);
break; break;
} }
} }

View File

@ -126,8 +126,6 @@ void OneWireWriteBit(uint8_t v)
delayMicroseconds(delay_high[v]); delayMicroseconds(delay_high[v]);
} }
/*
// Fails for reasons unknown to me
uint8_t OneWireReadBit(void) uint8_t OneWireReadBit(void)
{ {
if (!ds18x20_dual_mode) { if (!ds18x20_dual_mode) {
@ -135,44 +133,19 @@ uint8_t OneWireReadBit(void)
digitalWrite(ds18x20_pin, LOW); digitalWrite(ds18x20_pin, LOW);
delayMicroseconds(3); delayMicroseconds(3);
pinMode(ds18x20_pin, Settings.flag3.ds18x20_internal_pullup ? INPUT_PULLUP : INPUT); // SetOption74 - Enable internal pullup for single DS18x20 sensor pinMode(ds18x20_pin, Settings.flag3.ds18x20_internal_pullup ? INPUT_PULLUP : INPUT); // SetOption74 - Enable internal pullup for single DS18x20 sensor
delayMicroseconds(10);
uint8_t r = digitalRead(ds18x20_pin);
delayMicroseconds(53);
return r;
} else { } else {
digitalWrite(ds18x20_pin_out, LOW); digitalWrite(ds18x20_pin_out, LOW);
delayMicroseconds(3); delayMicroseconds(3);
digitalWrite(ds18x20_pin_out, HIGH); digitalWrite(ds18x20_pin_out, HIGH);
}
delayMicroseconds(10); delayMicroseconds(10);
uint8_t r = digitalRead(ds18x20_pin); uint8_t r = digitalRead(ds18x20_pin);
delayMicroseconds(53); delayMicroseconds(53);
return r; return r;
} }
*/
// Works fine in contrast to above. Why?
void OneWireReadBit1(void)
{
pinMode(ds18x20_pin, OUTPUT);
digitalWrite(ds18x20_pin, LOW);
delayMicroseconds(3);
pinMode(ds18x20_pin, Settings.flag3.ds18x20_internal_pullup ? INPUT_PULLUP : INPUT); // SetOption74 - Enable internal pullup for single DS18x20 sensor
}
void OneWireReadBit2(void)
{
digitalWrite(ds18x20_pin_out, LOW);
delayMicroseconds(3);
digitalWrite(ds18x20_pin_out, HIGH);
}
uint8_t OneWireReadBit(void)
{
if (!ds18x20_dual_mode) {
OneWireReadBit1();
} else {
OneWireReadBit2();
}
delayMicroseconds(10);
uint8_t r = digitalRead(ds18x20_pin);
delayMicroseconds(53);
return r;
} }
/*------------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------------*/

View File

@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef USE_I2C #ifdef USE_I2C
@ -23,6 +24,15 @@
* AHT10/15 - Temperature and Humidity * AHT10/15 - Temperature and Humidity
* *
* I2C Address: 0x38 * I2C Address: 0x38
*
* Attention: this Sensor is incompatible with other I2C devices on I2C bus.
*
* The Datasheet write:
* "Only a single AHT10 can be connected to the I2C bus and no other I2C
* devices can be connected".
*
* after lot of search and tests, now is confirmed that works only reliable with one sensor
* on I2C Bus
\*********************************************************************************************/ \*********************************************************************************************/
#define XSNS_63 63 #define XSNS_63 63
@ -31,7 +41,6 @@
#define AHT10_ADDR 0x38 #define AHT10_ADDR 0x38
uint8_t eSensorCalibrateCmd[3] = {0xE1, 0x08, 0x00}; uint8_t eSensorCalibrateCmd[3] = {0xE1, 0x08, 0x00};
uint8_t eSensorNormalCmd[3] = {0xA8, 0x00, 0x00};
uint8_t eSensorMeasureCmd[3] = {0xAC, 0x33, 0x00}; uint8_t eSensorMeasureCmd[3] = {0xAC, 0x33, 0x00};
uint8_t eSensorResetCmd = 0xBA; uint8_t eSensorResetCmd = 0xBA;
@ -52,7 +61,7 @@ bool AHT10Read(void)
Wire.beginTransmission(AHT10_ADDR); Wire.beginTransmission(AHT10_ADDR);
Wire.write(eSensorMeasureCmd, 3); Wire.write(eSensorMeasureCmd, 3);
Wire.endTransmission(); Wire.endTransmission();
delay(100); delay(80);
Wire.requestFrom(AHT10_ADDR, 6); Wire.requestFrom(AHT10_ADDR, 6);
for (uint32_t i = 0; Wire.available() > 0; i++) { for (uint32_t i = 0; Wire.available() > 0; i++) {
@ -80,10 +89,10 @@ bool AHT10Init(void)
{ {
Wire.begin(AHT10_ADDR); Wire.begin(AHT10_ADDR);
Wire.beginTransmission(AHT10_ADDR); Wire.beginTransmission(AHT10_ADDR);
Wire.write(eSensorCalibrateCmd, 3); Wire.write(eSensorCalibrateCmd, 3); // init with internal temp coef.
Wire.endTransmission(); Wire.endTransmission();
delay(500); // ?!?! too long delay(40); // after tests, its ok
return (0x08 == (AHT10ReadStatus() & 0x68)); return (0x08 == (AHT10ReadStatus() & 0x68));
} }