mqtt.ino 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #include <string.h>
  2. #include <ctype.h>
  3. char *strlwr(char *str) {
  4. unsigned char *p = (unsigned char *)str;
  5. while (*p) {
  6. *p = tolower((unsigned char) * p);
  7. p++;
  8. }
  9. return str;
  10. }
  11. // MQTT callback
  12. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  13. // Serial.print("MQTT payload arrived [");
  14. // Serial.print(topic);
  15. // Serial.print("] ");
  16. // for (int i = 0; i < length; i++) {
  17. // Serial.print((char)payload[i]);
  18. // }
  19. // Serial.println();
  20. char tmp_topic_pub[51];
  21. char tmp_payload_pub[51];
  22. if (strcmp(topic, mqtt_topic_in_cmd) == 0) { //if topic = mqtt_topic_in_cmd
  23. int len;
  24. if (length < 101) len = length; // if input is bigger than dest buffer, cut
  25. else len = 100;
  26. for (int i = 0; i < len; i++) {
  27. cmdPayload[i] = (char)payload[i];
  28. }
  29. //cmdPayload[len + 1] = '\0';
  30. cmdPayload[len] = '\0';
  31. // Serial.print("cmdPayload:");
  32. // Serial.println(cmdPayload);
  33. if (serialdebug) {
  34. Serial.print("received MQTT ");
  35. Serial.print(mqtt_topic_in_cmd);
  36. Serial.print(" - \"");
  37. Serial.print(cmdPayload);
  38. Serial.println("\"");
  39. }
  40. if (mqttdebug) {
  41. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "mqtt_received");
  42. sprintf(tmp_payload_pub, "%s: %s", mqtt_topic_in_cmd, cmdPayload);
  43. mqttclient.publish(tmp_topic_pub, tmp_payload_pub);
  44. }
  45. cmdInQueue = true; // payload is processed in "commands"
  46. }//if topic = mqtt_topic_in_cmd
  47. if (strcmp(topic, mqtt_topic_in_setTemp) == 0) { //if topic = mqtt_topic_in_setTemp
  48. int len;
  49. if (length < 101) len = length; // if input is bigger than dest buffer, cut
  50. else len = 100;
  51. for (int i = 0; i < len; i++) {
  52. cmdPayload[i] = (char)payload[i];
  53. }
  54. //cmdPayload[len + 1] = '\0';
  55. cmdPayload[len] = '\0';
  56. // Serial.print("cmdPayload:");
  57. // Serial.println(cmdPayload);
  58. char tmpPayload[11];
  59. strlcpy(tmpPayload, cmdPayload, 10);
  60. float valueFloat;
  61. valueFloat = round(atof(tmpPayload) * 2.0) / 2.0;
  62. setTempTo(valueFloat);
  63. if (serialdebug) {
  64. Serial.print("received MQTT ");
  65. Serial.print(mqtt_topic_in_setTemp);
  66. Serial.print(" - \"");
  67. Serial.print(cmdPayload);
  68. Serial.println("\"");
  69. }
  70. if (mqttdebug) {
  71. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "mqtt_received");
  72. sprintf(tmp_payload_pub, "%s: %s", mqtt_topic_in_setTemp, cmdPayload);
  73. mqttclient.publish(tmp_topic_pub, tmp_payload_pub);
  74. }
  75. cmdInQueue = false; // payload is processed in "commands"
  76. }//if topic = mqtt_topic_in_setTemp
  77. if (strcmp(topic, mqtt_topic_in_setMode) == 0) { //if topic = mqtt_topic_in_setMode
  78. int len;
  79. if (length < 101) len = length; // if input is bigger than dest buffer, cut
  80. else len = 100;
  81. for (int i = 0; i < len; i++) {
  82. cmdPayload[i] = (char)payload[i];
  83. }
  84. //cmdPayload[len + 1] = '\0';
  85. cmdPayload[len] = '\0';
  86. // Serial.print("cmdPayload:");
  87. // Serial.println(cmdPayload);
  88. char tmpPayload[21];
  89. strlcpy(tmpPayload, cmdPayload, 20);
  90. strlwr(tmpPayload);
  91. byte tmpHeatMode;
  92. char tmpModename0[15];
  93. char tmpModename1[15];
  94. strlcpy(tmpModename0, modename0, 14);
  95. strlcpy(tmpModename1, modename1, 14);
  96. strlwr(tmpModename0);
  97. strlwr(tmpModename1);
  98. if (strcmp(tmpPayload, tmpModename0) == 0) tmpHeatMode = 0;
  99. else if (strcmp(tmpPayload, tmpModename1) == 0) tmpHeatMode = 1;
  100. else if (strcmp(tmpPayload, "off") == 0) tmpHeatMode = 0;
  101. else if (strcmp(tmpPayload, "aus") == 0) tmpHeatMode = 0;
  102. else if (strcmp(tmpPayload, "on") == 0) tmpHeatMode = 1;
  103. else if (strcmp(tmpPayload, "ein") == 0) tmpHeatMode = 1;
  104. else if (atoi(tmpPayload) == 0) tmpHeatMode = 0;
  105. else if (atoi(tmpPayload) == 1) tmpHeatMode = 1;
  106. if (serialdebug) {
  107. Serial.print("set heatmode to: ");
  108. Serial.println(tmpHeatMode);
  109. }
  110. setHeatingmodeTo(tmpHeatMode);
  111. if (serialdebug) {
  112. Serial.print("received MQTT ");
  113. Serial.print(mqtt_topic_in_setMode);
  114. Serial.print(" - \"");
  115. Serial.print(cmdPayload);
  116. Serial.println("\"");
  117. }
  118. if (mqttdebug) {
  119. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "mqtt_received");
  120. sprintf(tmp_payload_pub, "%s: %s", mqtt_topic_in_setMode, cmdPayload);
  121. mqttclient.publish(tmp_topic_pub, tmp_payload_pub);
  122. }
  123. cmdInQueue = false; // payload is processed in "commands"
  124. }//if topic = mqtt_topic_in_setMode
  125. if (strcmp(topic, mqtt_topic_in_setPreset) == 0) { //if topic = mqtt_topic_in_setPreset
  126. int len;
  127. if (length < 101) len = length; // if input is bigger than dest buffer, cut
  128. else len = 100;
  129. for (int i = 0; i < len; i++) {
  130. cmdPayload[i] = (char)payload[i];
  131. }
  132. //cmdPayload[len + 1] = '\0';
  133. cmdPayload[len] = '\0';
  134. // Serial.print("cmdPayload:");
  135. // Serial.println(cmdPayload);
  136. char tmpPayload[21];
  137. strlcpy(tmpPayload, cmdPayload, 20);
  138. strlwr(tmpPayload);
  139. byte tmpPreset;
  140. char tmpPresetName0[15];
  141. char tmpPresetName1[15];
  142. char tmpPresetName2[15];
  143. strlcpy(tmpPresetName0, psetname0, 14);
  144. strlcpy(tmpPresetName1, psetname1, 14);
  145. strlcpy(tmpPresetName2, psetname2, 14);
  146. strlwr(tmpPresetName0);
  147. strlwr(tmpPresetName1);
  148. strlwr(tmpPresetName2);
  149. if (strcmp(tmpPayload, tmpPresetName0) == 0) tmpPreset = 0;
  150. else if (strcmp(tmpPayload, tmpPresetName1) == 0) tmpPreset = 1;
  151. else if (strcmp(tmpPayload, tmpPresetName2) == 0) tmpPreset = 2;
  152. else if (strcmp(tmpPayload, "none") == 0) tmpPreset = 0;
  153. else if (strcmp(tmpPayload, "red1") == 0) tmpPreset = 1;
  154. else if (strcmp(tmpPayload, "red2") == 0) tmpPreset = 2;
  155. else if (atoi(tmpPayload) == 0) tmpPreset = 0;
  156. else if (atoi(tmpPayload) == 1) tmpPreset = 1;
  157. else if (atoi(tmpPayload) == 2) tmpPreset = 2;
  158. if (serialdebug) {
  159. Serial.print("set preset to: ");
  160. Serial.println(tmpPreset);
  161. }
  162. setPresetTo(tmpPreset);
  163. if (serialdebug) {
  164. Serial.print("received MQTT ");
  165. Serial.print(mqtt_topic_in_setPreset);
  166. Serial.print(" - \"");
  167. Serial.print(cmdPayload);
  168. Serial.println("\"");
  169. }
  170. if (mqttdebug) {
  171. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "mqtt_received");
  172. sprintf(tmp_payload_pub, "%s: %s", mqtt_topic_in_setPreset, cmdPayload);
  173. mqttclient.publish(tmp_topic_pub, tmp_payload_pub);
  174. }
  175. cmdInQueue = false; // payload is processed in "commands"
  176. }//if topic = mqtt_topic_in_setPreset
  177. if (strcmp(topic, outTemp_topic_in) == 0) { //if topic = outTemp_topic_in
  178. int len;
  179. if (length < 6) len = length; // if input is bigger than dest buffer, cut
  180. else len = 5;
  181. for (int i = 0; i < len; i++) {
  182. outTemp_newValue[i] = (char)payload[i];
  183. }
  184. //outTemp_newValue[len + 1] = '\0';
  185. outTemp_newValue[len] = '\0';
  186. outTemp_parseNewValue = true;
  187. }//if topic = outTemp_topic_in
  188. if (strcmp(topic, outHum_topic_in) == 0) { //if topic = outHum_topic_in
  189. int len;
  190. if (length < 4) len = length; // if input is bigger than dest buffer, cut
  191. else len = 3;
  192. for (int i = 0; i < len; i++) {
  193. outHum_newValue[i] = (char)payload[i];
  194. }
  195. //outHum_newValue[len + 1] = '\0';
  196. outHum_newValue[len] = '\0';
  197. outHum_parseNewValue = true;
  198. }//if topic = outHum_topic_in
  199. if (strcmp(topic, domoticz_out_topic) == 0) { //if topic = domoticz_out_topic
  200. Serial.print("received MQTT ");
  201. Serial.println(domoticz_out_topic);
  202. if ( !domoticzOutParserBusy ) {
  203. int len;
  204. if (length < 450) len = length; // if input is bigger than dest buffer, cut
  205. else len = 449;
  206. for (int i = 0; i < len; i++) {
  207. domoticzOutPayload[i] = (char)payload[i];
  208. }
  209. //domoticzOutPayload[len + 1] = '\0';
  210. domoticzOutPayload[len] = '\0';
  211. domoticzOutParseData = true; // parse domoticz data in the next loop()
  212. }
  213. }//if topic = domoticz_out_topic
  214. }//mqttCallback
  215. void mqttPrepareConnection() {
  216. Serial.print("MQTT connection with ");
  217. if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) == 0) {
  218. // user and password, no Last Will
  219. Serial.println("user and password, no Last Will");
  220. mqttMode = 2;
  221. }
  222. else if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) > 0) {
  223. // user, password and Last Will
  224. Serial.println("user, password and Last Will");
  225. mqttMode = 3;
  226. }
  227. else if (strlen(mqtt_user) == 0 && strlen(mqtt_willTopic) > 0) {
  228. // Last Will but no user and password
  229. Serial.println("Last Will but no user and password");
  230. mqttMode = 4;
  231. }
  232. else {
  233. // no user, password and no Last Will
  234. Serial.println("no user, password and no Last Will");
  235. mqttMode = 1;
  236. }
  237. }
  238. void mqttPrepareSubscribeTopics() {
  239. //char tmp_topic_out[50];
  240. sprintf(mqtt_topic_in_cmd, "%s/%s", mqtt_topic_in, "cmd");
  241. sprintf(mqtt_topic_in_setTemp, "%s/%s", mqtt_topic_in_cmd, "setTemp");
  242. sprintf(mqtt_topic_in_setMode, "%s/%s", mqtt_topic_in_cmd, "setMode");
  243. sprintf(mqtt_topic_in_setPreset, "%s/%s", mqtt_topic_in_cmd, "setPreset");
  244. }
  245. boolean mqttReconnect() {
  246. mqttclient.disconnect();
  247. delay(10);
  248. // Create MQTT client ID from device name
  249. String mqttClientId = "ESP8266Client-";
  250. //mqttClientId += String(random(0xffff), HEX); //or random
  251. mqttClientId += String(deviceName);
  252. if (serialdebug) Serial.print("connecting to MQTT broker with ");
  253. boolean connRes;
  254. switch (mqttMode) {
  255. case 1:
  256. if (serialdebug) Serial.println("no user, password and no Last Will");
  257. connRes = mqttclient.connect(mqttClientId.c_str());
  258. break;
  259. case 2:
  260. if (serialdebug) Serial.println("user and password, no Last Will");
  261. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass);
  262. break;
  263. case 3:
  264. if (serialdebug) Serial.println("user, password and Last Will");
  265. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass, mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  266. break;
  267. case 4:
  268. if (serialdebug) Serial.println("Last Will, no user and password");
  269. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  270. break;
  271. }
  272. if (serialdebug) {
  273. Serial.print("... attempt: ");
  274. Serial.print(mqttReconnectAttempts);
  275. Serial.println();
  276. }
  277. if (connRes) {
  278. if (serialdebug) {
  279. Serial.print("MQTT connected. Reconnects: ");
  280. Serial.println(mqttReconnects);
  281. }
  282. displayShowMQTTConnected();
  283. mqttConnected = true;
  284. mqttReconnects++;
  285. // Once connected, publish an announcement...
  286. // char outMsg[30];
  287. // sprintf(outMsg, "connected, %d reconnects", mqttReconnects);
  288. // mqttclient.publish(mqtt_topic_out, outMsg, mqtt_outRetain);
  289. mqttclient.publish(mqtt_willTopic, mqtt_connMsg, mqtt_willRetain);
  290. publishStatus();
  291. //mqttclient.publish(mqtt_topic_out, "connected");
  292. // ... and resubscribe
  293. if (serialdebug) Serial.println("Subscribed to:");
  294. if (strlen(mqtt_topic_in_cmd) > 0) {
  295. char mqtt_topic_in_subscribe[52];
  296. sprintf(mqtt_topic_in_subscribe, "%s/%s", mqtt_topic_in_cmd, "#");
  297. if (mqttclient.subscribe(mqtt_topic_in_subscribe)) {
  298. if (serialdebug) Serial.println(mqtt_topic_in_subscribe);
  299. mqttInTopicSubscribed = true;
  300. }
  301. }
  302. // if (strlen(mqtt_topic_in_setTemp) > 0) {
  303. // if (mqttclient.subscribe(mqtt_topic_in_setTemp)) {
  304. // if (serialdebug) Serial.println(mqtt_topic_in_setTemp);
  305. // }
  306. // }
  307. // if (strlen(mqtt_topic_in_setMode) > 0) {
  308. // if (mqttclient.subscribe(mqtt_topic_in_setMode)) {
  309. // if (serialdebug) Serial.println(mqtt_topic_in_setMode);
  310. // }
  311. // }
  312. // if (strlen(mqtt_topic_in_setPreset) > 0) {
  313. // if (mqttclient.subscribe(mqtt_topic_in_setPreset)) {
  314. // if (serialdebug) Serial.println(mqtt_topic_in_setPreset);
  315. // }
  316. // }
  317. if (strlen(outTemp_topic_in) > 0) {
  318. if (mqttclient.subscribe(outTemp_topic_in)) {
  319. if (serialdebug) Serial.println(outTemp_topic_in);
  320. }
  321. }
  322. if (strlen(outHum_topic_in) > 0) {
  323. if (mqttclient.subscribe(outHum_topic_in)) {
  324. if (serialdebug) Serial.println(outHum_topic_in);
  325. }
  326. }
  327. if (useDomoticz && strlen(domoticz_out_topic) > 0) {
  328. if (mqttclient.subscribe(domoticz_out_topic)) {
  329. if (serialdebug) Serial.println(domoticz_out_topic);
  330. }
  331. }
  332. return mqttclient.connected();
  333. }
  334. else {
  335. if (serialdebug) {
  336. Serial.print("MQTT connect FAILED, rc=");
  337. Serial.println(mqttclient.state());
  338. }
  339. displayShowMQTTConnectionError();
  340. }
  341. } //mqttReconnect
  342. void mqttClientInit() {
  343. mqttclient.setServer(mqtt_server, mqtt_port);
  344. mqttclient.setCallback(mqttCallback);
  345. mqttLastReconnectAttempt = 0;
  346. mqttReconnectAttempts = 0;
  347. }
  348. int lastWifiStatus;
  349. void mqttHandleConnection() {
  350. int currWifiStatus = WiFi.status();
  351. if ( currWifiStatus != lastWifiStatus ) {
  352. lastWifiStatus = currWifiStatus;
  353. Serial.print("WiFi status changed to: ");
  354. Serial.println(currWifiStatus);
  355. }
  356. if ( currWifiStatus == WL_CONNECTED ) {
  357. // MQTT reconnect if not connected (nonblocking)
  358. boolean doReconnect = false;
  359. if (!mqttclient.connected()) doReconnect = true;
  360. if (mqttInTopicSubscribed) { // if mqtt_topic_in is subscribed
  361. if ( (millis() - mqttLastHeartbeat) > MQTT_HEARTBEAT_MAXAGE) { // also reconnect if no HEARTBEAT was received for some time
  362. doReconnect = true;
  363. mqttConnected = false;
  364. mqttLastHeartbeat = millis();
  365. Serial.println("MQTT HEARTBEAT overdue. Force-Reconnecting MQTT...");
  366. }
  367. }
  368. if (doReconnect) {
  369. unsigned int mqttReconnectAttemptDelay;
  370. if (mqttReconnectAttempts < 3) mqttReconnectAttemptDelay = 15000; // if this is the 1-3rd attempt, try again in 15s
  371. else if (mqttReconnectAttempts < 10) mqttReconnectAttemptDelay = 60000; // if more than 3 attempts failed, try again every min
  372. else if (mqttReconnectAttempts < 20) mqttReconnectAttemptDelay = 300000; // if more than 10 attempts failed, try again every 5 min
  373. else if (mqttReconnectAttempts >= 6) {
  374. // if more than 6 attempts (= > 30 min) failed, restart the ESP
  375. delay(100);
  376. ESP.restart();
  377. }
  378. if ((millis() - mqttLastReconnectAttempt) > mqttReconnectAttemptDelay) {
  379. mqttLastReconnectAttempt = millis();
  380. mqttReconnectAttempts++;
  381. if (mqttReconnect()) { // Attempt to reconnect
  382. // attempt successful - reset mqttReconnectAttempts
  383. mqttReconnectAttempts = 0;
  384. }
  385. }
  386. mqttclient.loop();
  387. }
  388. else {
  389. mqttclient.loop();
  390. }
  391. }
  392. }
  393. void publishStatus() {
  394. char outMsg[60];
  395. long upTime = millis() / 1000;
  396. sprintf(outMsg, "connected, reconnects: %d, uptime: %d, free heap: %d", mqttReconnects - 1, upTime, ESP.getFreeHeap());
  397. mqttclient.publish(mqtt_topic_out, outMsg, mqtt_outRetain);
  398. mqttclient.publish(mqtt_willTopic, mqtt_connMsg, mqtt_willRetain);
  399. yield();
  400. }
  401. void sendStatus(char* payload) {
  402. char buf[101];
  403. strlcpy(buf, payload, 101);
  404. Serial.println(buf);
  405. mqttclient.publish(mqtt_topic_out, buf, mqtt_outRetain);
  406. yield();
  407. }
  408. void publishCurrentThermostatValues() {
  409. char tmp_topic_out[50];
  410. updateCurrentHeatingModeName();
  411. updateCurrentPresetName();
  412. char ch_setTemp[6];
  413. char ch_currSetTemp[6];
  414. dtostrf(setTemp, 1, 1, ch_setTemp );
  415. dtostrf(currSetTemp, 1, 1, ch_currSetTemp );
  416. Serial.print("heatingMode: '");
  417. Serial.print(heatingMode);
  418. Serial.println("'");
  419. Serial.print("set temp: '");
  420. Serial.print(ch_setTemp);
  421. Serial.println("'");
  422. Serial.print("current set temp: '");
  423. Serial.print(ch_currSetTemp);
  424. Serial.println("'");
  425. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "setTemp");
  426. mqttclient.publish(tmp_topic_out, ch_setTemp);
  427. yield();
  428. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "currSetTemp");
  429. mqttclient.publish(tmp_topic_out, ch_currSetTemp);
  430. yield();
  431. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "mode");
  432. char ch_heatingMode[3];
  433. sprintf(ch_heatingMode, "%d", heatingMode);
  434. mqttclient.publish(tmp_topic_out, ch_heatingMode);
  435. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "modeName");
  436. mqttclient.publish(tmp_topic_out, currentModeName);
  437. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "preset");
  438. char ch_preset[3];
  439. sprintf(ch_preset, "%d", preset);
  440. mqttclient.publish(tmp_topic_out, ch_preset);
  441. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "presetName");
  442. mqttclient.publish(tmp_topic_out, currentPresetName);
  443. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "presetHA");
  444. if(preset == 0) mqttclient.publish(tmp_topic_out, "none");
  445. else mqttclient.publish(tmp_topic_out, currentPresetName);
  446. yield();
  447. char ch_turnHeatingOn[5];
  448. if (turnHeatingOn) strcpy(ch_turnHeatingOn, "on");
  449. else strcpy(ch_turnHeatingOn, "off");
  450. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heating");
  451. mqttclient.publish(tmp_topic_out, ch_turnHeatingOn);
  452. yield();
  453. char buf[101];
  454. sprintf(buf, "%d", heatingOnTime);
  455. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heatingOnTime");
  456. mqttclient.publish(tmp_topic_out, buf);
  457. yield();
  458. sprintf(buf, "%d", heatingOffTime);
  459. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heatingOffTime");
  460. mqttclient.publish(tmp_topic_out, buf);
  461. yield();
  462. }
  463. void publishCurrentSensorValues() {
  464. if ( lastTempUpdate != 0 && (millis() - lastTempUpdate) < 120000 ) {
  465. char tmp_topic_out[50];
  466. char temp_chararr[6];
  467. char hum_chararr[4];
  468. dtostrf(currTemp, 1, 1, temp_chararr );
  469. sprintf(hum_chararr, "%2i", currHum);
  470. Serial.print("temp: '");
  471. Serial.print(temp_chararr);
  472. Serial.println("'");
  473. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "temp");
  474. mqttclient.publish(tmp_topic_out, temp_chararr);
  475. yield();
  476. Serial.print("hum: '");
  477. Serial.print(currHum);
  478. Serial.println("'");
  479. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "hum");
  480. mqttclient.publish(tmp_topic_out, hum_chararr);
  481. yield();
  482. dtostrf(currTemp_raw, 1, 1, temp_chararr );
  483. sprintf(hum_chararr, "%2i", currHum_raw);
  484. Serial.print("temp_raw: '");
  485. Serial.print(temp_chararr);
  486. Serial.println("'");
  487. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "temp_raw");
  488. mqttclient.publish(tmp_topic_out, temp_chararr);
  489. yield();
  490. Serial.print("hum_raw: '");
  491. Serial.print(currHum_raw);
  492. Serial.println("'");
  493. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "hum_raw");
  494. mqttclient.publish(tmp_topic_out, hum_chararr);
  495. yield();
  496. }
  497. }
  498. void publishCurrentPIRValue() {
  499. char tmp_topic_out[50];
  500. char PIRStatus[4];
  501. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "PIR");
  502. if (PIRSensorOn) {
  503. strcpy(PIRStatus, "on");
  504. }
  505. else {
  506. strcpy(PIRStatus, "off");
  507. }
  508. mqttclient.publish(tmp_topic_out, PIRStatus);
  509. if (strlen(mqtt_topic_pir) > 0) {
  510. mqttclient.publish(mqtt_topic_pir, PIRStatus);
  511. }
  512. }
  513. void mqttPublishHeartbeat() {
  514. mqttclient.publish(mqtt_topic_in_cmd, "HEARTBEAT"); // publishes to IN-topic. MQTT will force-reconnect if it does not get a HEARTBEAT for 2 min
  515. }