Skip to content

Commit 8143be2

Browse files
authored
Merge pull request wled#2870 from Aircoookie/net_debug
UDP Network debugging
2 parents e5f9cfd + 1d8c9ac commit 8143be2

File tree

5 files changed

+134
-9
lines changed

5 files changed

+134
-9
lines changed

wled00/bus_manager.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb);
1515
void colorRGBtoRGBW(byte* rgb);
1616

1717
// enable additional debug output
18+
#if defined(WLED_DEBUG_HOST)
19+
#define DEBUGOUT NetDebug
20+
#else
21+
#define DEBUGOUT Serial
22+
#endif
23+
1824
#ifdef WLED_DEBUG
1925
#ifndef ESP8266
2026
#include <rom/rtc.h>
2127
#endif
22-
#define DEBUG_PRINT(x) Serial.print(x)
23-
#define DEBUG_PRINTLN(x) Serial.println(x)
24-
#define DEBUG_PRINTF(x...) Serial.printf(x)
28+
#define DEBUG_PRINT(x) DEBUGOUT.print(x)
29+
#define DEBUG_PRINTLN(x) DEBUGOUT.println(x)
30+
#define DEBUG_PRINTF(x...) DEBUGOUT.printf(x)
2531
#else
2632
#define DEBUG_PRINT(x)
2733
#define DEBUG_PRINTLN(x)

wled00/net_debug.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "wled.h"
2+
3+
#ifdef WLED_DEBUG_HOST
4+
5+
NetworkDebugPrinter NetDebug;
6+
7+
void NetworkDebugPrinter::print(const char *s, bool newline) {
8+
if (!WLED_CONNECTED || !udpConnected || s == nullptr) return;
9+
10+
if (!debugPrintHostIP && !debugPrintHostIP.fromString(netDebugPrintHost)) {
11+
#ifdef ESP8266
12+
WiFi.hostByName(netDebugPrintHost, debugPrintHostIP, 750);
13+
#else
14+
#ifdef WLED_USE_ETHERNET
15+
ETH.hostByName(netDebugPrintHost, debugPrintHostIP);
16+
#else
17+
WiFi.hostByName(netDebugPrintHost, debugPrintHostIP);
18+
#endif
19+
#endif
20+
}
21+
22+
WiFiUDP debugUdp;
23+
debugUdp.beginPacket(debugPrintHostIP, netDebugPrintPort);
24+
debugUdp.write(reinterpret_cast<const uint8_t *>(s), strlen(s));
25+
if (newline) debugUdp.write('\n');
26+
debugUdp.endPacket();
27+
}
28+
29+
void NetworkDebugPrinter::print(const __FlashStringHelper* s, bool newline) {
30+
char buf[512];
31+
strncpy_P(buf, (PGM_P)s, 512);
32+
print(buf, newline);
33+
}
34+
35+
void NetworkDebugPrinter::print(String s) {
36+
print(s.c_str());
37+
}
38+
39+
void NetworkDebugPrinter::print(unsigned int n, bool newline) {
40+
char s[10];
41+
snprintf_P(s, sizeof(s), PSTR("%d"), n);
42+
s[9] = '\0';
43+
print(s, newline);
44+
}
45+
46+
void NetworkDebugPrinter::println() {
47+
print("", true);
48+
}
49+
50+
void NetworkDebugPrinter::println(const char *s) {
51+
print(s, true);
52+
}
53+
54+
void NetworkDebugPrinter::println(const __FlashStringHelper* s) {
55+
print(s, true);
56+
}
57+
58+
void NetworkDebugPrinter::println(String s) {
59+
print(s.c_str(), true);
60+
}
61+
62+
void NetworkDebugPrinter::println(unsigned int n) {
63+
print(n, true);
64+
}
65+
66+
void NetworkDebugPrinter::printf(const char *fmt...) {
67+
va_list args;
68+
va_start(args, fmt);
69+
char s[1024];
70+
vsnprintf(s, sizeof(s), fmt, args);
71+
va_end(args);
72+
print(s);
73+
}
74+
75+
#endif

wled00/net_debug.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef WLED_NET_DEBUG_H
2+
#define WLED_NET_DEBUG_H
3+
4+
#include <WString.h>
5+
#include <WiFiUdp.h>
6+
7+
class NetworkDebugPrinter {
8+
private:
9+
IPAddress debugPrintHostIP;
10+
public:
11+
void print(const char *s, bool newline = false);
12+
void print(const __FlashStringHelper* s, bool newline = false);
13+
void print(String s);
14+
void print(unsigned int n, bool newline = false);
15+
void println();
16+
void println(const char *s);
17+
void println(const __FlashStringHelper* s);
18+
void println(String s);
19+
void println(unsigned int n);
20+
void printf(const char *fmt, ...);
21+
};
22+
23+
extern NetworkDebugPrinter NetDebug;
24+
25+
#endif

wled00/wled.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@
9595
#include "my_config.h"
9696
#endif
9797

98+
#ifdef WLED_DEBUG_HOST
99+
#include "net_debug.h"
100+
#endif
101+
98102
#include <ESPAsyncWebServer.h>
99103
#ifdef WLED_ADD_EEPROM_SUPPORT
100104
#include <EEPROM.h>
@@ -669,23 +673,37 @@ WLED_GLOBAL StaticJsonDocument<JSON_BUFFER_SIZE> doc;
669673
WLED_GLOBAL volatile uint8_t jsonBufferLock _INIT(0);
670674

671675
// enable additional debug output
676+
#if defined(WLED_DEBUG_HOST)
677+
// On the host side, use netcat to receive the log statements: nc -l 7868 -u
678+
// use -D WLED_DEBUG_HOST='"192.168.xxx.xxx"' or FQDN within quotes
679+
#define DEBUGOUT NetDebug
680+
WLED_GLOBAL char netDebugPrintHost[33] _INIT(WLED_DEBUG_HOST);
681+
#if defined(WLED_DEBUG_NET_PORT)
682+
WLED_GLOBAL int netDebugPrintPort _INIT(WLED_DEBUG_PORT);
683+
#else
684+
WLED_GLOBAL int netDebugPrintPort _INIT(7868);
685+
#endif
686+
#else
687+
#define DEBUGOUT Serial
688+
#endif
689+
672690
#ifdef WLED_DEBUG
673691
#ifndef ESP8266
674692
#include <rom/rtc.h>
675693
#endif
676-
#define DEBUG_PRINT(x) Serial.print(x)
677-
#define DEBUG_PRINTLN(x) Serial.println(x)
678-
#define DEBUG_PRINTF(x...) Serial.printf(x)
694+
#define DEBUG_PRINT(x) DEBUGOUT.print(x)
695+
#define DEBUG_PRINTLN(x) DEBUGOUT.println(x)
696+
#define DEBUG_PRINTF(x...) DEBUGOUT.printf(x)
679697
#else
680698
#define DEBUG_PRINT(x)
681699
#define DEBUG_PRINTLN(x)
682700
#define DEBUG_PRINTF(x...)
683701
#endif
684702

685703
#ifdef WLED_DEBUG_FS
686-
#define DEBUGFS_PRINT(x) Serial.print(x)
687-
#define DEBUGFS_PRINTLN(x) Serial.println(x)
688-
#define DEBUGFS_PRINTF(x...) Serial.printf(x)
704+
#define DEBUGFS_PRINT(x) DEBUGOUT.print(x)
705+
#define DEBUGFS_PRINTLN(x) DEBUGOUT.println(x)
706+
#define DEBUGFS_PRINTF(x...) DEBUGOUT.printf(x)
689707
#else
690708
#define DEBUGFS_PRINT(x)
691709
#define DEBUGFS_PRINTLN(x)

wled00/wled00.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@
227227
<ClCompile Include="wled_eeprom.cpp" />
228228
<ClCompile Include="wled_server.cpp" />
229229
<ClCompile Include="xml.cpp" />
230+
<ClCompile Include="net_debug.cpp" />
230231
</ItemGroup>
231232
<PropertyGroup>
232233
<DebuggerFlavor>VisualMicroDebugger</DebuggerFlavor>

0 commit comments

Comments
 (0)