Browse Source

MQTT autodiscover and autoconnect features

fastled
Xose Pérez 7 years ago
parent
commit
921c2e7726
3 changed files with 26 additions and 1 deletions
  1. +3
    -1
      code/espurna/config/general.h
  2. +22
    -0
      code/espurna/mqtt.ino
  3. +1
    -0
      code/espurna/wifi.ino

+ 3
- 1
code/espurna/config/general.h View File

@ -334,6 +334,8 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_SSL_FINGERPRINT "" #define MQTT_SSL_FINGERPRINT ""
#define MQTT_ENABLED 0 // Do not enable MQTT connection by default #define MQTT_ENABLED 0 // Do not enable MQTT connection by default
#define MQTT_AUTOCONNECT 1 // If enabled and MDNS_SUPPORT=1 will perform an autodiscover and
// autoconnect to the first MQTT broker found if none defined
#define MQTT_SERVER "" // Default MQTT broker address #define MQTT_SERVER "" // Default MQTT broker address
#define MQTT_PORT 1883 // MQTT broker port #define MQTT_PORT 1883 // MQTT broker port
#define MQTT_TOPIC "/test/switch/{identifier}" // Default MQTT base topic #define MQTT_TOPIC "/test/switch/{identifier}" // Default MQTT base topic
@ -342,7 +344,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_KEEPALIVE 30 // MQTT keepalive value #define MQTT_KEEPALIVE 30 // MQTT keepalive value
#define MQTT_RECONNECT_DELAY 10000 // Try to reconnect after 10s #define MQTT_RECONNECT_DELAY 10000 // Try to reconnect after 10s
#define MQTT_TRY_INTERVAL 30000 // Timeframe for disconnect retries #define MQTT_TRY_INTERVAL 30000 // Timeframe for disconnect retries
#define MQTT_MAX_TRIES 10 // After these many retries during the previous MQTT_TRY_INTERVAL the board will reset
#define MQTT_MAX_TRIES 5 // After these many retries during the previous MQTT_TRY_INTERVAL the board will reset
#define MQTT_SKIP_RETAINED 1 // Skip retained messages on connection #define MQTT_SKIP_RETAINED 1 // Skip retained messages on connection
#define MQTT_SKIP_TIME 1000 // Skip messages for 1 second anter connection #define MQTT_SKIP_TIME 1000 // Skip messages for 1 second anter connection


+ 22
- 0
code/espurna/mqtt.ino View File

@ -7,6 +7,7 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/ */
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <vector> #include <vector>
#include <Ticker.h> #include <Ticker.h>
@ -448,6 +449,27 @@ void mqttConfigure() {
} }
#ifdef MDNS_SUPPORT
boolean mqttDiscover() {
int count = MDNS.queryService("mqtt", "tcp");
DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count);
for (int i=0; i<count; i++) {
DEBUG_MSG_P("[MQTT] Broker at %s:%d\n", MDNS.IP(i).toString().c_str(), MDNS.port(i));
if ((i==0) && (getSetting("mqttServer").length() == 0)) {
setSetting("mqttServer", MDNS.IP(i).toString());
setSetting("mqttPort", MDNS.port(i));
mqttEnabled(MQTT_AUTOCONNECT);
}
}
}
#endif // MDNS_SUPPORT
void mqttSetup() { void mqttSetup() {
#if MQTT_USE_ASYNC #if MQTT_USE_ASYNC
mqtt.onConnect([](bool sessionPresent) { mqtt.onConnect([](bool sessionPresent) {


+ 1
- 0
code/espurna/wifi.ino View File

@ -231,6 +231,7 @@ void wifiSetup() {
if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) { if (MDNS.begin(WiFi.getMode() == WIFI_AP ? APP_NAME : (char *) WiFi.hostname().c_str())) {
MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt()); MDNS.addService("http", "tcp", getSetting("webPort", WEB_PORT).toInt());
DEBUG_MSG_P(PSTR("[MDNS] OK\n")); DEBUG_MSG_P(PSTR("[MDNS] OK\n"));
if (code == MESSAGE_CONNECTED) mqttDiscover();
} else { } else {
DEBUG_MSG_P(PSTR("[MDNS] FAIL\n")); DEBUG_MSG_P(PSTR("[MDNS] FAIL\n"));
} }


Loading…
Cancel
Save