diff --git a/config.sample.h b/config.sample.h index 5d70e37..e9b97dc 100644 --- a/config.sample.h +++ b/config.sample.h @@ -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. diff --git a/mqtt.h b/mqtt.h index 49b6d1e..fd5f062 100644 --- a/mqtt.h +++ b/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; iname, (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 diff --git a/pitrix.ino b/pitrix.ino index 77b3401..aaac99a 100644 --- a/pitrix.ino +++ b/pitrix.ino @@ -5,7 +5,7 @@ #define FASTLED_INTERNAL #include "FastLED.h" -#include + #include "SimpleList.h" #include "ntp.h" @@ -101,13 +101,18 @@ void setup() { ota_setup(); fastled_setup(); ntpClient.begin(); - mqtt_setup(); + #ifdef MQTT_ENABLE + mqtt_setup(); + #endif Serial.println("Core * Setup complete"); } Effect* current_effect = &clock; -#include "mqtt.h" +#ifdef MQTT_ENABLE + #include + #include "mqtt.h" +#endif uint8_t starting_up = OTA_STARTUP_DELAY; int loop_timeouts = 0; @@ -131,7 +136,9 @@ void loop() { } ntpClient.update(); - mqtt_loop(); + #ifdef MQTT_ENABLE + mqtt_loop(); + #endif EVERY_N_MILLISECONDS(1000 / FPS) { Serial.println("Core * loop running");