2018-03-07 22:37:07 +00:00
|
|
|
/*
|
|
|
|
xdrv_08_KNX.ino - KNX IP Protocol support for Sonoff-Tasmota
|
|
|
|
|
|
|
|
Copyright (C) 2018 Adrian Scillato
|
|
|
|
|
|
|
|
Based on esp-knx-ip library for KNX/IP communication on an ESP8266
|
|
|
|
Author: Nico Weichbrodt <envy>
|
|
|
|
Web: https://github.com/envy/esp-knx-ip
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_KNX
|
|
|
|
|
2018-03-17 06:43:14 +00:00
|
|
|
|
2018-03-10 02:52:33 +00:00
|
|
|
#include <esp-knx-ip.h> // Include KNX IP library
|
2018-03-17 06:43:14 +00:00
|
|
|
|
|
|
|
void relay_cb(message_t const &msg, void *arg);
|
2018-03-07 22:37:07 +00:00
|
|
|
|
2018-03-10 02:52:33 +00:00
|
|
|
address_t physaddr;
|
2018-03-17 06:43:14 +00:00
|
|
|
config_id_t enable_knx_id;
|
|
|
|
config_id_t disable_knx_id;
|
|
|
|
config_id_t ga_conf_id;
|
|
|
|
config_id_t cb_conf_id;
|
|
|
|
config_id_t update_rate_id;
|
|
|
|
|
|
|
|
typedef struct __device_parameters
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
config_id_t id;
|
|
|
|
bool show;
|
|
|
|
} device_parameters_t;
|
|
|
|
|
|
|
|
device_parameters_t device_param[] = {
|
|
|
|
{ D_SENSOR_RELAY " 1", 0, true},
|
|
|
|
{ D_SENSOR_RELAY " 2", 0, true},
|
|
|
|
{ D_SENSOR_RELAY " 3", 0, true},
|
|
|
|
{ D_SENSOR_RELAY " 4", 0, true},
|
|
|
|
// {"Send Temp", 5, true},
|
|
|
|
// {"BUTTON 1", 6, true},
|
|
|
|
{nullptr, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Translations
|
|
|
|
config_webUI_t config_webUI = {
|
|
|
|
D_CONFIGURE_KNX,
|
|
|
|
D_KNX_PHYSICAL_ADDRESS,
|
|
|
|
D_KNX_SET,
|
|
|
|
D_KNX_ADD,
|
|
|
|
D_DELETE
|
|
|
|
};
|
|
|
|
|
|
|
|
int device_param_quantity = 4;
|
|
|
|
bool flag_knx_enabled = true;
|
2018-03-07 22:37:07 +00:00
|
|
|
|
|
|
|
void KNXStart()
|
|
|
|
{
|
2018-03-17 06:43:14 +00:00
|
|
|
|
|
|
|
// KNX WebPage Configuration
|
|
|
|
// -------------------------
|
|
|
|
|
|
|
|
// The order of the knx.***_register_*** code, is the order that is going to be shown on the web page.
|
|
|
|
|
|
|
|
// Translations
|
|
|
|
knx.config_web_UI(config_webUI);
|
|
|
|
|
|
|
|
//knx.config_register_Title( D_KNX_PARAMETERS );
|
|
|
|
|
|
|
|
//knx.config_register_line();
|
|
|
|
|
|
|
|
//knx.config_register_SubTitle( D_KNX_GENERAL_CONFIG );
|
|
|
|
|
|
|
|
//Set Physical KNX Address of the device
|
|
|
|
knx.config_register_pa();
|
|
|
|
//knx.physical_address_set(Settings.knx_physs_addr);
|
|
|
|
knx.physical_address_set(knx.PA_to_address(1, 1, 1));
|
|
|
|
|
|
|
|
//knx.config_register_label( D_KNX_PHYSICAL_ADDRESS_NOTE );
|
|
|
|
|
|
|
|
//knx.config_register_blankspace();
|
|
|
|
|
|
|
|
knx.feedback_register_action("KNX: " D_ON, knx_toggle_flag_enabled, D_STOP, nullptr, knx_status_enabled);
|
|
|
|
knx.feedback_register_action("KNX: " D_OFF, knx_toggle_flag_enabled, D_START, nullptr, knx_status_disabled);
|
|
|
|
|
|
|
|
//knx.config_register_line();
|
|
|
|
|
|
|
|
//knx.config_register_SubTitle( D_KNX_GROUP_ADDRESS_TO_WRITE );
|
|
|
|
|
|
|
|
// Register Group Addresses to Send Data to
|
|
|
|
//for (int i = 0; i < Settings.knx_Registered_GA; ++i)
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
device_param[i].id = knx.config_register_ga(String(device_param[i].name));
|
|
|
|
|
|
|
|
//////buscar en la config el param para setearlo
|
|
|
|
//knx.config_set_ga(ga_conf, Settings.knx_GA_addr(i));
|
|
|
|
|
|
|
|
//knx.config_set_ga(device_param[i].id, knx.GA_to_address(2,2,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
knx.config_set_ga(device_param[0].id, knx.GA_to_address(2,2,1));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* /////config
|
|
|
|
Settings.knx.physs_addr = physical_address_get();
|
|
|
|
Settings.knx.flag_knx_enabled
|
|
|
|
|
|
|
|
k = 0
|
|
|
|
for j = 0 to max cant items (relay1,2,3,etc)
|
|
|
|
for i = 0 to max cant items por param (cant de addr a relay 1)
|
|
|
|
address_t = knx.config_get_ga(device_param[j].id, i + 1);
|
|
|
|
if address_t then
|
|
|
|
settings.knx.paramGA(k) = j;
|
|
|
|
settings.knx.addressGA(k) = address_t
|
|
|
|
k++
|
|
|
|
end if
|
|
|
|
next i
|
|
|
|
next j
|
|
|
|
settings.knx.Registered_GA = k - 1
|
|
|
|
|
|
|
|
settings.knx.update_rate = knx.config_get_int(update_rate_id)
|
|
|
|
|
|
|
|
k = 0
|
|
|
|
for j = 0 to max cant items (relay1,2,3,etc)
|
|
|
|
for i = 0 to max cant items por param (cant de addr a relay 1)
|
|
|
|
address_t = knx.config_get_cb(device_param[j].id, i + 1);
|
|
|
|
if address_t then
|
|
|
|
settings.knx.paramCB(k) = j;
|
|
|
|
settings.knx.addressCB(k) = address_t
|
|
|
|
k++
|
|
|
|
end if
|
|
|
|
next i
|
|
|
|
next j
|
|
|
|
settings.knx.Registered_CB = k - 1
|
|
|
|
|
|
|
|
|
|
|
|
*/ //////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//knx.config_register_blankspace();
|
|
|
|
|
|
|
|
//update_rate_id = knx.config_register_int( D_KNX_UPDATE_INTERVAL , Settings.knx_update_rate);
|
|
|
|
update_rate_id = knx.config_register_int( D_KNX_UPDATE_INTERVAL , 5);
|
|
|
|
|
|
|
|
//knx.config_register_line();
|
|
|
|
|
|
|
|
//knx.config_register_SubTitle( D_KNX_GROUP_ADDRESS_TO_READ );
|
|
|
|
|
|
|
|
// Register Group Addresses to Receive data from and execute callbacks
|
|
|
|
//for (int i = 0; i < Settings.knx_Registered_CB; ++i)
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
cb_conf_id = knx.callback_register(String(device_param[i].name), relay_cb, &device_param[i]);
|
|
|
|
//////cb_conf_id = knx.callback_register("Channel 1", relay_cb, &channels[i]);
|
|
|
|
//cb_conf_id = knx.callback_register(device_param[i].name, relay_cb, &device_param[i].id, &device_param[i].show, D_KNX_ADD);
|
|
|
|
//////buscar en la config el param para setearlo
|
|
|
|
//knx.callback_assign(ga_conf, Settings.knx_CB_addr(i));
|
|
|
|
knx.callback_assign(cb_conf_id, knx.GA_to_address(2,2,1));
|
|
|
|
}
|
|
|
|
|
|
|
|
//knx.config_register_line();
|
|
|
|
|
|
|
|
knx.feedback_register_action("", KNXSaveSettings, D_SAVE); // Save Button
|
|
|
|
|
|
|
|
knx.feedback_register_action("", KNX_Return_button, D_CONFIGURATION); // Save Button
|
|
|
|
|
|
|
|
// END KNX WebPage Configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-07 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KNXLoop()
|
|
|
|
{
|
2018-03-10 02:52:33 +00:00
|
|
|
// if (Settings.flag.knx_enabled) {
|
2018-03-17 06:43:14 +00:00
|
|
|
if (flag_knx_enabled) {
|
|
|
|
knx.loop(); // Process knx events
|
|
|
|
}
|
|
|
|
|
2018-03-07 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void KNX_EVERY_SECOND() {
|
2018-03-17 06:43:14 +00:00
|
|
|
|
2018-03-07 22:37:07 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-03-17 06:43:14 +00:00
|
|
|
void knx_toggle_flag_enabled(void *arg)
|
|
|
|
{
|
|
|
|
flag_knx_enabled = !flag_knx_enabled;
|
|
|
|
// if flag_knx_enabled then knx.pause else knx.stop
|
|
|
|
}
|
|
|
|
|
|
|
|
bool knx_status_enabled()
|
|
|
|
{
|
|
|
|
return flag_knx_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool knx_status_disabled()
|
|
|
|
{
|
|
|
|
return !flag_knx_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void relay_cb(message_t const &msg, void *arg)
|
|
|
|
{
|
|
|
|
device_parameters_t *chan = (device_parameters_t *)arg;
|
|
|
|
switch (msg.ct)
|
|
|
|
{
|
|
|
|
case KNX_CT_WRITE:
|
|
|
|
ExecuteCommandPower(chan->id, msg.data[0]);
|
|
|
|
knx.write_1bit(knx.config_get_ga(chan->id), msg.data[0]);
|
|
|
|
break;
|
|
|
|
case KNX_CT_READ:
|
|
|
|
//knx.answer_1bit(msg.received_on, chan->state);
|
|
|
|
knx.answer_1bit(msg.received_on, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KNXUpdatePowerState(byte device, power_t state)
|
|
|
|
{
|
|
|
|
bool power = bitRead(state, device -1);
|
|
|
|
knx.write_1bit(knx.config_get_ga(device_param[device -1].id), power);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KNX_Return_button(void *arg)
|
|
|
|
{
|
|
|
|
WebServer->sendHeader(F("Location"),F("/cn"));
|
|
|
|
WebServer->send(302);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KNXSaveSettings(void *arg)
|
2018-03-07 22:37:07 +00:00
|
|
|
{
|
2018-03-10 02:52:33 +00:00
|
|
|
/*
|
|
|
|
char stemp[20];
|
|
|
|
char ssensor_indices[6 * MAX_DOMOTICZ_SNS_IDX];
|
|
|
|
char tmp[100];
|
|
|
|
|
|
|
|
for (byte i = 0; i < MAX_DOMOTICZ_IDX; i++) {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("r%d"), i +1);
|
|
|
|
WebGetArg(stemp, tmp, sizeof(tmp));
|
|
|
|
Settings.domoticz_relay_idx[i] = (!strlen(tmp)) ? 0 : atoi(tmp);
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("k%d"), i +1);
|
|
|
|
WebGetArg(stemp, tmp, sizeof(tmp));
|
|
|
|
Settings.domoticz_key_idx[i] = (!strlen(tmp)) ? 0 : atoi(tmp);
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("s%d"), i +1);
|
|
|
|
WebGetArg(stemp, tmp, sizeof(tmp));
|
|
|
|
Settings.domoticz_switch_idx[i] = (!strlen(tmp)) ? 0 : atoi(tmp);
|
|
|
|
}
|
|
|
|
ssensor_indices[0] = '\0';
|
|
|
|
for (byte i = 0; i < DZ_MAX_SENSORS; i++) {
|
|
|
|
snprintf_P(stemp, sizeof(stemp), PSTR("l%d"), i +1);
|
|
|
|
WebGetArg(stemp, tmp, sizeof(tmp));
|
|
|
|
Settings.domoticz_sensor_idx[i] = (!strlen(tmp)) ? 0 : atoi(tmp);
|
|
|
|
snprintf_P(ssensor_indices, sizeof(ssensor_indices), PSTR("%s%s%d"), ssensor_indices, (strlen(ssensor_indices)) ? "," : "", Settings.domoticz_sensor_idx[i]);
|
2018-03-07 22:37:07 +00:00
|
|
|
}
|
2018-03-10 02:52:33 +00:00
|
|
|
WebGetArg("ut", tmp, sizeof(tmp));
|
|
|
|
Settings.domoticz_update_timer = (!strlen(tmp)) ? DOMOTICZ_UPDATE_TIMER : 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"),
|
|
|
|
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);
|
|
|
|
AddLog(LOG_LEVEL_INFO);
|
|
|
|
*/
|
2018-03-07 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XDRV_08
|
|
|
|
|
|
|
|
boolean Xdrv08(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
// if (Settings.flag.knx_enabled) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
KNXStart();
|
|
|
|
break;
|
|
|
|
case FUNC_LOOP:
|
|
|
|
KNXLoop();
|
|
|
|
break;
|
2018-03-17 06:43:14 +00:00
|
|
|
// case FUNC_SET_POWER:
|
|
|
|
// break;
|
2018-03-07 22:37:07 +00:00
|
|
|
// case FUNC_EVERY_SECOND;
|
|
|
|
// KNX_EVERY_SECOND();
|
2018-03-17 06:43:14 +00:00
|
|
|
// break;
|
2018-03-07 22:37:07 +00:00
|
|
|
// case FUNC_COMMAND:
|
|
|
|
// result = MqttCommand();
|
|
|
|
// break;
|
|
|
|
}
|
2018-03-17 06:43:14 +00:00
|
|
|
// }
|
2018-03-07 22:37:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_KNX
|