Skip to content

Commit

Permalink
Add simple RAM profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Nov 28, 2024
1 parent 8a42325 commit 7fffe0a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 2 additions & 3 deletions examples/arduino/w5500-mqtt/mongoose_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions examples/arduino/w5500-mqtt/w5500-mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 7fffe0a

Please sign in to comment.