Browse Source

Option to disable MQTT at build time

fastled
Xose Pérez 7 years ago
parent
commit
d1abe1fc65
22 changed files with 2723 additions and 2630 deletions
  1. +3
    -1
      code/espurna/analog.ino
  2. +4
    -0
      code/espurna/button.ino
  3. +8
    -3
      code/espurna/config/general.h
  4. +0
    -4
      code/espurna/config/prototypes.h
  5. +3
    -1
      code/espurna/counter.ino
  6. BIN
      code/espurna/data/index.html.gz
  7. +4
    -2
      code/espurna/dht.ino
  8. +3
    -1
      code/espurna/ds18b20.ino
  9. +6
    -2
      code/espurna/espurna.ino
  10. +5
    -1
      code/espurna/led.ino
  11. +27
    -19
      code/espurna/light.ino
  12. +2
    -0
      code/espurna/mdns.ino
  13. +5
    -0
      code/espurna/mqtt.ino
  14. +3
    -0
      code/espurna/power.ino
  15. +13
    -4
      code/espurna/relay.ino
  16. +12
    -3
      code/espurna/rfbridge.ino
  17. +8
    -6
      code/espurna/settings.ino
  18. +2523
    -2522
      code/espurna/static/index.html.gz.h
  19. +69
    -48
      code/espurna/utils.ino
  20. +3
    -1
      code/espurna/wifi.ino
  21. +17
    -7
      code/espurna/ws.ino
  22. +5
    -5
      code/html/index.html

+ 3
- 1
code/espurna/analog.ino View File

@ -55,7 +55,9 @@ void analogLoop() {
DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog); DEBUG_MSG_P(PSTR("[ANALOG] Value: %d\n"), analog);
// Send MQTT messages // Send MQTT messages
mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
#if MQTT_SUPPORT
mqttSend(getSetting("analogTopic", ANALOG_TOPIC).c_str(), String(analog).c_str());
#endif
// Send to Domoticz // Send to Domoticz
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT


+ 4
- 0
code/espurna/button.ino View File

@ -21,6 +21,7 @@ typedef struct {
std::vector<button_t> _buttons; std::vector<button_t> _buttons;
#if MQTT_SUPPORT
#ifdef MQTT_TOPIC_BUTTON #ifdef MQTT_TOPIC_BUTTON
void buttonMQTT(unsigned char id, uint8_t event) { void buttonMQTT(unsigned char id, uint8_t event) {
if (id >= _buttons.size()) return; if (id >= _buttons.size()) return;
@ -29,6 +30,7 @@ void buttonMQTT(unsigned char id, uint8_t event) {
mqttSend(MQTT_TOPIC_BUTTON, id, payload); mqttSend(MQTT_TOPIC_BUTTON, id, payload);
} }
#endif #endif
#endif
int buttonFromRelay(unsigned int relayID) { int buttonFromRelay(unsigned int relayID) {
for (unsigned int i=0; i < _buttons.size(); i++) { for (unsigned int i=0; i < _buttons.size(); i++) {
@ -81,9 +83,11 @@ void buttonEvent(unsigned int id, unsigned char event) {
DEBUG_MSG_P(PSTR("[BUTTON] Pressed #%d, event: %d\n"), id, event); DEBUG_MSG_P(PSTR("[BUTTON] Pressed #%d, event: %d\n"), id, event);
if (event == 0) return; if (event == 0) return;
#if MQTT_SUPPORT
#ifdef MQTT_TOPIC_BUTTON #ifdef MQTT_TOPIC_BUTTON
buttonMQTT(id, event); buttonMQTT(id, event);
#endif #endif
#endif
unsigned char action = buttonAction(id, event); unsigned char action = buttonAction(id, event);


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

@ -369,8 +369,13 @@ PROGMEM const char* const custom_reset_string[] = {
// MQTT // MQTT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#ifndef MQTT_SUPPORT
#define MQTT_SUPPORT 1 // MQTT support (22.38Kb async, 12.48Kb sync)
#endif
#ifndef MQTT_USE_ASYNC #ifndef MQTT_USE_ASYNC
#define MQTT_USE_ASYNC 1 // Use AysncMQTTClient (1) or PubSubClient (0, 9.83Kb less)
#define MQTT_USE_ASYNC 1 // Use AysncMQTTClient (1) or PubSubClient
#endif #endif
// MQTT OVER SSL // MQTT OVER SSL
@ -673,7 +678,7 @@ PROGMEM const char* const custom_reset_string[] = {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#ifndef DOMOTICZ_SUPPORT #ifndef DOMOTICZ_SUPPORT
#define DOMOTICZ_SUPPORT 1 // Build with domoticz support (1.72Kb)
#define DOMOTICZ_SUPPORT MQTT_SUPPORT // Build with domoticz (if MQTT) support (1.72Kb)
#endif #endif
#define DOMOTICZ_ENABLED 0 // Disable domoticz by default #define DOMOTICZ_ENABLED 0 // Disable domoticz by default
@ -686,7 +691,7 @@ PROGMEM const char* const custom_reset_string[] = {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#ifndef HOMEASSISTANT_SUPPORT #ifndef HOMEASSISTANT_SUPPORT
#define HOMEASSISTANT_SUPPORT 1 // Build with home assistant support (1.64Kb)
#define HOMEASSISTANT_SUPPORT MQTT_SUPPORT // Build with home assistant support (if MQTT, 1.64Kb)
#endif #endif
#define HOMEASSISTANT_ENABLED 0 // Integration not enabled by default #define HOMEASSISTANT_ENABLED 0 // Integration not enabled by default


+ 0
- 4
code/espurna/config/prototypes.h View File

@ -6,8 +6,6 @@ extern "C" {
#include "user_interface.h" #include "user_interface.h"
} }
#if WEB_SUPPORT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// WebServer // WebServer
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -34,8 +32,6 @@ void wsOnActionRegister(ws_on_action_callback_f callback);
typedef std::function<void(void)> ws_on_after_parse_callback_f; typedef std::function<void(void)> ws_on_after_parse_callback_f;
void wsOnAfterParseRegister(ws_on_after_parse_callback_f callback); void wsOnAfterParseRegister(ws_on_after_parse_callback_f callback);
#endif // WEB_SUPPORT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// MQTT // MQTT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------


+ 3
- 1
code/espurna/counter.ino View File

@ -82,7 +82,9 @@ void counterLoop() {
if (_counterBufferPointer == 0) { if (_counterBufferPointer == 0) {
// Send MQTT messages // Send MQTT messages
mqttSend(getSetting("counterTopic", COUNTER_TOPIC).c_str(), String(_counterValue).c_str());
#if MQTT_SUPPORT
mqttSend(getSetting("counterTopic", COUNTER_TOPIC).c_str(), String(_counterValue).c_str());
#endif
// Send to Domoticz // Send to Domoticz
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT


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


+ 4
- 2
code/espurna/dht.ino View File

@ -232,8 +232,10 @@ void dhtLoop() {
last_humidity = h; last_humidity = h;
// Send MQTT messages // Send MQTT messages
mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
#if MQTT_SUPPORT
mqttSend(getSetting("dhtTmpTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend(getSetting("dhtHumTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
#endif
// Send to Domoticz // Send to Domoticz
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT


+ 3
- 1
code/espurna/ds18b20.ino View File

@ -120,7 +120,9 @@ void dsLoop() {
last_temperature = _dsTemperature; last_temperature = _dsTemperature;
// Send MQTT messages // Send MQTT messages
mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
#if MQTT_SUPPORT
mqttSend(getSetting("dsTmpTopic", DS18B20_TEMPERATURE_TOPIC).c_str(), temperature);
#endif
// Send to Domoticz // Send to Domoticz
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT


+ 6
- 2
code/espurna/espurna.ino View File

@ -269,7 +269,9 @@ void setup() {
relaySetup(); relaySetup();
buttonSetup(); buttonSetup();
ledSetup(); ledSetup();
mqttSetup();
#if MQTT_SUPPORT
mqttSetup();
#endif
#if MDNS_SUPPORT #if MDNS_SUPPORT
mdnsSetup(); mdnsSetup();
@ -352,7 +354,9 @@ void loop() {
relayLoop(); relayLoop();
buttonLoop(); buttonLoop();
ledLoop(); ledLoop();
mqttLoop();
#if MQTT_SUPPORT
mqttLoop();
#endif
#ifdef ITEAD_SONOFF_RFBRIDGE #ifdef ITEAD_SONOFF_RFBRIDGE
rfbLoop(); rfbLoop();


+ 5
- 1
code/espurna/led.ino View File

@ -58,6 +58,7 @@ void showStatus() {
} }
#endif #endif
#if MQTT_SUPPORT
void ledMQTTCallback(unsigned int type, const char * topic, const char * payload) { void ledMQTTCallback(unsigned int type, const char * topic, const char * payload) {
static bool isFirstMessage = true; static bool isFirstMessage = true;
@ -99,6 +100,7 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload
} }
} }
#endif
unsigned char ledCount() { unsigned char ledCount() {
return _leds.size(); return _leds.size();
@ -130,7 +132,9 @@ void ledSetup() {
ledConfigure(); ledConfigure();
mqttRegister(ledMQTTCallback);
#if MQTT_SUPPORT
mqttRegister(ledMQTTCallback);
#endif
DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size()); DEBUG_MSG_P(PSTR("[LED] Number of leds: %d\n"), _leds.size());
DEBUG_MSG_P(PSTR("[LED] Led auto indicator is %s\n"), ledAuto ? "ON" : "OFF" ); DEBUG_MSG_P(PSTR("[LED] Led auto indicator is %s\n"), ledAuto ? "ON" : "OFF" );


+ 27
- 19
code/espurna/light.ino View File

@ -433,6 +433,7 @@ void _lightColorRestore() {
// MQTT // MQTT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if MQTT_SUPPORT
void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) { void _lightMQTTCallback(unsigned int type, const char * topic, const char * payload) {
@ -501,23 +502,6 @@ void _lightMQTTCallback(unsigned int type, const char * topic, const char * payl
} }
// -----------------------------------------------------------------------------
// API
// -----------------------------------------------------------------------------
unsigned char lightChannels() {
return _channels.size();
}
bool lightHasColor() {
bool useColor = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
return useColor && (_channels.size() > 2);
}
unsigned char lightWhiteChannels() {
return _channels.size() % 3;
}
void lightMQTT() { void lightMQTT() {
char buffer[12]; char buffer[12];
@ -549,12 +533,33 @@ void lightMQTT() {
} }
#endif
// -----------------------------------------------------------------------------
// API
// -----------------------------------------------------------------------------
unsigned char lightChannels() {
return _channels.size();
}
bool lightHasColor() {
bool useColor = getSetting("useColor", LIGHT_USE_COLOR).toInt() == 1;
return useColor && (_channels.size() > 2);
}
unsigned char lightWhiteChannels() {
return _channels.size() % 3;
}
void lightUpdate(bool save, bool forward) { void lightUpdate(bool save, bool forward) {
_lightProviderUpdate(); _lightProviderUpdate();
// Report color & brightness to MQTT broker // Report color & brightness to MQTT broker
if (forward) lightMQTT();
#if MQTT_SUPPORT
if (forward) lightMQTT();
#endif
// Report color to WS clients (using current brightness setting) // Report color to WS clients (using current brightness setting)
#if WEB_SUPPORT #if WEB_SUPPORT
@ -897,6 +902,7 @@ void lightSetup() {
DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _channels.size()); DEBUG_MSG_P(PSTR("[LIGHT] Number of channels: %d\n"), _channels.size());
_lightColorRestore(); _lightColorRestore();
#if WEB_SUPPORT #if WEB_SUPPORT
_lightAPISetup(); _lightAPISetup();
wsOnSendRegister(_lightWebSocketOnSend); wsOnSendRegister(_lightWebSocketOnSend);
@ -906,7 +912,9 @@ void lightSetup() {
#endif #endif
#endif #endif
mqttRegister(_lightMQTTCallback);
#if MQTT_SUPPORT
mqttRegister(_lightMQTTCallback);
#endif
} }


+ 2
- 0
code/espurna/mdns.ino View File

@ -13,6 +13,7 @@ Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
WiFiEventHandler _mdns_wifi_onSTA; WiFiEventHandler _mdns_wifi_onSTA;
WiFiEventHandler _mdns_wifi_onAP; WiFiEventHandler _mdns_wifi_onAP;
#if MQTT_SUPPORT
void mdnsFindMQTT() { void mdnsFindMQTT() {
int count = MDNS.queryService("mqtt", "tcp"); int count = MDNS.queryService("mqtt", "tcp");
DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count); DEBUG_MSG_P("[MQTT] MQTT brokers found: %d\n", count);
@ -21,6 +22,7 @@ void mdnsFindMQTT() {
mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i)); mqttSetBrokerIfNone(MDNS.IP(i), MDNS.port(i));
} }
} }
#endif
void _mdnsStart() { void _mdnsStart() {
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())) {


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

@ -6,6 +6,8 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/ */
#if MQTT_SUPPORT
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -201,6 +203,7 @@ void mqttRegister(mqtt_callback_f callback) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void _mqttWebSocketOnSend(JsonObject& root) { void _mqttWebSocketOnSend(JsonObject& root) {
root["mqttVisible"] = 1;
root["mqttStatus"] = mqttConnected(); root["mqttStatus"] = mqttConnected();
root["mqttEnabled"] = mqttEnabled(); root["mqttEnabled"] = mqttEnabled();
root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER); root["mqttServer"] = getSetting("mqttServer", MQTT_SERVER);
@ -602,3 +605,5 @@ void mqttLoop() {
#endif #endif
} }
#endif // MQTT_SUPPORT

+ 3
- 0
code/espurna/power.ino View File

@ -227,6 +227,8 @@ void _powerReport() {
dtostrf(_power_current, 1-sizeof(buf_current), POWER_CURRENT_DECIMALS, buf_current); dtostrf(_power_current, 1-sizeof(buf_current), POWER_CURRENT_DECIMALS, buf_current);
dtostrf(energy_delta * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_delta), POWER_ENERGY_DECIMALS, buf_energy_delta); dtostrf(energy_delta * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_delta), POWER_ENERGY_DECIMALS, buf_energy_delta);
dtostrf(_power_energy * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_total), POWER_ENERGY_DECIMALS, buf_energy_total); dtostrf(_power_energy * POWER_ENERGY_FACTOR, 1-sizeof(buf_energy_total), POWER_ENERGY_DECIMALS, buf_energy_total);
#if MQTT_SUPPORT
{ {
mqttSend(MQTT_TOPIC_CURRENT, buf_current); mqttSend(MQTT_TOPIC_CURRENT, buf_current);
mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str()); mqttSend(MQTT_TOPIC_POWER_APPARENT, String((int) _power_apparent).c_str());
@ -239,6 +241,7 @@ void _powerReport() {
mqttSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str()); mqttSend(MQTT_TOPIC_POWER_FACTOR, String((int) 100 * _power_factor).c_str());
#endif #endif
} }
#endif
#if DOMOTICZ_SUPPORT #if DOMOTICZ_SUPPORT
if (domoticzEnabled()) { if (domoticzEnabled()) {


+ 13
- 4
code/espurna/relay.ino View File

@ -143,6 +143,7 @@ void relayPulseMode(unsigned int value, bool report) {
setSetting("relayPulseMode", value); setSetting("relayPulseMode", value);
/* /*
#if MQTT_SUPPORT
if (report) { if (report) {
char topic[strlen(MQTT_TOPIC_RELAY) + 10]; char topic[strlen(MQTT_TOPIC_RELAY) + 10];
snprintf_P(topic, sizeof(topic), PSTR("%s/pulse"), MQTT_TOPIC_RELAY); snprintf_P(topic, sizeof(topic), PSTR("%s/pulse"), MQTT_TOPIC_RELAY);
@ -150,6 +151,7 @@ void relayPulseMode(unsigned int value, bool report) {
snprintf_P(value, sizeof(value), PSTR("%d"), value); snprintf_P(value, sizeof(value), PSTR("%d"), value);
mqttSend(topic, value); mqttSend(topic, value);
} }
#endif
*/ */
#if WEB_SUPPORT #if WEB_SUPPORT
@ -465,6 +467,8 @@ void relayWS() {
// MQTT // MQTT
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#if MQTT_SUPPORT
void relayMQTT(unsigned char id) { void relayMQTT(unsigned char id) {
if (id >= _relays.size()) return; if (id >= _relays.size()) return;
mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0"); mqttSend(MQTT_TOPIC_RELAY, id, relayStatus(id) ? "1" : "0");
@ -533,6 +537,8 @@ void relaySetupMQTT() {
mqttRegister(relayMQTTCallback); mqttRegister(relayMQTTCallback);
} }
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// InfluxDB // InfluxDB
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -593,7 +599,10 @@ void relaySetup() {
wsOnSendRegister(_relayWebSocketOnSend); wsOnSendRegister(_relayWebSocketOnSend);
wsOnActionRegister(_relayWebSocketOnAction); wsOnActionRegister(_relayWebSocketOnAction);
#endif #endif
relaySetupMQTT();
#if MQTT_SUPPORT
relaySetupMQTT();
#endif
DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size()); DEBUG_MSG_P(PSTR("[RELAY] Number of relays: %d\n"), _relays.size());
@ -621,9 +630,9 @@ void relayLoop(void) {
} }
// Send MQTT report if requested // Send MQTT report if requested
if (_relays[id].scheduledReport) {
relayMQTT(id);
}
#if MQTT_SUPPORT
if (_relays[id].scheduledReport) relayMQTT(id);
#endif
if (!recursive) { if (!recursive) {
relayPulse(id); relayPulse(id);


+ 12
- 3
code/espurna/rfbridge.ino View File

@ -131,8 +131,10 @@ void _rfbDecode() {
} }
if (action == RF_CODE_LEARN_OK || action == RF_CODE_RFIN) { if (action == RF_CODE_LEARN_OK || action == RF_CODE_RFIN) {
_rfbToChar(&_uartbuf[1], buffer);
mqttSend(MQTT_TOPIC_RFIN, buffer);
#if MQTT_SUPPORT
_rfbToChar(&_uartbuf[1], buffer);
mqttSend(MQTT_TOPIC_RFIN, buffer);
#endif
_rfbAck(); _rfbAck();
} }
@ -243,6 +245,7 @@ bool _rfbToChar(byte * in, char * out) {
return true; return true;
} }
#if MQTT_SUPPORT
void _rfbMqttCallback(unsigned int type, const char * topic, const char * payload) { void _rfbMqttCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) { if (type == MQTT_CONNECT_EVENT) {
@ -287,6 +290,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
} }
} }
#endif
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// PUBLIC // PUBLIC
@ -343,11 +347,16 @@ void rfbForget(unsigned char id, bool status) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
void rfbSetup() { void rfbSetup() {
mqttRegister(_rfbMqttCallback);
#if MQTT_SUPPORT
mqttRegister(_rfbMqttCallback);
#endif
#if WEB_SUPPORT #if WEB_SUPPORT
wsOnSendRegister(_rfbWebSocketOnSend); wsOnSendRegister(_rfbWebSocketOnSend);
wsOnActionRegister(_rfbWebSocketOnAction); wsOnActionRegister(_rfbWebSocketOnAction);
#endif #endif
} }
void rfbLoop() { void rfbLoop() {


+ 8
- 6
code/espurna/settings.ino View File

@ -132,12 +132,14 @@ void settingsSetup() {
DEBUG_MSG_P(PSTR("+OK\n")); DEBUG_MSG_P(PSTR("+OK\n"));
}); });
Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
mqttConfigure();
mqttDisconnect();
DEBUG_MSG_P(PSTR("+OK\n"));
});
#if MQTT_SUPPORT
Embedis::command( F("RESET.MQTT"), [](Embedis* e) {
mqttConfigure();
mqttDisconnect();
DEBUG_MSG_P(PSTR("+OK\n"));
});
#endif
Embedis::command( F("INFO"), [](Embedis* e) { Embedis::command( F("INFO"), [](Embedis* e) {
welcome(); welcome();
DEBUG_MSG_P(PSTR("+OK\n")); DEBUG_MSG_P(PSTR("+OK\n"));


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


+ 69
- 48
code/espurna/utils.ino View File

@ -81,67 +81,88 @@ void heartbeat() {
unsigned long uptime_seconds = getUptime(); unsigned long uptime_seconds = getUptime();
unsigned int free_heap = ESP.getFreeHeap(); unsigned int free_heap = ESP.getFreeHeap();
#if NTP_SUPPORT
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
// -------------------------------------------------------------------------
// MQTT
// -------------------------------------------------------------------------
#if MQTT_SUPPORT
#if (HEARTBEAT_REPORT_INTERVAL)
mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
#endif
#if (HEARTBEAT_REPORT_APP)
mqttSend(MQTT_TOPIC_APP, APP_NAME);
#endif
#if (HEARTBEAT_REPORT_VERSION)
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif
#if (HEARTBEAT_REPORT_HOSTNAME)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif
#if (HEARTBEAT_REPORT_IP)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());
#endif
#if (HEARTBEAT_REPORT_MAC)
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
#endif
#if (HEARTBEAT_REPORT_RSSI)
mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
#endif
#if (HEARTBEAT_REPORT_UPTIME)
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif
#if (HEARTBEAT_REPORT_FREEHEAP)
mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#endif
#if (HEARTBEAT_REPORT_RELAY)
relayMQTT();
#endif
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
lightMQTT();
#endif
#if (HEARTBEAT_REPORT_VCC)
#if ADC_VCC_ENABLED
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif
#endif
#if (HEARTBEAT_REPORT_STATUS)
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
#endif
bool serial = !mqttConnected();
#else
bool serial = true;
#endif #endif
if (!mqttConnected()) {
// -------------------------------------------------------------------------
// Serial
// -------------------------------------------------------------------------
if (serial) {
DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds); DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds);
DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap); DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap);
#if ADC_VCC_ENABLED #if ADC_VCC_ENABLED
DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc()); DEBUG_MSG_P(PSTR("[MAIN] Power: %d mV\n"), ESP.getVcc());
#endif #endif
} }
#if (HEARTBEAT_REPORT_INTERVAL)
mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
#endif
#if (HEARTBEAT_REPORT_APP)
mqttSend(MQTT_TOPIC_APP, APP_NAME);
#endif
#if (HEARTBEAT_REPORT_VERSION)
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif
#if (HEARTBEAT_REPORT_HOSTNAME)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif
#if (HEARTBEAT_REPORT_IP)
mqttSend(MQTT_TOPIC_IP, getIP().c_str());
#endif
#if (HEARTBEAT_REPORT_MAC)
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
#endif
#if (HEARTBEAT_REPORT_RSSI)
mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
#if NTP_SUPPORT
DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str());
#endif #endif
#if (HEARTBEAT_REPORT_UPTIME)
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#if INFLUXDB_SUPPORT
idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
// -------------------------------------------------------------------------
// InfluxDB
// -------------------------------------------------------------------------
#if INFLUXDB_SUPPORT
#if (HEARTBEAT_REPORT_UPTIME)
idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif #endif
#endif
#if (HEARTBEAT_REPORT_FREEHEAP)
mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#if INFLUXDB_SUPPORT
idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#if (HEARTBEAT_REPORT_FREEHEAP)
idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#endif #endif
#endif #endif
#if (HEARTBEAT_REPORT_RELAY)
relayMQTT();
#endif
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
lightMQTT();
#endif
#if (HEARTBEAT_REPORT_VCC)
#if ADC_VCC_ENABLED
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif
#endif
#if (HEARTBEAT_REPORT_STATUS)
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
#endif
// Send info to websocket clients
// -------------------------------------------------------------------------
// WebSockets
// -------------------------------------------------------------------------
#if WEB_SUPPORT #if WEB_SUPPORT
#if NTP_SUPPORT #if NTP_SUPPORT
{ {


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

@ -295,9 +295,11 @@ void wifiSetup() {
#endif // DEBUG_SUPPORT #endif // DEBUG_SUPPORT
#if MQTT_SUPPORT
#if MDNS_SUPPORT #if MDNS_SUPPORT
if (code == MESSAGE_CONNECTED) mdnsFindMQTT(); if (code == MESSAGE_CONNECTED) mdnsFindMQTT();
#endif // MDNS_SUPPORT
#endif // MDNS_SUPPORT
#endif // MQTT_SUPPORT
}); });


+ 17
- 7
code/espurna/ws.ino View File

@ -26,10 +26,12 @@ std::vector<ws_on_after_parse_callback_f> _ws_on_after_parse_callbacks;
// Private methods // Private methods
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if MQTT_SUPPORT
void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) { void _wsMQTTCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": true}")); if (type == MQTT_CONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": true}"));
if (type == MQTT_DISCONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": false}")); if (type == MQTT_DISCONNECT_EVENT) wsSend_P(PSTR("{\"mqttStatus\": false}"));
} }
#endif
void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) { void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
@ -100,7 +102,9 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
bool save = false; bool save = false;
bool changed = false; bool changed = false;
bool changedMQTT = false;
#if MQTT_SUPPORT
bool changedMQTT = false;
#endif
unsigned int network = 0; unsigned int network = 0;
unsigned int dczRelayIdx = 0; unsigned int dczRelayIdx = 0;
@ -216,7 +220,9 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
if (value != getSetting(key)) { if (value != getSetting(key)) {
setSetting(key, value); setSetting(key, value);
save = changed = true; save = changed = true;
if (key.startsWith("mqtt")) changedMQTT = true;
#if MQTT_SUPPORT
if (key.startsWith("mqtt")) changedMQTT = true;
#endif
} }
} }
@ -235,10 +241,12 @@ void _wsParse(AsyncWebSocketClient *client, uint8_t * payload, size_t length) {
// This should got to callback as well // This should got to callback as well
// but first change management has to be in place // but first change management has to be in place
if (changedMQTT) {
mqttConfigure();
mqttDisconnect();
}
#if MQTT_SUPPORT
if (changedMQTT) {
mqttConfigure();
mqttDisconnect();
}
#endif
// Persist settings // Persist settings
saveSettings(); saveSettings();
@ -406,7 +414,9 @@ void wsSetup() {
_ws.onEvent(_wsEvent); _ws.onEvent(_wsEvent);
wsConfigure(); wsConfigure();
webServer()->addHandler(&_ws); webServer()->addHandler(&_ws);
mqttRegister(_wsMQTTCallback);
#if MQTT_SUPPORT
mqttRegister(_wsMQTTCallback);
#endif
wsOnAfterParseRegister(wsConfigure); wsOnAfterParseRegister(wsConfigure);
} }


+ 5
- 5
code/html/index.html View File

@ -91,7 +91,7 @@
</li> </li>
<li class="pure-menu-item"> <li class="pure-menu-item">
<a href="#" class="pure-menu-link" data="panel-mqtt">MQTT</a>
<a href="#" class="pure-menu-link module module-mqtt" data="panel-mqtt">MQTT</a>
</li> </li>
<li class="pure-menu-item module module-ntp"> <li class="pure-menu-item module module-ntp">
@ -264,11 +264,11 @@
<div class="pure-u-1 pure-u-sm-1-4">Free space</div> <div class="pure-u-1 pure-u-sm-1-4">Free space</div>
<div class="pure-u-1 pure-u-sm-17-24"><span class="right" name="free_size" post=" bytes"></span></div> <div class="pure-u-1 pure-u-sm-17-24"><span class="right" name="free_size" post=" bytes"></span></div>
<div class="pure-u-1 pure-u-sm-1-4">MQTT Status</div>
<div class="pure-u-1 pure-u-sm-17-24"><span class="right" name="mqttStatus"></span></div>
<div class="pure-u-1 pure-u-sm-1-4 module module-mqtt">MQTT Status</div>
<div class="pure-u-1 pure-u-sm-17-24 module module-mqtt"><span class="right" name="mqttStatus"></span></div>
<div class="pure-u-1 pure-u-sm-1-4">NTP Status</div>
<div class="pure-u-1 pure-u-sm-17-24"><span class="right" name="ntpStatus"></span></div>
<div class="pure-u-1 pure-u-sm-1-4 module module-ntp">NTP Status</div>
<div class="pure-u-1 pure-u-sm-17-24 module module-ntp"><span class="right" name="ntpStatus"></span></div>
</div> </div>


Loading…
Cancel
Save