MQTT: Use new effect structure and logging.

This commit is contained in:
Fabian Schlenz 2019-06-18 18:17:48 +02:00
parent f1424d0277
commit 8c9e9e2e2e
2 changed files with 18 additions and 20 deletions

View File

@ -23,12 +23,14 @@ void mqtt_log(const char* message);
void mqtt_log(int number); void mqtt_log(int number);
void mqtt_log(long unsigned int number); void mqtt_log(long unsigned int number);
void mqtt_log(long int number); void mqtt_log(long int number);
void mqtt_log(size_t number);
void mqtt_log(String str); void mqtt_log(String str);
void mqtt_log_ln(const char* message); void mqtt_log_ln(const char* message);
void mqtt_log_ln(int number); void mqtt_log_ln(int number);
void mqtt_log_ln(long unsigned int number); void mqtt_log_ln(long unsigned int number);
void mqtt_log_ln(long int number); void mqtt_log_ln(long int number);
void mqtt_log_ln(size_t number);
void mqtt_log_ln(String str); void mqtt_log_ln(String str);

View File

@ -7,7 +7,6 @@
#include "my_wifi.h" #include "my_wifi.h"
#include <PubSubClient.h> #include <PubSubClient.h>
#include "EffectEntry.h"
#include "Effect.h" #include "Effect.h"
#include "effects.h" #include "effects.h"
#include "functions.h" #include "functions.h"
@ -24,11 +23,11 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
String payload((char*)pl); String payload((char*)pl);
String topic (original_topic); String topic (original_topic);
if (topic.compareTo(MQTT_TOPIC "log")==0) return; 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)) { if (topic.startsWith(MQTT_TOPIC_WEATHER)) {
// Weather stuff // Weather stuff
topic.remove(0, strlen(MQTT_TOPIC_WEATHER)); 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/")) { if (topic.startsWith("icons/")) {
topic.remove(0, 6); topic.remove(0, 6);
uint8_t id = topic.toInt(); 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(); uint8_t val = payload.toInt();
if (val==0) return; if (val==0) return;
weather_icon_ids[id] = val; 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/")) { } else if (topic.startsWith("temperatures/")) {
topic.remove(0, 13); topic.remove(0, 13);
uint8_t id = topic.toInt(); 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(); uint8_t val = payload.toInt();
if (val==0) return; if (val==0) return;
weather_temperatures[id] = val; 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; return;
} }
topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning 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) { if (topic.compareTo("free_heap")==0 || topic.compareTo("uptime")==0 || topic.compareTo("status")==0 || topic.compareTo("fps")==0) {
// Ignore our own messages. // Ignore our own messages.
return; return;
@ -59,38 +57,34 @@ void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
if(topic.compareTo("mode")==0) { if(topic.compareTo("mode")==0) {
LOGln("MQTT * Changing mode..."); LOGln("MQTT * Changing mode...");
for (int i=0; i<effects->size(); i++) { change_current_effect(payload);
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; return;
}
}
} else if (topic.compareTo("reboot")==0) { } else if (topic.compareTo("reboot")==0) {
LOGln("MQTT * Rebooting"); LOGln("MQTT * Rebooting");
ESP.restart(); ESP.restart();
return; // Will never be reached, but anyway...
} }
long value = payload.toInt(); 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 (topic.compareTo("brightness")==0) {
if (value > 0 && value <= 255) { if (value > 0 && value <= 255) {
LOGln("MQTT * Changing brightness..."); LOGln("MQTT * Changing brightness...");
FastLED.setBrightness(value); FastLED.setBrightness(value);
} else { } else {
LOG("MQTT * Ignoring brightness change: Value "); LOG(value); LOGln(" is out of bounds (0<x<=255)."); LOGln("MQTT * Ignoring brightness change: Value %d is out of bounds (0<x<=255).", value);
} }
return;
} }
LOGln("MQTT * Topic was not processed.");
} }
boolean mqtt_connect() { boolean mqtt_connect() {
LOG("MQTT * Connecting to MQTT server with client id "); LOGln(hostname); LOG("MQTT * Connecting to MQTT server with client id %s", hostname);
if (mqtt_client.connect(hostname, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) { if (mqtt_client.connect(hostname, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) {
LOGln("MQTT * Connected."); LOGln("MQTT * Connected.");
mqtt_client.publish(MQTT_TOPIC "status", "ONLINE"); mqtt_client.publish(MQTT_TOPIC "status", "ONLINE", true);
mqtt_client.subscribe(MQTT_TOPIC "+"); mqtt_client.subscribe(MQTT_TOPIC "+");
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#"); mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
} }
@ -137,6 +131,7 @@ void mqtt_log(int number) {
void mqtt_log(long unsigned int number) { mqtt_log(String(number).c_str()); } void mqtt_log(long unsigned int number) { mqtt_log(String(number).c_str()); }
void mqtt_log(long int number) { mqtt_log(String(number).c_str()); } void mqtt_log(long int number) { mqtt_log(String(number).c_str()); }
void mqtt_log(size_t number) { mqtt_log(String(number).c_str()); }
void mqtt_log(String str) { mqtt_log(str.c_str()); } void mqtt_log(String str) { mqtt_log(str.c_str()); }
@ -146,6 +141,7 @@ void mqtt_log_ln(int number) {
void mqtt_log_ln(long unsigned int number) { mqtt_log_ln(String(number).c_str()); } void mqtt_log_ln(long unsigned int number) { mqtt_log_ln(String(number).c_str()); }
void mqtt_log_ln(long int number) { mqtt_log_ln(String(number).c_str()); } void mqtt_log_ln(long int number) { mqtt_log_ln(String(number).c_str()); }
void mqtt_log_ln(size_t number) { mqtt_log_ln(String(number).c_str()); }
void mqtt_log_ln(String str) { mqtt_log_ln(str.c_str()); } void mqtt_log_ln(String str) { mqtt_log_ln(str.c_str()); }