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-06 10:12:58 +00:00
|
|
|
#if defined( ESP8266 )
|
2019-06-07 04:24:16 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
2019-06-06 10:12:58 +00:00
|
|
|
#elif defined( ESP32 )
|
2019-06-07 04:24:16 +00:00
|
|
|
#include <WiFi.h>
|
2019-06-06 10:12:58 +00:00
|
|
|
#else
|
2019-06-07 04:24:16 +00:00
|
|
|
#error "Neither ESP32 nor ESP8266 set..."
|
2019-06-06 10:12:58 +00:00
|
|
|
#endif
|
2019-05-29 22:49:54 +00:00
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include "EffectEntry.h"
|
|
|
|
#include "Effect.h"
|
|
|
|
#include "effects.h"
|
|
|
|
#include "functions.h"
|
|
|
|
|
|
|
|
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-05-27 16:11:37 +00:00
|
|
|
void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) {
|
2019-06-07 04:24:16 +00:00
|
|
|
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) {
|
|
|
|
// 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.
|
|
|
|
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.
|
|
|
|
if (id>=6) return;
|
|
|
|
weather_temperatures[id] = atol(payload);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* topic = complete_topic + strlen(MQTT_TOPIC); // Strip MQTT_TOPIC from the beginning
|
|
|
|
|
|
|
|
// Here, payload is a char* (but has to be casted).
|
|
|
|
if(strcmp(topic, "mode")==0) {
|
|
|
|
for (int i=0; i<effects->size(); i++) {
|
|
|
|
EffectEntry e = effects->get(i);
|
|
|
|
if (strcmp(e.name, payload)==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) {
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
long value = atol(payload);
|
|
|
|
|
|
|
|
if (strcmp(topic, "brightness")==0 && value > 0 && value <= 255) {
|
|
|
|
FastLED.setBrightness(value);
|
2019-06-07 04:08:21 +00:00
|
|
|
}
|
2019-05-21 03:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boolean mqtt_connect() {
|
2019-06-07 04:24:16 +00:00
|
|
|
char client_id[30];
|
|
|
|
int chipid;
|
|
|
|
#if defined( ESP8266 )
|
|
|
|
chipid = ESP.getChipId();
|
|
|
|
#elif defined( ESP32 )
|
|
|
|
chipid = ESP.getEfuseMac() & 0xFFFFFF;
|
|
|
|
#else
|
|
|
|
#error Neither ESP32 nor ESP8266 set.
|
|
|
|
#endif
|
|
|
|
snprintf(client_id, 30, HOSTNAME, chipid);
|
|
|
|
LOG("MQTT * Connecting to MQTT server with client id "); LOGln(client_id);
|
|
|
|
if (mqtt_client.connect(client_id, MQTT_USER, MQTT_PASS, MQTT_TOPIC "status", 0, true, "OFFLINE", true)) {
|
|
|
|
LOGln("MQTT * Connected.");
|
|
|
|
mqtt_client.publish(MQTT_TOPIC "status", "ONLINE");
|
|
|
|
mqtt_client.subscribe(MQTT_TOPIC "+");
|
|
|
|
mqtt_client.subscribe(MQTT_TOPIC_WEATHER "#");
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
|
2019-05-30 11:09:38 +00:00
|
|
|
void mqtt_publish(const char* topic, int number) {
|
2019-06-07 04:24:16 +00:00
|
|
|
char t[127];
|
|
|
|
sprintf(t, MQTT_TOPIC "%s", topic);
|
|
|
|
char b[32];
|
|
|
|
sprintf(b, "%d", number);
|
|
|
|
mqtt_client.publish(t, b);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_log(long unsigned int number) {
|
2019-06-07 04:24:16 +00:00
|
|
|
mqtt_log(String(number).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
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_log_ln(long unsigned 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
|
|
|
}
|
|
|
|
|
|
|
|
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()) {
|
|
|
|
mqtt_client.publish(MQTT_TOPIC_LOG, message);
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 04:43:50 +00:00
|
|
|
|
|
|
|
#endif // MQTT_ENABLE
|