Skip to content

Latest commit

 

History

History
143 lines (107 loc) · 4.88 KB

File metadata and controls

143 lines (107 loc) · 4.88 KB

Syslog Crash Fix - ESP32 Weather Logger

The Problem 🐛

When you enabled syslog debugging with #define ENABLE_SYSLOG_DEBUG true, the program crashed immediately after printing the syslog server IP address.

What was happening:

1. setup() starts
2. setSyslogServer() is called → prints "Syslog server: 192.168.1.100:514" ✅
3. First DEBUG_PRINTLN() is called → "ESP32-S3 Weather Logger Starting"
4. println() tries to send UDP packet to syslog server 💥 CRASH!
5. WiFi not connected yet → WiFiUDP operations fail catastrophically

Root Cause: The syslog implementation tried to send UDP packets immediately when DEBUG_PRINTLN() was called, but this happened in setup() BEFORE WiFi was connected. Attempting UDP operations without an initialized WiFi connection causes a crash on ESP32.

The Solution ✅

Added a WiFi connection check before attempting to send syslog messages:

// Syslog output (send immediately - but only if WiFi is connected!)
if (syslogEnabled && syslogUdp && syslogServer != IPAddress(0, 0, 0, 0)) {
  // Check if WiFi is connected before trying to send UDP
  if (WiFi.status() == WL_CONNECTED) {
    // RFC 3164 Syslog format: <PRI>TIMESTAMP HOSTNAME MESSAGE
    String syslogMsg = "<134>" + syslogHostname + " ESP32: " + message;
    
    syslogUdp->beginPacket(syslogServer, syslogPort);
    syslogUdp->write((const uint8_t*)syslogMsg.c_str(), syslogMsg.length());
    syslogUdp->endPacket();
  }
  // If WiFi not connected, silently skip syslog (messages still go to Serial/Remote)
}

Changes Made:

  1. ESP32_IoT_Utils.cpp - Added WiFi.status() == WL_CONNECTED check before sending syslog packets
  2. ESP32_IoT_Utils.h - Added explicit #include <WiFi.h> for WiFi status checking

How It Works Now 🎯

Before WiFi Connection:

  • ✅ Serial debug works (if enabled)
  • ✅ Remote debug buffered (sent after WiFi connects)
  • ⏭️ Syslog silently skipped (no crash!)

After WiFi Connection:

  • ✅ Serial debug works
  • ✅ Remote debug works
  • ✅ Syslog works (sends UDP packets)

Expected Behavior

=== Debug Logger Initialized ===
Serial Debug: ENABLED
Remote Debug: disabled
Syslog Debug: ENABLED
================================

Syslog server: 192.168.1.100:514  ← Prints successfully

=== ESP32-S3 Weather Logger Starting ===  ← No longer crashes!
Note: ESP32-S3 has BLE only (no Bluetooth Classic Serial)
Initializing HDC302x sensor on Wire1 (STEMMA QT)...
HDC302x sensor initialized!
...
Connecting to Adafruit IO...
.........
Adafruit IO connected!  ← From this point on, syslog messages are sent!

Syslog Message Timeline

Phase Serial Remote Syslog
Setup (before WiFi) ✅ Works ⏸️ Buffered ⏭️ Skipped
After WiFi connects ✅ Works ✅ Works ✅ Works
After disconnect ✅ Works ❌ Offline ❌ Offline

Alternative: Defer Syslog Initialization

If you want ALL messages to go to syslog (including pre-WiFi setup messages), you could modify the sketch to:

  1. Buffer messages before WiFi (like remote debug does)
  2. Send buffered messages after WiFi connects
  3. Send live messages after that

But the current solution is simpler and works well for most use cases - early setup messages go to Serial, and once WiFi is up, syslog takes over.

Testing Your Syslog Setup 🧪

  1. Set up a syslog server (examples: rsyslog on Linux, Kiwi Syslog on Windows)
  2. Configure in sketch:
    #define ENABLE_SYSLOG_DEBUG true
    #define SYSLOG_SERVER_IP 192, 168, 1, 100  // Your server IP
  3. Upload and monitor:
    • Serial Monitor shows all messages
    • Syslog server receives messages after WiFi connects
    • No crashes!

Syslog Message Format

Messages follow RFC 3164 format:

<134>ESP32-Weather ESP32: Temperature: 72.50 °F

Where:

  • <134> = Priority (Facility 16/local0, Severity 6/info)
  • ESP32-Weather = Hostname (configurable)
  • ESP32: = Application identifier
  • Rest = Your debug message

Benefits of Multi-Output Debugging

With Serial + Remote + Syslog working together:

  1. Development - Use Serial for instant feedback
  2. Deployed - Use Remote (Adafruit IO) to check battery-powered sensors
  3. Integration - Use Syslog to integrate with existing monitoring systems
  4. Redundancy - If one output fails, you still have the others

Files Changed

  • ESP32_IoT_Utils.cpp - Added WiFi.status() check before syslog UDP operations
  • ESP32_IoT_Utils.h - Added WiFi.h include

Pro Tip: You can enable/disable any combination of debug outputs at runtime without recompiling by changing the #define values:

#define ENABLE_SERIAL_DEBUG true   // Local USB debugging
#define ENABLE_REMOTE_DEBUG true   // Cloud logging (Adafruit IO)
#define ENABLE_SYSLOG_DEBUG true   // Network logging (centralized)

All three can be enabled simultaneously for maximum visibility!