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:
parent
7b9efa1357
commit
ad0ac8fb21
@ -12,19 +12,15 @@
|
||||
uint8_t config_brightness = 20; // Can be overwritten via MQTT_TOPIC_BRIGHTNESS
|
||||
|
||||
#define NTP_SERVER "pool.ntp.org"
|
||||
#define NTP_INTERVAL 60000
|
||||
#define NTP_INTERVAL 300000
|
||||
#define NTP_OFFSET 7200
|
||||
|
||||
#define MQTT_ENABLE
|
||||
#define MQTT_SERVER "....."
|
||||
#define MQTT_PORT 1883
|
||||
#define MQTT_USER "pitrix"
|
||||
#define MQTT_PASS "....."
|
||||
#define MQTT_TOPIC_MODE "pitrix/mode"
|
||||
#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 MQTT_TOPIC "pitrix/" // MQTT-Topic to listen to. Must not start with a slash, but must end with one."
|
||||
|
||||
#define HOSTNAME "pitrix-%08X"
|
||||
#define OTA_STARTUP_DELAY 5 // How many seconds to wait at startup. Set to 0 to disable.
|
||||
|
43
mqtt.h
43
mqtt.h
@ -1,32 +1,35 @@
|
||||
#ifdef MQTT_ENABLE
|
||||
PubSubClient mqtt_client(wifi);
|
||||
long mqtt_last_reconnect_attempt = 0;
|
||||
|
||||
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
||||
payload[length] = '\0';
|
||||
if(strcmp(topic, MQTT_TOPIC_MODE)==0) {
|
||||
void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) {
|
||||
pl[length] = '\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++) {
|
||||
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);
|
||||
current_effect = e->effect;
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else if (strcmp(topic, MQTT_TOPIC_BRIGHTNESS)==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) {
|
||||
} else if (strcmp(topic, "reboot")==0) {
|
||||
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() {
|
||||
@ -35,11 +38,8 @@ boolean mqtt_connect() {
|
||||
LOG("MQTT * Connecting to MQTT server with client id "); LOGln(client_id);
|
||||
if (mqtt_client.connect(client_id, MQTT_USER, MQTT_PASS)) {
|
||||
LOGln("MQTT * Connected.");
|
||||
mqtt_client.subscribe(MQTT_TOPIC_MODE);
|
||||
mqtt_client.subscribe(MQTT_TOPIC_BRIGHTNESS);
|
||||
mqtt_client.subscribe(MQTT_TOPIC_EFFECT_CYCLE_TIME);
|
||||
mqtt_client.subscribe(MQTT_TOPIC_REBOOT);
|
||||
mqtt_client.publish(MQTT_TOPIC_STATUS, "ONLINE");
|
||||
mqtt_client.publish(MQTT_TOPIC "status", "ONLINE");
|
||||
mqtt_client.subscribe(MQTT_TOPIC "+");
|
||||
}
|
||||
return mqtt_client.connected();
|
||||
}
|
||||
@ -63,3 +63,4 @@ void mqtt_loop() {
|
||||
mqtt_client.loop();
|
||||
}
|
||||
}
|
||||
#endif // MQTT_ENABLE
|
||||
|
11
pitrix.ino
11
pitrix.ino
@ -5,7 +5,7 @@
|
||||
|
||||
#define FASTLED_INTERNAL
|
||||
#include "FastLED.h"
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include "SimpleList.h"
|
||||
#include "ntp.h"
|
||||
|
||||
@ -101,13 +101,18 @@ void setup() {
|
||||
ota_setup();
|
||||
fastled_setup();
|
||||
ntpClient.begin();
|
||||
#ifdef MQTT_ENABLE
|
||||
mqtt_setup();
|
||||
#endif
|
||||
Serial.println("Core * Setup complete");
|
||||
}
|
||||
|
||||
Effect* current_effect = &clock;
|
||||
|
||||
#include "mqtt.h"
|
||||
#ifdef MQTT_ENABLE
|
||||
#include <PubSubClient.h>
|
||||
#include "mqtt.h"
|
||||
#endif
|
||||
|
||||
uint8_t starting_up = OTA_STARTUP_DELAY;
|
||||
int loop_timeouts = 0;
|
||||
@ -131,7 +136,9 @@ void loop() {
|
||||
}
|
||||
|
||||
ntpClient.update();
|
||||
#ifdef MQTT_ENABLE
|
||||
mqtt_loop();
|
||||
#endif
|
||||
|
||||
EVERY_N_MILLISECONDS(1000 / FPS) {
|
||||
Serial.println("Core * loop running");
|
||||
|
Loading…
Reference in New Issue
Block a user