xdrv_11_knx.ino 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. xdrv_11_knx.ino - KNX IP Protocol support for Sonoff-Tasmota
  3. Copyright (C) 2018 Adrian Scillato (https://github.com/ascillato)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifdef USE_KNX
  16. /*********************************************************************************************\
  17. * KNX support
  18. *
  19. * Using libraries:
  20. * ESP KNX IP library (https://github.com/envy/esp-knx-ip)
  21. Constants in sonoff.h
  22. -----------------------
  23. #define MAX_KNX_GA 10 Max number of KNX Group Addresses to read that can be set
  24. #define MAX_KNX_CB 10 Max number of KNX Group Addresses to write that can be set
  25. If you change MAX_KNX_CB you also have to change on the esp-knx-ip.h file the following:
  26. #define MAX_CALLBACK_ASSIGNMENTS 10
  27. #define MAX_CALLBACKS 10
  28. Both to MAX_KNX_CB
  29. Variables in settings.h
  30. -----------------------
  31. bool Settings.flag.knx_enabled Enable/Disable KNX Protocol
  32. uint16_t Settings.knx_physsical_addr Physical KNX address of this device
  33. byte Settings.knx_GA_registered Number of group address to read
  34. byte Settings.knx_CB_registered Number of group address to write
  35. uint16_t Settings.knx_GA_addr[MAX_KNX_GA] Group address to read
  36. uint16_t Settings.knx_CB_addr[MAX_KNX_CB] Group address to write
  37. byte Settings.knx_GA_param[MAX_KNX_GA] Type of Input (relay changed, button pressed, sensor read)
  38. byte Settings.knx_CB_param[MAX_KNX_CB] Type of Output (set relay, toggle relay, reply sensor value)
  39. \*********************************************************************************************/
  40. #define XDRV_11 11
  41. #include <esp-knx-ip.h> // KNX Library
  42. address_t KNX_physs_addr; // Physical KNX address of this device
  43. address_t KNX_addr; // KNX Address converter variable
  44. #define KNX_Empty 255
  45. #define TOGGLE_INHIBIT_TIME 15 // 15*50mseg = 750mseg (inhibit time for not toggling again relays by a KNX toggle command)
  46. float last_temp;
  47. float last_hum;
  48. byte toggle_inhibit;
  49. typedef struct __device_parameters
  50. {
  51. byte type; // PARAMETER_ID. Used as type of GA = relay, button, sensor, etc, (INPUTS)
  52. // used when an action on device triggers a MSG to send on KNX
  53. // Needed because this is the value that the ESP_KNX_IP library will pass as parameter
  54. // to identify the action to perform when a MSG is received
  55. bool show; // HARDWARE related. to identify if the parameter exists on the device.
  56. bool last_state; // LAST_STATE of relays
  57. callback_id_t CB_id; // ACTION_ID. To store the ID value of Registered_CB to the library.
  58. // The ESP_KNX_IP requires to register the callbacks, and then, to assign an address to the registered callback
  59. // So CB_id is needed to store the ID of the callback to then, assign multiple addresses to the same ID (callback)
  60. // It is used as type of CB = set relay, toggle relay, reply sensor, etc, (OUTPUTS)
  61. // used when a MSG receive KNX triggers an action on the device
  62. // - Multiples address to the same callback (i.e. Set Relay 1 Status) are used on scenes for example
  63. } device_parameters_t;
  64. // device parameters (information that can be sent)
  65. device_parameters_t device_param[] = {
  66. { 1, false, false, KNX_Empty }, // device_param[ 0] = Relay 1
  67. { 2, false, false, KNX_Empty }, // device_param[ 1] = Relay 2
  68. { 3, false, false, KNX_Empty }, // device_param[ 2] = Relay 3
  69. { 4, false, false, KNX_Empty }, // device_param[ 3] = Relay 4
  70. { 5, false, false, KNX_Empty }, // device_param[ 4] = Relay 5
  71. { 6, false, false, KNX_Empty }, // device_param[ 5] = Relay 6
  72. { 7, false, false, KNX_Empty }, // device_param[ 6] = Relay 7
  73. { 8, false, false, KNX_Empty }, // device_param[ 7] = Relay 8
  74. { 9, false, false, KNX_Empty }, // device_param[ 8] = Button 1
  75. { 10, false, false, KNX_Empty }, // device_param[ 9] = Button 2
  76. { 11, false, false, KNX_Empty }, // device_param[10] = Button 3
  77. { 12, false, false, KNX_Empty }, // device_param[11] = Button 4
  78. { 13, false, false, KNX_Empty }, // device_param[12] = Button 5
  79. { 14, false, false, KNX_Empty }, // device_param[13] = Button 6
  80. { 15, false, false, KNX_Empty }, // device_param[14] = Button 7
  81. { 16, false, false, KNX_Empty }, // device_param[15] = Button 8
  82. { KNX_TEMPERATURE, false, false, KNX_Empty }, // device_param[16] = Temperature
  83. { KNX_HUMIDITY , false, false, KNX_Empty }, // device_param[17] = humidity
  84. { KNX_ENERGY_VOLTAGE , false, false, KNX_Empty },
  85. { KNX_ENERGY_CURRENT , false, false, KNX_Empty },
  86. { KNX_ENERGY_POWER , false, false, KNX_Empty },
  87. { KNX_ENERGY_POWERFACTOR , false, false, KNX_Empty },
  88. { KNX_ENERGY_DAILY , false, false, KNX_Empty },
  89. { KNX_ENERGY_START , false, false, KNX_Empty },
  90. { KNX_ENERGY_TOTAL , false, false, KNX_Empty },
  91. { KNX_SLOT1 , false, false, KNX_Empty },
  92. { KNX_SLOT2 , false, false, KNX_Empty },
  93. { KNX_SLOT3 , false, false, KNX_Empty },
  94. { KNX_SLOT4 , false, false, KNX_Empty },
  95. { KNX_SLOT5 , false, false, KNX_Empty },
  96. { KNX_Empty, false, false, KNX_Empty}
  97. };
  98. // device parameters (information that can be sent)
  99. const char * device_param_ga[] = {
  100. D_TIMER_OUTPUT " 1", // Relay 1
  101. D_TIMER_OUTPUT " 2", // Relay 2
  102. D_TIMER_OUTPUT " 3", // Relay 3
  103. D_TIMER_OUTPUT " 4", // Relay 4
  104. D_TIMER_OUTPUT " 5", // Relay 5
  105. D_TIMER_OUTPUT " 6", // Relay 6
  106. D_TIMER_OUTPUT " 7", // Relay 7
  107. D_TIMER_OUTPUT " 8", // Relay 8
  108. D_SENSOR_BUTTON " 1", // Button 1
  109. D_SENSOR_BUTTON " 2", // Button 2
  110. D_SENSOR_BUTTON " 3", // Button 3
  111. D_SENSOR_BUTTON " 4", // Button 4
  112. D_SENSOR_BUTTON " 5", // Button 5
  113. D_SENSOR_BUTTON " 6", // Button 6
  114. D_SENSOR_BUTTON " 7", // Button 7
  115. D_SENSOR_BUTTON " 8", // Button 8
  116. D_TEMPERATURE , // Temperature
  117. D_HUMIDITY , // Humidity
  118. D_VOLTAGE ,
  119. D_CURRENT ,
  120. D_POWERUSAGE ,
  121. D_POWER_FACTOR ,
  122. D_ENERGY_TODAY ,
  123. D_ENERGY_YESTERDAY ,
  124. D_ENERGY_TOTAL ,
  125. D_KNX_TX_SLOT " 1",
  126. D_KNX_TX_SLOT " 2",
  127. D_KNX_TX_SLOT " 3",
  128. D_KNX_TX_SLOT " 4",
  129. D_KNX_TX_SLOT " 5",
  130. nullptr
  131. };
  132. // device actions (posible actions to be performed on the device)
  133. const char *device_param_cb[] = {
  134. D_TIMER_OUTPUT " 1", // Set Relay 1 (1-On or 0-OFF)
  135. D_TIMER_OUTPUT " 2",
  136. D_TIMER_OUTPUT " 3",
  137. D_TIMER_OUTPUT " 4",
  138. D_TIMER_OUTPUT " 5",
  139. D_TIMER_OUTPUT " 6",
  140. D_TIMER_OUTPUT " 7",
  141. D_TIMER_OUTPUT " 8",
  142. D_TIMER_OUTPUT " 1 " D_BUTTON_TOGGLE, // Relay 1 Toggle (1 or 0 will toggle)
  143. D_TIMER_OUTPUT " 2 " D_BUTTON_TOGGLE,
  144. D_TIMER_OUTPUT " 3 " D_BUTTON_TOGGLE,
  145. D_TIMER_OUTPUT " 4 " D_BUTTON_TOGGLE,
  146. D_TIMER_OUTPUT " 5 " D_BUTTON_TOGGLE,
  147. D_TIMER_OUTPUT " 6 " D_BUTTON_TOGGLE,
  148. D_TIMER_OUTPUT " 7 " D_BUTTON_TOGGLE,
  149. D_TIMER_OUTPUT " 8 " D_BUTTON_TOGGLE,
  150. D_REPLY " " D_TEMPERATURE, // Reply Temperature
  151. D_REPLY " " D_HUMIDITY, // Reply Humidity
  152. D_REPLY " " D_VOLTAGE ,
  153. D_REPLY " " D_CURRENT ,
  154. D_REPLY " " D_POWERUSAGE ,
  155. D_REPLY " " D_POWER_FACTOR ,
  156. D_REPLY " " D_ENERGY_TODAY ,
  157. D_REPLY " " D_ENERGY_YESTERDAY ,
  158. D_REPLY " " D_ENERGY_TOTAL ,
  159. D_KNX_RX_SLOT " 1",
  160. D_KNX_RX_SLOT " 2",
  161. D_KNX_RX_SLOT " 3",
  162. D_KNX_RX_SLOT " 4",
  163. D_KNX_RX_SLOT " 5",
  164. nullptr
  165. };
  166. // Commands
  167. #define D_CMND_KNXTXCMND "KnxTx_Cmnd"
  168. #define D_CMND_KNXTXVAL "KnxTx_Val"
  169. #define D_CMND_KNX_ENABLED "Knx_Enabled"
  170. #define D_CMND_KNX_ENHANCED "Knx_Enhanced"
  171. #define D_CMND_KNX_PA "Knx_PA"
  172. #define D_CMND_KNX_GA "Knx_GA"
  173. #define D_CMND_KNX_CB "Knx_CB"
  174. enum KnxCommands { CMND_KNXTXCMND, CMND_KNXTXVAL, CMND_KNX_ENABLED, CMND_KNX_ENHANCED, CMND_KNX_PA,
  175. CMND_KNX_GA, CMND_KNX_CB } ;
  176. const char kKnxCommands[] PROGMEM = D_CMND_KNXTXCMND "|" D_CMND_KNXTXVAL "|" D_CMND_KNX_ENABLED "|"
  177. D_CMND_KNX_ENHANCED "|" D_CMND_KNX_PA "|" D_CMND_KNX_GA "|" D_CMND_KNX_CB ;
  178. byte KNX_GA_Search( byte param, byte start = 0 )
  179. {
  180. for (byte i = start; i < Settings.knx_GA_registered; ++i)
  181. {
  182. if ( Settings.knx_GA_param[i] == param )
  183. {
  184. if ( Settings.knx_GA_addr[i] != 0 ) // Relay has group address set? GA=0/0/0 can not be used as KNX address, so it is used here as a: not set value
  185. {
  186. if ( i >= start ) { return i; }
  187. }
  188. }
  189. }
  190. return KNX_Empty;
  191. }
  192. byte KNX_CB_Search( byte param, byte start = 0 )
  193. {
  194. for (byte i = start; i < Settings.knx_CB_registered; ++i)
  195. {
  196. if ( Settings.knx_CB_param[i] == param )
  197. {
  198. if ( Settings.knx_CB_addr[i] != 0 )
  199. {
  200. if ( i >= start ) { return i; }
  201. }
  202. }
  203. }
  204. return KNX_Empty;
  205. }
  206. void KNX_ADD_GA( byte GAop, byte GA_FNUM, byte GA_AREA, byte GA_FDEF )
  207. {
  208. // Check if all GA were assigned. If yes-> return
  209. if ( Settings.knx_GA_registered >= MAX_KNX_GA ) { return; }
  210. if ( GA_FNUM == 0 && GA_AREA == 0 && GA_FDEF == 0 ) { return; }
  211. // Assign a GA to that address
  212. Settings.knx_GA_param[Settings.knx_GA_registered] = GAop;
  213. KNX_addr.ga.area = GA_FNUM;
  214. KNX_addr.ga.line = GA_AREA;
  215. KNX_addr.ga.member = GA_FDEF;
  216. Settings.knx_GA_addr[Settings.knx_GA_registered] = KNX_addr.value;
  217. Settings.knx_GA_registered++;
  218. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_ADD " GA #%d: %s " D_TO " %d/%d/%d"),
  219. Settings.knx_GA_registered,
  220. device_param_ga[GAop-1],
  221. GA_FNUM, GA_AREA, GA_FDEF );
  222. AddLog(LOG_LEVEL_DEBUG);
  223. }
  224. void KNX_DEL_GA( byte GAnum )
  225. {
  226. byte dest_offset = 0;
  227. byte src_offset = 0;
  228. byte len = 0;
  229. // Delete GA
  230. Settings.knx_GA_param[GAnum-1] = 0;
  231. if (GAnum == 1)
  232. {
  233. // start of array, so delete first entry
  234. src_offset = 1;
  235. // Settings.knx_GA_registered will be 1 in case of only one entry
  236. // Settings.knx_GA_registered will be 2 in case of two entries, etc..
  237. // so only copy anything, if there is it at least more then one element
  238. len = (Settings.knx_GA_registered - 1);
  239. }
  240. else if (GAnum == Settings.knx_GA_registered)
  241. {
  242. // last element, don't do anything, simply decrement counter
  243. }
  244. else
  245. {
  246. // somewhere in the middle
  247. // need to calc offsets
  248. // skip all prev elements
  249. dest_offset = GAnum -1 ; // GAnum -1 is equal to how many element are in front of it
  250. src_offset = dest_offset + 1; // start after the current element
  251. len = (Settings.knx_GA_registered - GAnum);
  252. }
  253. if (len > 0)
  254. {
  255. memmove(Settings.knx_GA_param + dest_offset, Settings.knx_GA_param + src_offset, len * sizeof(byte));
  256. memmove(Settings.knx_GA_addr + dest_offset, Settings.knx_GA_addr + src_offset, len * sizeof(uint16_t));
  257. }
  258. Settings.knx_GA_registered--;
  259. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_DELETE " GA #%d"),
  260. GAnum );
  261. AddLog(LOG_LEVEL_DEBUG);
  262. }
  263. void KNX_ADD_CB( byte CBop, byte CB_FNUM, byte CB_AREA, byte CB_FDEF )
  264. {
  265. // Check if all callbacks were assigned. If yes-> return
  266. if ( Settings.knx_CB_registered >= MAX_KNX_CB ) { return; }
  267. if ( CB_FNUM == 0 && CB_AREA == 0 && CB_FDEF == 0 ) { return; }
  268. // Check if a CB for CBop was registered on the ESP-KNX-IP Library
  269. if ( device_param[CBop-1].CB_id == KNX_Empty )
  270. {
  271. // if no, register the CB for CBop
  272. device_param[CBop-1].CB_id = knx.callback_register("", KNX_CB_Action, &device_param[CBop-1]);
  273. // KNX IP Library requires a parameter
  274. // to identify which action was requested on the KNX network
  275. // to be performed on this device (set relay, etc.)
  276. // Is going to be used device_param[j].type that stores the type number (1: relay 1, etc)
  277. }
  278. // Assign a callback to CB address
  279. Settings.knx_CB_param[Settings.knx_CB_registered] = CBop;
  280. KNX_addr.ga.area = CB_FNUM;
  281. KNX_addr.ga.line = CB_AREA;
  282. KNX_addr.ga.member = CB_FDEF;
  283. Settings.knx_CB_addr[Settings.knx_CB_registered] = KNX_addr.value;
  284. knx.callback_assign( device_param[CBop-1].CB_id, KNX_addr );
  285. Settings.knx_CB_registered++;
  286. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_ADD " CB #%d: %d/%d/%d " D_TO " %s"),
  287. Settings.knx_CB_registered,
  288. CB_FNUM, CB_AREA, CB_FDEF,
  289. device_param_cb[CBop-1] );
  290. AddLog(LOG_LEVEL_DEBUG);
  291. }
  292. void KNX_DEL_CB( byte CBnum )
  293. {
  294. byte oldparam = Settings.knx_CB_param[CBnum-1];
  295. byte dest_offset = 0;
  296. byte src_offset = 0;
  297. byte len = 0;
  298. // Delete assigment
  299. knx.callback_unassign(CBnum-1);
  300. Settings.knx_CB_param[CBnum-1] = 0;
  301. if (CBnum == 1)
  302. {
  303. // start of array, so delete first entry
  304. src_offset = 1;
  305. // Settings.knx_CB_registered will be 1 in case of only one entry
  306. // Settings.knx_CB_registered will be 2 in case of two entries, etc..
  307. // so only copy anything, if there is it at least more then one element
  308. len = (Settings.knx_CB_registered - 1);
  309. }
  310. else if (CBnum == Settings.knx_CB_registered)
  311. {
  312. // last element, don't do anything, simply decrement counter
  313. }
  314. else
  315. {
  316. // somewhere in the middle
  317. // need to calc offsets
  318. // skip all prev elements
  319. dest_offset = CBnum -1 ; // GAnum -1 is equal to how many element are in front of it
  320. src_offset = dest_offset + 1; // start after the current element
  321. len = (Settings.knx_CB_registered - CBnum);
  322. }
  323. if (len > 0)
  324. {
  325. memmove(Settings.knx_CB_param + dest_offset, Settings.knx_CB_param + src_offset, len * sizeof(byte));
  326. memmove(Settings.knx_CB_addr + dest_offset, Settings.knx_CB_addr + src_offset, len * sizeof(uint16_t));
  327. }
  328. Settings.knx_CB_registered--;
  329. // Check if there is no other assigment to that callback. If there is not. delete that callback register
  330. if ( KNX_CB_Search( oldparam ) == KNX_Empty ) {
  331. knx.callback_deregister( device_param[oldparam-1].CB_id );
  332. device_param[oldparam-1].CB_id = KNX_Empty;
  333. }
  334. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_DELETE " CB #%d"), CBnum );
  335. AddLog(LOG_LEVEL_DEBUG);
  336. }
  337. bool KNX_CONFIG_NOT_MATCH(void)
  338. {
  339. // Check for configured parameters that the device does not have (module changed)
  340. for (byte i = 0; i < KNX_MAX_device_param; ++i)
  341. {
  342. if ( !device_param[i].show ) { // device has this parameter ?
  343. // if not, search for all registered group address to this parameter for deletion
  344. // Checks all GA
  345. if ( KNX_GA_Search(i+1) != KNX_Empty ) { return true; }
  346. // Check all CB
  347. if ( i < 8 ) // check relays (i from 8 to 15 are toggle relays parameters)
  348. {
  349. if ( KNX_CB_Search(i+1) != KNX_Empty ) { return true; }
  350. if ( KNX_CB_Search(i+9) != KNX_Empty ) { return true; }
  351. }
  352. // check sensors and others
  353. if ( i > 15 )
  354. {
  355. if ( KNX_CB_Search(i+1) != KNX_Empty ) { return true; }
  356. }
  357. }
  358. }
  359. // Check for invalid or erroneous configuration (tasmota flashed without clearing the memory)
  360. for (byte i = 0; i < Settings.knx_GA_registered; ++i)
  361. {
  362. if ( Settings.knx_GA_param[i] != 0 ) // the GA[i] have a parameter defined?
  363. {
  364. if ( Settings.knx_GA_addr[i] == 0 ) // the GA[i] with parameter have the 0/0/0 as address?
  365. {
  366. return true; // So, it is invalid. Reset KNX configuration
  367. }
  368. }
  369. }
  370. for (byte i = 0; i < Settings.knx_CB_registered; ++i)
  371. {
  372. if ( Settings.knx_CB_param[i] != 0 ) // the CB[i] have a parameter defined?
  373. {
  374. if ( Settings.knx_CB_addr[i] == 0 ) // the CB[i] with parameter have the 0/0/0 as address?
  375. {
  376. return true; // So, it is invalid. Reset KNX configuration
  377. }
  378. }
  379. }
  380. return false;
  381. }
  382. void KNXStart(void)
  383. {
  384. knx.start(nullptr);
  385. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_START));
  386. AddLog(LOG_LEVEL_DEBUG);
  387. }
  388. void KNX_INIT(void)
  389. {
  390. // Check for incompatible config
  391. if (Settings.knx_GA_registered > MAX_KNX_GA) { Settings.knx_GA_registered = MAX_KNX_GA; }
  392. if (Settings.knx_CB_registered > MAX_KNX_CB) { Settings.knx_CB_registered = MAX_KNX_CB; }
  393. // Set Physical KNX Address of the device
  394. KNX_physs_addr.value = Settings.knx_physsical_addr;
  395. knx.physical_address_set( KNX_physs_addr );
  396. // Read Configuration
  397. // Check which relays, buttons and sensors where configured for this device
  398. // and activate options according to the hardware
  399. /*for (int i = GPIO_REL1; i < GPIO_REL8 + 1; ++i)
  400. {
  401. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_REL1].show = true; }
  402. }
  403. for (int i = GPIO_REL1_INV; i < GPIO_REL8_INV + 1; ++i)
  404. {
  405. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_REL1_INV].show = true; }
  406. }*/
  407. for (int i = 0; i < devices_present; ++i)
  408. {
  409. device_param[i].show = true;
  410. }
  411. for (int i = GPIO_SWT1; i < GPIO_SWT4 + 1; ++i)
  412. {
  413. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_SWT1 + 8].show = true; }
  414. }
  415. for (int i = GPIO_KEY1; i < GPIO_KEY4 + 1; ++i)
  416. {
  417. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_KEY1 + 8].show = true; }
  418. }
  419. for (int i = GPIO_SWT1_NP; i < GPIO_SWT4_NP + 1; ++i)
  420. {
  421. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_SWT1_NP + 8].show = true; }
  422. }
  423. for (int i = GPIO_KEY1_NP; i < GPIO_KEY4_NP + 1; ++i)
  424. {
  425. if (GetUsedInModule(i, my_module.gp.io)) { device_param[i - GPIO_KEY1_NP + 8].show = true; }
  426. }
  427. if (GetUsedInModule(GPIO_DHT11, my_module.gp.io)) { device_param[KNX_TEMPERATURE-1].show = true; }
  428. if (GetUsedInModule(GPIO_DHT22, my_module.gp.io)) { device_param[KNX_TEMPERATURE-1].show = true; }
  429. if (GetUsedInModule(GPIO_SI7021, my_module.gp.io)) { device_param[KNX_TEMPERATURE-1].show = true; }
  430. if (GetUsedInModule(GPIO_DSB, my_module.gp.io)) { device_param[KNX_TEMPERATURE-1].show = true; }
  431. if (GetUsedInModule(GPIO_DHT11, my_module.gp.io)) { device_param[KNX_HUMIDITY-1].show = true; }
  432. if (GetUsedInModule(GPIO_DHT22, my_module.gp.io)) { device_param[KNX_HUMIDITY-1].show = true; }
  433. if (GetUsedInModule(GPIO_SI7021, my_module.gp.io)) { device_param[KNX_HUMIDITY-1].show = true; }
  434. // Sonoff 31 or Sonoff Pow or any HLW8012 based device or Sonoff POW R2 or Any device with a Pzem004T
  435. if ( ( SONOFF_S31 == Settings.module ) || ( SONOFF_POW_R2 == Settings.module ) || ( energy_flg != ENERGY_NONE ) ) {
  436. device_param[KNX_ENERGY_POWER-1].show = true;
  437. device_param[KNX_ENERGY_DAILY-1].show = true;
  438. device_param[KNX_ENERGY_START-1].show = true;
  439. device_param[KNX_ENERGY_TOTAL-1].show = true;
  440. device_param[KNX_ENERGY_VOLTAGE-1].show = true;
  441. device_param[KNX_ENERGY_CURRENT-1].show = true;
  442. device_param[KNX_ENERGY_POWERFACTOR-1].show = true;
  443. }
  444. #ifdef USE_RULES
  445. device_param[KNX_SLOT1-1].show = true;
  446. device_param[KNX_SLOT2-1].show = true;
  447. device_param[KNX_SLOT3-1].show = true;
  448. device_param[KNX_SLOT4-1].show = true;
  449. device_param[KNX_SLOT5-1].show = true;
  450. #endif
  451. // Delete from KNX settings all configuration is not anymore related to this device
  452. if (KNX_CONFIG_NOT_MATCH()) {
  453. Settings.knx_GA_registered = 0;
  454. Settings.knx_CB_registered = 0;
  455. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_DELETE " " D_KNX_PARAMETERS ));
  456. AddLog(LOG_LEVEL_DEBUG);
  457. }
  458. // Register Group Addresses to listen to
  459. // Search on the settings if there is a group address set for receive KNX messages for the type: device_param[j].type
  460. // If there is, register the group address on the KNX_IP Library to Receive data for Executing Callbacks
  461. byte j;
  462. for (byte i = 0; i < Settings.knx_CB_registered; ++i)
  463. {
  464. j = Settings.knx_CB_param[i];
  465. if ( j > 0 )
  466. {
  467. device_param[j-1].CB_id = knx.callback_register("", KNX_CB_Action, &device_param[j-1]); // KNX IP Library requires a parameter
  468. // to identify which action was requested on the KNX network
  469. // to be performed on this device (set relay, etc.)
  470. // Is going to be used device_param[j].type that stores the type number (1: relay 1, etc)
  471. KNX_addr.value = Settings.knx_CB_addr[i];
  472. knx.callback_assign( device_param[j-1].CB_id, KNX_addr );
  473. }
  474. }
  475. }
  476. void KNX_CB_Action(message_t const &msg, void *arg)
  477. {
  478. device_parameters_t *chan = (device_parameters_t *)arg;
  479. if (!(Settings.flag.knx_enabled)) { return; }
  480. char tempchar[33];
  481. if (msg.data_len == 1) {
  482. // COMMAND
  483. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_RECEIVED_FROM " %d.%d.%d " D_COMMAND " %s: %d " D_TO " %s"),
  484. msg.received_on.ga.area, msg.received_on.ga.line, msg.received_on.ga.member,
  485. (msg.ct == KNX_CT_WRITE) ? D_KNX_COMMAND_WRITE : (msg.ct == KNX_CT_READ) ? D_KNX_COMMAND_READ : D_KNX_COMMAND_OTHER,
  486. msg.data[0],
  487. device_param_cb[(chan->type)-1]);
  488. } else {
  489. // VALUE
  490. float tempvar = knx.data_to_2byte_float(msg.data);
  491. dtostrfd(tempvar,2,tempchar);
  492. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_RECEIVED_FROM " %d.%d.%d " D_COMMAND " %s: %s " D_TO " %s"),
  493. msg.received_on.ga.area, msg.received_on.ga.line, msg.received_on.ga.member,
  494. (msg.ct == KNX_CT_WRITE) ? D_KNX_COMMAND_WRITE : (msg.ct == KNX_CT_READ) ? D_KNX_COMMAND_READ : D_KNX_COMMAND_OTHER,
  495. tempchar,
  496. device_param_cb[(chan->type)-1]);
  497. }
  498. AddLog(LOG_LEVEL_INFO);
  499. switch (msg.ct)
  500. {
  501. case KNX_CT_WRITE:
  502. if (chan->type < 9) // Set Relays
  503. {
  504. ExecuteCommandPower(chan->type, msg.data[0], SRC_KNX);
  505. }
  506. else if (chan->type < 17) // Toggle Relays
  507. {
  508. if (!toggle_inhibit) {
  509. ExecuteCommandPower((chan->type) -8, 2, SRC_KNX);
  510. if (Settings.flag.knx_enable_enhancement) {
  511. toggle_inhibit = TOGGLE_INHIBIT_TIME;
  512. }
  513. }
  514. }
  515. #ifdef USE_RULES
  516. else if ((chan->type >= KNX_SLOT1) && (chan->type <= KNX_SLOT5)) // KNX RX SLOTs (write command)
  517. {
  518. if (!toggle_inhibit) {
  519. char command[25];
  520. if (msg.data_len == 1) {
  521. // Command received
  522. snprintf_P(command, sizeof(command), PSTR("event KNXRX_CMND%d=%d"), ((chan->type) - KNX_SLOT1 + 1 ), msg.data[0]);
  523. } else {
  524. // Value received
  525. snprintf_P(command, sizeof(command), PSTR("event KNXRX_VAL%d=%s"), ((chan->type) - KNX_SLOT1 + 1 ), tempchar);
  526. }
  527. ExecuteCommand(command, SRC_KNX);
  528. if (Settings.flag.knx_enable_enhancement) {
  529. toggle_inhibit = TOGGLE_INHIBIT_TIME;
  530. }
  531. }
  532. }
  533. #endif
  534. break;
  535. case KNX_CT_READ:
  536. if (chan->type < 9) // reply Relays status
  537. {
  538. knx.answer_1bit(msg.received_on, chan->last_state);
  539. if (Settings.flag.knx_enable_enhancement) {
  540. knx.answer_1bit(msg.received_on, chan->last_state);
  541. knx.answer_1bit(msg.received_on, chan->last_state);
  542. }
  543. }
  544. else if (chan->type == KNX_TEMPERATURE) // Reply Temperature
  545. {
  546. knx.answer_2byte_float(msg.received_on, last_temp);
  547. if (Settings.flag.knx_enable_enhancement) {
  548. knx.answer_2byte_float(msg.received_on, last_temp);
  549. knx.answer_2byte_float(msg.received_on, last_temp);
  550. }
  551. }
  552. else if (chan->type == KNX_HUMIDITY) // Reply Humidity
  553. {
  554. knx.answer_2byte_float(msg.received_on, last_hum);
  555. if (Settings.flag.knx_enable_enhancement) {
  556. knx.answer_2byte_float(msg.received_on, last_hum);
  557. knx.answer_2byte_float(msg.received_on, last_hum);
  558. }
  559. }
  560. #ifdef USE_RULES
  561. else if ((chan->type >= KNX_SLOT1) && (chan->type <= KNX_SLOT5)) // KNX RX SLOTs (read command)
  562. {
  563. if (!toggle_inhibit) {
  564. char command[25];
  565. snprintf_P(command, sizeof(command), PSTR("event KNXRX_REQ%d"), ((chan->type) - KNX_SLOT1 + 1 ) );
  566. ExecuteCommand(command, SRC_KNX);
  567. if (Settings.flag.knx_enable_enhancement) {
  568. toggle_inhibit = TOGGLE_INHIBIT_TIME;
  569. }
  570. }
  571. }
  572. #endif
  573. break;
  574. }
  575. }
  576. void KnxUpdatePowerState(byte device, power_t state)
  577. {
  578. if (!(Settings.flag.knx_enabled)) { return; }
  579. device_param[device -1].last_state = bitRead(state, device -1); // power state (on/off)
  580. // Search all the registered GA that has that output (variable: device) as parameter
  581. byte i = KNX_GA_Search(device);
  582. while ( i != KNX_Empty ) {
  583. KNX_addr.value = Settings.knx_GA_addr[i];
  584. knx.write_1bit(KNX_addr, device_param[device -1].last_state);
  585. if (Settings.flag.knx_enable_enhancement) {
  586. knx.write_1bit(KNX_addr, device_param[device -1].last_state);
  587. knx.write_1bit(KNX_addr, device_param[device -1].last_state);
  588. }
  589. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %d " D_SENT_TO " %d.%d.%d"),
  590. device_param_ga[device -1], device_param[device -1].last_state,
  591. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member);
  592. AddLog(LOG_LEVEL_INFO);
  593. i = KNX_GA_Search(device, i + 1);
  594. }
  595. }
  596. void KnxSendButtonPower(byte key, byte device, byte state)
  597. {
  598. // key 0 = button_topic
  599. // key 1 = switch_topic
  600. // state 0 = off
  601. // state 1 = on
  602. // state 2 = toggle
  603. // state 3 = hold
  604. // state 9 = clear retain flag
  605. if (!(Settings.flag.knx_enabled)) { return; }
  606. // if (key)
  607. // {
  608. // Search all the registered GA that has that output (variable: device) as parameter
  609. byte i = KNX_GA_Search(device + 8);
  610. while ( i != KNX_Empty ) {
  611. KNX_addr.value = Settings.knx_GA_addr[i];
  612. knx.write_1bit(KNX_addr, !(state == 0));
  613. if (Settings.flag.knx_enable_enhancement) {
  614. knx.write_1bit(KNX_addr, !(state == 0));
  615. knx.write_1bit(KNX_addr, !(state == 0));
  616. }
  617. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %d " D_SENT_TO " %d.%d.%d"),
  618. device_param_ga[device + 7], !(state == 0),
  619. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member);
  620. AddLog(LOG_LEVEL_INFO);
  621. i = KNX_GA_Search(device + 8, i + 1);
  622. }
  623. // }
  624. }
  625. void KnxSensor(byte sensor_type, float value)
  626. {
  627. if (sensor_type == KNX_TEMPERATURE)
  628. {
  629. last_temp = value;
  630. } else if (sensor_type == KNX_HUMIDITY)
  631. {
  632. last_hum = value;
  633. }
  634. if (!(Settings.flag.knx_enabled)) { return; }
  635. byte i = KNX_GA_Search(sensor_type);
  636. while ( i != KNX_Empty ) {
  637. KNX_addr.value = Settings.knx_GA_addr[i];
  638. knx.write_2byte_float(KNX_addr, value);
  639. if (Settings.flag.knx_enable_enhancement) {
  640. knx.write_2byte_float(KNX_addr, value);
  641. knx.write_2byte_float(KNX_addr, value);
  642. }
  643. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s " D_SENT_TO " %d.%d.%d "),
  644. device_param_ga[sensor_type -1],
  645. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member);
  646. AddLog(LOG_LEVEL_INFO);
  647. i = KNX_GA_Search(sensor_type, i+1);
  648. }
  649. }
  650. /*********************************************************************************************\
  651. * Presentation
  652. \*********************************************************************************************/
  653. #ifdef USE_WEBSERVER
  654. #ifdef USE_KNX_WEB_MENU
  655. const char S_CONFIGURE_KNX[] PROGMEM = D_CONFIGURE_KNX;
  656. const char HTTP_BTN_MENU_KNX[] PROGMEM =
  657. "<br/><form action='kn' method='get'><button>" D_CONFIGURE_KNX "</button></form>";
  658. const char HTTP_FORM_KNX[] PROGMEM =
  659. "<fieldset><legend style='text-align:left;'><b>&nbsp;" D_KNX_PARAMETERS "&nbsp;</b></legend><form method='post' action='kn'>"
  660. "<br/><center>"
  661. "<b>" D_KNX_PHYSICAL_ADDRESS " </b>"
  662. "<input style='width:12%;' type='number' name='area' min='0' max='15' value='{kna'> . "
  663. "<input style='width:12%;' type='number' name='line' min='0' max='15' value='{knl'> . "
  664. "<input style='width:12%;' type='number' name='member' min='0' max='255' value='{knm'>"
  665. "<br/><br/>" D_KNX_PHYSICAL_ADDRESS_NOTE "<br/><br/>"
  666. "<input style='width:10%;' id='b1' name='b1' type='checkbox'";
  667. const char HTTP_FORM_KNX1[] PROGMEM =
  668. "><b>" D_KNX_ENABLE " </b><input style='width:10%;' id='b2' name='b2' type='checkbox'";
  669. const char HTTP_FORM_KNX2[] PROGMEM =
  670. "><b>" D_KNX_ENHANCEMENT "</b><br/></center><br/>"
  671. "<fieldset><center>"
  672. "<b>" D_KNX_GROUP_ADDRESS_TO_WRITE "</b><hr>"
  673. "<select name='GAop' style='width:25%;'>";
  674. const char HTTP_FORM_KNX_OPT[] PROGMEM =
  675. "<option value='{vop}'>{nop}</option>";
  676. const char HTTP_FORM_KNX_GA[] PROGMEM =
  677. "<input style='width:12%;' type='number' id='GAfnum' name='GAfnum' min='0' max='31' value='0'> / "
  678. "<input style='width:12%;' type='number' id='GAarea' name='GAarea' min='0' max='7' value='0'> / "
  679. "<input style='width:12%;' type='number' id='GAfdef' name='GAfdef' min='0' max='255' value='0'> ";
  680. const char HTTP_FORM_KNX_ADD_BTN[] PROGMEM =
  681. "<button type='submit' onclick='fncbtnadd()' btndis name='btn_add' value='{btnval}' style='width:18%;'>" D_ADD "</button><br/><br/>"
  682. "<table style='width:80%; font-size: 14px;'><col width='250'><col width='30'>";
  683. const char HTTP_FORM_KNX_ADD_TABLE_ROW[] PROGMEM =
  684. "<tr><td><b>{optex} -> GAfnum / GAarea / GAfdef </b></td>"
  685. "<td><button type='submit' name='btn_del_ga' value='{opval}' class='button bred'> " D_DELETE " </button></td></tr>";
  686. const char HTTP_FORM_KNX3[] PROGMEM =
  687. "</table></center></fieldset><br/>"
  688. "<fieldset><form method='post' action='kn'><center>"
  689. "<b>" D_KNX_GROUP_ADDRESS_TO_READ "</b><hr>";
  690. const char HTTP_FORM_KNX4[] PROGMEM =
  691. "-> <select name='CBop' style='width:25%;'>";
  692. const char HTTP_FORM_KNX_ADD_TABLE_ROW2[] PROGMEM =
  693. "<tr><td><b>GAfnum / GAarea / GAfdef -> {optex}</b></td>"
  694. "<td><button type='submit' name='btn_del_cb' value='{opval}' class='button bred'> " D_DELETE " </button></td></tr>";
  695. void HandleKNXConfiguration(void)
  696. {
  697. if (HttpUser()) { return; }
  698. if (!WebAuthenticate()) { return WebServer->requestAuthentication(); }
  699. AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_KNX);
  700. char tmp[100];
  701. String stmp;
  702. if ( WebServer->hasArg("save") ) {
  703. KNX_Save_Settings();
  704. HandleConfiguration();
  705. }
  706. else
  707. {
  708. if ( WebServer->hasArg("btn_add") ) {
  709. if ( WebServer->arg("btn_add") == "1" ) {
  710. stmp = WebServer->arg("GAop"); //option selected
  711. byte GAop = stmp.toInt();
  712. stmp = WebServer->arg("GA_FNUM");
  713. byte GA_FNUM = stmp.toInt();
  714. stmp = WebServer->arg("GA_AREA");
  715. byte GA_AREA = stmp.toInt();
  716. stmp = WebServer->arg("GA_FDEF");
  717. byte GA_FDEF = stmp.toInt();
  718. if (GAop) {
  719. KNX_ADD_GA( GAop, GA_FNUM, GA_AREA, GA_FDEF );
  720. }
  721. }
  722. else
  723. {
  724. stmp = WebServer->arg("CBop"); //option selected
  725. byte CBop = stmp.toInt();
  726. stmp = WebServer->arg("CB_FNUM");
  727. byte CB_FNUM = stmp.toInt();
  728. stmp = WebServer->arg("CB_AREA");
  729. byte CB_AREA = stmp.toInt();
  730. stmp = WebServer->arg("CB_FDEF");
  731. byte CB_FDEF = stmp.toInt();
  732. if (CBop) {
  733. KNX_ADD_CB( CBop, CB_FNUM, CB_AREA, CB_FDEF );
  734. }
  735. }
  736. }
  737. else if ( WebServer->hasArg("btn_del_ga") )
  738. {
  739. stmp = WebServer->arg("btn_del_ga");
  740. byte GA_NUM = stmp.toInt();
  741. KNX_DEL_GA(GA_NUM);
  742. }
  743. else if ( WebServer->hasArg("btn_del_cb") )
  744. {
  745. stmp = WebServer->arg("btn_del_cb");
  746. byte CB_NUM = stmp.toInt();
  747. KNX_DEL_CB(CB_NUM);
  748. }
  749. String page = FPSTR(HTTP_HEAD);
  750. page.replace(F("{v}"), FPSTR(S_CONFIGURE_KNX));
  751. page += FPSTR(HTTP_HEAD_STYLE);
  752. page.replace(F("340px"), F("530px"));
  753. page += FPSTR(HTTP_FORM_KNX);
  754. KNX_physs_addr.value = Settings.knx_physsical_addr;
  755. page.replace(F("{kna"), String(KNX_physs_addr.pa.area));
  756. page.replace(F("{knl"), String(KNX_physs_addr.pa.line));
  757. page.replace(F("{knm"), String(KNX_physs_addr.pa.member));
  758. if ( Settings.flag.knx_enabled ) { page += F(" checked"); }
  759. page += FPSTR(HTTP_FORM_KNX1);
  760. if ( Settings.flag.knx_enable_enhancement ) { page += F(" checked"); }
  761. page += FPSTR(HTTP_FORM_KNX2);
  762. for (byte i = 0; i < KNX_MAX_device_param ; i++)
  763. {
  764. if ( device_param[i].show )
  765. {
  766. page += FPSTR(HTTP_FORM_KNX_OPT);
  767. page.replace(F("{vop}"), String(device_param[i].type));
  768. page.replace(F("{nop}"), String(device_param_ga[i]));
  769. }
  770. }
  771. page += F("</select> -> ");
  772. page += FPSTR(HTTP_FORM_KNX_GA);
  773. page.replace(F("GAfnum"), F("GA_FNUM"));
  774. page.replace(F("GAarea"), F("GA_AREA"));
  775. page.replace(F("GAfdef"), F("GA_FDEF"));
  776. page.replace(F("GAfnum"), F("GA_FNUM"));
  777. page.replace(F("GAarea"), F("GA_AREA"));
  778. page.replace(F("GAfdef"), F("GA_FDEF"));
  779. page += FPSTR(HTTP_FORM_KNX_ADD_BTN);
  780. page.replace(F("{btnval}"), String(1));
  781. if (Settings.knx_GA_registered < MAX_KNX_GA) {
  782. page.replace(F("btndis"), F(" "));
  783. }
  784. else
  785. {
  786. page.replace(F("btndis"), F("disabled"));
  787. }
  788. page.replace(F("fncbtnadd"), F("GAwarning"));
  789. for (byte i = 0; i < Settings.knx_GA_registered ; ++i)
  790. {
  791. if ( Settings.knx_GA_param[i] )
  792. {
  793. page += FPSTR(HTTP_FORM_KNX_ADD_TABLE_ROW);
  794. page.replace(F("{opval}"), String(i+1));
  795. page.replace(F("{optex}"), String(device_param_ga[Settings.knx_GA_param[i]-1]));
  796. KNX_addr.value = Settings.knx_GA_addr[i];
  797. page.replace(F("GAfnum"), String(KNX_addr.ga.area));
  798. page.replace(F("GAarea"), String(KNX_addr.ga.line));
  799. page.replace(F("GAfdef"), String(KNX_addr.ga.member));
  800. }
  801. }
  802. page += FPSTR(HTTP_FORM_KNX3);
  803. page += FPSTR(HTTP_FORM_KNX_GA);
  804. page.replace(F("GAfnum"), F("CB_FNUM"));
  805. page.replace(F("GAarea"), F("CB_AREA"));
  806. page.replace(F("GAfdef"), F("CB_FDEF"));
  807. page.replace(F("GAfnum"), F("CB_FNUM"));
  808. page.replace(F("GAarea"), F("CB_AREA"));
  809. page.replace(F("GAfdef"), F("CB_FDEF"));
  810. page += FPSTR(HTTP_FORM_KNX4);
  811. byte j;
  812. for (byte i = 0; i < KNX_MAX_device_param ; i++)
  813. {
  814. // Check How many Relays are available and add: RelayX and TogleRelayX
  815. if ( (i > 8) && (i < 16) ) { j=i-8; } else { j=i; }
  816. if ( i == 8 ) { j = 0; }
  817. if ( device_param[j].show )
  818. {
  819. page += FPSTR(HTTP_FORM_KNX_OPT);
  820. page.replace(F("{vop}"), String(device_param[i].type));
  821. page.replace(F("{nop}"), String(device_param_cb[i]));
  822. }
  823. }
  824. page += F("</select> ");
  825. page += FPSTR(HTTP_FORM_KNX_ADD_BTN);
  826. page.replace(F("{btnval}"), String(2));
  827. if (Settings.knx_CB_registered < MAX_KNX_CB) {
  828. page.replace(F("btndis"), F(" "));
  829. }
  830. else
  831. {
  832. page.replace(F("btndis"), F("disabled"));
  833. }
  834. page.replace(F("fncbtnadd"), F("CBwarning"));
  835. for (byte i = 0; i < Settings.knx_CB_registered ; ++i)
  836. {
  837. if ( Settings.knx_CB_param[i] )
  838. {
  839. page += FPSTR(HTTP_FORM_KNX_ADD_TABLE_ROW2);
  840. page.replace(F("{opval}"), String(i+1));
  841. page.replace(F("{optex}"), String(device_param_cb[Settings.knx_CB_param[i]-1]));
  842. KNX_addr.value = Settings.knx_CB_addr[i];
  843. page.replace(F("GAfnum"), String(KNX_addr.ga.area));
  844. page.replace(F("GAarea"), String(KNX_addr.ga.line));
  845. page.replace(F("GAfdef"), String(KNX_addr.ga.member));
  846. }
  847. }
  848. page += F("</table></center></fieldset>");
  849. page += F("<br/><button name='save' type='submit' class='button bgrn'>" D_SAVE "</button></form></fieldset>");
  850. page += FPSTR(HTTP_BTN_CONF);
  851. page.replace( F("</script>"),
  852. F("function GAwarning()"
  853. "{"
  854. "var GA_FNUM = document.getElementById('GA_FNUM');"
  855. "var GA_AREA = document.getElementById('GA_AREA');"
  856. "var GA_FDEF = document.getElementById('GA_FDEF');"
  857. "if ( GA_FNUM != null && GA_FNUM.value == '0' && GA_AREA.value == '0' && GA_FDEF.value == '0' ) {"
  858. "alert('" D_KNX_WARNING "');"
  859. "}"
  860. "}"
  861. "function CBwarning()"
  862. "{"
  863. "var CB_FNUM = document.getElementById('CB_FNUM');"
  864. "var CB_AREA = document.getElementById('CB_AREA');"
  865. "var CB_FDEF = document.getElementById('CB_FDEF');"
  866. "if ( CB_FNUM != null && CB_FNUM.value == '0' && CB_AREA.value == '0' && CB_FDEF.value == '0' ) {"
  867. "alert('" D_KNX_WARNING "');"
  868. "}"
  869. "}"
  870. "</script>") );
  871. ShowPage(page);
  872. }
  873. }
  874. void KNX_Save_Settings(void)
  875. {
  876. String stmp;
  877. address_t KNX_addr;
  878. Settings.flag.knx_enabled = WebServer->hasArg("b1");
  879. Settings.flag.knx_enable_enhancement = WebServer->hasArg("b2");
  880. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_ENABLED ": %d, " D_KNX_ENHANCEMENT ": %d"),
  881. Settings.flag.knx_enabled, Settings.flag.knx_enable_enhancement );
  882. AddLog(LOG_LEVEL_DEBUG);
  883. stmp = WebServer->arg("area");
  884. KNX_addr.pa.area = stmp.toInt();
  885. stmp = WebServer->arg("line");
  886. KNX_addr.pa.line = stmp.toInt();
  887. stmp = WebServer->arg("member");
  888. KNX_addr.pa.member = stmp.toInt();
  889. Settings.knx_physsical_addr = KNX_addr.value;
  890. knx.physical_address_set( KNX_addr ); // Set Physical KNX Address of the device
  891. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX D_KNX_PHYSICAL_ADDRESS ": %d.%d.%d "),
  892. KNX_addr.pa.area, KNX_addr.pa.line, KNX_addr.pa.member );
  893. AddLog(LOG_LEVEL_DEBUG);
  894. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "GA: %d"),
  895. Settings.knx_GA_registered );
  896. AddLog(LOG_LEVEL_DEBUG);
  897. for (byte i = 0; i < Settings.knx_GA_registered ; ++i)
  898. {
  899. KNX_addr.value = Settings.knx_GA_addr[i];
  900. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "GA #%d: %s " D_TO " %d/%d/%d"),
  901. i+1, device_param_ga[Settings.knx_GA_param[i]-1],
  902. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member );
  903. AddLog(LOG_LEVEL_DEBUG);
  904. }
  905. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "CB: %d"),
  906. Settings.knx_CB_registered );
  907. AddLog(LOG_LEVEL_DEBUG);
  908. for (byte i = 0; i < Settings.knx_CB_registered ; ++i)
  909. {
  910. KNX_addr.value = Settings.knx_CB_addr[i];
  911. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "CB #%d: %d/%d/%d " D_TO " %s"),
  912. i+1,
  913. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member,
  914. device_param_cb[Settings.knx_CB_param[i]-1] );
  915. AddLog(LOG_LEVEL_DEBUG);
  916. }
  917. }
  918. #endif // USE_KNX_WEB_MENU
  919. #endif // USE_WEBSERVER
  920. boolean KnxCommand(void)
  921. {
  922. char command[CMDSZ];
  923. uint8_t index = XdrvMailbox.index;
  924. int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kKnxCommands);
  925. if (-1 == command_code) { return false; } // Unknown command
  926. else if ((CMND_KNXTXCMND == command_code) && (index > 0) && (index <= MAX_KNXTX_CMNDS) && (XdrvMailbox.data_len > 0)) {
  927. // index <- KNX SLOT to use
  928. // XdrvMailbox.payload <- data to send
  929. if (!(Settings.flag.knx_enabled)) { return false; }
  930. // Search all the registered GA that has that output (variable: KNX SLOTx) as parameter
  931. byte i = KNX_GA_Search(index + KNX_SLOT1 -1);
  932. while ( i != KNX_Empty ) {
  933. KNX_addr.value = Settings.knx_GA_addr[i];
  934. knx.write_1bit(KNX_addr, !(XdrvMailbox.payload == 0));
  935. if (Settings.flag.knx_enable_enhancement) {
  936. knx.write_1bit(KNX_addr, !(XdrvMailbox.payload == 0));
  937. knx.write_1bit(KNX_addr, !(XdrvMailbox.payload == 0));
  938. }
  939. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %d " D_SENT_TO " %d.%d.%d"),
  940. device_param_ga[index + KNX_SLOT1 -2], !(XdrvMailbox.payload == 0),
  941. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member);
  942. AddLog(LOG_LEVEL_INFO);
  943. i = KNX_GA_Search(index + KNX_SLOT1 -1, i + 1);
  944. }
  945. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"),
  946. command, index, XdrvMailbox.data );
  947. }
  948. else if ((CMND_KNXTXVAL == command_code) && (index > 0) && (index <= MAX_KNXTX_CMNDS) && (XdrvMailbox.data_len > 0)) {
  949. // index <- KNX SLOT to use
  950. // XdrvMailbox.payload <- data to send
  951. if (!(Settings.flag.knx_enabled)) { return false; }
  952. // Search all the registered GA that has that output (variable: KNX SLOTx) as parameter
  953. byte i = KNX_GA_Search(index + KNX_SLOT1 -1);
  954. while ( i != KNX_Empty ) {
  955. KNX_addr.value = Settings.knx_GA_addr[i];
  956. float tempvar = CharToDouble(XdrvMailbox.data);
  957. dtostrfd(tempvar,2,XdrvMailbox.data);
  958. knx.write_2byte_float(KNX_addr, tempvar);
  959. if (Settings.flag.knx_enable_enhancement) {
  960. knx.write_2byte_float(KNX_addr, tempvar);
  961. knx.write_2byte_float(KNX_addr, tempvar);
  962. }
  963. snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_KNX "%s = %s " D_SENT_TO " %d.%d.%d"),
  964. device_param_ga[index + KNX_SLOT1 -2], XdrvMailbox.data,
  965. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member);
  966. AddLog(LOG_LEVEL_INFO);
  967. i = KNX_GA_Search(index + KNX_SLOT1 -1, i + 1);
  968. }
  969. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s\"}"),
  970. command, index, XdrvMailbox.data );
  971. }
  972. else if (CMND_KNX_ENABLED == command_code) {
  973. if (!XdrvMailbox.data_len) {
  974. if (Settings.flag.knx_enabled) {
  975. snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("1"));
  976. } else {
  977. snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("0"));
  978. }
  979. } else {
  980. if (XdrvMailbox.payload == 1) {
  981. Settings.flag.knx_enabled = 1;
  982. } else if (XdrvMailbox.payload == 0) {
  983. Settings.flag.knx_enabled = 0;
  984. } else { return false; } // Incomplete command
  985. }
  986. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"),
  987. command, XdrvMailbox.data );
  988. }
  989. else if (CMND_KNX_ENHANCED == command_code) {
  990. if (!XdrvMailbox.data_len) {
  991. if (Settings.flag.knx_enable_enhancement) {
  992. snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("1"));
  993. } else {
  994. snprintf_P(XdrvMailbox.data, sizeof(XdrvMailbox.data), PSTR("0"));
  995. }
  996. } else {
  997. if (XdrvMailbox.payload == 1) {
  998. Settings.flag.knx_enable_enhancement = 1;
  999. } else if (XdrvMailbox.payload == 0) {
  1000. Settings.flag.knx_enable_enhancement = 0;
  1001. } else { return false; } // Incomplete command
  1002. }
  1003. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%s\"}"),
  1004. command, XdrvMailbox.data );
  1005. }
  1006. else if (CMND_KNX_PA == command_code) {
  1007. if (XdrvMailbox.data_len) {
  1008. if (strstr(XdrvMailbox.data, ".")) { // Process parameter entry
  1009. char sub_string[XdrvMailbox.data_len];
  1010. int pa_area = atoi(subStr(sub_string, XdrvMailbox.data, ".", 1));
  1011. int pa_line = atoi(subStr(sub_string, XdrvMailbox.data, ".", 2));
  1012. int pa_member = atoi(subStr(sub_string, XdrvMailbox.data, ".", 3));
  1013. if ( ((pa_area == 0) && (pa_line == 0) && (pa_member == 0))
  1014. || (pa_area > 15) || (pa_line > 15) || (pa_member > 255) ) {
  1015. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_ERROR "\"}"),
  1016. command );
  1017. return true;
  1018. } // Invalid command
  1019. KNX_addr.pa.area = pa_area;
  1020. KNX_addr.pa.line = pa_line;
  1021. KNX_addr.pa.member = pa_member;
  1022. Settings.knx_physsical_addr = KNX_addr.value;
  1023. }
  1024. }
  1025. KNX_addr.value = Settings.knx_physsical_addr;
  1026. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%d.%d.%d\"}"),
  1027. command, KNX_addr.pa.area, KNX_addr.pa.line, KNX_addr.pa.member );
  1028. }
  1029. else if ((CMND_KNX_GA == command_code) && (index > 0) && (index <= MAX_KNX_GA)) {
  1030. if (XdrvMailbox.data_len) {
  1031. if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry
  1032. char sub_string[XdrvMailbox.data_len];
  1033. int ga_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1));
  1034. int ga_area = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
  1035. int ga_line = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
  1036. int ga_member = atoi(subStr(sub_string, XdrvMailbox.data, ",", 4));
  1037. if ( ((ga_area == 0) && (ga_line == 0) && (ga_member == 0))
  1038. || (ga_area > 31) || (ga_line > 7) || (ga_member > 255)
  1039. || (ga_option < 0) || ((ga_option > KNX_MAX_device_param ) && (ga_option != KNX_Empty))
  1040. || (!device_param[ga_option-1].show) ) {
  1041. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_ERROR "\"}"), command );
  1042. return true;
  1043. } // Invalid command
  1044. KNX_addr.ga.area = ga_area;
  1045. KNX_addr.ga.line = ga_line;
  1046. KNX_addr.ga.member = ga_member;
  1047. if ( index > Settings.knx_GA_registered ) {
  1048. Settings.knx_GA_registered ++;
  1049. index = Settings.knx_GA_registered;
  1050. }
  1051. Settings.knx_GA_addr[index -1] = KNX_addr.value;
  1052. Settings.knx_GA_param[index -1] = ga_option;
  1053. } else {
  1054. if ( (XdrvMailbox.payload <= Settings.knx_GA_registered) && (XdrvMailbox.payload > 0) ) {
  1055. index = XdrvMailbox.payload;
  1056. } else {
  1057. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_ERROR "\"}"), command );
  1058. return true;
  1059. }
  1060. }
  1061. if ( index <= Settings.knx_GA_registered ) {
  1062. KNX_addr.value = Settings.knx_GA_addr[index -1];
  1063. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s, %d/%d/%d\"}"),
  1064. command, index, device_param_ga[Settings.knx_GA_param[index-1]-1],
  1065. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member );
  1066. }
  1067. } else {
  1068. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%d\"}"),
  1069. command, Settings.knx_GA_registered );
  1070. }
  1071. }
  1072. else if ((CMND_KNX_CB == command_code) && (index > 0) && (index <= MAX_KNX_CB)) {
  1073. if (XdrvMailbox.data_len) {
  1074. if (strstr(XdrvMailbox.data, ",")) { // Process parameter entry
  1075. char sub_string[XdrvMailbox.data_len];
  1076. int cb_option = atoi(subStr(sub_string, XdrvMailbox.data, ",", 1));
  1077. int cb_area = atoi(subStr(sub_string, XdrvMailbox.data, ",", 2));
  1078. int cb_line = atoi(subStr(sub_string, XdrvMailbox.data, ",", 3));
  1079. int cb_member = atoi(subStr(sub_string, XdrvMailbox.data, ",", 4));
  1080. if ( ((cb_area == 0) && (cb_line == 0) && (cb_member == 0))
  1081. || (cb_area > 31) || (cb_line > 7) || (cb_member > 255)
  1082. || (cb_option < 0) || ((cb_option > KNX_MAX_device_param ) && (cb_option != KNX_Empty))
  1083. || (!device_param[cb_option-1].show) ) {
  1084. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_ERROR "\"}"), command );
  1085. return true;
  1086. } // Invalid command
  1087. KNX_addr.ga.area = cb_area;
  1088. KNX_addr.ga.line = cb_line;
  1089. KNX_addr.ga.member = cb_member;
  1090. if ( index > Settings.knx_CB_registered ) {
  1091. Settings.knx_CB_registered ++;
  1092. index = Settings.knx_CB_registered;
  1093. }
  1094. Settings.knx_CB_addr[index -1] = KNX_addr.value;
  1095. Settings.knx_CB_param[index -1] = cb_option;
  1096. } else {
  1097. if ( (XdrvMailbox.payload <= Settings.knx_CB_registered) && (XdrvMailbox.payload > 0) ) {
  1098. index = XdrvMailbox.payload;
  1099. } else {
  1100. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"" D_ERROR "\"}"), command );
  1101. return true;
  1102. }
  1103. }
  1104. if ( index <= Settings.knx_CB_registered ) {
  1105. KNX_addr.value = Settings.knx_CB_addr[index -1];
  1106. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s%d\":\"%s, %d/%d/%d\"}"),
  1107. command, index, device_param_cb[Settings.knx_CB_param[index-1]-1],
  1108. KNX_addr.ga.area, KNX_addr.ga.line, KNX_addr.ga.member );
  1109. }
  1110. } else {
  1111. snprintf_P (mqtt_data, sizeof(mqtt_data), PSTR("{\"%s\":\"%d\"}"),
  1112. command, Settings.knx_CB_registered );
  1113. }
  1114. }
  1115. else { return false; } // Incomplete command
  1116. return true;
  1117. }
  1118. /*********************************************************************************************\
  1119. * Interface
  1120. \*********************************************************************************************/
  1121. boolean Xdrv11(byte function)
  1122. {
  1123. boolean result = false;
  1124. switch (function) {
  1125. case FUNC_PRE_INIT:
  1126. KNX_INIT();
  1127. break;
  1128. #ifdef USE_WEBSERVER
  1129. #ifdef USE_KNX_WEB_MENU
  1130. case FUNC_WEB_ADD_BUTTON:
  1131. strncat_P(mqtt_data, HTTP_BTN_MENU_KNX, sizeof(mqtt_data) - strlen(mqtt_data) -1);
  1132. break;
  1133. case FUNC_WEB_ADD_HANDLER:
  1134. WebServer->on("/kn", HandleKNXConfiguration);
  1135. break;
  1136. #endif // USE_KNX_WEB_MENU
  1137. #endif // USE_WEBSERVER
  1138. case FUNC_LOOP:
  1139. if (!global_state.wifi_down) { knx.loop(); } // Process knx events
  1140. break;
  1141. case FUNC_EVERY_50_MSECOND:
  1142. if (toggle_inhibit) {
  1143. toggle_inhibit--;
  1144. }
  1145. break;
  1146. case FUNC_COMMAND:
  1147. result = KnxCommand();
  1148. break;
  1149. // case FUNC_SET_POWER:
  1150. // break;
  1151. }
  1152. return result;
  1153. }
  1154. #endif // USE_KNX