mqtt.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // MQTT callback
  2. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  3. //Serial.print("Message arrived [");
  4. //Serial.print(topic);
  5. //Serial.println("] ");
  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, mqtt_allOnOffTopic) == 0) { //if topic = mqtt_allOnOffTopic
  22. Serial.print("received MQTT ");
  23. Serial.println(mqtt_allOnOffTopic);
  24. char tmpPayload[length + 1];
  25. for (int i = 0; i < length; i++) {
  26. tmpPayload[i] = (char)payload[i];
  27. }
  28. tmpPayload[length] = '\0';
  29. Serial.print("tmpPayload:");
  30. Serial.println(tmpPayload);
  31. if (strcmp(tmpPayload, "ON") == 0 || strcmp(tmpPayload, "On") == 0 || strcmp(tmpPayload, "on") == 0) {
  32. allRelaisOn();
  33. }
  34. else if (strcmp(tmpPayload, "OFF") == 0 || strcmp(tmpPayload, "Off") == 0 || strcmp(tmpPayload, "off") == 0) {
  35. allRelaisOff();
  36. }
  37. }//if topic = mqtt_allOnOffTopic
  38. if (strcmp(topic, domoticz_out_topic) == 0) { //if topic = domoticz_out_topic
  39. Serial.print("received MQTT ");
  40. Serial.println(domoticz_out_topic);
  41. if ( !domoticzOutParserBusy ) {
  42. for (int i = 0; i < length; i++) {
  43. domoticzOutPayload[i] = (char)payload[i];
  44. }
  45. domoticzOutPayload[length + 1] = '\0';
  46. domoticzOutParseData = true; // parse domoticz data in the next loop()
  47. }
  48. }//if topic = domoticz_out_topic
  49. }//mqttCallback
  50. void mqttPrepareConnection() {
  51. Serial.print("MQTT connection with ");
  52. if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) == 0) {
  53. // user and password, no Last Will
  54. Serial.println("user and password, no Last Will");
  55. mqttMode = 2;
  56. }
  57. else if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) > 0) {
  58. // user, password and Last Will
  59. Serial.println("user, password and Last Will");
  60. mqttMode = 3;
  61. }
  62. else if (strlen(mqtt_user) == 0 && strlen(mqtt_willTopic) > 0) {
  63. // Last Will but no user and password
  64. Serial.println("Last Will but no user and password");
  65. mqttMode = 4;
  66. }
  67. else {
  68. // no user, password and no Last Will
  69. Serial.println("no user, password and no Last Will");
  70. mqttMode = 1;
  71. }
  72. }
  73. boolean mqttReconnect() {
  74. // Create a random MQTT client ID
  75. String mqttClientId = "ESP8266Client-";
  76. mqttClientId += String(random(0xffff), HEX);
  77. Serial.print("MQTT connection with ");
  78. boolean connRes;
  79. switch (mqttMode) {
  80. case 1:
  81. Serial.println("no user, password and no Last Will");
  82. connRes = mqttclient.connect(mqttClientId.c_str());
  83. break;
  84. case 2:
  85. Serial.println("user and password, no Last Will");
  86. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass);
  87. break;
  88. case 3:
  89. Serial.println("user, password and Last Will");
  90. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass, mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  91. break;
  92. case 4:
  93. Serial.println("Last Will, no user and password");
  94. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  95. break;
  96. }
  97. if (connRes) {
  98. Serial.print("MQTT connected. Reconnects: ");
  99. Serial.println(mqttReconnects);
  100. mqttReconnects++;
  101. // Once connected, publish an announcement...
  102. // char outMsg[30];
  103. // sprintf(outMsg, "connected, %d reconnects", mqttReconnects);
  104. // mqttclient.publish(mqtt_topic_out, outMsg, mqtt_outRetain);
  105. publishStatus();
  106. //mqttclient.publish(mqtt_topic_out, "connected");
  107. // ... and resubscribe
  108. Serial.println("Subscribed to:");
  109. if (mqttclient.subscribe(mqtt_topic_in)) {
  110. Serial.println(mqtt_topic_in);
  111. }
  112. if (strlen(mqtt_allOnOffTopic) > 0) {
  113. if (mqttclient.subscribe(mqtt_allOnOffTopic)) {
  114. Serial.println(mqtt_allOnOffTopic);
  115. }
  116. }
  117. if (useDomoticz && strlen(domoticz_out_topic) > 0) {
  118. if (mqttclient.subscribe(domoticz_out_topic)) {
  119. Serial.println(domoticz_out_topic);
  120. }
  121. }
  122. }
  123. return mqttclient.connected();
  124. } //mqttReconnect
  125. void mqttClientInit() {
  126. mqttclient.setServer(mqtt_server, mqtt_port);
  127. mqttclient.setCallback(mqttCallback);
  128. mqttLastReconnectAttempt = 0;
  129. }
  130. void mqttHandleConnection() {
  131. if ( WiFi.status() == WL_CONNECTED ) {
  132. // MQTT reconnect if not connected (nonblocking)
  133. if (!mqttclient.connected()) {
  134. long now = millis();
  135. if (now - mqttLastReconnectAttempt > 5000) {
  136. mqttLastReconnectAttempt = now;
  137. if (mqttReconnect()) { // Attempt to reconnect
  138. mqttLastReconnectAttempt = 0;
  139. }
  140. }
  141. } else { // Client connected
  142. mqttclient.loop();
  143. }//else
  144. }
  145. }