domoticz.ino 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. else if (strcmp(svalue, "30") == 0) {
  122. setHeatingmodeTo(3);
  123. lastValueChange = 0; // force saving of values without delay
  124. checkValuesChanged();
  125. }
  126. }
  127. }
  128. }
  129. domoticzOutParserBusy = false;
  130. }
  131. void sendToDomoticz_thermostat() {
  132. if (domoticzIdx_Thermostat > 0) {
  133. //{"idx": 219,"nvalue":0,"svalue":"24.00"}
  134. char domSetTempTo[6];
  135. dtostrf(setTemp, 1, 1, domSetTempTo);
  136. char buf[101];
  137. sprintf(buf, "{\"idx\":%d,\"nvalue\":0,\"svalue\":\"%s\"}", domoticzIdx_Thermostat, domSetTempTo);
  138. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  139. count_sendToDomoticz_thermostat = 0;
  140. }
  141. }
  142. void sendToDomoticz_heatingMode() {
  143. //if (!skipNextDomoticzUpdate_ThermostatMode) {
  144. if (domoticzIdx_ThermostatMode > 0) {
  145. //skipNextDomoticzUpdate_ThermostatMode = false;
  146. //{"command": "switchlight", "idx": 2450, "switchcmd": "Set Level", "level": 100 }
  147. if (heatingMode == 0 || heatingMode == 1 || heatingMode == 2) {
  148. int domLevel;
  149. domLevel = heatingMode * 10;
  150. // 0 = Off, 10 = Normal, 20 = Reduction
  151. char buf[101];
  152. sprintf(buf, "{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"Set Level\",\"level\":%d}", domoticzIdx_ThermostatMode, domLevel);
  153. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  154. count_sendToDomoticz_heatingMode = 0;
  155. }
  156. }
  157. }
  158. void sendToDomoticz_TempHum() {
  159. if (domoticzIdx_TempHumSensor > 0) {
  160. if ( lastTempUpdate != 0 && (millis() - lastTempUpdate) < 120000 ) {
  161. //{"idx":idx,"nvalue":0,"svalue":"TEMP;HUM;0"}
  162. char buf[101];
  163. char buftemp[10];
  164. dtostrf(currTemp, 1, 1, buftemp );
  165. sprintf(buf, "{\"idx\":%d,\"nvalue\":0,\"svalue\":\"%s;%d;0\"}", domoticzIdx_TempHumSensor, buftemp, currHum);
  166. mqttclient.publish(DOMOTICZ_IN_TOPIC, buf);
  167. }
  168. }
  169. }
  170. void sendToDomoticz_Heating() {
  171. if (domoticzIdx_Heating > 0) {
  172. //{"command":"switchlight","idx":238,"switchcmd":"On"}
  173. //{"command":"switchlight","idx":238,"switchcmd":"Off"}
  174. char heatingStatus[4];
  175. if (turnHeatingOn) {
  176. strcpy(heatingStatus, "On");
  177. }
  178. else {
  179. strcpy(heatingStatus, "Off");
  180. }
  181. char domoticzJson[90];
  182. sprintf(domoticzJson, "{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}", domoticzIdx_Heating, heatingStatus);
  183. mqttclient.publish(DOMOTICZ_IN_TOPIC, domoticzJson);
  184. }
  185. }
  186. void sendToDomoticz_PIR() {
  187. if (domoticzIdx_PIR > 0) {
  188. //{"command":"switchlight","idx":277,"switchcmd":"On"}
  189. //{"command":"switchlight","idx":277,"switchcmd":"Off"}
  190. char PIRStatus[4];
  191. if (PIRSensorOn) {
  192. strcpy(PIRStatus, "On");
  193. }
  194. else {
  195. strcpy(PIRStatus, "Off");
  196. }
  197. char domoticzJson[90];
  198. sprintf(domoticzJson, "{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}", domoticzIdx_PIR, PIRStatus);
  199. mqttclient.publish(DOMOTICZ_IN_TOPIC, domoticzJson);
  200. }
  201. }
  202. void checkUseDomoticz() {
  203. if (domoticzIdx_Thermostat != 0 || domoticzIdx_ThermostatMode != 0) {
  204. useDomoticz = true;
  205. }
  206. else {
  207. useDomoticz = false;
  208. }
  209. }
  210. void updateDomoticzDevices() {
  211. // sends updates to domoticz, is called every minute by misc/everyMinute()
  212. // update TempHum-Sensor device every minute
  213. sendToDomoticz_TempHum();
  214. if (count_sendToDomoticz_thermostat >= domoticzUpdateInterval) {
  215. sendToDomoticz_thermostat();
  216. count_sendToDomoticz_thermostat = 0;
  217. }
  218. else count_sendToDomoticz_thermostat++;
  219. if (count_sendToDomoticz_heatingMode >= domoticzUpdateInterval) {
  220. sendToDomoticz_heatingMode();
  221. count_sendToDomoticz_heatingMode = 0;
  222. }
  223. else count_sendToDomoticz_heatingMode++;
  224. }