ESP32C3-ULP/src/main.cpp
XPS\Micro 58b386b012 Implementiere WiFi-Integration und Projektverbesserungen
- WiFi-Aktivierung für A/B-Test (bootCount ungerade = WiFi ON, gerade = WiFi OFF)
- Zyklusende nach TOTAL_CYCLES*2 Boots implementiert
- SERIAL_DEBUG-Guards für alle Debug-Ausgaben hinzugefügt
- Secrets-Schutz: config.h in .gitignore, config_example.h als Template
- Funktionsfehler behoben: esp_reset_cause → esp_reset_reason
- Dokumentation aktualisiert (README, Info.md, CLAUDE.md)
- USB-Anforderungen für Upload und Serial Monitor dokumentiert
2026-04-19 11:17:28 +02:00

81 lines
2.0 KiB
C++

#include <Arduino.h>
#include <WiFi.h>
#include <esp_sleep.h>
#include <config.h>
// RTC-Memory für Persistierung über Sleep hinweg
RTC_DATA_ATTR uint32_t bootCount = 0;
void setup() {
#if SERIAL_DEBUG
Serial.begin(115200);
delay(500);
Serial.println("\n\n=== ESP32-C3 Power Measurement ===");
Serial.printf("Boot #%lu\n", bootCount);
Serial.printf("Reset Reason: %d\n", esp_reset_reason());
#endif
bootCount++;
// GPIO initialisieren (optional, für externe Trigger/Status-LED)
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Messdauer aktiv sein
delay(ACTIVE_DURATION_MS);
// Status ausgeben
uint32_t currentCycle = (bootCount - 1) % (TOTAL_CYCLES * 2);
bool wifiEnabled = (bootCount % 2 == 1) ? WIFI_ENABLED_CYCLE_1 : WIFI_ENABLED_CYCLE_2;
#if SERIAL_DEBUG
Serial.printf("Cycle: %lu/%u | WiFi: %s\n", currentCycle + 1, TOTAL_CYCLES * 2,
wifiEnabled ? "ON" : "OFF");
#endif
// WiFi aktivieren falls konfiguriert
if (wifiEnabled) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
uint8_t attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
attempts++;
}
#if SERIAL_DEBUG
Serial.printf("WiFi: %s\n", WiFi.status() == WL_CONNECTED ? "Connected" : "Failed");
#endif
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
#if SERIAL_DEBUG
Serial.printf("Going to sleep for %d seconds...\n", SLEEP_DURATION_S);
Serial.flush();
#endif
// Nach allen Zyklen beenden
if (bootCount >= TOTAL_CYCLES * 2) {
#if SERIAL_DEBUG
Serial.println("Alle Zyklen abgeschlossen. Gerät bleibt wach.");
Serial.flush();
#endif
while (true) { delay(1000); }
}
// Deep Sleep mit Timer-Wakeup
esp_sleep_enable_timer_wakeup(SLEEP_DURATION_S * 1000000ULL);
// GPIO-Wakeup optional: Pin 9 = GPIO9 (RX)
// esp_sleep_enable_ext0_wakeup(GPIO_NUM_9, 0);
digitalWrite(LED_BUILTIN, LOW);
esp_deep_sleep_start();
}
void loop() {
// Code kommt hier nicht an (Deep Sleep)
}