Skip to content

Commit db849da

Browse files
committed
(2.5.0) Discovery is fixed
Optimize a few minor things
1 parent 3e05859 commit db849da

File tree

3 files changed

+40
-51
lines changed

3 files changed

+40
-51
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Espalexa
2-
version=2.4.8
2+
version=2.5.0
33
author=Christian Schwinne
44
maintainer=Christian Schwinne
55
sentence=Library to control an ESP module with the Alexa voice assistant

readme.md

-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
It comes in an easy to use Arduino library.
33
Now compatible with both ESP8266 and ESP32!
44

5-
### ⚠️ This library is currently not actively maintained
6-
**I've received plenty of bug reports about discovery or control failing.
7-
I hope to get back to it at some point, but currently I can't guarantee this library will work for you in any way.
8-
You can of course give it a try, but consider this as broken by default.**
9-
Sorry for the inconvenience. If you manage to improve upon Espalexa's current state, I will happily accept a pull request!
10-
Thank you! _(one thing some reported as working for discovery issues is rebooting your Echo device and discovering devices immediately after)_
11-
125
#### What does this do similar projects don't already?
136

147
It allows you to set a ranged value (e.g. Brightness, Temperature) and optionally a color, additionally to standard on/off control.

src/Espalexa.h

+39-43
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,24 @@ class Espalexa {
116116
return "";
117117
}
118118

119-
String encodeLightId(uint8_t idx)
119+
void encodeLightId(uint8_t idx, char* out)
120120
{
121-
String mac = WiFi.macAddress();
122-
mac.replace(":","");
123-
mac.toLowerCase();
124-
125121
//Unique id must be 12 character len
126-
//use the first part of the MAC followed by the device id in hex value
122+
//use the last 10 characters of the MAC followed by the device id in hex value
127123
//uniqueId: aabbccddeeii
128-
String uniqueId = mac.substring(0, DEVICE_UNIQUE_ID_LENGTH - 2);
129-
char bufIdx[3];
130-
snprintf(bufIdx, sizeof(bufIdx), "%.*x", 2, idx);
131-
uniqueId.concat(bufIdx, strlen(bufIdx));
132-
return uniqueId;
133-
}
134124

135-
uint32_t decodeLightId(uint32_t id) {
136-
return id & 0xF;
125+
uint8_t mac[6];
126+
WiFi.macAddress(mac);
127+
128+
//shift the mac address to the left (discard first byte)
129+
for (uint8_t i = 0; i < 5; i++) {
130+
mac[i] = mac[i+1];
131+
}
132+
mac[5] = idx;
133+
134+
for (uint8_t i = 0; i < 6; i++) {
135+
sprintf(out + i*2, "%.2x", mac[i]);
136+
}
137137
}
138138

139139
//device JSON string: color+temperature device emulates LCT015, dimmable device LWB010, (TODO: on/off Plug 01, color temperature device LWT010, color device LST001)
@@ -143,10 +143,8 @@ class Espalexa {
143143
if (deviceId >= currentDeviceCount) {strcpy(buf,"{}"); return;} //error
144144
EspalexaDevice* dev = devices[deviceId];
145145

146-
//char buf_bri[12] = "";
147-
//brightness support, add "bri" to JSON
148-
//if (dev->getType() != EspalexaDeviceType::onoff)
149-
// sprintf(buf_bri,",\"bri\":%u", dev->getLastValue()-1);
146+
char buf_lightid[13];
147+
encodeLightId(deviceId + 1, buf_lightid);
150148

151149
char buf_col[80] = "";
152150
//color support
@@ -168,7 +166,7 @@ class Espalexa {
168166
"\",\"uniqueid\":\"%s\",\"swversion\":\"espalexa-2.5.0\"}")
169167

170168
, (dev->getValue())?"true":"false", dev->getLastValue()-1, buf_col, buf_ct, buf_cm, typeString(dev->getType()),
171-
dev->getName().c_str(), modelidString(dev->getType()), static_cast<uint8_t>(dev->getType()), encodeLightId(deviceId+1).c_str());
169+
dev->getName().c_str(), modelidString(dev->getType()), static_cast<uint8_t>(dev->getType()), buf_lightid);
172170
}
173171

174172
//Espalexa status page /espalexa
@@ -226,7 +224,7 @@ class Espalexa {
226224
"<URLBase>http://%s:80/</URLBase>"
227225
"<device>"
228226
"<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>"
229-
"<friendlyName>Philips hue (%s:80)</friendlyName>"
227+
"<friendlyName>Espalexa (%s:80)</friendlyName>"
230228
"<manufacturer>Royal Philips Electronics</manufacturer>"
231229
"<manufacturerURL>http://www.philips.com</manufacturerURL>"
232230
"<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>"
@@ -363,30 +361,28 @@ class Espalexa {
363361

364362
if (!udpConnected) return;
365363
int packetSize = espalexaUdp.parsePacket();
366-
if (!packetSize) return; //no new udp packet
364+
if (packetSize < 1) return; //no new udp packet
367365

368366
EA_DEBUGLN("Got UDP!");
369-
if (packetSize > 0) {
370-
unsigned char packetBuffer[packetSize+1]; //buffer to hold incoming udp packet
371-
int len = espalexaUdp.read(packetBuffer, packetSize+1);
372-
packetBuffer[packetSize] = 0;
373-
374-
espalexaUdp.flush();
375-
if (!discoverable) return; //do not reply to M-SEARCH if not discoverable
376-
377-
String request = (const char *) packetBuffer;
378-
if(request.indexOf("M-SEARCH") >= 0) {
379-
EA_DEBUGLN(request);
380-
if(request.indexOf("ssdp:discover") > 0 &&
381-
(request.indexOf("upnp:rootdevice") > 0 ||
382-
//request.indexOf("asic:1") > 0 ||
383-
request.indexOf("ssdp:all") > 0 ||
384-
request.indexOf("device:basic:1") > 0 ))
385-
{
386-
EA_DEBUGLN("Responding search req...");
387-
respondToSearch();
388-
}
389-
}
367+
368+
unsigned char packetBuffer[packetSize+1]; //buffer to hold incoming udp packet
369+
espalexaUdp.read(packetBuffer, packetSize);
370+
packetBuffer[packetSize] = 0;
371+
372+
espalexaUdp.flush();
373+
if (!discoverable) return; //do not reply to M-SEARCH if not discoverable
374+
375+
const char* request = (const char *) packetBuffer;
376+
if (strstr(request, "M-SEARCH") == nullptr) return;
377+
378+
EA_DEBUGLN(request);
379+
if (strstr(request, "ssdp:disc") != nullptr && //short for "ssdp:discover"
380+
(strstr(request, "upnp:rootd") != nullptr || //short for "upnp:rootdevice"
381+
strstr(request, "ssdp:all") != nullptr ||
382+
strstr(request, "asic:1") != nullptr )) //short for "device:basic:1"
383+
{
384+
EA_DEBUGLN("Responding search req...");
385+
respondToSearch();
390386
}
391387
}
392388

@@ -594,7 +590,7 @@ class Espalexa {
594590
return perc / 255;
595591
}
596592

597-
~Espalexa(){delete devices;} //note: Espalexa is NOT meant to be destructed
593+
~Espalexa(){} //note: Espalexa is NOT meant to be destructed
598594
};
599595

600596
#endif

0 commit comments

Comments
 (0)