Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.0 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. ESPurna
  3. MQTT MODULE
  4. Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #include <ESP8266WiFi.h>
  7. #include <AsyncMqttClient.h>
  8. AsyncMqttClient mqtt;
  9. String mqttTopic;
  10. bool isCallbackMessage = false;
  11. // -----------------------------------------------------------------------------
  12. // MQTT
  13. // -----------------------------------------------------------------------------
  14. bool mqttConnected() {
  15. return mqtt.connected();
  16. }
  17. void mqttDisconnect() {
  18. mqtt.disconnect();
  19. }
  20. void buildTopics() {
  21. // Replace identifier
  22. mqttTopic = getSetting("mqttTopic", MQTT_TOPIC);
  23. mqttTopic.replace("{identifier}", getSetting("hostname"));
  24. }
  25. void mqttSend(char * topic, char * message) {
  26. if (!mqtt.connected()) return;
  27. if (isCallbackMessage) return;
  28. String path = mqttTopic + String(topic);
  29. DEBUG_MSG("[MQTT] Sending %s %s\n", (char *) path.c_str(), message);
  30. mqtt.publish(path.c_str(), MQTT_QOS, MQTT_RETAIN, message);
  31. }
  32. void _mqttOnConnect(bool sessionPresent) {
  33. DEBUG_MSG("[MQTT] Connected!\n");
  34. // Send status via webSocket
  35. wsSend((char *) "{\"mqttStatus\": true}");
  36. // Build MQTT topics
  37. buildTopics();
  38. // Say hello and report our IP and VERSION
  39. mqttSend((char *) MQTT_IP_TOPIC, (char *) getIP().c_str());
  40. mqttSend((char *) MQTT_VERSION_TOPIC, (char *) APP_VERSION);
  41. char buffer[10];
  42. getFSVersion(buffer);
  43. mqttSend((char *) MQTT_FSVERSION_TOPIC, buffer);
  44. // Publish current relay status
  45. mqttSend((char *) MQTT_STATUS_TOPIC, (char *) (relayStatus(0) ? "1" : "0"));
  46. // Subscribe to topic
  47. DEBUG_MSG("[MQTT] Subscribing to %s\n", (char *) mqttTopic.c_str());
  48. mqtt.subscribe(mqttTopic.c_str(), MQTT_QOS);
  49. }
  50. void _mqttOnDisconnect(AsyncMqttClientDisconnectReason reason) {
  51. // Send status via webSocket
  52. wsSend((char *) "{\"mqttStatus\": false}");
  53. }
  54. void _mqttOnMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
  55. static bool isFirstMessage = true;
  56. payload[len] = '\0';
  57. DEBUG_MSG("[MQTT] Received %s %s\n", topic, payload);
  58. // If relayMode is not SAME avoid responding to a retained message
  59. if (isFirstMessage) {
  60. isFirstMessage = false;
  61. byte relayMode = getSetting("relayMode", String(RELAY_MODE)).toInt();
  62. if (relayMode != 2) return;
  63. }
  64. // Action to perform
  65. if ((char)payload[0] == '0') {
  66. isCallbackMessage = true;
  67. relayStatus(0, false);
  68. }
  69. if ((char)payload[0] == '1') {
  70. isCallbackMessage = true;
  71. relayStatus(0, true);
  72. }
  73. if ((char)payload[0] == '2') {
  74. relayToggle(0);
  75. }
  76. isCallbackMessage = false;
  77. }
  78. void mqttConnect() {
  79. if (!mqtt.connected()) {
  80. String host = getSetting("mqttServer", MQTT_SERVER);
  81. String port = getSetting("mqttPort", String(MQTT_PORT));
  82. String user = getSetting("mqttUser");
  83. String pass = getSetting("mqttPassword");
  84. if (host.length() == 0) return;
  85. DEBUG_MSG("[MQTT] Connecting to broker at %s", (char *) host.c_str());
  86. mqtt.setServer(host.c_str(), port.toInt());
  87. mqtt
  88. .setKeepAlive(MQTT_KEEPALIVE)
  89. .setCleanSession(false)
  90. //.setWill("topic/online", 2, true, "no")
  91. .setClientId(getSetting("hostname", HOSTNAME).c_str());
  92. if ((user != "") & (pass != "")) {
  93. DEBUG_MSG(" as user %s.\n", (char *) user.c_str());
  94. mqtt.setCredentials(user.c_str(), pass.c_str());
  95. } else {
  96. DEBUG_MSG(" anonymously\n");
  97. }
  98. mqtt.connect();
  99. }
  100. }
  101. void mqttSetup() {
  102. mqtt.onConnect(_mqttOnConnect);
  103. mqtt.onDisconnect(_mqttOnDisconnect);
  104. mqtt.onMessage(_mqttOnMessage);
  105. }
  106. void mqttLoop() {
  107. static unsigned long lastPeriod = 0;
  108. if (WiFi.status() == WL_CONNECTED) {
  109. if (!mqtt.connected()) {
  110. unsigned long currPeriod = millis() / MQTT_RECONNECT_DELAY;
  111. if (currPeriod != lastPeriod) {
  112. lastPeriod = currPeriod;
  113. mqttConnect();
  114. }
  115. }
  116. }
  117. }