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.

280 lines
8.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 "libs/RFM69Wrap.h"
  7. #define RFM69_PACKET_SEPARATOR ':'
  8. // -----------------------------------------------------------------------------
  9. // Locals
  10. // -----------------------------------------------------------------------------
  11. RFM69Wrap * _rfm69_radio;
  12. struct _node_t {
  13. unsigned long count = 0;
  14. unsigned long missing = 0;
  15. unsigned long duplicates = 0;
  16. unsigned char lastPacketID = 0;
  17. };
  18. _node_t _rfm69_node_info[255];
  19. unsigned char _rfm69_node_count;
  20. unsigned long _rfm69_packet_count;
  21. // -----------------------------------------------------------------------------
  22. // WEB
  23. // -----------------------------------------------------------------------------
  24. #if WEB_SUPPORT
  25. void _rfm69WebSocketOnSend(JsonObject& root) {
  26. root["rfm69Visible"] = 1;
  27. root["rfm69Topic"] = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  28. root["packetCount"] = _rfm69_packet_count;
  29. root["nodeCount"] = _rfm69_node_count;
  30. JsonArray& mappings = root.createNestedArray("mapping");
  31. for (unsigned char i=0; i<RFM69_MAX_TOPICS; i++) {
  32. unsigned char node = getSetting("node", i, 0).toInt();
  33. if (0 == node) break;
  34. JsonObject& mapping = mappings.createNestedObject();
  35. mapping["node"] = node;
  36. mapping["key"] = getSetting("key", i, "");
  37. mapping["topic"] = getSetting("topic", i, "");
  38. }
  39. }
  40. bool _rfm69WebSocketOnReceive(const char * key, JsonVariant& value) {
  41. if (strncmp(key, "rfm69", 5) == 0) return true;
  42. if (strncmp(key, "node", 4) == 0) return true;
  43. if (strncmp(key, "key", 3) == 0) return true;
  44. if (strncmp(key, "topic", 5) == 0) return true;
  45. return false;
  46. }
  47. void _rfm69WebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  48. if (strcmp(action, "clear-counts") == 0) _rfm69Clear();
  49. }
  50. #endif // WEB_SUPPORT
  51. void _rfm69CleanNodes(unsigned char num) {
  52. // Look for the last defined node
  53. int i = 0;
  54. while (i < num) {
  55. if (getSetting("node", i, 0).toInt() == 0) break;
  56. if (getSetting("key", i, "").length() == 0) break;
  57. if (getSetting("topic", i, "").length() == 0) break;
  58. ++i;
  59. }
  60. // Delete all other settings
  61. while (i < WIFI_MAX_NETWORKS) {
  62. delSetting("node", i);
  63. delSetting("key", i);
  64. delSetting("topic", i);
  65. ++i;
  66. }
  67. }
  68. void _rfm69Configure() {
  69. _rfm69CleanNodes(RFM69_MAX_TOPICS);
  70. }
  71. // -----------------------------------------------------------------------------
  72. // Radio
  73. // -----------------------------------------------------------------------------
  74. void _rfm69Debug(const char * level, packet_t * data) {
  75. DEBUG_MSG_P(
  76. PSTR("[RFM69] %s: messageID:%05d senderID:%03d targetID:%03d packetID:%03d rssi:%-04d key:%s value:%s\n"),
  77. level,
  78. data->messageID,
  79. data->senderID,
  80. data->targetID,
  81. data->packetID,
  82. data->rssi,
  83. data->key,
  84. data->value
  85. );
  86. }
  87. void _rfm69Process(packet_t * data) {
  88. // Count seen nodes and packets
  89. if (_rfm69_node_info[data->senderID].count == 0) ++_rfm69_node_count;
  90. ++_rfm69_packet_count;
  91. // Detect duplicates and missing packets
  92. // packetID==0 means device is not sending packetID info
  93. if (data->packetID > 0) {
  94. if (_rfm69_node_info[data->senderID].count > 0) {
  95. unsigned char gap = data->packetID - _rfm69_node_info[data->senderID].lastPacketID;
  96. if (gap == 0) {
  97. _rfm69_node_info[data->senderID].duplicates = _rfm69_node_info[data->senderID].duplicates + 1;
  98. //_rfm69Debug("DUP", data);
  99. return;
  100. }
  101. if ((gap > 1) && (data->packetID > 1)) {
  102. _rfm69_node_info[data->senderID].missing = _rfm69_node_info[data->senderID].missing + gap - 1;
  103. DEBUG_MSG_P(PSTR("[RFM69] %u missing packets detected\n"), gap - 1);
  104. }
  105. }
  106. }
  107. _rfm69Debug("OK ", data);
  108. _rfm69_node_info[data->senderID].lastPacketID = data->packetID;
  109. _rfm69_node_info[data->senderID].count = _rfm69_node_info[data->senderID].count + 1;
  110. // Send info to websocket clients
  111. {
  112. char buffer[200];
  113. snprintf_P(
  114. buffer,
  115. sizeof(buffer) - 1,
  116. PSTR("{\"nodeCount\": %d, \"packetCount\": %lu, \"packet\": {\"senderID\": %u, \"targetID\": %u, \"packetID\": %u, \"key\": \"%s\", \"value\": \"%s\", \"rssi\": %d, \"duplicates\": %d, \"missing\": %d}}"),
  117. _rfm69_node_count, _rfm69_packet_count,
  118. data->senderID, data->targetID, data->packetID, data->key, data->value, data->rssi,
  119. _rfm69_node_info[data->senderID].duplicates , _rfm69_node_info[data->senderID].missing);
  120. wsSend(buffer);
  121. }
  122. // If we are the target of the message, forward it via MQTT, otherwise quit
  123. if (!RFM69_PROMISCUOUS_SENDS && (RFM69_GATEWAY_ID != data->targetID)) return;
  124. // Try to find a matching mapping
  125. for (unsigned int i=0; i<RFM69_MAX_TOPICS; i++) {
  126. unsigned char node = getSetting("node", i, 0).toInt();
  127. if (0 == node) break;
  128. if ((node == data->senderID) && (getSetting("key", i, "").equals(data->key))) {
  129. mqttSendRaw((char *) getSetting("topic", i, "").c_str(), (char *) String(data->value).c_str());
  130. return;
  131. }
  132. }
  133. // Mapping not found, use default topic
  134. String topic = getSetting("rfm69Topic", RFM69_DEFAULT_TOPIC);
  135. if (topic.length() > 0) {
  136. topic.replace("{node}", String(data->senderID));
  137. topic.replace("{key}", String(data->key));
  138. mqttSendRaw((char *) topic.c_str(), (char *) String(data->value).c_str());
  139. }
  140. }
  141. void _rfm69Loop() {
  142. if (_rfm69_radio->receiveDone()) {
  143. uint8_t senderID = _rfm69_radio->SENDERID;
  144. uint8_t targetID = _rfm69_radio->TARGETID;
  145. int16_t rssi = _rfm69_radio->RSSI;
  146. uint8_t length = _rfm69_radio->DATALEN;
  147. char buffer[length + 1];
  148. strncpy(buffer, (const char *) _rfm69_radio->DATA, length);
  149. buffer[length] = 0;
  150. // Do not send ACKs in promiscuous mode,
  151. // we want to listen without being heard
  152. if (!RFM69_PROMISCUOUS) {
  153. if (_rfm69_radio->ACKRequested()) _rfm69_radio->sendACK();
  154. }
  155. uint8_t parts = 1;
  156. for (uint8_t i=0; i<length; i++) {
  157. if (buffer[i] == RFM69_PACKET_SEPARATOR) ++parts;
  158. }
  159. if (parts > 1) {
  160. char sep[2] = {RFM69_PACKET_SEPARATOR, 0};
  161. uint8_t packetID = 0;
  162. char * key = strtok(buffer, sep);
  163. char * value = strtok(NULL, sep);
  164. if (parts > 2) {
  165. char * packet = strtok(NULL, sep);
  166. packetID = atoi(packet);
  167. }
  168. packet_t message;
  169. message.messageID = ++_rfm69_packet_count;
  170. message.packetID = packetID;
  171. message.senderID = senderID;
  172. message.targetID = targetID;
  173. message.key = key;
  174. message.value = value;
  175. message.rssi = rssi;
  176. _rfm69Process(&message);
  177. }
  178. }
  179. }
  180. void _rfm69Clear() {
  181. for(unsigned int i=0; i<255; i++) {
  182. _rfm69_node_info[i].duplicates = 0;
  183. _rfm69_node_info[i].missing = 0;
  184. }
  185. _rfm69_node_count = 0;
  186. _rfm69_packet_count = 0;
  187. }
  188. // -----------------------------------------------------------------------------
  189. // RFM69
  190. // -----------------------------------------------------------------------------
  191. void rfm69Setup() {
  192. delay(10);
  193. _rfm69Configure();
  194. _rfm69_radio = new RFM69Wrap(RFM69_CS_PIN, RFM69_IRQ_PIN, RFM69_IS_RFM69HW, digitalPinToInterrupt(RFM69_IRQ_PIN));
  195. _rfm69_radio->initialize(RFM69_FREQUENCY, RFM69_NODE_ID, RFM69_NETWORK_ID);
  196. _rfm69_radio->encrypt(RFM69_ENCRYPTKEY);
  197. _rfm69_radio->promiscuous(RFM69_PROMISCUOUS);
  198. if (RFM69_IS_RFM69HW) _rfm69_radio->setHighPower();
  199. DEBUG_MSG_P(PSTR("[RFM69] Worning at %u MHz\n"), RFM69_FREQUENCY == RF69_433MHZ ? 433 : RFM69_FREQUENCY == RF69_868MHZ ? 868 : 915);
  200. DEBUG_MSG_P(PSTR("[RFM69] Node %u\n"), RFM69_NODE_ID);
  201. DEBUG_MSG_P(PSTR("[RFM69] Network %u\n"), RFM69_NETWORK_ID);
  202. DEBUG_MSG_P(PSTR("[RFM69] Promiscuous mode %s\n"), RFM69_PROMISCUOUS ? "ON" : "OFF");
  203. DEBUG_MSG_P(PSTR("[RFM69] Auto Transmission Control (ATC) enabled\n"));
  204. #if WEB_SUPPORT
  205. wsOnSendRegister(_rfm69WebSocketOnSend);
  206. wsOnReceiveRegister(_rfm69WebSocketOnReceive);
  207. wsOnAfterParseRegister(_rfm69Configure);
  208. wsOnActionRegister(_rfm69WebSocketOnAction);
  209. #endif
  210. // Register loop
  211. espurnaRegisterLoop(_rfm69Loop);
  212. }
  213. #endif // RFM69_SUPPORT