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.

218 lines
6.6 KiB

  1. /*
  2. RFM69 MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if RFM69_SUPPORT
  6. #include "RFM69Manager.h"
  7. // -----------------------------------------------------------------------------
  8. // Locals
  9. // -----------------------------------------------------------------------------
  10. RFM69Manager * _rfm69_radio;
  11. struct _node_t {
  12. unsigned long count = 0;
  13. unsigned long missing = 0;
  14. unsigned long duplicates = 0;
  15. unsigned char lastPacketID = 0;
  16. };
  17. _node_t _rfm69_node_info[255];
  18. unsigned char _rfm69_node_count;
  19. unsigned long _rfm69_packet_count;
  20. // -----------------------------------------------------------------------------
  21. // WEB
  22. // -----------------------------------------------------------------------------
  23. #if WEB_SUPPORT
  24. void _rfm69WebSocketOnSend(JsonObject& root) {
  25. root["rfm69Visible"] = 1;
  26. root["rfm69Topic"] = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  27. root["packetCount"] = _rfm69_packet_count;
  28. root["nodeCount"] = _rfm69_node_count;
  29. JsonArray& mappings = root.createNestedArray("mapping");
  30. for (unsigned char i=0; i<RFM69_MAX_TOPICS; i++) {
  31. unsigned char node = getSetting("node", i, 0).toInt();
  32. if (0 == node) break;
  33. JsonObject& mapping = mappings.createNestedObject();
  34. mapping["node"] = node;
  35. mapping["key"] = getSetting("key", i, "");
  36. mapping["topic"] = getSetting("topic", i, "");
  37. }
  38. }
  39. bool _rfm69WebSocketOnReceive(const char * key, JsonVariant& value) {
  40. if (strncmp(key, "rfm69", 5) == 0) return true;
  41. if (strncmp(key, "node", 4) == 0) return true;
  42. if (strncmp(key, "key", 3) == 0) return true;
  43. if (strncmp(key, "topic", 5) == 0) return true;
  44. return false;
  45. }
  46. void _rfm69WebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  47. if (strcmp(action, "clear-counts") == 0) _rfm69Clear();
  48. }
  49. #endif // WEB_SUPPORT
  50. void _rfm69CleanNodes(unsigned char num) {
  51. // Look for the last defined node
  52. int i = 0;
  53. while (i < num) {
  54. if (getSetting("node", i, 0).toInt() == 0) break;
  55. if (getSetting("key", i, "").length() == 0) break;
  56. if (getSetting("topic", i, "").length() == 0) break;
  57. ++i;
  58. }
  59. // Delete all other settings
  60. while (i < WIFI_MAX_NETWORKS) {
  61. delSetting("node", i);
  62. delSetting("key", i);
  63. delSetting("topic", i);
  64. ++i;
  65. }
  66. }
  67. void _rfm69Configure() {
  68. _rfm69CleanNodes(RFM69_MAX_TOPICS);
  69. }
  70. // -----------------------------------------------------------------------------
  71. // Radio
  72. // -----------------------------------------------------------------------------
  73. void _rfm69Process(packet_t * data) {
  74. char response[4] = "OK ";
  75. // Count seen nodes and packets
  76. if (_rfm69_node_info[data->senderID].count == 0) ++_rfm69_node_count;
  77. ++_rfm69_packet_count;
  78. // Detect duplicates and missing packets
  79. // packetID==0 means device is not sending packetID info
  80. if (data->packetID > 0) {
  81. if (_rfm69_node_info[data->senderID].count > 0) {
  82. unsigned char gap = data->packetID - _rfm69_node_info[data->senderID].lastPacketID;
  83. if (gap == 0) {
  84. strncpy(response, "DUP", sizeof(response));
  85. _rfm69_node_info[data->senderID].duplicates = _rfm69_node_info[data->senderID].duplicates + 1;
  86. return;
  87. }
  88. if ((gap > 1) && (data->packetID > 1)) {
  89. strncpy(response, "MIS", sizeof(response));
  90. _rfm69_node_info[data->senderID].missing = _rfm69_node_info[data->senderID].missing + gap - 1;
  91. }
  92. }
  93. }
  94. DEBUG_MSG_P(
  95. PSTR("[RFM69] %s: messageID:%05d senderID:%03d targetID:%03d packetID:%03d rssi:%-04d name:%s value:%s\n"),
  96. response,
  97. data->messageID,
  98. data->senderID,
  99. data->targetID,
  100. data->packetID,
  101. data->rssi,
  102. data->name,
  103. data->value
  104. );
  105. _rfm69_node_info[data->senderID].lastPacketID = data->packetID;
  106. _rfm69_node_info[data->senderID].count = _rfm69_node_info[data->senderID].count + 1;
  107. // Send info to websocket clients
  108. {
  109. char buffer[200];
  110. snprintf_P(
  111. buffer,
  112. sizeof(buffer) - 1,
  113. PSTR("{\"nodeCount\": %d, \"packetCount\": %lu, \"packet\": {\"senderID\": %u, \"targetID\": %u, \"packetID\": %u, \"name\": \"%s\", \"value\": \"%s\", \"rssi\": %d, \"duplicates\": %d, \"missing\": %d}}"),
  114. _rfm69_node_count, _rfm69_packet_count,
  115. data->senderID, data->targetID, data->packetID, data->name, data->value, data->rssi,
  116. _rfm69_node_info[data->senderID].duplicates , _rfm69_node_info[data->senderID].missing);
  117. wsSend(buffer);
  118. }
  119. // If we are the target of the message, forward it via MQTT, otherwise quit
  120. //if (RFM69_GATEWAY_ID != data->targetID) return;
  121. // Try to find a matching mapping
  122. for (unsigned int i=0; i<RFM69_MAX_TOPICS; i++) {
  123. unsigned char node = getSetting("node", i, 0).toInt();
  124. if (0 == node) break;
  125. if ((node == data->senderID) && (getSetting("key", i, "").equals(data->name))) {
  126. mqttSendRaw((char *) getSetting("topic", i, "").c_str(), (char *) String(data->value).c_str());
  127. return;
  128. }
  129. }
  130. // Mapping not found, use default topic
  131. String topic = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  132. if (topic.length() > 0) {
  133. topic.replace("{node}", String(data->senderID));
  134. topic.replace("{key}", String(data->name));
  135. mqttSendRaw((char *) topic.c_str(), (char *) String(data->value).c_str());
  136. }
  137. }
  138. void _rfm69Loop() {
  139. _rfm69_radio->loop();
  140. }
  141. void _rfm69Clear() {
  142. for(unsigned int i=0; i<255; i++) {
  143. _rfm69_node_info[i].duplicates = 0;
  144. _rfm69_node_info[i].missing = 0;
  145. }
  146. _rfm69_node_count = 0;
  147. _rfm69_packet_count = 0;
  148. }
  149. // -----------------------------------------------------------------------------
  150. // RFM69
  151. // -----------------------------------------------------------------------------
  152. void rfm69Setup() {
  153. delay(10);
  154. _rfm69Configure();
  155. _rfm69_radio = new RFM69Manager(RFM69_CS_PIN, RFM69_IRQ_PIN, RFM69_IS_RFM69HW, digitalPinToInterrupt(RFM69_IRQ_PIN));
  156. _rfm69_radio->initialize(RFM69_FREQUENCY, RFM69_NODE_ID, RFM69_NETWORK_ID, RFM69_ENCRYPTKEY);
  157. _rfm69_radio->promiscuous(RFM69_PROMISCUOUS);
  158. _rfm69_radio->onMessage(_rfm69Process);
  159. #if WEB_SUPPORT
  160. wsOnSendRegister(_rfm69WebSocketOnSend);
  161. wsOnReceiveRegister(_rfm69WebSocketOnReceive);
  162. wsOnAfterParseRegister(_rfm69Configure);
  163. wsOnActionRegister(_rfm69WebSocketOnAction);
  164. #endif
  165. // Register loop
  166. espurnaRegisterLoop(_rfm69Loop);
  167. }
  168. #endif // RFM69_SUPPORT