MQTT: Subscribe to just one topic. Subtopics are used for parsing. Also, you can now disable MQTT via MQTT_ENABLE.

This commit is contained in:
Fabian Schlenz 2019-05-27 18:11:37 +02:00
parent 7b9efa1357
commit ad0ac8fb21
3 changed files with 36 additions and 32 deletions

View File

@ -12,19 +12,15 @@
uint8_t config_brightness = 20; // Can be overwritten via MQTT_TOPIC_BRIGHTNESS uint8_t config_brightness = 20; // Can be overwritten via MQTT_TOPIC_BRIGHTNESS
#define NTP_SERVER "pool.ntp.org" #define NTP_SERVER "pool.ntp.org"
#define NTP_INTERVAL 60000 #define NTP_INTERVAL 300000
#define NTP_OFFSET 7200 #define NTP_OFFSET 7200
#define MQTT_ENABLE
#define MQTT_SERVER "....." #define MQTT_SERVER "....."
#define MQTT_PORT 1883 #define MQTT_PORT 1883
#define MQTT_USER "pitrix" #define MQTT_USER "pitrix"
#define MQTT_PASS "....." #define MQTT_PASS "....."
#define MQTT_TOPIC_MODE "pitrix/mode" #define MQTT_TOPIC "pitrix/" // MQTT-Topic to listen to. Must not start with a slash, but must end with one."
#define MQTT_TOPIC_STATUS "pitrix/status"
#define MQTT_TOPIC_LOG "pitrix/log"
#define MQTT_TOPIC_BRIGHTNESS "pitrix/brightness"
#define MQTT_TOPIC_EFFECT_CYCLE_TIME "pitrix/cycle_time"
#define MQTT_TOPIC_REBOOT "pitrix/reboot"
#define HOSTNAME "pitrix-%08X" #define HOSTNAME "pitrix-%08X"
#define OTA_STARTUP_DELAY 5 // How many seconds to wait at startup. Set to 0 to disable. #define OTA_STARTUP_DELAY 5 // How many seconds to wait at startup. Set to 0 to disable.

43
mqtt.h
View File

@ -1,32 +1,35 @@
#ifdef MQTT_ENABLE
PubSubClient mqtt_client(wifi); PubSubClient mqtt_client(wifi);
long mqtt_last_reconnect_attempt = 0; long mqtt_last_reconnect_attempt = 0;
void mqtt_callback(char* topic, byte* payload, unsigned int length) { void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) {
payload[length] = '\0'; pl[length] = '\0';
if(strcmp(topic, MQTT_TOPIC_MODE)==0) { char* payload = (char*)pl;
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<NUM_EFFECTS; i++) { for (int i=0; i<NUM_EFFECTS; i++) {
EffectEntry* e = &effects[i]; EffectEntry* e = &effects[i];
if (strcmp(e->name, (char*)payload)==0) { if (strcmp(e->name, payload)==0) {
//Serial.printf("Effect found in mqtt_callback: %p\n", (void *)&e->effect); //Serial.printf("Effect found in mqtt_callback: %p\n", (void *)&e->effect);
current_effect = e->effect; current_effect = e->effect;
clear(); clear();
return; return;
} }
} }
} else if (strcmp(topic, MQTT_TOPIC_BRIGHTNESS)==0) { } else if (strcmp(topic, "reboot")==0) {
long new_value = atol((char *) payload);
if (new_value > 0 && new_value <= 255) {
config_brightness = new_value;
FastLED.setBrightness(config_brightness);
}
} else if (strcmp(topic, MQTT_TOPIC_EFFECT_CYCLE_TIME)==0) {
long new_value = atol((char *) payload);
if (new_value > 0) {
config_effect_cycle_time = new_value;
}
} else if (strcmp(topic, MQTT_TOPIC_REBOOT)==0) {
ESP.restart(); ESP.restart();
} }
long value = atol(payload);
if (strcmp(topic, "brightness")==0 && value > 0 && value <= 255) {
config_brightness = value;
FastLED.setBrightness(config_brightness);
} else if (strcmp(topic, "cycle_time")==0 && value > 0) {
config_effect_cycle_time = value;
}
} }
boolean mqtt_connect() { boolean mqtt_connect() {
@ -35,11 +38,8 @@ boolean mqtt_connect() {
LOG("MQTT * Connecting to MQTT server with client id "); LOGln(client_id); LOG("MQTT * Connecting to MQTT server with client id "); LOGln(client_id);
if (mqtt_client.connect(client_id, MQTT_USER, MQTT_PASS)) { if (mqtt_client.connect(client_id, MQTT_USER, MQTT_PASS)) {
LOGln("MQTT * Connected."); LOGln("MQTT * Connected.");
mqtt_client.subscribe(MQTT_TOPIC_MODE); mqtt_client.publish(MQTT_TOPIC "status", "ONLINE");
mqtt_client.subscribe(MQTT_TOPIC_BRIGHTNESS); mqtt_client.subscribe(MQTT_TOPIC "+");
mqtt_client.subscribe(MQTT_TOPIC_EFFECT_CYCLE_TIME);
mqtt_client.subscribe(MQTT_TOPIC_REBOOT);
mqtt_client.publish(MQTT_TOPIC_STATUS, "ONLINE");
} }
return mqtt_client.connected(); return mqtt_client.connected();
} }
@ -63,3 +63,4 @@ void mqtt_loop() {
mqtt_client.loop(); mqtt_client.loop();
} }
} }
#endif // MQTT_ENABLE

View File

@ -5,7 +5,7 @@
#define FASTLED_INTERNAL #define FASTLED_INTERNAL
#include "FastLED.h" #include "FastLED.h"
#include <PubSubClient.h>
#include "SimpleList.h" #include "SimpleList.h"
#include "ntp.h" #include "ntp.h"
@ -101,13 +101,18 @@ void setup() {
ota_setup(); ota_setup();
fastled_setup(); fastled_setup();
ntpClient.begin(); ntpClient.begin();
mqtt_setup(); #ifdef MQTT_ENABLE
mqtt_setup();
#endif
Serial.println("Core * Setup complete"); Serial.println("Core * Setup complete");
} }
Effect* current_effect = &clock; Effect* current_effect = &clock;
#include "mqtt.h" #ifdef MQTT_ENABLE
#include <PubSubClient.h>
#include "mqtt.h"
#endif
uint8_t starting_up = OTA_STARTUP_DELAY; uint8_t starting_up = OTA_STARTUP_DELAY;
int loop_timeouts = 0; int loop_timeouts = 0;
@ -131,7 +136,9 @@ void loop() {
} }
ntpClient.update(); ntpClient.update();
mqtt_loop(); #ifdef MQTT_ENABLE
mqtt_loop();
#endif
EVERY_N_MILLISECONDS(1000 / FPS) { EVERY_N_MILLISECONDS(1000 / FPS) {
Serial.println("Core * loop running"); Serial.println("Core * loop running");