2019-05-21 03:52:57 +00:00
|
|
|
PubSubClient mqtt_client(wifi);
|
|
|
|
long mqtt_last_reconnect_attempt = 0;
|
|
|
|
|
|
|
|
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
payload[length] = '\0';
|
|
|
|
for (int i=0; i<NUM_EFFECTS; i++) {
|
2019-05-23 19:18:15 +00:00
|
|
|
EffectEntry* e = &effects[i];
|
|
|
|
if (strcmp(e->name, (char*)payload)==0) {
|
|
|
|
//Serial.printf("Effect found in mqtt_callback: %p\n", (void *)&e->effect);
|
|
|
|
current_effect = e->effect;
|
2019-05-21 03:52:57 +00:00
|
|
|
clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean mqtt_connect() {
|
2019-05-23 19:18:15 +00:00
|
|
|
char client_id[30];
|
|
|
|
snprintf(client_id, 30, HOSTNAME, ESP.getChipId());
|
|
|
|
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.");
|
2019-05-21 03:52:57 +00:00
|
|
|
mqtt_client.subscribe(MQTT_TOPIC_MODE);
|
|
|
|
mqtt_client.publish(MQTT_TOPIC_STATUS, "ONLINE");
|
|
|
|
}
|
|
|
|
return mqtt_client.connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_setup() {
|
|
|
|
mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
|
|
|
|
mqtt_client.setCallback(mqtt_callback);
|
|
|
|
mqtt_last_reconnect_attempt = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_loop() {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|