Update example

This commit is contained in:
Mathieu Carbou
2024-10-16 22:21:29 +02:00
parent 7923d93336
commit 5aba428151

View File

@@ -1,3 +1,10 @@
/**
*
* Connect to AP and run in bash:
*
* > while true; do echo "PING"; sleep 0.1; done | websocat ws://192.168.4.1/ws
*
*/
#include <Arduino.h> #include <Arduino.h>
#ifdef ESP8266 #ifdef ESP8266
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
@@ -7,6 +14,12 @@
#endif #endif
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <list>
size_t msgCount = 0;
size_t window = 100;
std::list<uint32_t> times;
void connect_wifi() { void connect_wifi() {
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
WiFi.softAP("esp-captive"); WiFi.softAP("esp-captive");
@@ -48,9 +61,18 @@ void printStackSize() {
} }
void onWsEventEmpty(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) { void onWsEventEmpty(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
msgCount++;
Serial.printf("count: %d\n", msgCount);
times.push_back(millis());
while (times.size() > window)
times.pop_front();
if (times.size() == window)
Serial.printf("%f req/s\n", 1000.0 * window / (times.back() - times.front()));
printStackSize(); printStackSize();
// Just answer
client->text("PONGTEST"); client->text("PONG");
} }
void serve_upload(AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len, bool final) { void serve_upload(AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len, bool final) {
@@ -88,21 +110,18 @@ void setup() {
String msg = ""; String msg = "";
uint32_t count = 0; uint32_t count = 0;
void loop() { void loop() {
// ws.cleanupClients();
ws.cleanupClients(); // count += 1;
// // Concatenate some string, and clear it after some time
count += 1; // static unsigned long millis_string = millis();
// if (millis() - millis_string > 1) {
// Concatenate some string, and clear it after some time // millis_string += 100;
static unsigned long millis_string = millis(); // if (count % 100 == 0) {
if (millis() - millis_string > 1) { // // printStackSize();
millis_string += 100; // // Serial.print(msg);
if (count % 100 == 0) { // msg = String();
// printStackSize(); // } else {
// Serial.print(msg); // msg += 'T';
msg = String(); // }
} else { // }
msg += 'T';
}
}
} }