2019-05-29 22:49:54 +00:00
|
|
|
#include "my_mqtt.h"
|
|
|
|
#include "config.h"
|
2019-06-06 04:43:50 +00:00
|
|
|
|
2019-06-06 04:51:07 +00:00
|
|
|
#ifndef MQTT_ENABLE
|
2019-06-07 04:24:16 +00:00
|
|
|
#pragma message "MQTT_ENABLE is false. Skipping MQTT."
|
2019-06-06 04:51:07 +00:00
|
|
|
#else
|
2019-06-06 04:43:50 +00:00
|
|
|
|
2019-06-13 04:01:44 +00:00
|
|
|
#include "my_wifi.h"
|
2019-05-29 22:49:54 +00:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include "Effect.h"
|
|
|
|
#include "effects.h"
|
|
|
|
#include "functions.h"
|
2019-06-19 20:26:38 +00:00
|
|
|
#include "tests.h"
|
2019-05-29 22:49:54 +00:00
|
|
|
|
|
|
|
WiFiClient wifi;
|
2019-05-21 03:52:57 +00:00
|
|
|
PubSubClient mqtt_client(wifi);
|
|
|
|
long mqtt_last_reconnect_attempt = 0;
|
|
|
|
|
2019-06-07 04:08:21 +00:00
|
|
|
uint8_t weather_icon_ids[6] = {0, 0, 0, 0, 0, 0};
|
|
|
|
int8_t weather_temperatures[6] = {0, 0, 0, 0, 0, 0};
|
|
|
|
|
2019-06-11 17:48:42 +00:00
|
|
|
void mqtt_callback(char* original_topic, byte* pl, unsigned int length) {
|
2019-06-07 04:24:16 +00:00
|
|
|
pl[length] = '\0';
|
2019-06-11 17:48:42 +00:00
|
|
|
String payload((char*)pl);
|
|
|
|
String topic (original_topic);
|
2020-04-29 18:24:59 +00:00
|
|
|
if (topic.equals(MQTT_TOPIC "log") || topic.equals(MQTT_TOPIC "status") || topic.startsWith(MQTT_TOPIC "metrics")) {
|
2019-09-25 04:40:24 +00:00
|
|
|
// Return our own messages
|
|
|
|
return;
|
|
|
|
}
|
2019-06-18 16:17:48 +00:00
|
|
|
LOGln("MQTT * In: %s = %s", topic.c_str(), payload.c_str());
|
2019-06-11 17:48:42 +00:00
|
|
|
if (topic.startsWith(MQTT_TOPIC_WEATHER)) {
|
2019-06-07 04:24:16 +00:00
|
|
|
// Weather stuff
|
2019-06-11 17:48:42 +00:00
|
|
|
topic.remove(0, strlen(MQTT_TOPIC_WEATHER));
|
2020-04-30 04:42:18 +00:00
|
|
|
DBG("MQTT * Weather stuff.");
|
2019-06-11 17:48:42 +00:00
|
|
|
if (topic.startsWith("icons/")) {
|
|
|
|
topic.remove(0, 6);
|
|
|
|
uint8_t id = topic.toInt();
|
2019-06-07 04:24:16 +00:00
|
|
|
if (id>=6) return;
|
2019-06-11 17:48:42 +00:00
|
|
|
uint8_t val = payload.toInt();
|
|
|
|
if (val==0) return;
|
|
|
|
weather_icon_ids[id] = val;
|
2020-04-30 04:42:18 +00:00
|
|
|
DBG("Set weather_icon_ids[%d] to value %d", id, val);
|
2019-06-11 17:48:42 +00:00
|
|
|
} else if (topic.startsWith("temperatures/")) {
|
|
|
|
topic.remove(0, 13);
|
|
|
|
uint8_t id = topic.toInt();
|
2019-06-07 04:24:16 +00:00
|
|
|
if (id>=6) return;
|
2019-06-11 17:48:42 +00:00
|
|
|
uint8_t val = payload.toInt();
|
|
|
|
if (val==0) return;
|
|
|
|
weather_temperatures[id] = val;
|
2020-04-30 04:42:18 +00:00
|
|
|
DBG("Set weather_temperatures[%d] to value %d", id, val);
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
|
|
|
return;
|
2019-12-21 13:21:31 +00:00
|
|
|
} else if (topic.equals(MQTT_TOPIC_TIMER)) {
|
|
|
|
timer = payload.toInt();
|
|
|
|
LOGln("Set timer to %lu.", timer);
|
|
|
|
return;
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 17:48:42 +00:00
|
|
|
topic.remove(0, strlen(MQTT_TOPIC)); // Strip MQTT_TOPIC from the beginning
|
|
|
|
|
|
|
|
if(topic.compareTo("mode")==0) {
|
|
|
|
LOGln("MQTT * Changing mode...");
|
2019-06-19 20:27:33 +00:00
|
|
|
bool result = change_current_effect(payload);
|
|
|
|
if (result) {
|
|
|
|
LOGln("MQTT * Effect changed.");
|
|
|
|
} else {
|
|
|
|
LOGln("MQTT * Could not change effect.");
|
|
|
|
}
|
2019-06-18 16:17:48 +00:00
|
|
|
return;
|
2019-06-11 17:48:42 +00:00
|
|
|
} else if (topic.compareTo("reboot")==0) {
|
|
|
|
LOGln("MQTT * Rebooting");
|
2019-06-07 04:24:16 +00:00
|
|
|
ESP.restart();
|
2019-06-18 16:17:48 +00:00
|
|
|
return; // Will never be reached, but anyway...
|
2019-06-19 20:26:38 +00:00
|
|
|
} else if (topic.compareTo("run_tests")==0) {
|
|
|
|
tests::run();
|
|
|
|
return;
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
2019-10-04 10:11:05 +00:00
|
|
|
|
2019-06-11 17:48:42 +00:00
|
|
|
long value = payload.toInt();
|
2019-06-18 16:17:48 +00:00
|
|
|
LOGln("MQTT * Payload as number: %d", value);
|
2019-10-04 10:11:05 +00:00
|
|
|
|
|
|
|
if (topic.startsWith("settings.")) {
|
|
|
|
topic.remove(0, 9);
|
|
|
|
change_setting(topic.c_str(), value);
|
|
|
|
return;
|
|
|
|
} else if (topic.compareTo("brightness")==0) {
|
2019-06-11 17:48:42 +00:00
|
|
|
if (value > 0 && value <= 255) {
|
|
|
|
LOGln("MQTT * Changing brightness...");
|
|
|
|
FastLED.setBrightness(value);
|
|
|
|
} else {
|
2019-06-18 16:17:48 +00:00
|
|
|
LOGln("MQTT * Ignoring brightness change: Value %d is out of bounds (0<x<=255).", value);
|
2019-06-11 17:48:42 +00:00
|
|
|
}
|
2019-06-18 16:17:48 +00:00
|
|
|
return;
|
2019-06-07 04:08:21 +00:00
|
|
|
}
|
2019-06-18 16:17:48 +00:00
|
|
|
|
|
|
|
LOGln("MQTT * Topic was not processed.");
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean mqtt_connect() {
|
2019-06-19 20:27:33 +00:00
|
|
|
LOGln("MQTT * Connecting to MQTT server with client id %s", hostname);
|
2019-06-14 03:35:17 +00:00
|
|
|
if (mqtt_client.connect(hostname, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) {
|
2019-06-07 04:24:16 +00:00
|
|
|
LOGln("MQTT * Connected.");
|
2019-10-23 03:42:10 +00:00
|
|
|
LOGln("Core * Flash chip id: 0x%X. Size: %d bytes. 'Real' size: %d bytes.", ESP.getFlashChipId(), ESP.getFlashChipSize(), ESP.getFlashChipRealSize());
|
2019-06-19 20:27:33 +00:00
|
|
|
char buffer[40];
|
|
|
|
snprintf(buffer, 40, "ONLINE %s %s", hostname, WiFi.localIP().toString().c_str());
|
|
|
|
mqtt_client.publish(MQTT_TOPIC "status", buffer, true);
|
2019-06-07 04:24:16 +00:00
|
|
|
mqtt_client.subscribe(MQTT_TOPIC "+");
|
|
|
|
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
|
2019-12-21 13:21:31 +00:00
|
|
|
mqtt_client.subscribe(MQTT_TOPIC_TIMER);
|
2019-06-07 04:24:16 +00:00
|
|
|
}
|
|
|
|
return mqtt_client.connected();
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_setup() {
|
2019-06-07 04:24:16 +00:00
|
|
|
mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
|
|
|
|
mqtt_client.setCallback(mqtt_callback);
|
|
|
|
mqtt_last_reconnect_attempt = 0;
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_loop() {
|
2019-06-07 04:24:16 +00:00
|
|
|
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();
|
|
|
|
}
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
2019-05-30 11:09:38 +00:00
|
|
|
|
2019-05-31 21:57:46 +00:00
|
|
|
String mqtt_log_str = String();
|
|
|
|
|
2020-04-29 18:25:41 +00:00
|
|
|
void mqtt_publish(const char* topic, int number, bool retain) {
|
2019-06-07 04:24:16 +00:00
|
|
|
char b[32];
|
|
|
|
sprintf(b, "%d", number);
|
2020-04-29 18:25:41 +00:00
|
|
|
mqtt_publish(topic, b, retain);
|
2019-09-25 04:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 18:25:41 +00:00
|
|
|
void mqtt_publish(const char* topic, const char* message, bool retain) {
|
2019-09-25 04:40:24 +00:00
|
|
|
char t[127];
|
|
|
|
sprintf(t, MQTT_TOPIC "%s", topic);
|
2020-04-29 18:25:41 +00:00
|
|
|
mqtt_client.publish(t, message, retain);
|
2019-05-30 11:09:38 +00:00
|
|
|
}
|
2019-05-31 21:57:46 +00:00
|
|
|
|
|
|
|
void mqtt_log(const char* message) {
|
|
|
|
mqtt_log_str.concat(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_log(int number) {
|
2019-06-07 04:24:16 +00:00
|
|
|
mqtt_log(String(number).c_str());
|
2019-05-31 21:57:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 17:48:42 +00:00
|
|
|
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()); }
|
2019-06-18 16:17:48 +00:00
|
|
|
void mqtt_log(size_t number) { mqtt_log(String(number).c_str()); }
|
2019-06-11 17:48:42 +00:00
|
|
|
|
|
|
|
void mqtt_log(String str) { mqtt_log(str.c_str()); }
|
2019-05-31 21:57:46 +00:00
|
|
|
|
|
|
|
void mqtt_log_ln(int number) {
|
2019-06-07 04:24:16 +00:00
|
|
|
mqtt_log_ln(String(number).c_str());
|
2019-05-31 21:57:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 17:48:42 +00:00
|
|
|
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()); }
|
2019-06-18 16:17:48 +00:00
|
|
|
void mqtt_log_ln(size_t number) { mqtt_log_ln(String(number).c_str()); }
|
2019-06-11 17:48:42 +00:00
|
|
|
|
|
|
|
void mqtt_log_ln(String str) { mqtt_log_ln(str.c_str()); }
|
2019-05-31 21:57:46 +00:00
|
|
|
|
|
|
|
void mqtt_log_ln(const char* message) {
|
2019-06-07 04:24:16 +00:00
|
|
|
if (mqtt_log_str.length()==0) {
|
2019-05-31 21:57:46 +00:00
|
|
|
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()) {
|
2019-06-11 17:48:42 +00:00
|
|
|
mqtt_client.publish(MQTT_TOPIC "log", message);
|
2019-05-31 21:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 04:43:50 +00:00
|
|
|
|
|
|
|
#endif // MQTT_ENABLE
|