further optimization
This commit is contained in:
parent
415dfd2750
commit
0462755922
@ -1,6 +1,6 @@
|
|||||||
//this code is a modified version of https://github.com/Makuna/NeoPixelBus/issues/103
|
//this code is a modified version of https://github.com/Makuna/NeoPixelBus/issues/103
|
||||||
|
|
||||||
#define WORKAROUND_ESP32_BITBANG
|
//#define WORKAROUND_ESP32_BITBANG
|
||||||
//see https://github.com/Aircoookie/WLED/issues/2 for flicker free ESP32 support
|
//see https://github.com/Aircoookie/WLED/issues/2 for flicker free ESP32 support
|
||||||
|
|
||||||
//uncomment this if red and green are swapped
|
//uncomment this if red and green are swapped
|
||||||
|
@ -264,8 +264,7 @@ WiFiUDP notifierUdp, rgbUdp;
|
|||||||
WiFiUDP ntpUdp;
|
WiFiUDP ntpUdp;
|
||||||
IPAddress ntpServerIP;
|
IPAddress ntpServerIP;
|
||||||
unsigned int ntpLocalPort = 2390;
|
unsigned int ntpLocalPort = 2390;
|
||||||
const uint16_t NTP_PACKET_SIZE = 48;
|
#define NTP_PACKET_SIZE 48
|
||||||
byte ntpPacketBuffer[NTP_PACKET_SIZE];
|
|
||||||
unsigned long ntpLastSyncTime = 999000000L;
|
unsigned long ntpLastSyncTime = 999000000L;
|
||||||
unsigned long ntpPacketSentTime = 999000000L;
|
unsigned long ntpPacketSentTime = 999000000L;
|
||||||
|
|
||||||
@ -364,9 +363,6 @@ void loop() {
|
|||||||
handleBlynk();
|
handleBlynk();
|
||||||
if (briT) strip.service(); //do not update strip if off, prevents flicker on ESP32
|
if (briT) strip.service(); //do not update strip if off, prevents flicker on ESP32
|
||||||
}
|
}
|
||||||
/*#ifdef ARDUINO_ARCH_ESP32
|
|
||||||
delay(1);
|
|
||||||
#endif*/
|
|
||||||
|
|
||||||
//DEBUG
|
//DEBUG
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
@ -75,20 +75,20 @@ void sendNTPPacket()
|
|||||||
WiFi.hostByName(ntpServerName, ntpServerIP);
|
WiFi.hostByName(ntpServerName, ntpServerIP);
|
||||||
DEBUG_PRINTLN("send NTP packet");
|
DEBUG_PRINTLN("send NTP packet");
|
||||||
|
|
||||||
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
|
memset(obuf, 0, NTP_PACKET_SIZE);
|
||||||
|
|
||||||
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
obuf[0] = 0b11100011; // LI, Version, Mode
|
||||||
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
|
obuf[1] = 0; // Stratum, or type of clock
|
||||||
ntpPacketBuffer[2] = 6; // Polling Interval
|
obuf[2] = 6; // Polling Interval
|
||||||
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
|
obuf[3] = 0xEC; // Peer Clock Precision
|
||||||
// 8 bytes of zero for Root Delay & Root Dispersion
|
// 8 bytes of zero for Root Delay & Root Dispersion
|
||||||
ntpPacketBuffer[12] = 49;
|
obuf[12] = 49;
|
||||||
ntpPacketBuffer[13] = 0x4E;
|
obuf[13] = 0x4E;
|
||||||
ntpPacketBuffer[14] = 49;
|
obuf[14] = 49;
|
||||||
ntpPacketBuffer[15] = 52;
|
obuf[15] = 52;
|
||||||
|
|
||||||
ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123
|
ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123
|
||||||
ntpUdp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
|
ntpUdp.write((byte*)obuf, NTP_PACKET_SIZE);
|
||||||
ntpUdp.endPacket();
|
ntpUdp.endPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,13 +96,13 @@ bool checkNTPResponse()
|
|||||||
{
|
{
|
||||||
int cb = ntpUdp.parsePacket();
|
int cb = ntpUdp.parsePacket();
|
||||||
if (cb) {
|
if (cb) {
|
||||||
DEBUG_PRINT("packet received, length=");
|
DEBUG_PRINT("packet received, l=");
|
||||||
DEBUG_PRINTLN(cb);
|
DEBUG_PRINTLN(cb);
|
||||||
|
|
||||||
ntpUdp.read(ntpPacketBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
ntpUdp.read(obuf, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||||
|
|
||||||
unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
|
unsigned long highWord = word(obuf[40], obuf[41]);
|
||||||
unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
|
unsigned long lowWord = word(obuf[42], obuf[43]);
|
||||||
if (highWord == 0 && lowWord == 0) return false;
|
if (highWord == 0 && lowWord == 0) return false;
|
||||||
|
|
||||||
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
||||||
|
@ -53,12 +53,7 @@ void alexaOn()
|
|||||||
applyMacro(macroAlexaOn);
|
applyMacro(macroAlexaOn);
|
||||||
}
|
}
|
||||||
|
|
||||||
String body = "[{\"success\":{\"/lights/1/state/on\":true}}]";
|
server.send(200, "application/json", "[{\"success\":{\"/lights/1/state/on\":true}}]");
|
||||||
|
|
||||||
server.send(200, "text/xml", body.c_str());
|
|
||||||
|
|
||||||
DEBUG_PRINT("Sending :");
|
|
||||||
DEBUG_PRINTLN(body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void alexaOff()
|
void alexaOff()
|
||||||
@ -71,19 +66,17 @@ void alexaOff()
|
|||||||
applyMacro(macroAlexaOff);
|
applyMacro(macroAlexaOff);
|
||||||
}
|
}
|
||||||
|
|
||||||
String body = "[{\"success\":{\"/lights/1/state/on\":false}}]";
|
server.send(200, "application/json", "[{\"success\":{\"/lights/1/state/on\":false}}]");
|
||||||
|
|
||||||
server.send(200, "application/json", body.c_str());
|
|
||||||
|
|
||||||
DEBUG_PRINT("Sending:");
|
|
||||||
DEBUG_PRINTLN(body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void alexaDim(byte briL)
|
void alexaDim(byte briL)
|
||||||
{
|
{
|
||||||
String body = "[{\"success\":{\"/lights/1/state/bri\":"+ String(briL) +"}}]";
|
olen = 0;
|
||||||
|
oappend("[{\"success\":{\"/lights/1/state/bri\":");
|
||||||
|
oappendi(briL);
|
||||||
|
oappend("}}]");
|
||||||
|
|
||||||
server.send(200, "application/json", body.c_str());
|
server.send(200, "application/json", obuf);
|
||||||
|
|
||||||
String ct = (alexaNotify)?"win&IN&A=":"win&NN&IN&A=";
|
String ct = (alexaNotify)?"win&IN&A=":"win&NN&IN&A=";
|
||||||
if (briL < 255)
|
if (briL < 255)
|
||||||
@ -113,22 +106,29 @@ void respondToSearch() {
|
|||||||
char s[16];
|
char s[16];
|
||||||
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
|
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
|
||||||
|
|
||||||
String response =
|
olen = 0;
|
||||||
|
oappend(
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
"EXT:\r\n"
|
"EXT:\r\n"
|
||||||
"CACHE-CONTROL: max-age=100\r\n" // SSDP_INTERVAL
|
"CACHE-CONTROL: max-age=100\r\n" // SSDP_INTERVAL
|
||||||
"LOCATION: http://"+ String(s) +":80/description.xml\r\n"
|
"LOCATION: http://");
|
||||||
|
oappend(s);
|
||||||
|
oappend(":80/description.xml\r\n"
|
||||||
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber
|
"SERVER: FreeRTOS/6.0.5, UPnP/1.0, IpBridge/1.17.0\r\n" // _modelName, _modelNumber
|
||||||
"hue-bridgeid: "+ escapedMac +"\r\n"
|
"hue-bridgeid: ");
|
||||||
|
oappend((char*)escapedMac.c_str());
|
||||||
|
oappend("\r\n"
|
||||||
"ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType
|
"ST: urn:schemas-upnp-org:device:basic:1\r\n" // _deviceType
|
||||||
"USN: uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"::upnp:rootdevice\r\n" // _uuid::_deviceType
|
"USN: uuid:2f402f80-da50-11e1-9b23-");
|
||||||
"\r\n";
|
oappend((char*)escapedMac.c_str());
|
||||||
|
oappend("::upnp:rootdevice\r\n" // _uuid::_deviceType
|
||||||
|
"\r\n");
|
||||||
|
|
||||||
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
|
UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
UDP.write((byte*)response.c_str(), response.length());
|
UDP.write((byte*)obuf, olen);
|
||||||
#else
|
#else
|
||||||
UDP.write(response.c_str());
|
UDP.write(obuf);
|
||||||
#endif
|
#endif
|
||||||
UDP.endPacket();
|
UDP.endPacket();
|
||||||
|
|
||||||
@ -144,21 +144,30 @@ void alexaInitPages() {
|
|||||||
char s[16];
|
char s[16];
|
||||||
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
|
sprintf(s, "%d.%d.%d.%d", localIP[0], localIP[1], localIP[2], localIP[3]);
|
||||||
|
|
||||||
String setup_xml = "<?xml version=\"1.0\" ?>"
|
olen = 0;
|
||||||
|
oappend("<?xml version=\"1.0\" ?>"
|
||||||
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
|
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
|
||||||
"<specVersion><major>1</major><minor>0</minor></specVersion>"
|
"<specVersion><major>1</major><minor>0</minor></specVersion>"
|
||||||
"<URLBase>http://"+ String(s) +":80/</URLBase>"
|
"<URLBase>http://");
|
||||||
|
oappend(s);
|
||||||
|
oappend(":80/</URLBase>"
|
||||||
"<device>"
|
"<device>"
|
||||||
"<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>"
|
"<deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType>"
|
||||||
"<friendlyName>Philips hue ("+ String(s) +")</friendlyName>"
|
"<friendlyName>Philips hue (");
|
||||||
|
oappend(s);
|
||||||
|
oappend(")</friendlyName>"
|
||||||
"<manufacturer>Royal Philips Electronics</manufacturer>"
|
"<manufacturer>Royal Philips Electronics</manufacturer>"
|
||||||
"<manufacturerURL>http://www.philips.com</manufacturerURL>"
|
"<manufacturerURL>http://www.philips.com</manufacturerURL>"
|
||||||
"<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>"
|
"<modelDescription>Philips hue Personal Wireless Lighting</modelDescription>"
|
||||||
"<modelName>Philips hue bridge 2012</modelName>"
|
"<modelName>Philips hue bridge 2012</modelName>"
|
||||||
"<modelNumber>929000226503</modelNumber>"
|
"<modelNumber>929000226503</modelNumber>"
|
||||||
"<modelURL>http://www.meethue.com</modelURL>"
|
"<modelURL>http://www.meethue.com</modelURL>"
|
||||||
"<serialNumber>"+ escapedMac +"</serialNumber>"
|
"<serialNumber>");
|
||||||
"<UDN>uuid:2f402f80-da50-11e1-9b23-"+ escapedMac +"</UDN>"
|
oappend((char*)escapedMac.c_str());
|
||||||
|
oappend("</serialNumber>"
|
||||||
|
"<UDN>uuid:2f402f80-da50-11e1-9b23-");
|
||||||
|
oappend((char*)escapedMac.c_str());
|
||||||
|
oappend("</UDN>"
|
||||||
"<presentationURL>index.html</presentationURL>"
|
"<presentationURL>index.html</presentationURL>"
|
||||||
"<iconList>"
|
"<iconList>"
|
||||||
" <icon>"
|
" <icon>"
|
||||||
@ -177,9 +186,9 @@ void alexaInitPages() {
|
|||||||
" </icon>"
|
" </icon>"
|
||||||
"</iconList>"
|
"</iconList>"
|
||||||
"</device>"
|
"</device>"
|
||||||
"</root>";
|
"</root>");
|
||||||
|
|
||||||
server.send(200, "text/xml", setup_xml.c_str());
|
server.send(200, "text/xml", obuf);
|
||||||
|
|
||||||
DEBUG_PRINT("Sending :");
|
DEBUG_PRINT("Sending :");
|
||||||
DEBUG_PRINTLN(setup_xml);
|
DEBUG_PRINTLN(setup_xml);
|
||||||
@ -201,9 +210,9 @@ void alexaInitPages() {
|
|||||||
server.on("/status.html", HTTP_GET, [](){
|
server.on("/status.html", HTTP_GET, [](){
|
||||||
DEBUG_PRINTLN("Got status request");
|
DEBUG_PRINTLN("Got status request");
|
||||||
|
|
||||||
String statrespone = "0";
|
char statrespone[] = "0";
|
||||||
if (bri > 0) {
|
if (bri > 0) {
|
||||||
statrespone = "1";
|
statrespone[0] = '1';
|
||||||
}
|
}
|
||||||
server.send(200, "text/plain", statrespone);
|
server.send(200, "text/plain", statrespone);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user