domoticz.ino 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //sample domoticz/out payload switch:
  2. //{
  3. // "Battery" : 255,
  4. // "RSSI" : 12,
  5. // "description" : "Deckenleuchte Arbeitszimmer\nSonoff-Touch-01",
  6. // "dtype" : "Light/Switch",
  7. // "id" : "00014121",
  8. // "idx" : 209,
  9. // "name" : "Arbeitszimmer Licht",
  10. // "nvalue" : 1,
  11. // "stype" : "Switch",
  12. // "switchType" : "On/Off",
  13. // "unit" : 1
  14. //}
  15. // -> we need idx and nvalue
  16. //sample domoticz/out payload thermostat:
  17. //{
  18. // "command": "udevice",
  19. // "idx": 219,
  20. // "nvalue": 0,
  21. // "svalue": "24.00"
  22. //}
  23. //
  24. //
  25. //domoticz/out:
  26. //{
  27. // "Battery" : 255,
  28. // "RSSI" : 12,
  29. // "description" : "",
  30. // "dtype" : "Thermostat",
  31. // "id" : "001412B",
  32. // "idx" : 219,
  33. // "meterType" : "Energy",
  34. // "name" : "Raumthermostat Wohnzimmer",
  35. // "nvalue" : 0,
  36. // "stype" : "SetPoint",
  37. // "svalue1" : "22.00",
  38. // "unit" : 1
  39. //} -> we need idx and svalue
  40. //sample domoticz/out payload selector switch:
  41. //{
  42. // "Battery" : 255,
  43. // "LevelActions" : "||",
  44. // "LevelNames" : "Off|Normal|Nachtabsenkung",
  45. // "LevelOffHidden" : "false",
  46. // "RSSI" : 12,
  47. // "SelectorStyle" : "0",
  48. // "description" : "",
  49. // "dtype" : "Light/Switch",
  50. // "id" : "0001415C",
  51. // "idx" : 268,
  52. // "name" : "Heizung Betriebsart",
  53. // "nvalue" : 2,
  54. // "stype" : "Selector Switch",
  55. // "svalue1" : "10",
  56. // "switchType" : "Selector",
  57. // "unit" : 1
  58. //}
  59. // -> we need idx and svalue ("0"="Off", "10"="Normal", "20"="Nachtabsenkung"
  60. // counter for automatic domoticz update used in function updateDomoticzDevices()
  61. // initially set to domoticzUpdateInterval, to ensure soon update after boot
  62. int count_sendToDomoticz_thermostat = domoticzUpdateInterval;
  63. int count_sendToDomoticz_heatingMode = domoticzUpdateInterval;
  64. void parseDomoticzOut() {
  65. domoticzOutParseData = false;
  66. domoticzOutParserBusy = true;
  67. unsigned long idx = 0;
  68. int16_t nvalue;
  69. char svalue[21];
  70. int16_t found = 0;
  71. StaticJsonBuffer<500> jsonBuf;
  72. JsonObject& domoticz = jsonBuf.parseObject(domoticzOutPayload);
  73. idx = domoticz["idx"];
  74. yield();
  75. if (idx == domoticzIdx_Thermostat || idx == domoticzIdx_ThermostatMode) {
  76. nvalue = domoticz["nvalue"];
  77. strlcpy(svalue, domoticz["svalue1"] | "", 21);
  78. if ( idx == domoticzIdx_Thermostat ) {
  79. if ((millis() - lastUpdate_setTemp) > dismissUpdateFromDomoticzTimeout) {
  80. Serial.print(domoticz_out_topic);
  81. Serial.print(" received: ");
  82. Serial.print(" idx=");
  83. Serial.print(idx);
  84. Serial.print(", nvalue=");
  85. Serial.print(nvalue);
  86. Serial.print(", svalue=");
  87. Serial.println(svalue);
  88. yield();
  89. float valueFloat = round(atof(svalue) * 2.0) / 2.0;
  90. setTempTo(valueFloat);
  91. lastValueChange = 0; // force saving of values without delay
  92. checkValuesChanged();
  93. }
  94. }
  95. else if ( idx == domoticzIdx_ThermostatMode ) {
  96. if ((millis() - lastUpdate_heatingMode) > dismissUpdateFromDomoticzTimeout) {
  97. Serial.print(domoticz_out_topic);
  98. Serial.print(" received: ");
  99. Serial.print(" idx=");
  100. Serial.print(idx);
  101. Serial.print(", nvalue=");
  102. Serial.print(nvalue);
  103. Serial.print(", svalue=");
  104. Serial.println(svalue);
  105. yield();
  106. if (strcmp(svalue, "0") == 0) {
  107. setHeatingmodeTo(0);
  108. lastValueChange = 0; // force saving of values without delay
  109. checkValuesChanged();
  110. }
  111. else if (strcmp(svalue, "10") == 0) {
  112. setHeatingmodeTo(1);
  113. lastValueChange = 0; // force saving of values without delay
  114. checkValuesChanged();
  115. }
  116. else if (strcmp(svalue, "20") == 0) {
  117. setHeatingmodeTo(2);
  118. lastValueChange = 0; // force saving of values without delay
  119. checkValuesChanged();
  120. }
  121. }
  122. }
  123. }
  124. domoticzOutParserBusy = false;
  125. }
  126. void sendToDomoticz_thermostat() {
  127. if (domoticzIdx_Thermostat > 0) {
  128. //{"idx": 219,"nvalue":0,"svalue":"24.00"}
  129. char domSetTempTo[6];
  130. dtostrf(setTemp, 1, 1, domSetTempTo);
  131. char buf[101];
  132. sprintf(buf, "{\"idx\":%d,\"nvalue\":0,\"svalue\":\"%s\"}", domoticzIdx_Thermostat, domSetTempTo);
  133. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  134. count_sendToDomoticz_thermostat = 0;
  135. }
  136. }
  137. void sendToDomoticz_heatingMode() {
  138. //if (!skipNextDomoticzUpdate_ThermostatMode) {
  139. if (domoticzIdx_ThermostatMode > 0) {
  140. //skipNextDomoticzUpdate_ThermostatMode = false;
  141. //{"command": "switchlight", "idx": 2450, "switchcmd": "Set Level", "level": 100 }
  142. if (heatingMode == 0 || heatingMode == 1 || heatingMode == 2) {
  143. int domLevel;
  144. domLevel = heatingMode * 10;
  145. // 0 = Off, 10 = Normal, 20 = Reduction
  146. char buf[101];
  147. sprintf(buf, "{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"Set Level\",\"level\":%d}", domoticzIdx_ThermostatMode, domLevel);
  148. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  149. count_sendToDomoticz_heatingMode = 0;
  150. }
  151. }
  152. }
  153. void sendToDomoticz_TempHum() {
  154. if (domoticzIdx_TempHumSensor > 0) {
  155. if ( lastTempUpdate != 0 && (millis() - lastTempUpdate) < 120000 ) {
  156. //{"idx":idx,"nvalue":0,"svalue":"TEMP;HUM;0"}
  157. char buf[101];
  158. char buftemp[10];
  159. dtostrf(currTemp, 1, 1, buftemp );
  160. sprintf(buf, "{\"idx\":%d,\"nvalue\":0,\"svalue\":\"%s;%d;0\"}", domoticzIdx_TempHumSensor, buftemp, currHum);
  161. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  162. }
  163. }
  164. }
  165. void checkUseDomoticz() {
  166. if (domoticzIdx_Thermostat != 0 || domoticzIdx_ThermostatMode != 0) {
  167. useDomoticz = true;
  168. }
  169. else {
  170. useDomoticz = false;
  171. }
  172. }
  173. void updateDomoticzDevices() {
  174. // sends updates to domoticz, is called every minute by misc/everyMinute()
  175. // update TempHum-Sensor device every minute
  176. sendToDomoticz_TempHum();
  177. if (count_sendToDomoticz_thermostat >= domoticzUpdateInterval) {
  178. sendToDomoticz_thermostat();
  179. count_sendToDomoticz_thermostat = 0;
  180. }
  181. else count_sendToDomoticz_thermostat++;
  182. if (count_sendToDomoticz_heatingMode >= domoticzUpdateInterval) {
  183. sendToDomoticz_heatingMode();
  184. count_sendToDomoticz_heatingMode = 0;
  185. }
  186. else count_sendToDomoticz_heatingMode++;
  187. }