Skip to content

Commit

Permalink
Bugfix.
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Aug 27, 2023
1 parent 457dd14 commit 51d62af
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 56 deletions.
5 changes: 5 additions & 0 deletions wled00/remote.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wled.h"
#ifndef WLED_DISABLE_ESPNOW

#define NIGHT_MODE_DEACTIVATED -1
#define NIGHT_MODE_BRIGHTNESS 5
Expand Down Expand Up @@ -144,3 +145,7 @@ void handleRemote(uint8_t *incomingData, size_t len) {
}
last_seq = cur_seq;
}

#else
void handleRemote(uint8_t *incomingData, size_t len) {}
#endif
44 changes: 19 additions & 25 deletions wled00/set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,40 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
//WIFI SETTINGS
if (subPage == SUBPAGE_WIFI)
{
char oldSSID[sizeof(clientSSID)];

strcpy(oldSSID, clientSSID);
strlcpy(clientSSID,request->arg(F("CS")).c_str(), 33);
if (!strcmp(oldSSID, clientSSID)) forceReconnect = true;

if (!isAsterisksOnly(request->arg(F("CP")).c_str(), 65)) strlcpy(clientPass, request->arg(F("CP")).c_str(), 65);
if (!isAsterisksOnly(request->arg(F("CP")).c_str(), 65)) {
strlcpy(clientPass, request->arg(F("CP")).c_str(), 65);
forceReconnect = true;
}

strlcpy(cmDNS, request->arg(F("CM")).c_str(), 33);

apBehavior = request->arg(F("AB")).toInt();
strcpy(oldSSID, apSSID);
strlcpy(apSSID, request->arg(F("AS")).c_str(), 33);
if (!strcmp(oldSSID, apSSID) && apActive) forceReconnect = true;
apHide = request->hasArg(F("AH"));
int passlen = request->arg(F("AP")).length();
if (passlen == 0 || (passlen > 7 && !isAsterisksOnly(request->arg(F("AP")).c_str(), 65))) strlcpy(apPass, request->arg(F("AP")).c_str(), 65);
int t = request->arg(F("AC")).toInt(); if (t > 0 && t < 14) apChannel = t;
if (passlen == 0 || (passlen > 7 && !isAsterisksOnly(request->arg(F("AP")).c_str(), 65))) {
strlcpy(apPass, request->arg(F("AP")).c_str(), 65);
forceReconnect = true;
}
int t = request->arg(F("AC")).toInt();
if (t != apChannel) forceReconnect = true;
if (t > 0 && t < 14) apChannel = t;

noWifiSleep = request->hasArg(F("WS"));

#ifndef WLED_DISABLE_ESPNOW
bool oldESPNow = enableESPNow;
enableESPNow = request->hasArg(F("RE"));
if (oldESPNow != enableESPNow) {
if (!enableESPNow) {
DEBUG_PRINTLN(F("ESP-NOW stopping."));
if (statusESPNow == ESP_NOW_STATE_ON) quickEspNow.stop();
statusESPNow = ESP_NOW_STATE_UNINIT;
} else {
quickEspNow.onDataRcvd(espNowReceiveCB);
if (apActive || !WLED_CONNECTED) {
DEBUG_PRINTLN(F("ESP-NOW initing in AP mode."));
#ifdef ESP32
quickEspNow.setWiFiBandwidth(WIFI_IF_AP, WIFI_BW_HT20); // Only needed for ESP32 in case you need coexistence with ESP8266 in the same network
#endif //ESP32
if (quickEspNow.begin(apChannel, WIFI_IF_AP)) { // Same channel must be used for both AP and ESP-NOW
statusESPNow = ESP_NOW_STATE_ERROR; // error
} else {
statusESPNow = ESP_NOW_STATE_ON; // ok
}
}
}
}
strlcpy(linked_remote, request->arg(F("RMAC")).c_str(), 12);
linked_remote[12] = '\0';
if (oldESPNow != enableESPNow) forceReconnect = true;
strlcpy(linked_remote, request->arg(F("RMAC")).c_str(), 13);
strlwr(linked_remote); //Normalize MAC format to lowercase
#endif

Expand Down
36 changes: 18 additions & 18 deletions wled00/udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,35 +150,33 @@ void notify(byte callMode, bool followUp)
//next value to be added has index: udpOut[offs + 0]

#ifndef WLED_DISABLE_ESPNOW
if (enableESPNow && useESPNowSync && (apActive || !WLED_CONNECTED)) {
if (enableESPNow && useESPNowSync && statusESPNow == ESP_NOW_STATE_ON /*&& (apActive || !WLED_CONNECTED)*/) {
partial_packet_t buffer = {'W', 0, (uint8_t)s, {0}};
// send global data
DEBUG_PRINTF("ESP-NOW sending first packet. (%d)\n", 41+3);
DEBUG_PRINTLN(F("ESP-NOW sending first packet."));
memcpy(buffer.data, udpOut, 41);
auto err = quickEspNow.send(ESPNOW_BROADCAST_ADDRESS, reinterpret_cast<const uint8_t*>(&buffer), 41+3);
if (!err) {
// send segment data
buffer.packet++;
size_t packetSize = 0;
int32_t err = 0;
for (size_t i = 0; i < s; i++) {
memcpy(buffer.data + packetSize, &udpOut[41+i*UDP_SEG_SIZE], UDP_SEG_SIZE);
packetSize += UDP_SEG_SIZE;
if (packetSize + UDP_SEG_SIZE < sizeof(buffer.data)/sizeof(uint8_t)) continue;
DEBUG_PRINTF("ESP-NOW sending packet: %d (%d)\n", (int)buffer.packet, packetSize+3);
auto err = quickEspNow.send(ESPNOW_BROADCAST_ADDRESS, reinterpret_cast<const uint8_t*>(&buffer), packetSize+3);
err = quickEspNow.send(ESPNOW_BROADCAST_ADDRESS, reinterpret_cast<const uint8_t*>(&buffer), packetSize+3);
buffer.packet++;
packetSize = 0;
if (err) {
DEBUG_PRINTLN(F("ESP-NOW sending failed."));
break;
}
if (err) break;
}
if (packetSize > 0) {
if (!err && packetSize > 0) {
DEBUG_PRINTF("ESP-NOW sending last packet: %d (%d)\n", (int)buffer.packet, packetSize+3);
auto err = quickEspNow.send(ESPNOW_BROADCAST_ADDRESS, reinterpret_cast<const uint8_t*>(&buffer), packetSize+3);
if (err) {
DEBUG_PRINTLN(F("ESP-NOW sending last failed."));
}
err = quickEspNow.send(ESPNOW_BROADCAST_ADDRESS, reinterpret_cast<const uint8_t*>(&buffer), packetSize+3);
}
if (err) {
DEBUG_PRINTLN(F("ESP-NOW sending packet failed."));
}
}
} else if (udpConnected)
Expand All @@ -187,7 +185,7 @@ void notify(byte callMode, bool followUp)
DEBUG_PRINTLN(F("UDP sending packet."));
IPAddress broadcastIp = ~uint32_t(Network.subnetMask()) | uint32_t(Network.gatewayIP());
notifierUdp.beginPacket(broadcastIp, udpPort);
notifierUdp.write(udpOut, WLEDPACKETSIZE);
notifierUdp.write(udpOut, WLEDPACKETSIZE); // TODO: add actual used buffer size
notifierUdp.endPacket();
}
notificationSentCallMode = callMode;
Expand Down Expand Up @@ -904,24 +902,26 @@ uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, uint8

#ifndef WLED_DISABLE_ESPNOW
// ESP-NOW message receive callback function
// WARNING: ATM clashes with remote.cpp WizMote handling.
void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rssi, bool broadcast) {
sprintf_P(last_signal_src, PSTR("%02x%02x%02x%02x%02x%02x"), address[0], address[1], address[2], address[3], address[4], address[5]);

DEBUG_PRINT(F("ESP-NOW: ")); DEBUG_PRINT(last_signal_src); DEBUG_PRINT(F(" -> ")); DEBUG_PRINTLN(len);
#ifdef WLED_DEBUG
DEBUG_PRINT(F("ESP-NOW: ")); DEBUG_PRINT(last_signal_src); DEBUG_PRINT(F(" -> ")); DEBUG_PRINTLN(len);
for (int i=0; i<len; i++) DEBUG_PRINTF("%02x ", data[i]);
DEBUG_PRINTLN();
#endif

// handle WiZ Mote data
if (data[0] == 0x91 || data[0] == 0x81) {
if (data[0] == 0x91 || data[0] == 0x81 || data[0] == 0x80) {
handleRemote(data, len);
return;
}

partial_packet_t *buffer = reinterpret_cast<partial_packet_t *>(data);
if (len < 3 || !broadcast || buffer->magic != 'W' || !useESPNowSync || WLED_CONNECTED) return;
if (len < 3 || !broadcast || buffer->magic != 'W' || !useESPNowSync || WLED_CONNECTED) {
DEBUG_PRINTLN(F("ESP-NOW unexpected packet, not syncing or connected to WiFi."));
return;
}

static uint8_t *udpIn = nullptr;
static uint8_t packetsReceived = 0; // bitfield (max 5 packets ATM)
Expand Down Expand Up @@ -955,7 +955,7 @@ void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rs

if (segsReceived == buffer->segs) {
// last packet received
if (millis() - lastProcessed > 500) {
if (millis() - lastProcessed > 250) {
DEBUG_PRINTLN(F("ESP-NOW processing complete message."));
parseNotifyPacket(udpIn);
lastProcessed = millis();
Expand Down
8 changes: 2 additions & 6 deletions wled00/wled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ void WLED::initConnection()
#endif

#ifndef WLED_DISABLE_ESPNOW
if (enableESPNow && statusESPNow == ESP_NOW_STATE_ON) {
if (statusESPNow == ESP_NOW_STATE_ON) {
DEBUG_PRINTLN(F("ESP-NOW stopping."));
quickEspNow.stop();
statusESPNow = ESP_NOW_STATE_UNINIT;
Expand Down Expand Up @@ -714,11 +714,6 @@ void WLED::initConnection()
// convert the "serverDescription" into a valid DNS hostname (alphanumeric)
char hostname[25];
prepareHostname(hostname);

#ifdef ESP8266
WiFi.hostname(hostname);
#endif

WiFi.begin(clientSSID, clientPass);
#ifdef ARDUINO_ARCH_ESP32
#if defined(LOLIN_WIFI_FIX) && (defined(ARDUINO_ARCH_ESP32C3) || defined(ARDUINO_ARCH_ESP32S2) || defined(ARDUINO_ARCH_ESP32S3))
Expand All @@ -728,6 +723,7 @@ void WLED::initConnection()
WiFi.setHostname(hostname);
#else
wifi_set_sleep_type((noWifiSleep) ? NONE_SLEEP_T : MODEM_SLEEP_T);
WiFi.hostname(hostname);
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions wled00/wled.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

// version code in format yymmddb (b = daily build)
#define VERSION 2308220
#define VERSION 2308270

//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG
Expand Down Expand Up @@ -463,11 +463,11 @@ WLED_GLOBAL bool hueApplyColor _INIT(true);
WLED_GLOBAL uint16_t serialBaud _INIT(1152); // serial baud rate, multiply by 100

#ifndef WLED_DISABLE_ESPNOW
WLED_GLOBAL bool enableESPNow _INIT(false); // global on/off for ESP-NOW
WLED_GLOBAL byte statusESPNow _INIT(0); // state of ESP-NOW stack (0 uninitialised, 1 ninitialised, 2 error)
WLED_GLOBAL bool useESPNowSync _INIT(false); // use ESP-NOW wireless technology for sync
WLED_GLOBAL char linked_remote[13] _INIT(""); // MAC of ESP-NOW remote (Wiz Mote)
WLED_GLOBAL char last_signal_src[13] _INIT(""); // last seen ESP-NOW sender
WLED_GLOBAL bool enableESPNow _INIT(false); // global on/off for ESP-NOW
WLED_GLOBAL byte statusESPNow _INIT(ESP_NOW_STATE_UNINIT); // state of ESP-NOW stack (0 uninitialised, 1 initialised, 2 error)
WLED_GLOBAL bool useESPNowSync _INIT(false); // use ESP-NOW wireless technology for sync
WLED_GLOBAL char linked_remote[13] _INIT(""); // MAC of ESP-NOW remote (Wiz Mote)
WLED_GLOBAL char last_signal_src[13] _INIT(""); // last seen ESP-NOW sender
#endif

// Time CONFIG
Expand Down
2 changes: 1 addition & 1 deletion wled00/wled_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void serveSettings(AsyncWebServerRequest* request, bool post)
char s2[45] = "";

switch (subPage) {
case SUBPAGE_WIFI : strcpy_P(s, PSTR("WiFi")); strcpy_P(s2, PSTR("Please connect to the new IP (if changed)")); forceReconnect = true; break;
case SUBPAGE_WIFI : strcpy_P(s, PSTR("WiFi")); strcpy_P(s2, PSTR("Please connect to the new IP (if changed)")); break;
case SUBPAGE_LEDS : strcpy_P(s, PSTR("LED")); break;
case SUBPAGE_UI : strcpy_P(s, PSTR("UI")); break;
case SUBPAGE_SYNC : strcpy_P(s, PSTR("Sync")); break;
Expand Down

0 comments on commit 51d62af

Please sign in to comment.