mqtt.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ( useDomoticz ) {
  22. if (strcmp(topic, domoticz_out_topic) == 0) { //if topic = DOMOTICZ_OUT_TOPIC
  23. Serial.print("received MQTT ");
  24. Serial.println(domoticz_out_topic);
  25. if ( !domoticzOutParserBusy ) {
  26. for (int i = 0; i < length; i++) {
  27. domoticzOutPayload[i] = (char)payload[i];
  28. }
  29. domoticzOutPayload[length + 1] = '\0';
  30. domoticzOutParseData = true; // parse domoticz data in the next loop()
  31. }
  32. }
  33. //}
  34. }//mqttCallback
  35. int mqttReconnects=0;
  36. boolean mqttReconnect() {
  37. // Create a random MQTT client ID
  38. String mqttClientId = "ESP8266Client-";
  39. mqttClientId += String(random(0xffff), HEX);
  40. boolean connRes;
  41. Serial.print("MQTT connection with ");
  42. if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) == 0) {
  43. // user and password, no Last Will
  44. // boolean connect(const char* id, const char* user, const char* pass);
  45. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass);
  46. Serial.println("user and password, no Last Will");
  47. }
  48. else if (strlen(mqtt_user) > 0 && strlen(mqtt_willTopic) > 0) {
  49. // user, password and Last Will
  50. // boolean connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
  51. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_user, mqtt_pass, mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  52. Serial.println("user, password and Last Will");
  53. }
  54. else if (strlen(mqtt_user) == 0 && strlen(mqtt_willTopic) > 0) {
  55. // Last Will but no user and password
  56. // boolean connect(const char* id, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage);
  57. connRes = mqttclient.connect(mqttClientId.c_str(), mqtt_willTopic, mqtt_willQos, mqtt_willRetain, mqtt_willMsg);
  58. Serial.println("Last Will but no user and password");
  59. }
  60. else {
  61. // no user, password and no Last Will
  62. Serial.println("no user, password and no Last Will");
  63. connRes = mqttclient.connect(mqttClientId.c_str());
  64. }
  65. if (connRes) {
  66. Serial.print("MQTT connected. Reconnects: ");
  67. Serial.println(mqttReconnects);
  68. // Once connected, publish an announcement...
  69. char outMsg[30];
  70. sprintf(outMsg, "connected, %d reconnects", mqttReconnects);
  71. mqttclient.publish(mqtt_topic_out, outMsg);
  72. //mqttclient.publish(mqtt_topic_out, "connected");
  73. // ... and resubscribe
  74. Serial.println("Subscribed to:");
  75. if (mqttclient.subscribe(mqtt_topic_in)) {
  76. Serial.println(mqtt_topic_in);
  77. }
  78. if (useDomoticz && strlen(domoticz_out_topic) > 0) {
  79. if (mqttclient.subscribe(domoticz_out_topic)) {
  80. Serial.println(domoticz_out_topic);
  81. }
  82. }
  83. }
  84. mqttReconnects++;
  85. return mqttclient.connected();
  86. } //mqttReconnect
  87. void mqttClientInit() {
  88. mqttclient.setServer(mqtt_server, mqtt_port);
  89. mqttclient.setCallback(mqttCallback);
  90. mqttLastReconnectAttempt = 0;
  91. }
  92. void mqttHandleConnection() {
  93. if ( WiFi.status() == WL_CONNECTED ) {
  94. // MQTT reconnect if not connected (nonblocking)
  95. if (!mqttclient.connected()) {
  96. long now = millis();
  97. if (now - mqttLastReconnectAttempt > 5000) {
  98. mqttLastReconnectAttempt = now;
  99. if (mqttReconnect()) { // Attempt to reconnect
  100. mqttLastReconnectAttempt = 0;
  101. }
  102. }
  103. } else { // Client connected
  104. mqttclient.loop();
  105. }//else
  106. }
  107. }