Skip to content

Commit

Permalink
Merge pull request #51 from arendst/development
Browse files Browse the repository at this point in the history
Update from Tasmota
  • Loading branch information
ascillato authored Apr 13, 2018
2 parents 93ac1a3 + 2b196bb commit ea07c79
Show file tree
Hide file tree
Showing 11 changed files with 528 additions and 55 deletions.
3 changes: 2 additions & 1 deletion sonoff/_releasenotes.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* 5.12.0k
* Prepare for simple rules by enlarging Settings area to now 2048 bytes
* Prepare for simple rules of up to 255 characters by enlarging Settings area to now 2048 bytes
* Change Timer parameter name from Power to Action
* Add commands Publish, Rule, RuleTimer and Event. See Wiki about Rule restriction, usage and examples
* Fix freeing more code space when emulation is disabled (#1592)
* Fix update temperature on DS18x20 drivers (#2328)
* Fix compile error when not defined USE_TIMERS (#2400)
Expand Down
47 changes: 25 additions & 22 deletions sonoff/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,28 +138,6 @@
#define D_RSLT_WARNING "WARNING"

// Commands sonoff.ino
#define D_CMND_MQTTHOST "MqttHost"
#define D_CMND_MQTTPORT "MqttPort"
#define D_CMND_MQTTRETRY "MqttRetry"
#define D_CMND_STATETEXT "StateText"
#define D_CMND_MQTTFINGERPRINT "MqttFingerprint"
#define D_CMND_MQTTCLIENT "MqttClient"
#define D_CMND_MQTTUSER "MqttUser"
#define D_CMND_MQTTPASSWORD "MqttPassword"
#define D_CMND_FULLTOPIC "FullTopic"
#define D_CMND_PREFIX "Prefix"
#define PRFX_MAX_STRING_LENGTH 5
#define D_CMND "cmnd"
#define D_STAT "stat"
#define D_TELE "tele"
#define D_CMND_GROUPTOPIC "GroupTopic"
#define D_CMND_TOPIC "Topic"
#define D_CMND_BUTTONTOPIC "ButtonTopic"
#define D_CMND_SWITCHTOPIC "SwitchTopic"
#define D_CMND_BUTTONRETAIN "ButtonRetain"
#define D_CMND_SWITCHRETAIN "SwitchRetain"
#define D_CMND_POWERRETAIN "PowerRetain"
#define D_CMND_SENSORRETAIN "SensorRetain"
#define D_CMND_BACKLOG "Backlog"
#define D_CMND_DELAY "Delay"
#define D_CMND_STATUS "Status"
Expand Down Expand Up @@ -251,6 +229,31 @@
#define D_CMND_BAUDRATE "Baudrate"
#define D_CMND_EXCEPTION "Exception"

// Commands xdrv_00_mqtt.ino
#define D_CMND_MQTTHOST "MqttHost"
#define D_CMND_MQTTPORT "MqttPort"
#define D_CMND_MQTTRETRY "MqttRetry"
#define D_CMND_STATETEXT "StateText"
#define D_CMND_MQTTFINGERPRINT "MqttFingerprint"
#define D_CMND_MQTTCLIENT "MqttClient"
#define D_CMND_MQTTUSER "MqttUser"
#define D_CMND_MQTTPASSWORD "MqttPassword"
#define D_CMND_FULLTOPIC "FullTopic"
#define D_CMND_PREFIX "Prefix"
#define PRFX_MAX_STRING_LENGTH 5
#define D_CMND "cmnd"
#define D_STAT "stat"
#define D_TELE "tele"
#define D_CMND_GROUPTOPIC "GroupTopic"
#define D_CMND_TOPIC "Topic"
#define D_CMND_BUTTONTOPIC "ButtonTopic"
#define D_CMND_SWITCHTOPIC "SwitchTopic"
#define D_CMND_BUTTONRETAIN "ButtonRetain"
#define D_CMND_SWITCHRETAIN "SwitchRetain"
#define D_CMND_POWERRETAIN "PowerRetain"
#define D_CMND_SENSORRETAIN "SensorRetain"
#define D_CMND_PUBLISH "Publish"

// Commands xdrv_01_light.ino
#define D_CMND_CHANNEL "Channel"
#define D_CMND_COLOR "Color"
Expand Down
4 changes: 1 addition & 3 deletions sonoff/sonoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ enum LichtSubtypes {LST_NONE, LST_SINGLE, LST_COLDWARM, LST_RGB, LST_RGBW, LST_R
enum LichtSchemes {LS_POWER, LS_WAKEUP, LS_CYCLEUP, LS_CYCLEDN, LS_RANDOM, LS_MAX};

enum XsnsFunctions {FUNC_INIT, FUNC_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_SECOND, FUNC_PREP_BEFORE_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_APPEND, FUNC_SAVE_BEFORE_RESTART,
FUNC_COMMAND, FUNC_NTP_INIT, FUNC_NTP_SET, FUNC_CLOCK_TIMER,
FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_MQTT_DISCONNECTED, FUNC_MQTT_CONNECTED,
FUNC_SET_POWER, FUNC_SHOW_SENSOR};
FUNC_COMMAND, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_MQTT_DATA, FUNC_SET_POWER, FUNC_SHOW_SENSOR};

const uint8_t kDefaultRfCode[9] PROGMEM = { 0x21, 0x16, 0x01, 0x0E, 0x03, 0x48, 0x2E, 0x1A, 0x00 };

Expand Down
21 changes: 13 additions & 8 deletions sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ void MqttDataHandler(char* topic, byte* data, unsigned int data_len)

/********************************************************************************************/

boolean send_button_power(byte key, byte device, byte state)
boolean SendKey(byte key, byte device, byte state)
{
// key 0 = button_topic
// key 1 = switch_topic
Expand Down Expand Up @@ -1130,13 +1130,18 @@ boolean send_button_power(byte key, byte device, byte state)
snprintf_P(mqtt_data, sizeof(mqtt_data), GetStateText(state));
}
#ifdef USE_DOMOTICZ
if (!(DomoticzButton(key, device, state, strlen(mqtt_data)))) {
if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) {
MqttPublishDirect(stopic, (key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain);
}
#else
MqttPublishDirect(stopic, (key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain);
#endif // USE_DOMOTICZ
result = true;
#ifdef USE_RULES
} else {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":{\"State\":%d}}"), (key) ? "Switch" : "Button", device, state);
result = RulesProcess();
#endif // USE_RULES
}
#ifdef USE_KNX
KNX_Send_Button_Power(key, device, state);
Expand Down Expand Up @@ -1533,7 +1538,7 @@ void ButtonHandler()
if (!holdbutton[button_index]) button_pressed = true; // Do not allow within 1 second
}
if (button_pressed) {
if (!send_button_power(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
ExecuteCommandPower(button_index +1, POWER_TOGGLE); // Execute Toggle command internally
}
}
Expand All @@ -1542,7 +1547,7 @@ void ButtonHandler()
if (Settings.flag.button_single) { // Allow only single button press for immediate action
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1);
AddLog(LOG_LEVEL_DEBUG);
if (!send_button_power(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
ExecuteCommandPower(button_index +1, POWER_TOGGLE); // Execute Toggle command internally
}
} else {
Expand All @@ -1568,7 +1573,7 @@ void ButtonHandler()
if (Settings.flag.button_restrict) { // Button restriction
if (holdbutton[button_index] == Settings.param[P_HOLD_TIME] * (STATES / 10)) { // Button hold
multipress[button_index] = 0;
send_button_power(0, button_index +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
SendKey(0, button_index +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
}
} else {
if (holdbutton[button_index] == (Settings.param[P_HOLD_TIME] * (STATES / 10)) * hold_time_extent) { // Button held for factor times longer
Expand All @@ -1594,7 +1599,7 @@ void ButtonHandler()
multipress[button_index] = 1;
}
}
if (single_press && send_button_power(0, button_index + multipress[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
if (single_press && SendKey(0, button_index + multipress[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
// Success
} else {
if (multipress[button_index] < 3) { // Single or Double press
Expand Down Expand Up @@ -1635,7 +1640,7 @@ void SwitchHandler()
if (holdwallswitch[i]) {
holdwallswitch[i]--;
if (0 == holdwallswitch[i]) {
send_button_power(1, i +1, 3); // Execute command via MQTT
SendKey(1, i +1, 3); // Execute command via MQTT
}
}

Expand Down Expand Up @@ -1688,7 +1693,7 @@ void SwitchHandler()
}

if (switchflag < 3) {
if (!send_button_power(1, i +1, switchflag)) { // Execute command via MQTT
if (!SendKey(1, i +1, switchflag)) { // Execute command via MQTT
ExecuteCommandPower(i +1, switchflag); // Execute command internally (if i < devices_present)
}
}
Expand Down
8 changes: 5 additions & 3 deletions sonoff/support.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1376,12 +1376,14 @@ void RtcSecond()
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION "(" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str());
AddLog(LOG_LEVEL_DEBUG);

#ifdef USE_RULES
if (local_time < 1451602800) { // 2016-01-01
XdrvCall(FUNC_NTP_INIT);
strncpy_P(mqtt_data, PSTR("{\"Time\":{\"Initialized\":1}}"), sizeof(mqtt_data));
} else {
XdrvCall(FUNC_NTP_SET);
strncpy_P(mqtt_data, PSTR("{\"Time\":{\"Set\":1}}"), sizeof(mqtt_data));
}
RulesProcess();
#endif // USE_RULES
} else {
ntp_sync_minute++; // Try again in next minute
}
Expand Down
3 changes: 3 additions & 0 deletions sonoff/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@
#define USE_TIMERS_WEB // Add timer webpage support (+4k5 code)
#define USE_SUNRISE // Add support for Sunrise and sunset tools (+16k)

// -- Rules ---------------------------------------
#define USE_RULES // Add support for rules (+4k4 code)

// -- Internal Analog input -----------------------
#define USE_ADC_VCC // Display Vcc in Power status. Disable for use as Analog input on selected devices

Expand Down
41 changes: 32 additions & 9 deletions sonoff/xdrv_00_mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@

enum MqttCommands {
CMND_MQTTHOST, CMND_MQTTPORT, CMND_MQTTRETRY, CMND_STATETEXT, CMND_MQTTFINGERPRINT, CMND_MQTTCLIENT,
CMND_MQTTUSER, CMND_MQTTPASSWORD, CMND_FULLTOPIC, CMND_PREFIX, CMND_GROUPTOPIC, CMND_TOPIC,
CMND_MQTTUSER, CMND_MQTTPASSWORD, CMND_FULLTOPIC, CMND_PREFIX, CMND_GROUPTOPIC, CMND_TOPIC, CMND_PUBLISH,
CMND_BUTTONTOPIC, CMND_SWITCHTOPIC, CMND_BUTTONRETAIN, CMND_SWITCHRETAIN, CMND_POWERRETAIN, CMND_SENSORRETAIN };
const char kMqttCommands[] PROGMEM =
D_CMND_MQTTHOST "|" D_CMND_MQTTPORT "|" D_CMND_MQTTRETRY "|" D_CMND_STATETEXT "|" D_CMND_MQTTFINGERPRINT "|" D_CMND_MQTTCLIENT "|"
D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|"
D_CMND_MQTTUSER "|" D_CMND_MQTTPASSWORD "|" D_CMND_FULLTOPIC "|" D_CMND_PREFIX "|" D_CMND_GROUPTOPIC "|" D_CMND_TOPIC "|" D_CMND_PUBLISH "|"
D_CMND_BUTTONTOPIC "|" D_CMND_SWITCHTOPIC "|" D_CMND_BUTTONRETAIN "|" D_CMND_SWITCHRETAIN "|" D_CMND_POWERRETAIN "|" D_CMND_SENSORRETAIN ;

uint8_t mqtt_retry_counter = 1; // MQTT connection retry counter
Expand Down Expand Up @@ -316,8 +316,10 @@ void MqttDisconnected(int state)
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_MQTT D_CONNECT_FAILED_TO " %s:%d, rc %d. " D_RETRY_IN " %d " D_UNIT_SECOND),
Settings.mqtt_host, Settings.mqtt_port, state, mqtt_retry_counter);
AddLog(LOG_LEVEL_INFO);

XdrvCall(FUNC_MQTT_DISCONNECTED);
#ifdef USE_RULES
strncpy_P(mqtt_data, PSTR("{\"MQTT\":{\"Disconnected\":1}}"), sizeof(mqtt_data));
RulesProcess();
#endif // USE_RULES
}

void MqttConnected()
Expand Down Expand Up @@ -369,12 +371,17 @@ void MqttConnected()
tele_period = Settings.tele_period -9;
}
status_update_timer = 2;

#ifdef USE_RULES
strncpy_P(mqtt_data, PSTR("{\"System\":{\"Boot\":1}}"), sizeof(mqtt_data));
RulesProcess();
#endif // USE_RULES
XdrvCall(FUNC_MQTT_INIT);
}
mqtt_initial_connection_state = 0;

XdrvCall(FUNC_MQTT_CONNECTED);
#ifdef USE_RULES
strncpy_P(mqtt_data, PSTR("{\"MQTT\":{\"Connected\":1}}"), sizeof(mqtt_data));
RulesProcess();
#endif // USE_RULES
}

#ifdef USE_MQTT_TLS
Expand Down Expand Up @@ -614,6 +621,22 @@ bool MqttCommand()
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_INDEX_SVALUE, command, index, Settings.mqtt_prefix[index -1]);
}
else if (CMND_PUBLISH == command_code) {
if (data_len > 0) {
char *mqtt_part = strtok(dataBuf, " ");
if (mqtt_part) {
snprintf(stemp1, sizeof(stemp1), mqtt_part);
mqtt_part = strtok(NULL, " ");
if (mqtt_part) {
snprintf(mqtt_data, sizeof(mqtt_data), mqtt_part);
} else {
mqtt_data[0] = '\0';
}
MqttPublishDirect(stemp1, false);
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_COMMAND_SVALUE, command, D_JSON_DONE);
}
}
}
else if (CMND_GROUPTOPIC == command_code) {
if ((data_len > 0) && (data_len < sizeof(Settings.mqtt_grptopic))) {
MakeValidMqtt(0, dataBuf);
Expand Down Expand Up @@ -658,7 +681,7 @@ bool MqttCommand()
strlcpy(Settings.button_topic, mqtt_topic, sizeof(Settings.button_topic));
if (!payload) {
for(i = 1; i <= MAX_KEYS; i++) {
send_button_power(0, i, 9); // Clear MQTT retain in broker
SendKey(0, i, 9); // Clear MQTT retain in broker
}
}
Settings.flag.mqtt_button_retain = payload;
Expand All @@ -670,7 +693,7 @@ bool MqttCommand()
strlcpy(Settings.button_topic, mqtt_topic, sizeof(Settings.button_topic));
if (!payload) {
for(i = 1; i <= MAX_SWITCHES; i++) {
send_button_power(1, i, 9); // Clear MQTT retain in broker
SendKey(1, i, 9); // Clear MQTT retain in broker
}
}
Settings.flag.mqtt_switch_retain = payload;
Expand Down
2 changes: 1 addition & 1 deletion sonoff/xdrv_05_domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ boolean DomoticzCommand()
return serviced;
}

boolean DomoticzButton(byte key, byte device, byte state, byte svalflg)
boolean DomoticzSendKey(byte key, byte device, byte state, byte svalflg)
{
if ((Settings.domoticz_key_idx[device -1] || Settings.domoticz_switch_idx[device -1]) && (svalflg)) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
Expand Down
6 changes: 3 additions & 3 deletions sonoff/xdrv_09_timers.ino
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ void TimerEverySecond()
Settings.timer[i].arm = Settings.timer[i].repeat;
#ifdef USE_RULES
if (3 == Settings.timer[i].power) { // Blink becomes Rule disregarding device and allowing use of Backlog commands
XdrvMailbox.index = i;
XdrvCall(FUNC_CLOCK_TIMER);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Clock\":{\"Timer\":%d}}"), i +1);
RulesProcess();
} else
#endif
#endif // USE_RULES
ExecuteCommandPower(Settings.timer[i].device +1, Settings.timer[i].power);
}
}
Expand Down
Loading

0 comments on commit ea07c79

Please sign in to comment.