2021-01-08 04:54:55 +00:00
|
|
|
#include "mqtt.h"
|
|
|
|
#include "tools.h"
|
|
|
|
#include "prototypes.h"
|
|
|
|
|
|
|
|
void connect() {
|
|
|
|
LOGln("Connecting to MQTT broker...");
|
|
|
|
if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC_STATE, 0, true, "OFFLINE")) {
|
|
|
|
LOGln("Connected.");
|
|
|
|
mqtt.publish(MQTT_TOPIC_STATE, "ONLINE", true);
|
|
|
|
char buffer[40];
|
|
|
|
snprintf(buffer, 40, "ONLINE %s", wifi.localIP().toString().c_str());
|
|
|
|
mqtt.publish(MQTT_TOPIC_STATE_LONG, buffer, true);
|
|
|
|
mqtt.subscribe(MQTT_TOPIC_COMMANDS);
|
|
|
|
} else {
|
|
|
|
LOGln("Connection failed. Reason: %d", mqtt.state());
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void callback(char* topic, byte* pl, unsigned int length) {
|
|
|
|
pl[length] = 0;
|
|
|
|
String payload((char*)pl);
|
2021-01-08 15:20:05 +00:00
|
|
|
// Syntax: key=value&key2=value2...
|
2021-01-08 04:54:55 +00:00
|
|
|
uint16_t duration = 0;
|
2021-01-08 15:20:05 +00:00
|
|
|
AnimationMode new_mode = AM_NONE;
|
|
|
|
String current_part;
|
2021-01-08 16:10:46 +00:00
|
|
|
LOGln("Received command %s", payload.c_str());
|
2021-01-08 15:20:05 +00:00
|
|
|
while (payload.length() > 0) {
|
|
|
|
int offset = payload.indexOf("&");
|
|
|
|
if (offset != -1) {
|
|
|
|
current_part = payload.substring(0, offset);
|
|
|
|
payload = payload.substring(offset + 1);
|
2021-01-08 16:10:46 +00:00
|
|
|
} else {
|
|
|
|
current_part = payload;
|
|
|
|
payload = "";
|
2021-01-08 15:20:05 +00:00
|
|
|
}
|
|
|
|
offset = current_part.indexOf("=");
|
2021-01-08 16:10:46 +00:00
|
|
|
if (offset==-1) {
|
|
|
|
LOGln("Parameter without '=' detected: %s", current_part.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-08 15:20:05 +00:00
|
|
|
String key = current_part.substring(0, offset);
|
|
|
|
String value = current_part.substring(offset+1);
|
2021-01-08 16:10:46 +00:00
|
|
|
LOGln(" Processing key %s with value %s", key.c_str(), value.c_str());
|
2021-01-08 15:20:05 +00:00
|
|
|
|
|
|
|
if (key.equals("mode")) {
|
|
|
|
if (value.equals("nodes")) { new_mode = AM_NODES; }
|
|
|
|
else if (value.equals("first_node")) { new_mode = AM_FIRST_NODE; }
|
|
|
|
else if (value.equals("corners")) { new_mode = AM_CORNERS; }
|
|
|
|
else if (value.equals("first_corner")) { new_mode = AM_FIRST_CORNER; }
|
|
|
|
else if (value.equals("off")) { new_mode = AM_OFF; }
|
|
|
|
else if (value.equals("flash")) { new_mode = AM_FLASH; }
|
2021-01-08 21:22:06 +00:00
|
|
|
else if (value.equals("static")) { new_mode = AM_STATIC; }
|
2021-01-08 15:20:05 +00:00
|
|
|
else { LOGln("Unknown mode '%s'.", value.c_str()); }
|
|
|
|
} else if (key.equals("duration")) {
|
|
|
|
duration = value.toInt();
|
|
|
|
} else if (key.equals("brightness")) {
|
|
|
|
if (temp_mode_until == 0) {
|
|
|
|
return_to_brightness = FastLED.getBrightness();
|
|
|
|
}
|
|
|
|
FastLED.setBrightness(value.toInt());
|
|
|
|
} else if (key.equals("color")) {
|
|
|
|
if (value.equals("red")) { color = CRGB::Red; }
|
|
|
|
else if (value.equals("green")) { color = CRGB::Green; }
|
|
|
|
else if (value.equals("blue")) { color = CRGB::Blue; }
|
2021-01-08 21:22:06 +00:00
|
|
|
else if (value.equals("pink")) { color = CRGB::Pink; }
|
|
|
|
else if (value.equals("yellow")) { color = CRGB::Yellow; }
|
|
|
|
else if (value.equals("orange")) { color = CRGB::Orange; }
|
2021-01-08 15:20:05 +00:00
|
|
|
else { LOGln("Unknown color name %s.", value.c_str());}
|
2021-01-10 15:27:49 +00:00
|
|
|
} else if (key.equals("speedup")) {
|
|
|
|
speedup = value.toInt();
|
2021-01-08 15:20:05 +00:00
|
|
|
} else {
|
|
|
|
LOGln("Unknown key '%s'. (For reference: Value is '%s'.)", key.c_str(), value.c_str());
|
|
|
|
}
|
2021-01-08 04:54:55 +00:00
|
|
|
}
|
2021-01-08 16:10:46 +00:00
|
|
|
LOGln("Finished processing the command.");
|
2021-01-08 04:54:55 +00:00
|
|
|
|
2021-01-08 15:20:05 +00:00
|
|
|
if (new_mode != AM_NONE) {
|
|
|
|
if (duration > 0) {
|
|
|
|
temp_mode = new_mode;
|
|
|
|
temp_mode_until = millis() + duration*1000;
|
|
|
|
} else {
|
|
|
|
mode = new_mode;
|
|
|
|
}
|
2021-01-08 04:54:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_setup() {
|
|
|
|
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
|
|
|
|
mqtt.setCallback(callback);
|
|
|
|
mqtt.setSocketTimeout(1);
|
|
|
|
connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_loop() {
|
|
|
|
if (!mqtt.connected()) {
|
|
|
|
connect();
|
|
|
|
}
|
|
|
|
mqtt.loop();
|
|
|
|
}
|