软件
- Arduino IDE
- LoRaLib Arduino 库 (可在 GitHub 上获得)
LoRaLib 库
SX1278有多种不同设置,允许用户完全自定义范围、数据速率和功耗,但是最重要的三个设置如下所示:
- 带宽 SX1278允许的带宽设置为7.8 kHz至500 kHz。带宽值越高,数据传输越快。然而,这是以降低总灵敏度为代价的,因此降低了最大范围。
- 扩频因子 在LoRa调制中,每个信息位由多个啁啾表示。扩频因子是指每位数据有多少啁啾。SX1278支持7种不同的设置,扩频因子越高,数据传输越慢,范围越大。
- 编码速率 为了提高传输的稳定性,SX1278可以执行错误检查功能。此错误检查的度量称为编码速率,可以设定四个值。编码速率设为最低的4/5时,传输不太稳定,速度稍快。编码速率设为最高的4/8时,链路更可靠,但代价是数据传输速率较慢。
库的默认设置为:带宽为500 kHz、编码速率为4/5和扩频因子为12。这些设置是范围、稳定性和数据速率之间的合理平衡。当然,这些设置可以通过函数随时更改。
该库内置数据包类和寻址系统。地址长度为8字节,那么最大的寻址数量就是1.8千亿亿(1.8 × 10^19)。这个数值大的离谱。相比之下,NASA估计我们银河系中的恒星数量仅为“4亿”(4×10^11)。每个数据包由源地址、目标地址和最多240字节的有效负载组成。当然,该库还提供了几种读取和写入分组数据的方法。
让我们来看一下使用这个库是多么容易。假设我们有两个带有SX1278模块的LoRenz开发板。它们相距几百米,所以我们可以使用默认设置。首先,我们必须包含库头文件。然后,我们用默认设置创建 LoRa 类的一个实例,用目标地址和消息创建 packet 类的一个实例。源地址由库自动生成并写入Arduino EEPROM。要检查所有内容是否已正确保存,我们会读取数据包信息并将其打印到串行端口。接下来,我们只需调用 tx() 函数即可。一会儿之后……完成!只需一个命令,我们的数据包就传送成功了!
- // include the library
- #include <LoRaLib.h>
- // create instance of LoRa class with default settings
- LoRa lora;
- // create instance of packet class
- // destination: "20:05:55:FE:E1:92:8B:95"
- // data: "Hello World !"
- packet pack("20:05:55:FE:E1:92:8B:95", "Hello World!");
- void setup() {
- Serial.begin(9600);
- // initialize the LoRa module with default settings
- lora.init();
- // create a string to store the packet information
- char str[24];
- // print the source of the packet
- pack.getSourceStr(str);
- Serial.println(str);
- // print the destination of the packet
- pack.getDestinationStr(str);
- Serial.println(str);
- // print the length of the packet
- Serial.println(pack.length);
- // print the data of the packet
- Serial.println(pack.data);
- }
- void loop() {
- Serial.print("Sending packet ");
- // start transmitting the packet
- uint8_t state = lora.tx(pack);
- if(state == 0) {
- // if the function returned 0, a packet was successfully transmitted
- Serial.println(" success!");
- } else if(state == 1) {
- // if the function returned 1, the packet was longer than 256 bytes
- Serial.println(" too long!");
- }
- // wait a second before transmitting again
- delay(1000);
- }
- // include the library
- #include <LoRaLib.h>
- // create instances of LoRa and packet classes with default settings
- LoRa lora;
- packet pack;
- void setup() {
- Serial.begin(9600);
- // initialize the LoRa module with default settings
- lora.init();
- }
- void loop() {
- Serial.print("Waiting for incoming transmission ... ");
- // start receiving single packet
- uint8_t state = lora.rx(pack);
- if(state == 0) {
- // if the function returned 0, a packet was successfully received
- Serial.println("success!");
- // create a string to store the packet information
- char str[24];
- // print the source of the packet
- pack.getSourceStr(str);
- Serial.println(str);
- // print the destination of the packet
- pack.getDestinationStr(str);
- Serial.println(str);
- // print the length of the packet
- Serial.println(pack.length);
- // print the data of the packet
- Serial.println(pack.data);
- } else if(state == 1) {
- // if the function returned 1, no packet was received before timeout
- Serial.println("timeout!");
- } else if(state == 2) {
- // if the function returned 2, a packet was received, but is malformed
- Serial.println("CRC error!");
- }
- }
Arduino 加密
本文结束之前,我还想讨论一下Arduino的加密。我在上一篇文章中提到了这个问题。现在,我们发送的所有数据都是未加密的。这意味着拥有相同配置、使用相同模块和相同设置的任何人都能拦截和阅读我们的消息。攻击者甚至可以发送自己的消息,而我们却无法分辨。显然,这并不安全。
最简单的解决方案就是使用某种加密。具体地,我决定使用 Rijndael 密码。没听说过吧?这是因为这个名字是荷兰语,因此不好记忆和发音。密码本身实际上非常普遍,但名称更加引人注目:AES。它是一种对称密码,可在加密速度和安全性之间提供出色的平衡。此外,Arduino还提供了几个AES库!本项目使用的库是Davy Landman开发的AESLib(可从 GitHub 上获得)。
如上所述,AES是一种对称密码 – 这意味着它使用相同的密钥来加密和解密消息。现在,我们只有两个设备,因此将密钥硬编码到Arduino中非常容易。当然,如果我们想要动态添加更多设备并创建某种无线网络,我们必须以某种方式实现安全密钥交换,例如使用Diffie-Hellman交换。但是我们现在不会深入这个领域,我们只需将密钥硬编码到我们的Arduino程序中即可。
那么我们应该如何修改上一章的代码呢?修改并不多,说实话,我们只需添加密钥以及一个加密或解密数据包中的数据。这是发射机部分,加密通过 aes128_enc_single() 函数完成。
- // include the libraries
- #include <LoRaLib.h>
- #include <AESLib.h>
- // create instance of LoRa class with default settings
- LoRa lora;
- // create instance of packet class
- // destination: "20:05:55:FE:E1:92:8B:95"
- // data: "Hello World !"
- packet pack("20:05:55:FE:E1:92:8B:95", "Hello World! ");
- // our secret 16-byte long key
- uint8_t key[] = {0x2C, 0x66, 0x54, 0x94, 0xE3, 0xAE, 0xC7, 0x32,
- 0xC4, 0x66, 0xC8, 0xBE, 0xF3, 0x71, 0x22, 0x36};
- void setup() {
- Serial.begin(9600);
- // initialize the LoRa module with default settings
- lora.init();
- // create strings to store the packet information
- char src[24];
- char dest[24];
- // print the source of the packet
- pack.getSourceStr(src);
- Serial.print("Source:\t\t\t");
- Serial.println(src);
- // print the destination of the packet
- pack.getDestinationStr(dest);
- Serial.print("Destination:\t\t");
- Serial.println(dest);
- // print the length of the packet
- Serial.print("Total # of bytes:\t");
- Serial.println(pack.length);
- // print the contents of unencrypted packet
- Serial.println("-------- Plain text ---------");
- Serial.println(pack.data);
- // encrypt the data
- aes128_enc_single(key, pack.data);
- // print the contents of encrypted packet
- Serial.println("--- Encrypted with AES128 ---");
- Serial.println(pack.data);
- }
- void loop() {
- Serial.print("Sending packet ");
- // start transmitting the packet
- uint8_t state = lora.tx(pack);
- if(state == 0) {
- // if the function returned 0, a packet was successfully transmitted
- Serial.println(" success!");
- } else if(state == 1) {
- // if the function returned 1, the packet was longer than 256 bytes
- Serial.println(" too long!");
- }
- // wait a second before transmitting again
- delay(1000);
- }
- // include the libraries
- #include <LoRaLib.h>
- #include <AESLib.h>
- // create instances of LoRa and packet classes with default settings
- LoRa lora;
- packet pack;
- // our secret 16-byte long key
- uint8_t key[] = {0x2C, 0x66, 0x54, 0x94, 0xE3, 0xAE, 0xC7, 0x32,
- 0xC4, 0x66, 0xC8, 0xBE, 0xF3, 0x71, 0x22, 0x36};
- void setup() {
- Serial.begin(9600);
- // initialize the LoRa module with default settings
- lora.init();
- }
- void loop() {
- Serial.print("Waiting for incoming transmission ... ");
- // start receiving single packet
- uint8_t state = lora.rx(pack);
- if(state == 0) {
- // if the function returned 0, a packet was successfully received
- Serial.println("success!");
- // create strings to store the packet information
- char src[24];
- char dest[24];
- // print the source of the packet
- pack.getSourceStr(src);
- Serial.print("Source:\t\t\t");
- Serial.println(src);
- // print the destination of the packet
- pack.getDestinationStr(dest);
- Serial.print("Destination:\t\t");
- Serial.println(dest);
- // print the length of the packet
- Serial.print("Total # of bytes:\t");
- Serial.println(pack.length);
- // print the contents of encrypted packet
- Serial.print("Encrypted (AES128):\t");
- Serial.println(pack.data);
- // decrypt the data
- aes128_dec_single(key, pack.data);
- // print the contents of unencrypted packet
- Serial.print("Plain text:\t\t");
- Serial.println(pack.data);
- } else if(state == 1) {
- // if the function returned 1, no packet was received before timeout
- Serial.println("timeout!");
- } else if(state == 2) {
- // if the function returned 2, a packet was received, but is malformed
- Serial.println("CRC error!");
- }
- }
来源:techclass.rohm