From 73b8d162c71a30028bf5b4c8a34ab2825061758b Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 25 Sep 2019 06:40:24 +0200 Subject: [PATCH] Metrics logging now includes average time taken per frame. Also, metrics now come in a single JSON payload to /metrics. --- include/my_mqtt.h | 1 + src/mqtt.cpp | 20 +++++++++++--------- src/pitrix.cpp | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/include/my_mqtt.h b/include/my_mqtt.h index fe97c0e..0d4503c 100644 --- a/include/my_mqtt.h +++ b/include/my_mqtt.h @@ -18,6 +18,7 @@ void mqtt_setup(); void mqtt_loop(); void mqtt_publish(const char* topic, int number); +void mqtt_publish(const char* topic, const char* message); void mqtt_log(const char* message); void mqtt_log(int number); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index f3bac73..1b084f4 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -23,7 +23,10 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { pl[length] = '\0'; String payload((char*)pl); String topic (original_topic); - if (topic.compareTo(MQTT_TOPIC "log")==0) return; + if (topic.compareTo(MQTT_TOPIC "log")==0 || topic.compareTo(MQTT_TOPIC "status") || topic.compareTo(MQTT_TOPIC "metrics")) { + // Return our own messages + return; + } LOGln("MQTT * In: %s = %s", topic.c_str(), payload.c_str()); if (topic.startsWith(MQTT_TOPIC_WEATHER)) { // Weather stuff @@ -51,11 +54,6 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning - if (topic.compareTo("free_heap")==0 || topic.compareTo("uptime")==0 || topic.compareTo("status")==0 || topic.compareTo("fps")==0) { - // Ignore our own messages. - return; - } - if(topic.compareTo("mode")==0) { LOGln("MQTT * Changing mode..."); bool result = change_current_effect(payload); @@ -125,11 +123,15 @@ void mqtt_loop() { String mqtt_log_str = String(); void mqtt_publish(const char* topic, int number) { - char t[127]; - sprintf(t, MQTT_TOPIC "%s", topic); char b[32]; sprintf(b, "%d", number); - mqtt_client.publish(t, b); + mqtt_publish(topic, b); +} + +void mqtt_publish(const char* topic, const char* message) { + char t[127]; + sprintf(t, MQTT_TOPIC "%s", topic); + mqtt_client.publish(t, message); } void mqtt_log(const char* message) { diff --git a/src/pitrix.cpp b/src/pitrix.cpp index d9f4c5c..5bf98b2 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -19,6 +19,10 @@ char hostname[30]; // defined as extern in prototypes.h #ifdef RECORDER_ENABLE Recorder* recorder; #endif +#ifdef MQTT_REPORT_METRICS +uint16_t metrics_frame_count = 0; +unsigned long metrics_frame_time = 0; +#endif void setup() { Serial.begin(74880); @@ -91,6 +95,10 @@ void loop() { effect_clock.loop(current_effect->clock_as_mask(), CRGB(0xFFFFFF), CRGB(0x000000)); } FastLED.show(); + #ifdef MQTT_REPORT_METRICS + metrics_frame_count++; + metrics_frame_time += (millis() - loop_started_at); + #endif #ifdef RECORDER_ENABLE recorder->loop(); @@ -99,9 +107,11 @@ void loop() { #if defined(MQTT_ENABLE) && defined(MQTT_REPORT_METRICS) EVERY_N_SECONDS(15) { - mqtt_publish("free_heap", ESP.getFreeHeap()); - mqtt_publish("uptime", millis()/1000); - mqtt_publish("fps", FastLED.getFPS()); + char json[120]; + snprintf(json, 120, "{\"free_heap\":%u, \"uptime\":%lu, \"fps\":%d, \"time_per_frame\":%lu}", ESP.getFreeHeap(), millis()/1000, FastLED.getFPS(), metrics_frame_time / metrics_frame_count); + mqtt_publish("metrics", json); + metrics_frame_count = 0; + metrics_frame_time = 0; } #endif // MQTT_REPORT_METRICS