From b7aa3cd88466260dce97050187925cb66cdedf5f Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 27 Sep 2019 18:13:00 +0200 Subject: [PATCH] Add initial support for MQTT logging Add initial support for MQTT logging using command MqttLog (#6498) --- sonoff/_changelog.ino | 1 + sonoff/i18n.h | 1 + sonoff/settings.h | 3 ++- sonoff/sonoff.h | 2 +- sonoff/support.ino | 1 + sonoff/xdrv_02_mqtt.ino | 41 +++++++++++++++++++++++++++++++++++++++-- 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index fa2183986..e6fb28658 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -2,6 +2,7 @@ * 6.6.0.14 20190925 * Change command Tariffx to allow time entries like 23 (hours), 1320 (minutes) or 23:00. NOTE: As this is development branch previous tariffs are lost! (#6488) * Remove support for define USE_DS18x20_LEGACY and legacy DS18x20 driver (#6486) + * Add initial support for MQTT logging using command MqttLog (#6498) * * 6.6.0.13 20190922 * Add command EnergyReset4 x,x to initialize total usage for two tarrifs diff --git a/sonoff/i18n.h b/sonoff/i18n.h index 023a528e8..46bc52ea0 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -291,6 +291,7 @@ #define D_JSON_BASE "BASE" // Commands xdrv_01_mqtt.ino +#define D_CMND_MQTTLOG "MqttLog" #define D_CMND_MQTTHOST "MqttHost" #define D_CMND_MQTTPORT "MqttPort" #define D_CMND_MQTTRETRY "MqttRetry" diff --git a/sonoff/settings.h b/sonoff/settings.h index ac2079412..a4470799a 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -227,7 +227,8 @@ struct SYSCFG { uint8_t weblog_level; // 1AC uint8_t mqtt_fingerprint[2][20]; // 1AD uint8_t adc_param_type; // 1D5 - uint8_t register8[18]; // 1D6 - 18 x 8-bit registers indexed by enum SettingsRegister8 + uint8_t register8[17]; // 1D6 - 17 x 8-bit registers indexed by enum SettingsRegister8 + uint8_t mqttlog_level; // 1E7 uint8_t sps30_inuse_hours; // 1E8 char mqtt_host[33]; // 1E9 - Keep together with below as being copied as one chunck with reset 6 uint16_t mqtt_port; // 20A - Keep together diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index ec317d7f5..1b29754a5 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -256,7 +256,7 @@ enum SettingsRegister8 { R8_SPARE00, R8_SPARE01, R8_SPARE02, R8_SPARE03, R8_SPARE04, R8_SPARE05, R8_SPARE06, R8_SPARE07, R8_SPARE08, R8_SPARE09, R8_SPARE10, R8_SPARE11, R8_SPARE12, R8_SPARE13, R8_SPARE14, R8_SPARE15, - R8_SPARE16, R8_SPARE17 }; // Max size is 18 (Settings.register8[]) + R8_SPARE16 }; // Max size is 17 (Settings.register8[]) enum DomoticzSensors {DZ_TEMP, DZ_TEMP_HUM, DZ_TEMP_HUM_BARO, DZ_POWER_ENERGY, DZ_ILLUMINANCE, DZ_COUNT, DZ_VOLTAGE, DZ_CURRENT, DZ_AIRQUALITY, DZ_P1_SMART_METER, DZ_MAX_SENSORS}; diff --git a/sonoff/support.ino b/sonoff/support.ino index 6fd41b8f0..10bbd2194 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -1571,6 +1571,7 @@ void AddLog(uint32_t loglevel) if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string } #endif // USE_WEBSERVER + if (!global_state.mqtt_down && (loglevel <= Settings.mqttlog_level)) { MqttPublishLogging(mxtime); } if (!global_state.wifi_down && (loglevel <= syslog_level)) { Syslog(); } } diff --git a/sonoff/xdrv_02_mqtt.ino b/sonoff/xdrv_02_mqtt.ino index 052c881a9..d1bf375a6 100644 --- a/sonoff/xdrv_02_mqtt.ino +++ b/sonoff/xdrv_02_mqtt.ino @@ -39,7 +39,7 @@ const char kMqttCommands[] PROGMEM = "|" // No prefix D_CMND_TLSKEY "|" #endif D_CMND_MQTTHOST "|" D_CMND_MQTTPORT "|" D_CMND_MQTTRETRY "|" D_CMND_STATETEXT "|" D_CMND_MQTTCLIENT "|" - D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|" + D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|" D_CMND_MQTTLOG "|" D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ; void (* const MqttCommand[])(void) PROGMEM = { @@ -53,7 +53,7 @@ void (* const MqttCommand[])(void) PROGMEM = { &CmndTlsKey, #endif &CmndMqttHost, &CmndMqttPort, &CmndMqttRetry, &CmndStateText, &CmndMqttClient, - &CmndFullTopic, &CmndPrefix, &CmndGroupTopic, &CmndTopic, &CmndPublish, + &CmndFullTopic, &CmndPrefix, &CmndGroupTopic, &CmndTopic, &CmndPublish, &CmndMqttlog, &CmndButtonTopic, &CmndSwitchTopic, &CmndButtonRetain, &CmndSwitchRetain, &CmndPowerRetain, &CmndSensorRetain }; struct MQTT { @@ -305,6 +305,35 @@ void MqttUnsubscribe(const char *topic) MqttUnsubscribeLib(topic); } +void MqttPublishLogging(const char *mxtime) +{ + if (Settings.flag.mqtt_enabled) { + if (MqttIsConnected()) { + + char saved_mqtt_data[MESSZ]; + memcpy(saved_mqtt_data, mqtt_data, sizeof(saved_mqtt_data)); +// ResponseTime_P(PSTR(",\"Log\":{\"%s\"}}"), log_data); // Will fail as some messages contain JSON + Response_P(PSTR("%s%s"), mxtime, log_data); // No JSON and ugly!! + + char romram[33]; + char stopic[TOPSZ]; + snprintf_P(romram, sizeof(romram), PSTR("LOGGING")); + GetTopic_P(stopic, STAT, mqtt_topic, romram); + + char *me; + if (!strcmp(Settings.mqtt_prefix[0], Settings.mqtt_prefix[1])) { + me = strstr(stopic, Settings.mqtt_prefix[0]); + if (me == stopic) { + mqtt_cmnd_publish += 3; + } + } + MqttPublishLib(stopic, false); + + memcpy(mqtt_data, saved_mqtt_data, sizeof(saved_mqtt_data)); + } + } +} + void MqttPublishDirect(const char* topic, bool retained) { char sretained[CMDSZ]; @@ -724,6 +753,14 @@ void CmndMqttPassword(void) } #endif // USE_MQTT_AWS_IOT +void CmndMqttlog(void) +{ + if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_ALL)) { + Settings.mqttlog_level = XdrvMailbox.payload; + } + ResponseCmndNumber(Settings.mqttlog_level); +} + void CmndMqttHost(void) { #if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT)