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(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);

View File

@ -7,7 +7,6 @@
#include "my_wifi.h"
#include <PubSubClient.h>
#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; i<effects->size(); 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<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() {
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)) {
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_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 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()); }
@ -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 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()); }