Handling of incoming MQTT messages improved.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
83254f2eaa
commit
2072877159
@ -29,7 +29,6 @@
|
||||
#define MQTT_USER "..."
|
||||
#define MQTT_PASS "..."
|
||||
#define MQTT_TOPIC "pitrix/" // MQTT-Topic to listen to. Must not start with a slash, but must end with one."
|
||||
#define MQTT_TOPIC_LOG "pitrix/log"
|
||||
#define MQTT_REPORT_METRICS
|
||||
#define MQTT_TOPIC_WEATHER "accuweather/pitrix/" // MQTT topic to listen for weather data. Must not start with a slash, but must end with one.
|
||||
|
||||
|
@ -22,10 +22,14 @@ void mqtt_publish(const char* topic, int number);
|
||||
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(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(String str);
|
||||
|
||||
|
||||
void mqtt_log_send(const char* message);
|
||||
|
83
src/mqtt.cpp
83
src/mqtt.cpp
@ -25,51 +25,70 @@ long mqtt_last_reconnect_attempt = 0;
|
||||
uint8_t weather_icon_ids[6] = {0, 0, 0, 0, 0, 0};
|
||||
int8_t weather_temperatures[6] = {0, 0, 0, 0, 0, 0};
|
||||
|
||||
void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) {
|
||||
void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
|
||||
pl[length] = '\0';
|
||||
char* payload = (char*)pl;
|
||||
LOG("MQTT * Received data for topic "); LOGln(complete_topic);
|
||||
|
||||
if (strncmp(complete_topic, MQTT_TOPIC_WEATHER, strlen(MQTT_TOPIC_WEATHER))==0) {
|
||||
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);
|
||||
if (topic.startsWith(MQTT_TOPIC_WEATHER)) {
|
||||
// Weather stuff
|
||||
complete_topic += strlen(MQTT_TOPIC_WEATHER);
|
||||
if (strncmp(complete_topic, "icons/", 6)==0) {
|
||||
complete_topic += 6; // strlen("icons/")=6
|
||||
uint8_t id = complete_topic[0] - '0'; // ID is 1 digit. Substract '0' from it to get it as number.
|
||||
topic.remove(0, strlen(MQTT_TOPIC_WEATHER));
|
||||
LOG("MQTT * Weather stuff. Remaining topic: "); LOGln(topic.c_str());
|
||||
if (topic.startsWith("icons/")) {
|
||||
topic.remove(0, 6);
|
||||
uint8_t id = topic.toInt();
|
||||
if (id>=6) return;
|
||||
weather_icon_ids[id] = atol(payload);
|
||||
} else if (strncmp(complete_topic, "temperatures/", 13)==0) {
|
||||
complete_topic += 13; // strlen("temperatures/")=13
|
||||
uint8_t id = complete_topic[0] - '0'; // ID is 1 digit. Substract '0' from it to get it as number.
|
||||
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);
|
||||
} else if (topic.startsWith("temperatures/")) {
|
||||
topic.remove(0, 13);
|
||||
uint8_t id = topic.toInt();
|
||||
if (id>=6) return;
|
||||
weather_temperatures[id] = atol(payload);
|
||||
uint8_t val = payload.toInt();
|
||||
if (val==0) return;
|
||||
weather_temperatures[id] = val;
|
||||
LOG("Set weather_temperatures["); LOG(id); LOG("] to value "); LOGln(val);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
char* topic = complete_topic + strlen(MQTT_TOPIC); // Strip MQTT_TOPIC from the beginning
|
||||
topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning
|
||||
|
||||
// Here, payload is a char* (but has to be casted).
|
||||
if(strcmp(topic, "mode")==0) {
|
||||
LOG("MQTT * Remaining topic is: "); LOGln(topic.c_str());
|
||||
if (topic.compareTo("free_heap")==0 || topic.compareTo("uptime")==0 || topic.compareTo("status")==0) {
|
||||
// Ignore our own messages.
|
||||
return;
|
||||
}
|
||||
|
||||
if(topic.compareTo("mode")==0) {
|
||||
LOGln("MQTT * Changing mode...");
|
||||
for (int i=0; i<effects->size(); i++) {
|
||||
EffectEntry e = effects->get(i);
|
||||
if (strcmp(e.name, payload)==0) {
|
||||
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();
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (strcmp(topic, "reboot")==0) {
|
||||
} else if (topic.compareTo("reboot")==0) {
|
||||
LOGln("MQTT * Rebooting");
|
||||
ESP.restart();
|
||||
}
|
||||
long value = payload.toInt();
|
||||
LOG("MQTT * payload after converting to a number: "); LOGln(value);
|
||||
|
||||
long value = atol(payload);
|
||||
|
||||
if (strcmp(topic, "brightness")==0 && value > 0 && value <= 255) {
|
||||
FastLED.setBrightness(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).");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,17 +151,19 @@ void mqtt_log(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 unsigned int number) { mqtt_log(String(number).c_str()); }
|
||||
void mqtt_log(long int number) { mqtt_log(String(number).c_str()); }
|
||||
|
||||
void mqtt_log(String str) { mqtt_log(str.c_str()); }
|
||||
|
||||
void mqtt_log_ln(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 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(String str) { mqtt_log_ln(str.c_str()); }
|
||||
|
||||
void mqtt_log_ln(const char* message) {
|
||||
if (mqtt_log_str.length()==0) {
|
||||
@ -157,7 +178,7 @@ void mqtt_log_ln(const char* message) {
|
||||
|
||||
void mqtt_log_send(const char* message) {
|
||||
if (mqtt_client.connected()) {
|
||||
mqtt_client.publish(MQTT_TOPIC_LOG, message);
|
||||
mqtt_client.publish(MQTT_TOPIC "log", message);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user