From 7fffe0abaa6a71a57a7f5dad82d4ef5083ef4352 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 28 Nov 2024 13:11:04 +0000 Subject: [PATCH] Add simple RAM profiler --- examples/arduino/w5500-mqtt/mongoose_config.h | 5 ++--- examples/arduino/w5500-mqtt/w5500-mqtt.ino | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/arduino/w5500-mqtt/mongoose_config.h b/examples/arduino/w5500-mqtt/mongoose_config.h index 31a4d635..782aa509 100644 --- a/examples/arduino/w5500-mqtt/mongoose_config.h +++ b/examples/arduino/w5500-mqtt/mongoose_config.h @@ -15,8 +15,7 @@ #define mkdir(a, b) (-1) #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_CUSTOM_MILLIS 1 +#define MG_ENABLE_CUSTOM_RANDOM 1 +// #define MG_TLS MG_TLS_BUILTIN #define MG_IO_SIZE 128 -// Enable TLS -// #define MG_TLS MG_TLS_BUILTIN -// #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/examples/arduino/w5500-mqtt/w5500-mqtt.ino b/examples/arduino/w5500-mqtt/w5500-mqtt.ino index fa9432d1..48307651 100644 --- a/examples/arduino/w5500-mqtt/w5500-mqtt.ino +++ b/examples/arduino/w5500-mqtt/w5500-mqtt.ino @@ -20,6 +20,11 @@ struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface uint64_t mg_millis(void) { return millis(); } +bool mg_random(void *buf, size_t len) { // For TLS + uint8_t *p = (uint8_t *) buf; + while (len--) *p++ = (unsigned char) (rand() & 255); + return true; +} void mqtt_publish(const char *message) { struct mg_mqtt_opts opts = {}; @@ -28,6 +33,15 @@ void mqtt_publish(const char *message) { if (mqtt_connection) mg_mqtt_pub(mqtt_connection, &opts); } +// Crude function to get available RAM, for quick profiling +size_t getFreeRAM(void) { + size_t size = 0, increment = 100; + void *p; + while ((p = malloc(size)) != NULL) free(p), size += increment; + return size; +} + +// Implement LED control over MQTT: "on" and "off" commands void handle_command(struct mg_str msg) { if (msg.len == 3 && memcmp(msg.buf, "off", 3) == 0) { digitalWrite(LED_PIN, LOW); @@ -36,6 +50,7 @@ void handle_command(struct mg_str msg) { digitalWrite(LED_PIN, HIGH); mqtt_publish("done - on"); } + MG_INFO(("Free RAM: %u bytes", getFreeRAM())); } static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) {