code-sammler-flat-room-group-06b — ESP32 simulation

ESP32-S3 Lichtsteuerung: Schalter/Taster-Eingänge, RGB-LED-Ausgang, Web-UI (ArduinoJson, WebServer). Board-Position korrigiert (war negativ).

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by bmw550i.

Components

Sketch code

//06b Funktionion i.O.
// "s" und "h" wieder über Serial.printf

// ===================================================================
// SKETCH.INO - TEIL 1 (Definitionen, Variablen & Prototypen)
// ===================================================================
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);

#include <esp_now.h>
#include <ArduinoJson.h>
#include <esp_task_wdt.h> 
#include "esp_mac.h" 
#include "config.h"
#include "webpage.h"
#include <stdarg.h> 

#define WDT_TIMEOUT_SECONDS 5 

uint32_t myLocalSequenceNumber = 0;
uint32_t simSequenceCounter = 0; 

bool logToSerialActive = true;    
bool logToEspNowActive = false;   
bool logToWebActive = true;    

#define WEB_LOG_MAX_LINES 15
String webLogBuffer[WEB_LOG_MAX_LINES];
uint8_t webLogBufferIdx = 0;

struct MsgFingerprint { uint16_t srcId; uint32_t seqNum; };
#define MESH_HISTORY_SIZE 20
MsgFingerprint meshHistory[MESH_HISTORY_SIZE];
uint8_t meshHistoryIdx = 0;

unsigned long lastWifiCheckTime = 0;
const unsigned long wifiCheckInterval = 10000; 

// --- COMPILER-FORWARD-PROTOTYPEN ---
void sendBroadcast(LightControlPayload &payload);
void processDebugCommand(const char* cmdStr);
void checkLocalInputs();
void evaluatePrioritiesAndOutputs();
void printSystemConfig(const SystemConfig& cfg);
void printGlobalRegistry();
void handleWebDebugCommand(const char* webCmdStr);
String webProcessor(const String& var);
void handleApiStatus();
void registerWebEndpoints();
void loadConfiguration();
void checkWifiBackgroundEngine();
void processReceivedPacket(const uint8_t* sourceMac, const LightControlPayload& packet);

// ===================================================================
// KORREKTUR IN SKETCH.INO TEIL 2: Speicher-sicheres Heap-Logging
// ===================================================================
void systemLogf(const char* format, ...) {
    // 🛡️ FIX: 'static' verschiebt den 512-Byte-Buffer dauerhaft aus dem engen Stack!
    // Das verhindert den lautlosen Stack-Overflow beim Aufruf von 's' oder 'h'.
    static char logBuffer[512]; 
    
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof(logBuffer), format, args);
    va_end(args);

    if (logToSerialActive) { 
        Serial.print(logBuffer); 
    }

    if (logToWebActive) {
        String cleanStr = String(logBuffer);
        cleanStr.replace("\n", ""); 
        cleanStr.replace("\r", "");
        if(cleanStr.length() > 0) {
            webLogBuffer[webLogBufferIdx] = "[LOG] " + cleanStr;
            webLogBufferIdx = (webLogBufferIdx + 1) % WEB_LOG_MAX_LINES;
        }
    }
}

void processDebugCommand(const char* cmdStr) {
    if (strcmp(cmdStr, "l 0") == 0) { logToSerialActive = false; Serial.println("[SYS] Serial-Log AUS"); }
    else if (strcmp(cmdStr, "l 1") == 0) { logToSerialActive = true; systemLogf("[SYS] Serial-Log EIN\n"); }
}

uint16_t calculateCRC16(const uint8_t* data, size_t length) {
    uint16_t crc = 0xFFFF;
    for (size_t i = 0; i < length; i++) {

More projects by bmw550i