Browse Source

Option to disable MQTT connection from web interface

fastled
Xose Pérez 7 years ago
parent
commit
9d14a9cc78
6 changed files with 2878 additions and 2859 deletions
  1. +2
    -1
      code/espurna/config/general.h
  2. BIN
      code/espurna/data/index.html.gz
  3. +39
    -22
      code/espurna/mqtt.ino
  4. +2825
    -2825
      code/espurna/static/index.html.gz.h
  5. +6
    -10
      code/espurna/web.ino
  6. +6
    -1
      code/html/index.html

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

@ -333,6 +333,7 @@ PROGMEM const char* const custom_reset_string[] = {
#define MQTT_SSL_ENABLED 0 #define MQTT_SSL_ENABLED 0
#define MQTT_SSL_FINGERPRINT "" #define MQTT_SSL_FINGERPRINT ""
#define MQTT_ENABLED 0 // Do not enable MQTT connection by default
#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
@ -341,7 +342,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 12 // After these many retries during the previous MQTT_TRY_INTERVAL the board will reset
#define MQTT_MAX_TRIES 10 // 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


BIN
code/espurna/data/index.html.gz View File


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

@ -28,29 +28,30 @@ bool _mqttConnected = false;
WiFiClient _mqttClient; WiFiClient _mqttClient;
#if ASYNC_TCP_SSL_ENABLED #if ASYNC_TCP_SSL_ENABLED
WiFiClientSecure _mqttClientSecure; WiFiClientSecure _mqttClientSecure;
#endif
#endif // ASYNC_TCP_SSL_ENABLED
#endif
#endif // MQTT_USE_ASYNC
bool _mqttEnabled = MQTT_ENABLED;
String _mqttTopic; String _mqttTopic;
String _mqttSetter; String _mqttSetter;
String _mqttGetter; String _mqttGetter;
bool _mqttForward; bool _mqttForward;
char *_mqttUser = 0; char *_mqttUser = 0;
char *_mqttPass = 0; char *_mqttPass = 0;
char *_mqttWill; char *_mqttWill;
std::vector<void (*)(unsigned int, const char *, const char *)> _mqtt_callbacks;
#if MQTT_SKIP_RETAINED #if MQTT_SKIP_RETAINED
unsigned long mqttConnectedAt = 0;
unsigned long _mqttConnectedAt = 0;
#endif #endif
std::vector<void (*)(unsigned int, const char *, const char *)> _mqtt_callbacks;
typedef struct { typedef struct {
char * topic; char * topic;
char * message; char * message;
} mqtt_message_t; } mqtt_message_t;
std::vector<mqtt_message_t> _mqtt_queue; std::vector<mqtt_message_t> _mqtt_queue;
Ticker mqttFlushTicker;
Ticker _mqttFlushTicker;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Public API // Public API
@ -126,7 +127,7 @@ void mqttSend(const char * topic, const char * message, bool force) {
element.topic = strdup(topic); element.topic = strdup(topic);
element.message = strdup(message); element.message = strdup(message);
_mqtt_queue.push_back(element); _mqtt_queue.push_back(element);
mqttFlushTicker.once_ms(MQTT_USE_JSON_DELAY, _mqttFlush);
_mqttFlushTicker.once_ms(MQTT_USE_JSON_DELAY, _mqttFlush);
} else { } else {
String path = _mqttTopic + String(topic) + _mqttGetter; String path = _mqttTopic + String(topic) + _mqttGetter;
mqttSendRaw(path.c_str(), message); mqttSendRaw(path.c_str(), message);
@ -203,12 +204,9 @@ void _mqttOnConnect() {
DEBUG_MSG_P(PSTR("[MQTT] Connected!\n")); DEBUG_MSG_P(PSTR("[MQTT] Connected!\n"));
#if MQTT_SKIP_RETAINED #if MQTT_SKIP_RETAINED
mqttConnectedAt = millis();
_mqttConnectedAt = millis();
#endif #endif
// Build MQTT topics
mqttConfigure();
// Send first Heartbeat // Send first Heartbeat
heartbeat(); heartbeat();
@ -238,7 +236,7 @@ void _mqttOnMessage(char* topic, char* payload, unsigned int len) {
strlcpy(message, (char *) payload, len + 1); strlcpy(message, (char *) payload, len + 1);
#if MQTT_SKIP_RETAINED #if MQTT_SKIP_RETAINED
if (millis() - mqttConnectedAt < MQTT_SKIP_TIME) {
if (millis() - _mqttConnectedAt < MQTT_SKIP_TIME) {
DEBUG_MSG_P(PSTR("[MQTT] Received %s => %s - SKIPPED\n"), topic, message); DEBUG_MSG_P(PSTR("[MQTT] Received %s => %s - SKIPPED\n"), topic, message);
return; return;
} }
@ -293,21 +291,27 @@ bool mqttFormatFP(const char * fingerprint, char * destination) {
#endif #endif
void mqttEnabled(bool status) {
_mqttEnabled = status;
setSetting("mqttEnabled", status ? 1 : 0);
}
void mqttConnect() {
bool mqttEnabled() {
return _mqttEnabled;
}
if (!mqtt.connected()) {
void mqttConnect() {
if (getSetting("mqttServer", MQTT_SERVER).length() == 0) return;
if (_mqttEnabled & !mqtt.connected()) {
// Last option: reconnect to wifi after MQTT_MAX_TRIES attemps in a row
// Disable MQTT after MQTT_MAX_TRIES attemps in a row
#if MQTT_MAX_TRIES > 0 #if MQTT_MAX_TRIES > 0
static unsigned int tries = 0; static unsigned int tries = 0;
static unsigned long last_try = millis(); static unsigned long last_try = millis();
if (millis() - last_try < MQTT_TRY_INTERVAL) { if (millis() - last_try < MQTT_TRY_INTERVAL) {
if (++tries > MQTT_MAX_TRIES) { if (++tries > MQTT_MAX_TRIES) {
DEBUG_MSG_P(PSTR("[MQTT] MQTT_MAX_TRIES met, disconnecting from WiFi\n"));
wifiDisconnect();
DEBUG_MSG_P(PSTR("[MQTT] MQTT_MAX_TRIES met, disabling MQTT\n"));
mqttEnabled(false);
tries = 0; tries = 0;
return; return;
} }
@ -321,6 +325,7 @@ void mqttConnect() {
if (_mqttPass) free(_mqttPass); if (_mqttPass) free(_mqttPass);
char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str()); char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str());
if (strlen(host) == 0) return;
unsigned int port = getSetting("mqttPort", MQTT_PORT).toInt(); unsigned int port = getSetting("mqttPort", MQTT_PORT).toInt();
_mqttUser = strdup(getSetting("mqttUser").c_str()); _mqttUser = strdup(getSetting("mqttUser").c_str());
_mqttPass = strdup(getSetting("mqttPassword").c_str()); _mqttPass = strdup(getSetting("mqttPassword").c_str());
@ -423,13 +428,24 @@ void mqttConnect() {
} }
void mqttConfigure() { void mqttConfigure() {
// Replace identifier // Replace identifier
_mqttTopic = getSetting("mqttTopic", MQTT_TOPIC); _mqttTopic = getSetting("mqttTopic", MQTT_TOPIC);
_mqttTopic.replace("{identifier}", getSetting("hostname")); _mqttTopic.replace("{identifier}", getSetting("hostname"));
if (!_mqttTopic.endsWith("/")) _mqttTopic = _mqttTopic + "/"; if (!_mqttTopic.endsWith("/")) _mqttTopic = _mqttTopic + "/";
// Getters and setters
_mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER); _mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
_mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); _mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
_mqttForward = !_mqttGetter.equals(_mqttSetter); _mqttForward = !_mqttGetter.equals(_mqttSetter);
// Enable
if (getSetting("mqttServer", MQTT_SERVER).length() == 0) {
mqttEnabled(false);
} else {
_mqttEnabled = getSetting("mqttEnabled", MQTT_ENABLED).toInt() == 1;
}
} }
void mqttSetup() { void mqttSetup() {
@ -475,13 +491,14 @@ void mqttSetup() {
}); });
#endif #endif
mqttConfigure();
mqttRegister(_mqttCallback); mqttRegister(_mqttCallback);
} }
void mqttLoop() { void mqttLoop() {
static unsigned long lastPeriod = 0;
if (!_mqttEnabled) return;
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
@ -494,9 +511,9 @@ void mqttLoop() {
} }
#endif #endif
unsigned long currPeriod = millis() / MQTT_RECONNECT_DELAY;
if (currPeriod != lastPeriod) {
lastPeriod = currPeriod;
static unsigned long last = 0;
if (millis() - last > MQTT_RECONNECT_DELAY) {
last = millis();
mqttConnect(); mqttConnect();
} }


+ 2825
- 2825
code/espurna/static/index.html.gz.h
File diff suppressed because it is too large
View File


+ 6
- 10
code/espurna/web.ino View File

@ -346,6 +346,11 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
saveSettings(); saveSettings();
wifiConfigure(); wifiConfigure();
otaConfigure(); otaConfigure();
if (changedMQTT) {
mqttConfigure();
mqttDisconnect();
}
#if ALEXA_SUPPORT #if ALEXA_SUPPORT
alexaConfigure(); alexaConfigure();
#endif #endif
@ -355,22 +360,12 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) {
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
domoticzConfigure(); domoticzConfigure();
#endif #endif
mqttConfigure();
#if RF_SUPPORT #if RF_SUPPORT
rfBuildCodes(); rfBuildCodes();
#endif #endif
#if EMON_SUPPORT #if EMON_SUPPORT
setCurrentRatio(getSetting("emonRatio").toFloat()); setCurrentRatio(getSetting("emonRatio").toFloat());
#endif #endif
// Check if we should reconfigure MQTT connection
if (changedMQTT) {
mqttDisconnect();
}
// Check if we should reconfigure NTP connection
#if NTP_SUPPORT #if NTP_SUPPORT
if (changedNTP) ntpConnect(); if (changedNTP) ntpConnect();
#endif #endif
@ -437,6 +432,7 @@ void _wsStart(uint32_t client_id) {
#endif #endif
root["mqttStatus"] = mqttConnected(); root["mqttStatus"] = mqttConnected();
root["mqttEnabled"] = mqttEnabled();
root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER);
root["mqttPort"] = getSetting("mqttPort", MQTT_PORT); root["mqttPort"] = getSetting("mqttPort", MQTT_PORT);
root["mqttUser"] = getSetting("mqttUser"); root["mqttUser"] = getSetting("mqttUser");


+ 6
- 1
code/html/index.html View File

@ -519,7 +519,12 @@
<fieldset> <fieldset>
<div class="pure-g"> <div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="mqttServer">MQTT Server</label>
<div class="pure-u-1 pure-u-sm-1-4"><label for="mqttEnabled">Enable MQTT</label></div>
<div class="pure-u-1 pure-u-sm-1-4"><input type="checkbox" name="mqttEnabled" tabindex="30" /></div>
</div>
<div class="pure-g">
<label class="pure-u-1 pure-u-md-1-4" for="mqttServer">MQTT Broker</label>
<input class="pure-u-1 pure-u-md-3-4" name="mqttServer" type="text" size="20" tabindex="21" placeholder="IP or address of your broker" /> <input class="pure-u-1 pure-u-md-3-4" name="mqttServer" type="text" size="20" tabindex="21" placeholder="IP or address of your broker" />
</div> </div>


Loading…
Cancel
Save