From 8c9e9e2e2e222bbd0e4feb1464fb8707b1e1ca54 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Tue, 18 Jun 2019 18:17:48 +0200 Subject: [PATCH] MQTT: Use new effect structure and logging. --- include/my_mqtt.h | 2 ++ src/mqtt.cpp | 36 ++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/my_mqtt.h b/include/my_mqtt.h index 507588c..fe97c0e 100644 --- a/include/my_mqtt.h +++ b/include/my_mqtt.h @@ -23,12 +23,14 @@ void mqtt_log(const char* message); void mqtt_log(int number); void mqtt_log(long unsigned int number); void mqtt_log(long int number); +void mqtt_log(size_t number); void mqtt_log(String str); void mqtt_log_ln(const char* message); void mqtt_log_ln(int number); void mqtt_log_ln(long unsigned int number); void mqtt_log_ln(long int number); +void mqtt_log_ln(size_t number); void mqtt_log_ln(String str); diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 9856693..34dcff9 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -7,7 +7,6 @@ #include "my_wifi.h" #include -#include "EffectEntry.h" #include "Effect.h" #include "effects.h" #include "functions.h" @@ -24,11 +23,11 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { String payload((char*)pl); String topic (original_topic); if (topic.compareTo(MQTT_TOPIC "log")==0) return; - LOG("MQTT * Received data for topic "); LOG(topic); LOG(" with payload "); LOGln(payload); + LOGln("MQTT * In: %s = %s", topic.c_str(), payload.c_str()); if (topic.startsWith(MQTT_TOPIC_WEATHER)) { // Weather stuff topic.remove(0, strlen(MQTT_TOPIC_WEATHER)); - LOG("MQTT * Weather stuff. Remaining topic: "); LOGln(topic.c_str()); + LOGln("MQTT * Weather stuff."); if (topic.startsWith("icons/")) { topic.remove(0, 6); uint8_t id = topic.toInt(); @@ -36,7 +35,7 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { uint8_t val = payload.toInt(); if (val==0) return; weather_icon_ids[id] = val; - LOG("Set weather_icon_ids["); LOG(id); LOG("] to value "); LOGln(val); + LOGln("Set weather_icon_ids[%d] to value %d", id, val); } else if (topic.startsWith("temperatures/")) { topic.remove(0, 13); uint8_t id = topic.toInt(); @@ -44,14 +43,13 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { uint8_t val = payload.toInt(); if (val==0) return; weather_temperatures[id] = val; - LOG("Set weather_temperatures["); LOG(id); LOG("] to value "); LOGln(val); + LOGln("Set weather_temperatures[%d] to value %d", id, val); } return; } topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning - LOG("MQTT * Remaining topic is: "); LOGln(topic.c_str()); if (topic.compareTo("free_heap")==0 || topic.compareTo("uptime")==0 || topic.compareTo("status")==0 || topic.compareTo("fps")==0) { // Ignore our own messages. return; @@ -59,38 +57,34 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) { if(topic.compareTo("mode")==0) { LOGln("MQTT * Changing mode..."); - for (int i=0; isize(); i++) { - EffectEntry e = effects->get(i); - if (payload.compareTo(e.name)==0) { - //Serial.printf("Effect found in mqtt_callback: %p\n", (void *)&e->effect); - current_effect->stop(); - current_effect = e.effect; - current_effect->start(); - return; - } - } + change_current_effect(payload); + return; } else if (topic.compareTo("reboot")==0) { LOGln("MQTT * Rebooting"); ESP.restart(); + return; // Will never be reached, but anyway... } long value = payload.toInt(); - LOG("MQTT * payload after converting to a number: "); LOGln(value); + LOGln("MQTT * Payload as number: %d", value); if (topic.compareTo("brightness")==0) { if (value > 0 && value <= 255) { LOGln("MQTT * Changing brightness..."); FastLED.setBrightness(value); } else { - LOG("MQTT * Ignoring brightness change: Value "); LOG(value); LOGln(" is out of bounds (0