Skip to content

Commit

Permalink
Domoticz support for fanspeed
Browse files Browse the repository at this point in the history
Allow control of fanspeed from domoticz. To set up in domoticz create a
virtual selector switch and name the levels low/medium/high at priority
10/20/30 (the defaults). Add the device ID into the domoticz
configuration page under fan.
  • Loading branch information
mzealey committed Dec 3, 2018
1 parent 5b2ad61 commit 4d5c099
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions sonoff/language/en-GB.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
#define D_DOMOTICZ_CURRENT "Current/PM10"
#define D_DOMOTICZ_AIRQUALITY "AirQuality"
#define D_DOMOTICZ_UPDATE_TIMER "Update timer"
#define D_DOMOTICZ_FAN "Fan"

// xdrv_09_timers.ino
#define D_CONFIGURE_TIMER "Configure Timer"
Expand Down
4 changes: 3 additions & 1 deletion sonoff/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ struct SYSCFG {
uint16_t mcp230xx_int_timer; // 718
uint8_t rgbwwTable[5]; // 71A

byte free_71F[117]; // 71F
unsigned long domoticz_fan_idx; // 71F

byte free_723[113]; // 723

uint32_t drivers[3]; // 794
uint32_t monitors; // 7A0
Expand Down
6 changes: 5 additions & 1 deletion sonoff/sonoff.ino
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ void SetFanspeed(uint8_t fanspeed)
// uint8_t state = pgm_read_byte(kIFan02Speed +(speed *3) +i);
ExecuteCommandPower(i +2, state, SRC_IGNORE); // Use relay 2, 3 and 4
}
#ifdef USE_DOMOTICZ
DomoticzUpdateFanState();
#endif // USE_DOMOTICZ
}

void SetPulseTimer(uint8_t index, uint16_t time)
Expand Down Expand Up @@ -1393,7 +1396,8 @@ void ExecuteCommandPower(byte device, byte state, int source)
}
SetDevicePower(power, source);
#ifdef USE_DOMOTICZ
DomoticzUpdatePowerState(device);
// Don't publish power on eg fan devices
if (!IS_FAN_MODULE || publish_power) DomoticzUpdatePowerState(device);
#endif // USE_DOMOTICZ
#ifdef USE_KNX
KnxUpdatePowerState(device, power);
Expand Down
44 changes: 42 additions & 2 deletions sonoff/xdrv_07_domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ void MqttPublishDomoticzPowerState(byte device)
}
}

void MqttPublishDomoticzFanState()
{
if (Settings.flag.mqtt_enabled && Settings.domoticz_fan_idx) {
char sfan[8];
uint8_t fan_speed = GetFanspeed();
snprintf_P(sfan, sizeof(sfan), PSTR("%d"), (int)fan_speed * 10);
snprintf_P(mqtt_data, sizeof(mqtt_data), DOMOTICZ_MESSAGE,
Settings.domoticz_fan_idx, fan_speed == 0 ? 0 : 2, sfan, DomoticzBatteryQuality(), DomoticzRssiQuality());
MqttPublish(domoticz_in_topic);
}
}

void DomoticzUpdateFanState()
{
if (domoticz_update_flag)
MqttPublishDomoticzFanState();

domoticz_update_flag = 1;
}

void DomoticzUpdatePowerState(byte device)
{
if (domoticz_update_flag) {
Expand All @@ -110,6 +130,9 @@ void DomoticzMqttUpdate(void)
void DomoticzMqttSubscribe(void)
{
uint8_t maxdev = (devices_present > MAX_DOMOTICZ_IDX) ? MAX_DOMOTICZ_IDX : devices_present;
if (Settings.domoticz_fan_idx)
domoticz_subscribe = true;

for (byte i = 0; i < maxdev; i++) {
if (Settings.domoticz_relay_idx[i]) {
domoticz_subscribe = true;
Expand Down Expand Up @@ -178,6 +201,13 @@ boolean DomoticzMqttData(void)

if ((idx > 0) && (nvalue >= 0) && (nvalue <= 15)) {
uint8_t maxdev = (devices_present > MAX_DOMOTICZ_IDX) ? MAX_DOMOTICZ_IDX : devices_present;

if( idx == Settings.domoticz_fan_idx ) {
found = 1;
snprintf_P(XdrvMailbox.topic, XdrvMailbox.index, PSTR("/" D_CMND_FANSPEED));
snprintf_P(XdrvMailbox.data, XdrvMailbox.data_len, PSTR("%d"), nvalue == 2 ? atoi(domoticz["svalue1"]) / 10 : 0);
}

for (byte i = 0; i < maxdev; i++) {
if (idx == Settings.domoticz_relay_idx[i]) {
bool iscolordimmer = strcmp_P(domoticz["dtype"],PSTR("Color Switch")) == 0;
Expand Down Expand Up @@ -392,6 +422,8 @@ const char HTTP_FORM_DOMOTICZ_SENSOR[] PROGMEM =
"<tr><td style='width:260px'><b>" D_DOMOTICZ_SENSOR_IDX " {1</b> {2</td><td style='width:70px'><input id='l{1' name='l{1' placeholder='0' value='{5'></td></tr>";
const char HTTP_FORM_DOMOTICZ_TIMER[] PROGMEM =
"<tr><td style='width:260px'><b>" D_DOMOTICZ_UPDATE_TIMER "</b> (" STR(DOMOTICZ_UPDATE_TIMER) ")</td><td style='width:70px'><input id='ut' name='ut' placeholder='" STR(DOMOTICZ_UPDATE_TIMER) "' value='{6'</td></tr>";
const char HTTP_FORM_DOMOTICZ_FAN[] PROGMEM =
"<tr><td style='width:260px'><b>" D_DOMOTICZ_FAN "</b></td><td style='width:70px'><input id='fan' name='fan' value='{9'></td></tr>";

void HandleDomoticzConfiguration(void)
{
Expand Down Expand Up @@ -431,6 +463,10 @@ void HandleDomoticzConfiguration(void)
}
page += FPSTR(HTTP_FORM_DOMOTICZ_TIMER);
page.replace("{6", String((int)Settings.domoticz_update_timer));
if( IS_FAN_MODULE ) {
page += FPSTR(HTTP_FORM_DOMOTICZ_FAN);
page.replace("{9", String((int)Settings.domoticz_fan_idx));
}
page += F("</table>");
page += FPSTR(HTTP_FORM_END);
page += FPSTR(HTTP_BTN_CONF);
Expand Down Expand Up @@ -463,12 +499,16 @@ void DomoticzSaveSettings(void)
}
WebGetArg("ut", tmp, sizeof(tmp));
Settings.domoticz_update_timer = (!strlen(tmp)) ? DOMOTICZ_UPDATE_TIMER : atoi(tmp);
if (IS_FAN_MODULE) {
WebGetArg("fan", tmp, sizeof(tmp));
Settings.domoticz_fan_idx = (!strlen(tmp)) ? 0 : atoi(tmp);
}

snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DOMOTICZ D_CMND_IDX " %d,%d,%d,%d, " D_CMND_KEYIDX " %d,%d,%d,%d, " D_CMND_SWITCHIDX " %d,%d,%d,%d, " D_CMND_SENSORIDX " %s, " D_CMND_UPDATETIMER " %d"),
snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_DOMOTICZ D_CMND_IDX " %d,%d,%d,%d, " D_CMND_KEYIDX " %d,%d,%d,%d, " D_CMND_SWITCHIDX " %d,%d,%d,%d, " D_CMND_SENSORIDX " %s, " D_CMND_UPDATETIMER " %d, fan %d"),
Settings.domoticz_relay_idx[0], Settings.domoticz_relay_idx[1], Settings.domoticz_relay_idx[2], Settings.domoticz_relay_idx[3],
Settings.domoticz_key_idx[0], Settings.domoticz_key_idx[1], Settings.domoticz_key_idx[2], Settings.domoticz_key_idx[3],
Settings.domoticz_switch_idx[0], Settings.domoticz_switch_idx[1], Settings.domoticz_switch_idx[2], Settings.domoticz_switch_idx[3],
ssensor_indices, Settings.domoticz_update_timer);
ssensor_indices, Settings.domoticz_update_timer, Settings.domoticz_fan_idx);
AddLog(LOG_LEVEL_INFO);
}
#endif // USE_WEBSERVER
Expand Down

0 comments on commit 4d5c099

Please sign in to comment.