mqtt.ino 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // MQTT callback
  2. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  3. // Serial.print("Message arrived [");
  4. // Serial.print(topic);
  5. // Serial.print("] ");
  6. // for (int i = 0; i < length; i++) {
  7. // Serial.print((char)payload[i]);
  8. // }
  9. // Serial.println();
  10. if (strcmp(topic, mqtt_topic_in) == 0) { //if topic = mqtt_topic_in
  11. Serial.print("received MQTT ");
  12. Serial.println(mqtt_topic_in);
  13. for (int i = 0; i < length; i++) {
  14. cmdPayload[i] = (char)payload[i];
  15. }
  16. cmdPayload[length + 1] = '\0';
  17. Serial.print("cmdPayload:");
  18. Serial.println(cmdPayload);
  19. cmdInQueue = true;
  20. }//if topic = mqtt_topic_in
  21. if (strcmp(topic, outTemp_topic_in) == 0) { //if topic = outTemp_topic_in
  22. char inValue[length + 1];
  23. for (int i = 0; i < length; i++) {
  24. inValue[i] = (char)payload[i];
  25. }
  26. inValue[sizeof(inValue) - 1] = '\0';
  27. //Serial.print(inValue);
  28. //Serial.println();
  29. float invalueFloat = atof(inValue);
  30. outTemp = invalueFloat;
  31. outTempHumLastUpdate = millis();
  32. Serial.print("atemp=");
  33. Serial.println(invalueFloat);
  34. }//if topic = outTemp_topic_in
  35. if (strcmp(topic, outHum_topic_in) == 0) { //if topic = outHum_topic_in
  36. char inValue[length + 1];
  37. for (int i = 0; i < length; i++) {
  38. inValue[i] = (char)payload[i];
  39. }
  40. inValue[sizeof(inValue) - 1] = '\0';
  41. //Serial.print(inValue);
  42. //Serial.println();
  43. outHum = atoi(inValue);
  44. outTempHumLastUpdate = millis();
  45. Serial.print("ahum=");
  46. Serial.println(inValue);
  47. }//if topic = outHum_topic_in
  48. if (strcmp(topic, domoticz_out_topic) == 0) { //if topic = domoticz_out_topic
  49. Serial.print("received MQTT ");
  50. Serial.println(domoticz_out_topic);
  51. if ( !domoticzOutParserBusy ) {
  52. for (int i = 0; i < length; i++) {
  53. domoticzOutPayload[i] = (char)payload[i];
  54. }
  55. domoticzOutPayload[length + 1] = '\0';
  56. domoticzOutParseData = true; // parse domoticz data in the next loop()
  57. }
  58. }//if topic = domoticz_out_topic
  59. }//mqttCallback
  60. void mqttPrepareConnection() {
  61. Serial.print("MQTT connection with ");
  62. if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) == 0) {
  63. // user and password, no Last Will
  64. Serial.println("user and password, no Last Will");
  65. mqttMode = 2;
  66. }
  67. else if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) > 0) {
  68. // user, password and Last Will
  69. Serial.println("user, password and Last Will");
  70. mqttMode = 3;
  71. }
  72. else if (strlen(mqtt_user) == 0 && strlen(mqtt_willTopic) > 0) {
  73. // Last Will but no user and password
  74. Serial.println("Last Will but no user and password");
  75. mqttMode = 4;
  76. }
  77. else {
  78. // no user, password and no Last Will
  79. Serial.println("no user, password and no Last Will");
  80. mqttMode = 1;
  81. }
  82. }
  83. boolean mqttReconnect() {
  84. // Create a random MQTT client ID
  85. String mqttClientId = "ESP8266Client-";
  86. mqttClientId += String(random(0xffff), HEX);
  87. Serial.print("MQTT connection with ");
  88. boolean connRes;
  89. switch (mqttMode) {
  90. case 1:
  91. Serial.println("no user, password and no Last Will");
  92. connRes = mqttclient.connect(mqttClientId.c_str());
  93. break;
  94. case 2:
  95. Serial.println("user and password, no Last Will");
  96. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass);
  97. break;
  98. case 3:
  99. Serial.println("user, password and Last Will");
  100. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass, mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  101. break;
  102. case 4:
  103. Serial.println("Last Will, no user and password");
  104. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  105. break;
  106. }
  107. if (connRes) {
  108. Serial.print("MQTT connected. Reconnects: ");
  109. Serial.println(mqttReconnects);
  110. mqttReconnects++;
  111. // Once connected, publish an announcement...
  112. // char outMsg[30];
  113. // sprintf(outMsg, "connected, %d reconnects", mqttReconnects);
  114. // mqttclient.publish(mqtt_topic_out, outMsg, mqtt_outRetain);
  115. publishStatus();
  116. //mqttclient.publish(mqtt_topic_out, "connected");
  117. // ... and resubscribe
  118. Serial.println("Subscribed to:");
  119. if (strlen(mqtt_topic_in) > 0) {
  120. if (mqttclient.subscribe(mqtt_topic_in)) {
  121. Serial.println(mqtt_topic_in);
  122. }
  123. }
  124. if (strlen(outTemp_topic_in) > 0) {
  125. if (mqttclient.subscribe(outTemp_topic_in)) {
  126. Serial.println(outTemp_topic_in);
  127. }
  128. }
  129. if (strlen(outHum_topic_in) > 0) {
  130. if (mqttclient.subscribe(outHum_topic_in)) {
  131. Serial.println(outHum_topic_in);
  132. }
  133. }
  134. if (useDomoticz && strlen(domoticz_out_topic) > 0) {
  135. if (mqttclient.subscribe(domoticz_out_topic)) {
  136. Serial.println(domoticz_out_topic);
  137. }
  138. }
  139. }
  140. return mqttclient.connected();
  141. } //mqttReconnect
  142. void mqttClientInit() {
  143. mqttclient.setServer(mqtt_server, mqtt_port);
  144. mqttclient.setCallback(mqttCallback);
  145. mqttLastReconnectAttempt = 0;
  146. }
  147. void mqttHandleConnection() {
  148. if ( WiFi.status() == WL_CONNECTED ) {
  149. // MQTT reconnect if not connected (nonblocking)
  150. if (!mqttclient.connected()) {
  151. long now = millis();
  152. if (now - mqttLastReconnectAttempt > 5000) {
  153. mqttLastReconnectAttempt = now;
  154. if (mqttReconnect()) { // Attempt to reconnect
  155. mqttLastReconnectAttempt = 0;
  156. }
  157. }
  158. } else { // Client connected
  159. mqttclient.loop();
  160. }//else
  161. }
  162. }
  163. void publishStatus() {
  164. char outMsg[60];
  165. long upTime = millis() / 1000;
  166. sprintf(outMsg, "connected, reconnects: %d, uptime: %d, free heap: %d", mqttReconnects - 1, upTime, ESP.getFreeHeap());
  167. mqttclient.publish(mqtt_topic_out, outMsg, mqtt_outRetain);
  168. }
  169. void sendStatus(char* payload) {
  170. char buf[101];
  171. strlcpy(buf, payload, 101);
  172. Serial.println(buf);
  173. if (mqttclient.connected()) mqttclient.publish(mqtt_topic_out, buf, mqtt_outRetain);
  174. }
  175. void publishCurrentThermostatValues() {
  176. char tmp_topic_out[50];
  177. float curr_setTemp;
  178. if (heatingMode == 1) curr_setTemp = setTemp;
  179. else if (heatingMode == 2) curr_setTemp = setTempLow;
  180. char setTemp_chararr[6];
  181. char currSetTemp_chararr[6];
  182. dtostrf(setTemp, 1, 1, setTemp_chararr );
  183. dtostrf(curr_setTemp, 1, 1, currSetTemp_chararr );
  184. Serial.print("heatingMode: '");
  185. Serial.print(heatingMode);
  186. Serial.println("'");
  187. Serial.print("set temp: '");
  188. Serial.print(setTemp_chararr);
  189. Serial.println("'");
  190. Serial.print("current set temp: '");
  191. Serial.print(currSetTemp_chararr);
  192. Serial.println("'");
  193. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "setTemp");
  194. mqttclient.publish(tmp_topic_out, setTemp_chararr);
  195. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "currSetTemp");
  196. mqttclient.publish(tmp_topic_out, currSetTemp_chararr);
  197. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heatingMode");
  198. char heatingMode_chararr[3];
  199. sprintf(heatingMode_chararr, "%d", heatingMode);
  200. mqttclient.publish(tmp_topic_out, heatingMode_chararr);
  201. char turnHeatingOn_char[5];
  202. if (turnHeatingOn) strcpy(turnHeatingOn_char, "on");
  203. else strcpy(turnHeatingOn_char, "off");
  204. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heating");
  205. mqttclient.publish(tmp_topic_out, turnHeatingOn_char);
  206. char buf[101];
  207. sprintf(buf, "%d", heatingOnTime);
  208. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heatingOnTime");
  209. mqttclient.publish(tmp_topic_out, buf);
  210. sprintf(buf, "%d", heatingOffTime);
  211. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "heatingOffTime");
  212. mqttclient.publish(tmp_topic_out, buf);
  213. }
  214. void publishCurrentSensorValues() {
  215. if ( lastTempUpdate != 0 && (millis() - lastTempUpdate) < 120000 ) {
  216. char tmp_topic_out[50];
  217. char temp_chararr[6];
  218. char hum_chararr[4];
  219. dtostrf(currTemp, 1, 1, temp_chararr );
  220. sprintf(hum_chararr, "%2i", currHum);
  221. Serial.print("temp: '");
  222. Serial.print(temp_chararr);
  223. Serial.println("'");
  224. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "temp");
  225. mqttclient.publish(tmp_topic_out, temp_chararr);
  226. Serial.print("hum: '");
  227. Serial.print(currHum);
  228. Serial.println("'");
  229. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "hum");
  230. mqttclient.publish(tmp_topic_out, hum_chararr);
  231. dtostrf(currTemp_raw, 1, 1, temp_chararr );
  232. sprintf(hum_chararr, "%2i", currHum_raw);
  233. Serial.print("temp_raw: '");
  234. Serial.print(temp_chararr);
  235. Serial.println("'");
  236. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "temp_raw");
  237. mqttclient.publish(tmp_topic_out, temp_chararr);
  238. Serial.print("hum_raw: '");
  239. Serial.print(currHum_raw);
  240. Serial.println("'");
  241. sprintf(tmp_topic_out, "%s/%s", mqtt_topic_out, "hum_raw");
  242. mqttclient.publish(tmp_topic_out, hum_chararr);
  243. }
  244. }