{"id":6932,"date":"2019-10-24T16:13:25","date_gmt":"2019-10-24T07:13:25","guid":{"rendered":"https:\/\/techclass.wfjydl.com\/?p=6932"},"modified":"2022-03-09T17:17:38","modified_gmt":"2022-03-09T09:17:38","slug":"arduino%e9%95%bf%e8%b7%9d%e7%a6%bb%e9%80%9a%e4%bf%a1%e6%95%99%e7%a8%8b-loralib%e5%ba%93-2","status":"publish","type":"post","link":"https:\/\/techclass.wfjydl.com\/knowledge\/deviceplus\/how-tos\/arduino\/arduino-long-range-communication-tutorial-loralib-library","title":{"rendered":"Arduino\u957f\u8ddd\u79bb\u901a\u4fe1\u6559\u7a0b\u2013LoRaLib\u5e93"},"content":{"rendered":"<div class=\u201cthe_content_wrapper\u201d>\n\u8fd9\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e<a href=\"https:\/\/www.deviceplus.com\/\">DevicePlus.com<\/a>\u82f1\u8bed\u7f51\u7ad9\u7684\u7ffb\u8bd1\u7a3f\u3002<\/p>\n<p><a href=\"https:\/\/techclass.wfjydl.com\/knowledge\/deviceplus\/how-tos\/arduino\/arduino-long-range-communication-tutorial-lorenz-shield\" style=\"font-size: 20px\"><strong>\u70b9\u51fb\u8fd9\u91cc\u9605\u8bfb LoRaLib \u5f00\u53d1\u677f &gt;<\/strong><\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6089\" src=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5.jpg\" alt=\"loralib\" width=\"700\" height=\"430\" srcset=\"https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5.jpg 700w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5-300x184.jpg 300w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5-238x146.jpg 238w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5-50x31.jpg 50w, https:\/\/techclass.wfjydl.com\/wp-content\/uploads\/2017\/07\/image001-5-122x75.jpg 122w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">\u4e3a\u4e86\u63a7\u5236 <\/span><a href=\"https:\/\/techclass.wfjydl.com\/knowledge\/deviceplus\/how-tos\/arduino\/6930\"><span style=\"font-weight: 400;\">Arduino\u957f\u8ddd\u79bb\u901a\u4fe1\u6559\u7a0b<\/span>\u2013<\/a>LoRenz \u5f00\u53d1\u677f\u4e2d\u6784\u5efa\u7684LoRenz\u5f00\u53d1\u677f\uff0c\u6211\u5f00\u53d1\u4e86LoRaLib\u2014\u2014\u7528\u4e8eSX1278\u82af\u7247\u7684\u5f00\u6e90Arduino\u5e93\u3002\u8fd9\u4e2a\u5e93\u4ece\u96f6\u5f00\u59cb\u8bbe\u8ba1\uff0c\u76ee\u7684\u53ea\u6709\u4e00\u4e2a\uff1a\u5236\u4f5c\u6613\u4e8e\u4f7f\u7528\u7684API\uff0c\u5373\u4f7f\u662f\u521d\u5b66\u8005\u4e5f\u53ef\u4ee5\u5b9e\u73b0LoRa\u901a\u4fe1\u3002\u8be5\u5e93\u7684\u76ee\u6807\u662f\u4f7f\u8fdc\u7a0b\u901a\u4fe1\u4e0e\u4e32\u884c\u901a\u4fe1\u4e00\u6837\u7b80\u5355\u3002<\/span><\/p>\n<h2><b>\u8f6f\u4ef6<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Arduino IDE<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">LoRaLib Arduino \u5e93 (\u53ef\u5728 <\/span><a href=\"https:\/\/github.com\/jgromes\/LoRaLib\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-weight: 400;\">GitHub<\/span><\/a><span style=\"font-weight: 400;\"> \u4e0a\u83b7\u5f97)<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2><b>LoRaLib \u5e93<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">SX1278\u6709\u591a\u79cd\u4e0d\u540c\u8bbe\u7f6e\uff0c\u5141\u8bb8\u7528\u6237\u5b8c\u5168\u81ea\u5b9a\u4e49\u8303\u56f4\u3001\u6570\u636e\u901f\u7387\u548c\u529f\u8017\uff0c\u4f46\u662f\u6700\u91cd\u8981\u7684\u4e09\u4e2a\u8bbe\u7f6e\u5982\u4e0b\u6240\u793a\uff1a<\/span><\/p>\n<ul>\n<li><b>\u5e26\u5bbd <\/b><span style=\"font-weight: 400;\">SX1278\u5141\u8bb8\u7684\u5e26\u5bbd\u8bbe\u7f6e\u4e3a7.8 kHz\u81f3500 kHz\u3002\u5e26\u5bbd\u503c\u8d8a\u9ad8\uff0c\u6570\u636e\u4f20\u8f93\u8d8a\u5feb\u3002\u7136\u800c\uff0c\u8fd9\u662f\u4ee5\u964d\u4f4e\u603b\u7075\u654f\u5ea6\u4e3a\u4ee3\u4ef7\u7684\uff0c\u56e0\u6b64\u964d\u4f4e\u4e86\u6700\u5927\u8303\u56f4\u3002<\/span><\/li>\n<\/ul>\n<ul>\n<li><b>\u6269\u9891\u56e0\u5b50 <\/b><span style=\"font-weight: 400;\">\u5728LoRa\u8c03\u5236\u4e2d\uff0c\u6bcf\u4e2a\u4fe1\u606f\u4f4d\u7531\u591a\u4e2a\u5541\u557e\u8868\u793a\u3002\u6269\u9891\u56e0\u5b50\u662f\u6307\u6bcf\u4f4d\u6570\u636e\u6709\u591a\u5c11\u5541\u557e\u3002SX1278\u652f\u63017\u79cd\u4e0d\u540c\u7684\u8bbe\u7f6e\uff0c\u6269\u9891\u56e0\u5b50\u8d8a\u9ad8\uff0c\u6570\u636e\u4f20\u8f93\u8d8a\u6162\uff0c\u8303\u56f4\u8d8a\u5927\u3002<\/span><\/li>\n<\/ul>\n<ul>\n<li><b>\u7f16\u7801\u901f\u7387 <\/b><span style=\"font-weight: 400;\">\u4e3a\u4e86\u63d0\u9ad8\u4f20\u8f93\u7684\u7a33\u5b9a\u6027\uff0cSX1278\u53ef\u4ee5\u6267\u884c\u9519\u8bef\u68c0\u67e5\u529f\u80fd\u3002\u6b64\u9519\u8bef\u68c0\u67e5\u7684\u5ea6\u91cf\u79f0\u4e3a\u7f16\u7801\u901f\u7387\uff0c\u53ef\u4ee5\u8bbe\u5b9a\u56db\u4e2a\u503c\u3002\u7f16\u7801\u901f\u7387\u8bbe\u4e3a\u6700\u4f4e\u76844\/5\u65f6\uff0c\u4f20\u8f93\u4e0d\u592a\u7a33\u5b9a\uff0c\u901f\u5ea6\u7a0d\u5feb\u3002\u7f16\u7801\u901f\u7387\u8bbe\u4e3a\u6700\u9ad8\u76844\/8\u65f6\uff0c\u94fe\u8def\u66f4\u53ef\u9760\uff0c\u4f46\u4ee3\u4ef7\u662f\u6570\u636e\u4f20\u8f93\u901f\u7387\u8f83\u6162\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u5e93\u7684\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a\uff1a\u5e26\u5bbd\u4e3a500 kHz\u3001\u7f16\u7801\u901f\u7387\u4e3a4\/5\u548c\u6269\u9891\u56e0\u5b50\u4e3a12\u3002\u8fd9\u4e9b\u8bbe\u7f6e\u662f\u8303\u56f4\u3001\u7a33\u5b9a\u6027\u548c\u6570\u636e\u901f\u7387\u4e4b\u95f4\u7684\u5408\u7406\u5e73\u8861\u3002\u5f53\u7136\uff0c\u8fd9\u4e9b\u8bbe\u7f6e\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u968f\u65f6\u66f4\u6539\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u8be5\u5e93\u5185\u7f6e\u6570\u636e\u5305\u7c7b\u548c\u5bfb\u5740\u7cfb\u7edf\u3002\u5730\u5740\u957f\u5ea6\u4e3a8\u5b57\u8282\uff0c\u90a3\u4e48\u6700\u5927\u7684\u5bfb\u5740\u6570\u91cf\u5c31\u662f1.8\u5343\u4ebf\u4ebf(1.8 \u00d7 10^19)\u3002\u8fd9\u4e2a\u6570\u503c\u5927\u7684\u79bb\u8c31\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0cNASA\u4f30\u8ba1\u6211\u4eec\u94f6\u6cb3\u7cfb\u4e2d\u7684\u6052\u661f\u6570\u91cf\u4ec5\u4e3a\u201c4\u4ebf\u201d(4&#215;10^11)\u3002\u6bcf\u4e2a\u6570\u636e\u5305\u7531\u6e90\u5730\u5740\u3001\u76ee\u6807\u5730\u5740\u548c\u6700\u591a240\u5b57\u8282\u7684\u6709\u6548\u8d1f\u8f7d\u7ec4\u6210\u3002\u5f53\u7136\uff0c\u8be5\u5e93\u8fd8\u63d0\u4f9b\u4e86\u51e0\u79cd\u8bfb\u53d6\u548c\u5199\u5165\u5206\u7ec4\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u4f7f\u7528\u8fd9\u4e2a\u5e93\u662f\u591a\u4e48\u5bb9\u6613\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u5e26\u6709SX1278\u6a21\u5757\u7684LoRenz\u5f00\u53d1\u677f\u3002\u5b83\u4eec\u76f8\u8ddd\u51e0\u767e\u7c73\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u8bbe\u7f6e\u3002\u9996\u5148\uff0c\u6211\u4eec\u5fc5\u987b\u5305\u542b\u5e93\u5934\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u7528\u9ed8\u8ba4\u8bbe\u7f6e\u521b\u5efa<\/span><b> LoRa <\/b><span style=\"font-weight: 400;\">\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u7528\u76ee\u6807\u5730\u5740\u548c\u6d88\u606f\u521b\u5efa<\/span><b> packet <\/b><span style=\"font-weight: 400;\">\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u6e90\u5730\u5740\u7531\u5e93\u81ea\u52a8\u751f\u6210\u5e76\u5199\u5165Arduino EEPROM\u3002\u8981\u68c0\u67e5\u6240\u6709\u5185\u5bb9\u662f\u5426\u5df2\u6b63\u786e\u4fdd\u5b58\uff0c\u6211\u4eec\u4f1a\u8bfb\u53d6\u6570\u636e\u5305\u4fe1\u606f\u5e76\u5c06\u5176\u6253\u5370\u5230\u4e32\u884c\u7aef\u53e3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ea\u9700\u8c03\u7528<\/span><b> tx() <\/b><span style=\"font-weight: 400;\">\u51fd\u6570\u5373\u53ef\u3002\u4e00\u4f1a\u513f\u4e4b\u540e\u2026\u2026\u5b8c\u6210\uff01\u53ea\u9700\u4e00\u4e2a\u547d\u4ee4\uff0c\u6211\u4eec\u7684\u6570\u636e\u5305\u5c31\u4f20\u9001\u6210\u529f\u4e86\uff01<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\/\/ include the library\r\n#include &lt;LoRaLib.h&gt;\r\n\r\n\/\/ create instance of LoRa class with default settings\r\nLoRa lora;\r\n\r\n\/\/ create instance of packet class\r\n\/\/ destination: \"20:05:55:FE:E1:92:8B:95\"\r\n\/\/ data:        \"Hello World !\"\r\npacket pack(\"20:05:55:FE:E1:92:8B:95\", \"Hello World!\");\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  \/\/ initialize the LoRa module with default settings\r\n  lora.init();\r\n\r\n  \/\/ create a string to store the packet information\r\n  char str[24];\r\n\r\n  \/\/ print the source of the packet\r\n  pack.getSourceStr(str);\r\n  Serial.println(str);\r\n\r\n  \/\/ print the destination of the packet\r\n  pack.getDestinationStr(str);\r\n  Serial.println(str);\r\n\r\n  \/\/ print the length of the packet\r\n  Serial.println(pack.length);\r\n  \r\n  \/\/ print the data of the packet\r\n  Serial.println(pack.data);\r\n}\r\n\r\nvoid loop() {\r\n  Serial.print(\"Sending packet \");\r\n\r\n  \/\/ start transmitting the packet\r\n  uint8_t state = lora.tx(pack);\r\n  \r\n  if(state == 0) {\r\n    \/\/ if the function returned 0, a packet was successfully transmitted\r\n    Serial.println(\" success!\");\r\n    \r\n  } else if(state == 1) {\r\n    \/\/ if the function returned 1, the packet was longer than 256 bytes\r\n    Serial.println(\" too long!\");\r\n    \r\n  }\r\n\r\n  \/\/ wait a second before transmitting again\r\n  delay(1000);\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u5f53\u7136\uff0c\u6211\u4eec\u9700\u8981\u7b2c\u4e8c\u5957\u914d\u6709LoRenz \u5f00\u53d1\u677f\u7684Arduino\u6765\u63a5\u6536\u8be5\u6570\u636e\u5305\u3002 \u7cfb\u7edf\u8bbe\u7f6e\u4e0d\u53d8\uff0c\u53ea\u662f\u8fd9\u6b21\u6211\u4eec\u8c03\u7528<\/span><b> rx() <\/b><span style=\"font-weight: 400;\">\u51fd\u6570\uff0c\u7136\u540e\u6253\u5370\u63a5\u6536\u5230\u7684\u6570\u636e\u5305\u3002\u6b64\u51fd\u6570\u5c06\u7b49\u5f85\u6570\u636e\u5305\uff0c\u5982\u679c\u6570\u636e\u6ca1\u6709\u5728\u67d0\u4e2a\u65f6\u95f4\u5185\u5230\u8fbe\uff0c\u8be5\u51fd\u6570\u5c06\u8d85\u65f6\uff0c\u4ee5\u4fbf\u60a8\u7684\u4ee3\u7801\u4e0d\u4f1a\u5b8c\u5168\u6302\u8d77\u3002\u8be5\u5e93\u751a\u81f3\u8fd8\u4f1a\u68c0\u67e5\u4f20\u8f93\u7684\u6570\u636e\u5305\u662f\u5426\u5df2\u635f\u574f\uff0c\u5982\u679c\u662f\uff0c\u5219\u5c06\u5176\u4e22\u5f03\u3002<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\/\/ include the library\r\n#include &lt;LoRaLib.h&gt;\r\n\r\n\/\/ create instances of LoRa and packet classes with default settings\r\nLoRa lora;\r\npacket pack;\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  \/\/ initialize the LoRa module with default settings\r\n  lora.init();\r\n}\r\n\r\nvoid loop() {\r\n  Serial.print(\"Waiting for incoming transmission ... \");\r\n  \r\n  \/\/ start receiving single packet\r\n  uint8_t state = lora.rx(pack);\r\n\r\n  if(state == 0) {\r\n    \/\/ if the function returned 0, a packet was successfully received\r\n    Serial.println(\"success!\");\r\n\r\n    \/\/ create a string to store the packet information\r\n    char str[24];\r\n\r\n    \/\/ print the source of the packet\r\n    pack.getSourceStr(str);\r\n    Serial.println(str);\r\n\r\n    \/\/ print the destination of the packet\r\n    pack.getDestinationStr(str);\r\n    Serial.println(str);\r\n\r\n    \/\/ print the length of the packet\r\n    Serial.println(pack.length);\r\n\r\n    \/\/ print the data of the packet\r\n    Serial.println(pack.data);\r\n    \r\n  } else if(state == 1) {\r\n    \/\/ if the function returned 1, no packet was received before timeout\r\n    Serial.println(\"timeout!\");\r\n    \r\n  } else if(state == 2) {\r\n    \/\/ if the function returned 2, a packet was received, but is malformed\r\n    Serial.println(\"CRC error!\");\r\n    \r\n  }\r\n  \r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u5f53\u7136\uff0c\u8fd9\u53ea\u662f\u6700\u57fa\u672c\u7684\u4f8b\u5b50\u3002\u5e93\u672c\u8eab\u53ef\u4ee5\u505a\u66f4\u591a\u4e8b\u60c5\uff0c\u800c\u4e14\u6211\u8fd8\u5728\u7ee7\u7eed\u5f00\u53d1\u66f4\u591a\u7684\u529f\u80fd\u3002\u6709\u5173\u8be5\u5e93\u548c\u6240\u6709\u5176\u4ed6\u529f\u80fd\u7684\u66f4\u6df1\u5165\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u6211\u7684 <\/span><a href=\"https:\/\/github.com\/jgromes\/LoRaLib\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-weight: 400;\">GitHub<\/span><\/a><span style=\"font-weight: 400;\"> \u4ee5\u53ca\u90a3\u91cc\u6258\u7ba1\u7684\u6587\u6863\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><b>Arduino \u52a0\u5bc6<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">\u672c\u6587\u7ed3\u675f\u4e4b\u524d\uff0c\u6211\u8fd8\u60f3\u8ba8\u8bba\u4e00\u4e0bArduino\u7684\u52a0\u5bc6\u3002\u6211\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u63d0\u5230\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53d1\u9001\u7684\u6240\u6709\u6570\u636e\u90fd\u662f\u672a\u52a0\u5bc6\u7684\u3002\u8fd9\u610f\u5473\u7740\u62e5\u6709\u76f8\u540c\u914d\u7f6e\u3001\u4f7f\u7528\u76f8\u540c\u6a21\u5757\u548c\u76f8\u540c\u8bbe\u7f6e\u7684\u4efb\u4f55\u4eba\u90fd\u80fd\u62e6\u622a\u548c\u9605\u8bfb\u6211\u4eec\u7684\u6d88\u606f\u3002\u653b\u51fb\u8005\u751a\u81f3\u53ef\u4ee5\u53d1\u9001\u81ea\u5df1\u7684\u6d88\u606f\uff0c\u800c\u6211\u4eec\u5374\u65e0\u6cd5\u5206\u8fa8\u3002\u663e\u7136\uff0c\u8fd9\u5e76\u4e0d\u5b89\u5168\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u6700\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u4f7f\u7528\u67d0\u79cd\u52a0\u5bc6\u3002\u5177\u4f53\u5730\uff0c\u6211\u51b3\u5b9a\u4f7f\u7528 <\/span><i><span style=\"font-weight: 400;\">Rijndael \u5bc6\u7801<\/span><\/i><span style=\"font-weight: 400;\">\u3002\u6ca1\u542c\u8bf4\u8fc7\u5427\uff1f\u8fd9\u662f\u56e0\u4e3a\u8fd9\u4e2a\u540d\u5b57\u662f\u8377\u5170\u8bed\uff0c\u56e0\u6b64\u4e0d\u597d\u8bb0\u5fc6\u548c\u53d1\u97f3\u3002\u5bc6\u7801\u672c\u8eab\u5b9e\u9645\u4e0a\u975e\u5e38\u666e\u904d\uff0c\u4f46\u540d\u79f0\u66f4\u52a0\u5f15\u4eba\u6ce8\u76ee\uff1a<\/span><i><span style=\"font-weight: 400;\">AES<\/span><\/i><span style=\"font-weight: 400;\">\u3002\u5b83\u662f\u4e00\u79cd\u5bf9\u79f0\u5bc6\u7801\uff0c\u53ef\u5728\u52a0\u5bc6\u901f\u5ea6\u548c\u5b89\u5168\u6027\u4e4b\u95f4\u63d0\u4f9b\u51fa\u8272\u7684\u5e73\u8861\u3002\u6b64\u5916\uff0cArduino\u8fd8\u63d0\u4f9b\u4e86\u51e0\u4e2aAES\u5e93\uff01\u672c\u9879\u76ee\u4f7f\u7528\u7684\u5e93\u662fDavy Landman\u5f00\u53d1\u7684AESLib\uff08\u53ef\u4ece <\/span><a href=\"https:\/\/github.com\/DavyLandman\/AESLib\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"font-weight: 400;\">GitHub<\/span><\/a><span style=\"font-weight: 400;\"> \u4e0a\u83b7\u5f97\uff09\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u5982\u4e0a\u6240\u8ff0\uff0cAES\u662f\u4e00\u79cd\u5bf9\u79f0\u5bc6\u7801 &#8211; \u8fd9\u610f\u5473\u7740\u5b83\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\u6765\u52a0\u5bc6\u548c\u89e3\u5bc6\u6d88\u606f\u3002\u73b0\u5728\uff0c\u6211\u4eec\u53ea\u6709\u4e24\u4e2a\u8bbe\u5907\uff0c\u56e0\u6b64\u5c06\u5bc6\u94a5\u786c\u7f16\u7801\u5230Arduino\u4e2d\u975e\u5e38\u5bb9\u6613\u3002\u5f53\u7136\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u52a8\u6001\u6dfb\u52a0\u66f4\u591a\u8bbe\u5907\u5e76\u521b\u5efa\u67d0\u79cd\u65e0\u7ebf\u7f51\u7edc\uff0c\u6211\u4eec\u5fc5\u987b\u4ee5\u67d0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5b89\u5168\u5bc6\u94a5\u4ea4\u6362\uff0c\u4f8b\u5982\u4f7f\u7528Diffie-Hellman\u4ea4\u6362\u3002\u4f46\u662f\u6211\u4eec\u73b0\u5728\u4e0d\u4f1a\u6df1\u5165\u8fd9\u4e2a\u9886\u57df\uff0c\u6211\u4eec\u53ea\u9700\u5c06\u5bc6\u94a5\u786c\u7f16\u7801\u5230\u6211\u4eec\u7684Arduino\u7a0b\u5e8f\u4e2d\u5373\u53ef\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u4fee\u6539\u4e0a\u4e00\u7ae0\u7684\u4ee3\u7801\u5462\uff1f\u4fee\u6539\u5e76\u4e0d\u591a\uff0c\u8bf4\u5b9e\u8bdd\uff0c\u6211\u4eec\u53ea\u9700\u6dfb\u52a0\u5bc6\u94a5\u4ee5\u53ca\u4e00\u4e2a\u52a0\u5bc6\u6216\u89e3\u5bc6\u6570\u636e\u5305\u4e2d\u7684\u6570\u636e\u3002\u8fd9\u662f\u53d1\u5c04\u673a\u90e8\u5206\uff0c\u52a0\u5bc6\u901a\u8fc7 <\/span><b>aes128_enc_single()<\/b><span style=\"font-weight: 400;\"> \u51fd\u6570\u5b8c\u6210\u3002<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\/\/ include the libraries\r\n#include &lt;LoRaLib.h&gt;\r\n#include &lt;AESLib.h&gt;\r\n\r\n\/\/ create instance of LoRa class with default settings\r\nLoRa lora;\r\n\r\n\/\/ create instance of packet class\r\n\/\/ destination: \"20:05:55:FE:E1:92:8B:95\"\r\n\/\/ data:        \"Hello World !\"\r\npacket pack(\"20:05:55:FE:E1:92:8B:95\", \"Hello World!   \");\r\n\r\n\/\/ our secret 16-byte long key\r\nuint8_t key[] = {0x2C, 0x66, 0x54, 0x94, 0xE3, 0xAE, 0xC7, 0x32,\r\n                 0xC4, 0x66, 0xC8, 0xBE, 0xF3, 0x71, 0x22, 0x36};\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  \/\/ initialize the LoRa module with default settings\r\n  lora.init();\r\n\r\n  \/\/ create strings to store the packet information\r\n  char src[24];\r\n  char dest[24];\r\n  \r\n  \/\/ print the source of the packet\r\n  pack.getSourceStr(src);\r\n  Serial.print(\"Source:\\t\\t\\t\");\r\n  Serial.println(src);\r\n\r\n  \/\/ print the destination of the packet\r\n  pack.getDestinationStr(dest);\r\n  Serial.print(\"Destination:\\t\\t\");\r\n  Serial.println(dest);\r\n\r\n  \/\/ print the length of the packet\r\n  Serial.print(\"Total # of bytes:\\t\");\r\n  Serial.println(pack.length);\r\n\r\n  \/\/ print the contents of unencrypted packet\r\n  Serial.println(\"-------- Plain text ---------\");\r\n  Serial.println(pack.data);\r\n\r\n  \/\/ encrypt the data\r\n  aes128_enc_single(key, pack.data);\r\n\r\n  \/\/ print the contents of encrypted packet\r\n  Serial.println(\"--- Encrypted with AES128 ---\");\r\n  Serial.println(pack.data);\r\n}\r\n\r\nvoid loop() {\r\n  Serial.print(\"Sending packet \");\r\n\r\n  \/\/ start transmitting the packet\r\n  uint8_t state = lora.tx(pack);\r\n  \r\n  if(state == 0) {\r\n    \/\/ if the function returned 0, a packet was successfully transmitted\r\n    Serial.println(\" success!\");\r\n    \r\n  } else if(state == 1) {\r\n    \/\/ if the function returned 1, the packet was longer than 256 bytes\r\n    Serial.println(\" too long!\");\r\n    \r\n  }\r\n\r\n  \/\/ wait a second before transmitting again\r\n  delay(1000);\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u63a5\u6536\u673a\u90e8\u5206\u5982\u4e0b\u6240\u793a\uff0c\u89e3\u5bc6\u901a\u8fc7\u76f8\u540c\u5bc6\u94a5\u548c\u51fd\u6570 <\/span><b>aes128_dec_single()<\/b><span style=\"font-weight: 400;\"> \u5b8c\u6210\u3002<\/span><\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-c\" data-lang=\"C\"><code>\/\/ include the libraries\r\n#include &lt;LoRaLib.h&gt;\r\n#include &lt;AESLib.h&gt;\r\n\r\n\/\/ create instances of LoRa and packet classes with default settings\r\nLoRa lora;\r\npacket pack;\r\n\r\n\/\/ our secret 16-byte long key\r\nuint8_t key[] = {0x2C, 0x66, 0x54, 0x94, 0xE3, 0xAE, 0xC7, 0x32,\r\n                 0xC4, 0x66, 0xC8, 0xBE, 0xF3, 0x71, 0x22, 0x36};\r\n\r\nvoid setup() {\r\n  Serial.begin(9600);\r\n\r\n  \/\/ initialize the LoRa module with default settings\r\n  lora.init();\r\n}\r\n\r\nvoid loop() {\r\n  Serial.print(\"Waiting for incoming transmission ... \");\r\n  \r\n  \/\/ start receiving single packet\r\n  uint8_t state = lora.rx(pack);\r\n\r\n  if(state == 0) {\r\n    \/\/ if the function returned 0, a packet was successfully received\r\n    Serial.println(\"success!\");\r\n\r\n    \/\/ create strings to store the packet information\r\n    char src[24];\r\n    char dest[24];\r\n    \r\n    \/\/ print the source of the packet\r\n    pack.getSourceStr(src);\r\n    Serial.print(\"Source:\\t\\t\\t\");\r\n    Serial.println(src);\r\n  \r\n    \/\/ print the destination of the packet\r\n    pack.getDestinationStr(dest);\r\n    Serial.print(\"Destination:\\t\\t\");\r\n    Serial.println(dest);\r\n  \r\n    \/\/ print the length of the packet\r\n    Serial.print(\"Total # of bytes:\\t\");\r\n    Serial.println(pack.length);\r\n\r\n    \/\/ print the contents of encrypted packet\r\n    Serial.print(\"Encrypted (AES128):\\t\");\r\n    Serial.println(pack.data);\r\n  \r\n    \/\/ decrypt the data\r\n    aes128_dec_single(key, pack.data);\r\n  \r\n    \/\/ print the contents of unencrypted packet\r\n    Serial.print(\"Plain text:\\t\\t\");\r\n    Serial.println(pack.data);\r\n    \r\n  } else if(state == 1) {\r\n    \/\/ if the function returned 1, no packet was received before timeout\r\n    Serial.println(\"timeout!\");\r\n    \r\n  } else if(state == 2) {\r\n    \/\/ if the function returned 2, a packet was received, but is malformed\r\n    Serial.println(\"CRC error!\");\r\n    \r\n  }\r\n  \r\n}\r\n<\/code><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">\u4f7f\u7528\u4e86\u5bc6\u94a5\u4e4b\u540e\uff0c\u6211\u4eec\u7684\u6d88\u606f\u73b0\u5728\u662f\u5b89\u5168\u7684\u3002\u5982\u679c\u6709\u4eba\u5077\u542c\u6211\u4eec\u7684\u8c08\u8bdd\uff0c\u4ed6\u65e0\u6cd5\u770b\u5230\u9664\u5730\u5740\u4e4b\u5916\u7684\u4efb\u4f55\u5185\u5bb9\uff0c\u6bcf\u4e2a\u6570\u636e\u5305\u4e2d\u90fd\u662f240\u5b57\u8282\u7684\u4e71\u7801\u3002\u540c\u6837\uff0c\u5982\u679c\u653b\u51fb\u8005\u8bd5\u56fe\u4f20\u8f93\u4ed6\u81ea\u5df1\u7684\u6d88\u606f\uff0c\u6211\u4eec\u4f1a\u7acb\u5373\u77e5\u9053\uff0c\u56e0\u4e3a\u4ed6\u4f20\u8f93\u7684\u6d88\u606f\u4e0d\u4f1a\u52a0\u5bc6\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u5728Arduino\u4e0a\u7528AES\u52a0\u5bc6\u975e\u5e38\u7b80\u5355\uff0c\u6240\u4ee5\u6211\u63a8\u8350\u4f7f\u7528\u8be5\u52a0\u5bc6\u65b9\u6cd5\u3002\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u7f16\u7a0b\u5b9e\u8df5\u3002\u60a8\u6c38\u8fdc\u4e0d\u77e5\u9053\u8c01\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u53ef\u80fd\u4f1a\u5077\u542c\u60a8\u770b\u4f3c\u65e0\u8f9c\u7684\u5bf9\u8bdd\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><b>\u7ed3\u8bba<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">\u73b0\u5728\uff0cArduino\u8fdc\u7a0b\u65e0\u7ebf\u901a\u4fe1\u7684\u77ed\u6682\u65c5\u9014\u5c31\u8981\u7ed3\u675f\u4e86\u3002\u5982\u679c\u60a8\u5f00\u53d1\u81ea\u5df1\u7684LoRenz\u5f00\u53d1\u677f\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u4e00\u4e9b\u5f88\u9177\u7684Arduino\u9879\u76ee\uff0c\u8bf7\u544a\u8bc9\u6211\uff01\u5982\u679c\u60a8\u6709\u6539\u8fdbLoRenz\u5f00\u53d1\u677f\u548cLoRaLib\u5e93\u7684\u60f3\u6cd5\uff0c\u8bf7\u5728GitHub\u4e0a\u4e0e\u6211\u5206\u4eab\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u6211\u5bf9\u5f00\u53d1\u677f\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u5f53\u5e26\u5bbd\u4e3a500kHz\u3001\u6269\u5c55\u56e0\u5b50\u4e3a12\u3001\u7f16\u7801\u7387\u4e3a4\/8\u65f6\uff0c\u5728\u65e0\u969c\u788d\u73af\u5883\u4e2d\u6211\u80fd\u591f\u5b9e\u73b0\u8d85\u8fc7500\u7c73\u7684\u53ef\u9760\u4f20\u8f93\uff1b\u5728\u8302\u5bc6\u7684\u68ee\u6797\u4e2d\u4f20\u8f93\u8ddd\u79bb\u5219\u8d85\u8fc7200\u7c73\u3002\u6240\u6709\u8fd9\u4e00\u5207\u90fd\u53ea\u662f\u901a\u8fc7\u4e00\u683910cm\u5929\u7ebf\u5b9e\u73b0\u7684\uff0c\u800c\u4e14\u53d1\u5c04\u5668\u7535\u6e90\u4e5f\u53ea\u662f\u5ec9\u4ef7\u76849V\u7535\u6c60\u800c\u5df2\uff08\u63a5\u6536\u5668\u4eceUSB\u7aef\u53e3\u63a5\u7535\uff0c\u6700\u7ec8\u901a\u8fc7Arduino\u677f\u8f7d\u7a33\u538b\u5668\u4f9b\u7535\uff09\u3002\u8fd9\u4e2a\u8ddd\u79bb\u8fd8\u53ef\u4ee5\u66f4\u957f\uff08\u901a\u8fc7\u964d\u4f4e\u5e26\u5bbd\uff09\uff0c\u4f46\u662f\u8fd9\u4f1a\u5bfc\u81f4\u4f20\u8f93\u901f\u5ea6\u663e\u8457\u964d\u4f4e\uff0c\u5728\u4e0a\u8ff0\u8bbe\u7f6e\u4e0d\u53d8\u7684\u60c5\u51b5\u4e0b\u4f20\u8f93\u901f\u5ea6\u5927\u7ea6\u4e3a1 kbps\u3002<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u7136\u800c\uff0c\u5bf9\u4e8e\u6211\u5c06\u6765\u7684\u9879\u76ee\uff0c\u8fd9\u4e9b\u8ddd\u79bb\u7ef0\u7ef0\u6709\u4f59\u3002\u8bf7\u5728\u793e\u4ea4\u5a92\u4f53\u4e0a\u5173\u6ce8DevicePlus\uff0c\u8fd9\u6837\u60a8\u5c31\u4e0d\u4f1a\u9519\u8fc7\u4efb\u4f55\u6709\u8da3\u7684\u5185\u5bb9\uff01<\/span><\/p>\n<p>\n&nbsp;<br \/>\n<a href=\"https:\/\/techclass.wfjydl.com\/knowledge\/deviceplus\/how-tos\/arduino\/arduino-long-range-communication-tutorial-lorenz-shield\" style=\"font-size: 20px\"><strong>\u70b9\u51fb\u8fd9\u91cc\u9605\u8bfb LoRaLib \u5f00\u53d1\u677f &gt;<\/strong><\/a><\/p>\n<div class=\"author-box-wrapper\">\n<div class=\"avatar-wrapper\">\n\t\t<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/micro.wfjydl.com\/deviceplus\/wp-content\/uploads\/2019\/03\/Jan-Gromes_avatar_1472220138-64x64.jpg\" width=\"64\" height=\"64\" alt=\"DevicePlus \u7f16\u8f91\u56e2\u961f\">\n\t<\/div>\n<div class=\"desc-wrapper\">\n<h5>Jan Gromes<\/h5>\n<div class=\"desc\">\n<p>Jan\u76ee\u524d\u5728\u5e03\u5c14\u8bfa\u7406\u5de5\u5927\u5b66\u5b66\u4e60\u7535\u6c14\u5de5\u7a0b\u3002\u4ed6\u62e5\u6709\u591a\u5e74\u4f7f\u7528Arduino\u548c\u5176\u4ed6\u5fae\u63a7\u5236\u5668\u6784\u5efa\u9879\u76ee\u7684\u7ecf\u9a8c\u3002\u4ed6\u7684\u7279\u6b8a\u5174\u8da3\u5728\u4e8e\u673a\u5668\u4eba\u7cfb\u7edf\u7684\u673a\u68b0\u8bbe\u8ba1\u3002<\/p>\n<\/div><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"\u8fd9\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8eDevicePlus.com\u82f1\u8bed\u7f51\u7ad9\u7684\u7ffb\u8bd1\u7a3f\u3002 \u70b9\u51fb\u8fd9\u91cc\u9605\u8bfb LoRaLib \u5f00\u53d1\u677f &gt; \u4e3a\u4e86\u63a7\u5236 Arduino\u957f\u8ddd\u79bb\u901a\u4fe1\u6559\u7a0b\u2013LoRenz \u5f00\u53d1\u677f\u4e2d\u6784\u5efa\u7684LoRenz\u5f00\u53d1\u677f\uff0c\u6211\u5f00\u53d1\u4e86LoRaLib [&hellip;]","protected":false},"author":3,"featured_media":16774,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1130],"tags":[],"class_list":["post-6932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino"],"acf":[],"wps_subtitle":"","_links":{"self":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/6932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/comments?post=6932"}],"version-history":[{"count":7,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/6932\/revisions"}],"predecessor-version":[{"id":11508,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/posts\/6932\/revisions\/11508"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/media\/16774"}],"wp:attachment":[{"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/media?parent=6932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/categories?post=6932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techclass.wfjydl.com\/wp-json\/wp\/v2\/tags?post=6932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}