From 372c0ad4b531148f884cd8286eacaecc13356268 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Sun, 10 May 2020 23:42:49 +0200 Subject: [PATCH] (2.4.6) Reduce memory usage by 1.6kB Lower heap fragmentation --- library.properties | 2 +- readme.md | 2 +- src/Espalexa.h | 143 +++++++++++++++++++++-------------------- src/EspalexaDevice.cpp | 1 + 4 files changed, 75 insertions(+), 73 deletions(-) diff --git a/library.properties b/library.properties index e88fca4..5747432 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Espalexa -version=2.4.5 +version=2.4.6 author=Christian Schwinne maintainer=Christian Schwinne sentence=Library to control an ESP module with the Alexa voice assistant diff --git a/readme.md b/readme.md index fec7a4f..d62e1bf 100644 --- a/readme.md +++ b/readme.md @@ -96,7 +96,7 @@ Here is an overview of the devices (light types) Espalexa can emulate: | EspalexaDeviceType::whitespectrum | Color temperature adjustment not working on Dot | | EspalexaDeviceType::color | Works as intended (dimming + color) | | EspalexaDeviceType::extendedcolor | Color temperature adjustment not working on Dot | -| EspalexaDeviceType::onoff (experimental) | Works, but is treated as dimmable by Alexa | +| EspalexaDeviceType::onoff (experimental) | Deprecated. Treated as dimmable. | See the example `EspalexaFullyFeatured` to learn how to define each device type and use the new EspalexaDevice pointer callback function type! diff --git a/src/Espalexa.h b/src/Espalexa.h index 37c6560..6632adf 100644 --- a/src/Espalexa.h +++ b/src/Espalexa.h @@ -10,7 +10,7 @@ */ /* * @title Espalexa library - * @version 2.4.5 + * @version 2.4.6 * @author Christian Schwinne * @license MIT * @contributors d-999 @@ -49,7 +49,7 @@ #include #ifdef ESPALEXA_DEBUG - #pragma message "Espalexa 2.4.5 debug mode" + #pragma message "Espalexa 2.4.6 debug mode" #define EA_DEBUG(x) Serial.print (x) #define EA_DEBUGLN(x) Serial.println (x) #else @@ -81,35 +81,29 @@ class Espalexa { WiFiUDP espalexaUdp; IPAddress ipMulti; bool udpConnected = false; - char packetBuffer[255]; //buffer to hold incoming udp packet String escapedMac=""; //lowercase mac address //private member functions - String boolString(bool st) - { - return(st)?"true":"false"; - } - - String modeString(EspalexaColorMode m) + const char* modeString(EspalexaColorMode m) { if (m == EspalexaColorMode::xy) return "xy"; if (m == EspalexaColorMode::hs) return "hs"; return "ct"; } - String typeString(EspalexaDeviceType t) + const char* typeString(EspalexaDeviceType t) { switch (t) { - case EspalexaDeviceType::dimmable: return "Dimmable light"; - case EspalexaDeviceType::whitespectrum: return "Color temperature light"; - case EspalexaDeviceType::color: return "Color light"; - case EspalexaDeviceType::extendedcolor: return "Extended color light"; + case EspalexaDeviceType::dimmable: return PSTR("Dimmable light"); + case EspalexaDeviceType::whitespectrum: return PSTR("Color temperature light"); + case EspalexaDeviceType::color: return PSTR("Color light"); + case EspalexaDeviceType::extendedcolor: return PSTR("Extended color light"); } - return "Light"; + return ""; } - String modelidString(EspalexaDeviceType t) + const char* modelidString(EspalexaDeviceType t) { switch (t) { @@ -118,7 +112,7 @@ class Espalexa { case EspalexaDeviceType::color: return "LST001"; case EspalexaDeviceType::extendedcolor: return "LCT015"; } - return "Plug"; + return ""; } //Workaround functions courtesy of Sonoff-Tasmota @@ -135,38 +129,38 @@ class Espalexa { } //device JSON string: color+temperature device emulates LCT015, dimmable device LWB010, (TODO: on/off Plug 01, color temperature device LWT010, color device LST001) - String deviceJsonString(uint8_t deviceId) + void deviceJsonString(uint8_t deviceId, char* buf) { deviceId--; - if (deviceId >= currentDeviceCount) return "{}"; //error + if (deviceId >= currentDeviceCount) {strcpy(buf,"{}"); return;} //error EspalexaDevice* dev = devices[deviceId]; - - String json = "{\"state\":{\"on\":"; - json += boolString(dev->getValue()); - if (dev->getType() != EspalexaDeviceType::onoff) //bri support - { - json += ",\"bri\":" + String(dev->getLastValue()-1); - if (static_cast(dev->getType()) > 2) //color support - { - json += ",\"hue\":" + String(dev->getHue()) + ",\"sat\":" + String(dev->getSat()); - json += ",\"effect\":\"none\",\"xy\":[" + String(dev->getX()) + "," + String(dev->getY()) + "]"; - } - if (static_cast(dev->getType()) > 1 && dev->getType() != EspalexaDeviceType::color) //white spectrum support - { - json += ",\"ct\":" + String(dev->getCt()); - } - } - json += ",\"alert\":\"none"; - if (static_cast(dev->getType()) > 1) json += "\",\"colormode\":\"" + modeString(dev->getColorMode()); - json += "\",\"mode\":\"homeautomation\",\"reachable\":true},"; - json += "\"type\":\"" + typeString(dev->getType()); - json += "\",\"name\":\"" + dev->getName(); - json += "\",\"modelid\":\"" + modelidString(dev->getType()); - json += "\",\"manufacturername\":\"Philips\",\"productname\":\"E" + String(static_cast(dev->getType())); - json += "\",\"uniqueid\":\"" + String(encodeLightId(deviceId+1)); - json += "\",\"swversion\":\"espalexa-2.4.5\"}"; - return json; + //char buf_bri[12] = ""; + //brightness support, add "bri" to JSON + //if (dev->getType() != EspalexaDeviceType::onoff) + // sprintf(buf_bri,",\"bri\":%u", dev->getLastValue()-1); + + char buf_col[80] = ""; + //color support + if (static_cast(dev->getType()) > 2) + sprintf_P(buf_col,PSTR(",\"hue\":%u,\"sat\":%u,\"effect\":\"none\",\"xy\":[%f,%f]") + ,dev->getHue(), dev->getSat(), dev->getX(), dev->getY()); + + char buf_ct[16] = ""; + //white spectrum support + if (static_cast(dev->getType()) > 1 && dev->getType() != EspalexaDeviceType::color) + sprintf(buf_ct, ",\"ct\":%u", dev->getCt()); + + char buf_cm[20] = ""; + if (static_cast(dev->getType()) > 1) + sprintf(buf_cm,PSTR("\",\"colormode\":\"%s"), modeString(dev->getColorMode())); + + sprintf_P(buf, PSTR("{\"state\":{\"on\":%s,\"bri\":%u%s%s,\"alert\":\"none%s\",\"mode\":\"homeautomation\",\"reachable\":true}," + "\"type\":\"%s\",\"name\":\"%s\",\"modelid\":\"%s\",\"manufacturername\":\"Philips\",\"productname\":\"E%u" + "\",\"uniqueid\":\"%u\",\"swversion\":\"espalexa-2.4.6\"}") + + , (dev->getValue())?"true":"false", dev->getLastValue()-1, buf_col, buf_ct, buf_cm, typeString(dev->getType()), + dev->getName().c_str(), modelidString(dev->getType()), static_cast(dev->getType()), encodeLightId(deviceId+1)); } //Espalexa status page /espalexa @@ -181,14 +175,14 @@ class Espalexa { res += "Value of device " + String(i+1) + " (" + dev->getName() + "): " + String(dev->getValue()) + " (" + typeString(dev->getType()); if (static_cast(dev->getType()) > 1) //color support { - res += ", colormode=" + modeString(dev->getColorMode()) + ", r=" + String(dev->getR()) + ", g=" + String(dev->getG()) + ", b=" + String(dev->getB()); + res += ", colormode=" + String(modeString(dev->getColorMode())) + ", r=" + String(dev->getR()) + ", g=" + String(dev->getG()) + ", b=" + String(dev->getB()); res +=", ct=" + String(dev->getCt()) + ", hue=" + String(dev->getHue()) + ", sat=" + String(dev->getSat()) + ", x=" + String(dev->getX()) + ", y=" + String(dev->getY()); } res += ")\r\n"; } res += "\r\nFree Heap: " + (String)ESP.getFreeHeap(); res += "\r\nUptime: " + (String)millis(); - res += "\r\n\r\nEspalexa library v2.4.5 by Christian Schwinne 2020"; + res += "\r\n\r\nEspalexa library v2.4.6 by Christian Schwinne 2020"; server->send(200, "text/plain", res); } #endif @@ -206,7 +200,7 @@ class Espalexa { EA_DEBUGLN("Body: " + body); if(!handleAlexaApiCall(server)) #endif - server->send(404, "text/plain", "Not Found (espalexa-internal)"); + server->send(404, "text/plain", "Not Found (espalexa)"); } //send description.xml device property page @@ -216,30 +210,31 @@ class Espalexa { IPAddress localIP = WiFi.localIP(); char s[16]; sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]); - - String setup_xml = "" + char buf[1024]; + + sprintf_P(buf,PSTR("" "" "10" - "http://"+ String(s) +":80/" + "http://%s:80/" "" "urn:schemas-upnp-org:device:Basic:1" - "Espalexa ("+ String(s) +")" + "Espalexa (%s)" "Royal Philips Electronics" "http://www.philips.com" "Philips hue Personal Wireless Lighting" "Philips hue bridge 2012" "929000226503" "http://www.meethue.com" - ""+ escapedMac +"" - "uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"" + "%s" + "uuid:2f402f80-da50-11e1-9b23-%s" "index.html" "" - ""; + ""),s,s,escapedMac.c_str(),escapedMac.c_str()); - server->send(200, "text/xml", setup_xml.c_str()); + server->send(200, "text/xml", buf); - EA_DEBUG("Sending :"); - EA_DEBUGLN(setup_xml); + EA_DEBUG("Send setup.xml"); + //EA_DEBUGLN(setup_xml); } //init the server @@ -290,22 +285,23 @@ class Espalexa { char s[16]; sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]); - String response = - "HTTP/1.1 200 OK\r\n" + char buf[1024]; + + sprintf_P(buf,PSTR("HTTP/1.1 200 OK\r\n" "EXT:\r\n" "CACHE-CONTROL: max-age=100\r\n" // SSDP_INTERVAL - "LOCATION: http://"+ String(s) +":80/description.xml\r\n" + "LOCATION: http://%s:80/description.xml\r\n" "SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber - "hue-bridgeid: "+ escapedMac +"\r\n" + "hue-bridgeid: %s\r\n" "ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType - "USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::ssdp:all\r\n" // _uuid::_deviceType - "\r\n"; + "USN: uuid:2f402f80-da50-11e1-9b23-%s::ssdp:all\r\n" // _uuid::_deviceType + "\r\n"),s,escapedMac.c_str(),escapedMac.c_str()); espalexaUdp.beginPacket(espalexaUdp.remoteIP(), espalexaUdp.remotePort()); #ifdef ARDUINO_ARCH_ESP32 - espalexaUdp.write((uint8_t*)response.c_str(), response.length()); + espalexaUdp.write((uint8_t*)buf, response.length()); #else - espalexaUdp.write(response.c_str()); + espalexaUdp.write(buf); #endif espalexaUdp.endPacket(); } @@ -362,7 +358,8 @@ class Espalexa { if (!packetSize) return; //no new udp packet EA_DEBUGLN("Got UDP!"); - int len = espalexaUdp.read(packetBuffer, 254); + char packetBuffer[255]; //buffer to hold incoming udp packet + uint16_t len = espalexaUdp.read(packetBuffer, 254); if (len > 0) { packetBuffer[len] = 0; } @@ -447,13 +444,13 @@ class Espalexa { { EA_DEBUGLN("devType"); body = ""; - server->send(200, "application/json", "[{\"success\":{\"username\":\"2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr\"}}]"); + server->send(200, "application/json", F("[{\"success\":{\"username\":\"2WLEDHardQrI3WHYTHoMcXHgEspsM8ZZRpSKtBQr\"}}]")); return true; } if (req.indexOf("state") > 0) //client wants to control light { - server->send(200, "application/json", "[{\"success\":{\"/lights/1/state/\": true}}]"); + server->send(200, "application/json", F("[{\"success\":{\"/lights/1/state/\": true}}]")); uint32_t devId = req.substring(req.indexOf("lights")+7).toInt(); EA_DEBUG("ls"); EA_DEBUGLN(devId); @@ -530,7 +527,9 @@ class Espalexa { for (int i = 0; isend(200, "application/json", "{}"); } else { - server->send(200, "application/json", deviceJsonString(devId)); + char buf[512]; + deviceJsonString(devId, buf); + server->send(200, "application/json", buf); } } diff --git a/src/EspalexaDevice.cpp b/src/EspalexaDevice.cpp index 520cf44..8bdd780 100644 --- a/src/EspalexaDevice.cpp +++ b/src/EspalexaDevice.cpp @@ -28,6 +28,7 @@ EspalexaDevice::EspalexaDevice(String deviceName, DeviceCallbackFunction gnCallb _callbackDev = gnCallback; _type = t; if (t == EspalexaDeviceType::onoff) _type = EspalexaDeviceType::dimmable; //on/off is broken, so make dimmable device instead + if (t == EspalexaDeviceType::whitespectrum) _mode = EspalexaColorMode::ct; _val = initialValue; _val_last = _val; }