pitrix/src/mqtt.cpp

208 lines
6.1 KiB
C++

#include "my_mqtt.h"
#include "config.h"
#ifndef MQTT_ENABLE
#pragma message "MQTT_ENABLE is false. Skipping MQTT."
#else
#include "my_wifi.h"
#include <PubSubClient.h>
#include "Effect.h"
#include "effects.h"
#include "functions.h"
#include "tests.h"
WiFiClient wifi;
PubSubClient mqtt_client(wifi);
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* original_topic, byte* pl, unsigned int length) {
pl[length] = '\0';
String payload((char*)pl);
String topic (original_topic);
if (topic.equals(MQTT_TOPIC "log") || topic.equals(MQTT_TOPIC "status") || topic.equals(MQTT_TOPIC "status_details") || topic.startsWith(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
topic.remove(0, strlen(MQTT_TOPIC_WEATHER));
DBG("MQTT * Weather stuff.");
if (topic.startsWith("icons/")) {
topic.remove(0, 6);
uint8_t id = topic.toInt();
if (id>=6) return;
uint8_t val = payload.toInt();
if (val==0) return;
weather_icon_ids[id] = val;
DBG("Set weather_icon_ids[%d] to value %d", id, val);
} else if (topic.startsWith("temperatures/")) {
topic.remove(0, 13);
uint8_t id = topic.toInt();
if (id>=6) return;
uint8_t val = payload.toInt();
if (val==0) return;
weather_temperatures[id] = val;
DBG("Set weather_temperatures[%d] to value %d", id, val);
}
return;
} else if (topic.equals(MQTT_TOPIC_TIMER)) {
timer = payload.toInt();
LOGln("Set timer to %lu.", timer);
return;
}
topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning
if(topic.compareTo("mode")==0) {
LOGln("MQTT * Changing mode...");
bool result = change_current_effect(payload);
if (result) {
LOGln("MQTT * Effect changed.");
} else {
LOGln("MQTT * Could not change effect.");
}
return;
} else if (topic.compareTo("reboot")==0) {
LOGln("MQTT * Rebooting");
ESP.restart();
return; // Will never be reached, but anyway...
} else if (topic.compareTo("run_tests")==0) {
tests::run();
return;
}
long value = payload.toInt();
LOGln("MQTT * Payload as number: %d", value);
if (topic.startsWith("settings.")) {
topic.remove(0, 9);
change_setting(topic.c_str(), value);
return;
} else if (topic.compareTo("brightness")==0) {
if (value > 0 && value <= 255) {
LOGln("MQTT * Changing brightness...");
FastLED.setBrightness(value);
} else {
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() {
LOGln("MQTT * Connecting to MQTT server with client id %s", hostname);
mqtt_client.setBufferSize(350);
if (mqtt_client.connect(hostname, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) {
LOGln("MQTT * Connected.");
LOGln("Core * Flash chip id: 0x%X. Size: %d bytes. 'Real' size: %d bytes.", ESP.getFlashChipId(), ESP.getFlashChipSize(), ESP.getFlashChipRealSize());
char buffer[40];
snprintf(buffer, 40, "ONLINE %s %s", hostname, WiFi.localIP().toString().c_str());
mqtt_client.publish(MQTT_TOPIC "status", "ONLINE", true);
mqtt_client.publish(MQTT_TOPIC "status_details", buffer, true);
mqtt_client.subscribe(MQTT_TOPIC "+");
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
mqtt_client.subscribe(MQTT_TOPIC_TIMER);
#ifdef MQTT_TOPIC_HOMEASSISTANT
// Set MQTT values for homeassistant auto device discovery
String topic = MQTT_TOPIC_HOMEASSISTANT "/light/";
topic += hostname;
topic += "/config";
String message = "{\"~\":\"" MQTT_TOPIC "\",\"opt\":1,\"avty_t\":\"~status\",\"pl_avail\":\"ONLINE\",\"pl_not_avail\":\"OFFLINE\",";
message += "\"bri_cmd_t\": \"~brightness\",\"bri_scl\":255,\"fx_cmd_t\":\"~modus\",\"name\":\"Pitrix\",\"uniq_id\":\"";
message += hostname;
message += "\",";
message += "\"stat_t\":\"~modus\",\"cmd_t\":\"~modus\",\"pl_on\":\"cycle\",\"pl_off\":\"night_clock\"}";
mqtt_client.publish(topic.c_str(), message.c_str(), true);
DBG("MQTT * Homeassistant data:");
DBG("MQTT * Topic: %s", topic.c_str());
DBG("MQTT * Data: %s", message.c_str());
#endif
}
return mqtt_client.connected();
}
void mqtt_setup() {
mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
mqtt_client.setCallback(mqtt_callback);
mqtt_last_reconnect_attempt = 0;
}
void mqtt_loop() {
if (!mqtt_client.connected()) {
long now = millis();
if (now - mqtt_last_reconnect_attempt > 5000) {
mqtt_last_reconnect_attempt = now;
if (mqtt_connect()) {
mqtt_last_reconnect_attempt = 0;
}
}
} else {
mqtt_client.loop();
}
}
String mqtt_log_str = String();
void mqtt_publish(const char* topic, int number, bool retain) {
char b[32];
sprintf(b, "%d", number);
mqtt_publish(topic, b, retain);
}
void mqtt_publish(const char* topic, const char* message, bool retain) {
char t[127];
sprintf(t, MQTT_TOPIC "%s", topic);
mqtt_client.publish(t, message, retain);
}
void mqtt_log(const char* message) {
mqtt_log_str.concat(message);
}
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 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_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 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(const char* message) {
if (mqtt_log_str.length()==0) {
mqtt_log_send(message);
return;
} else {
mqtt_log_str.concat(message);
mqtt_log_send(mqtt_log_str.c_str());
mqtt_log_str = String();
}
}
void mqtt_log_send(const char* message) {
if (mqtt_client.connected()) {
mqtt_client.publish(MQTT_TOPIC "log", message);
}
}
#endif // MQTT_ENABLE