#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
// WiFi-Konfiguration
const char* ssid = "Dein_WLAN_SSID";
const char* password = "Dein_WLAN_Passwort";
// WebSocket-Server
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
volatile int impulseCount = 0;
volatile unsigned long lastImpulseTime = 0;
volatile unsigned long impulseInterval = 0;
void IRAM_ATTR handleImpulse() {
unsigned long currentTime = micros();
impulseInterval = currentTime - lastImpulseTime;
lastImpulseTime = currentTime;
impulseCount++;
}
void notifyClients() {
// Berechnung der Impulsfrequenz
float frequency = (impulseInterval > 0) ? (1000000.0 / impulseInterval) : 0;
// JSON-Payload erstellen
String json = "{\"impulseCount\":" + String(impulseCount) +
",\"frequency\":" + String(frequency) + "}";
ws.textAll(json);
}
void setup() {
Serial.begin(115200);
pinMode(4, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(4), handleImpulse, FALLING);
// WLAN-Verbindung
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Verbinde mit WLAN...");
}
Serial.println("Verbunden! IP-Adresse: " + WiFi.localIP().toString());
// WebSocket einrichten
ws.onEvent([](AsyncWebSocket * server, AsyncWebSocketClient * client,
AwsEventType type, void * arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.println("WebSocket verbunden");
}
});
server.addHandler(&ws);
server.begin();
}
void loop() {
notifyClients();
delay(1000); // Update alle 1 Sekunde
}
Loading
xiao-esp32-c3
xiao-esp32-c3