This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_lora_comms_test.ino
100 lines (86 loc) · 2.4 KB
/
node_lora_comms_test.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <Sodaq_RN2483.h>
#define debugSerial SerialUSB
#define loraSerial Serial2
// USE YOUR OWN KEYS!
const uint8_t devAddr[4] =
{
0x00, 0x00, 0x00, 0x00
};
// USE YOUR OWN KEYS!
const uint8_t appSKey[16] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// USE YOUR OWN KEYS!
const uint8_t nwkSKey[16] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 6 bytes of our random data + 6 bytes of their random data
const size_t payload_size = 12;
uint8_t recv_payload[payload_size];
void setup()
{
while ((!debugSerial) && (millis() < 10000));
debugSerial.begin(57600);
loraSerial.begin(LoRaBee.getDefaultBaudRate());
LoRaBee.setDiag(debugSerial); // optional
if (LoRaBee.initABP(loraSerial, devAddr, appSKey, nwkSKey, false))
{
debugSerial.println("Connection to the network was successful.");
}
else
{
debugSerial.println("Connection to the network failed!");
}
randomSeed(analogRead(0));
}
#include <stdarg.h>
void p(char *fmt, ... ){
char buf[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(buf, 128, fmt, args);
va_end (args);
debugSerial.print(buf);
}
void loop()
{
uint8_t send_payload[payload_size];
// Fill our random data
for (size_t i = 0; i < payload_size/2; i++)
{
send_payload[i] = random(256);
}
// Fill their random data. First time round this won't match what
// they're expecting but they should just ignore it and send their packet.
for (size_t i = payload_size/2; i < payload_size; i++)
{
send_payload[i] = recv_payload[i];
//p("%02x %02x ", i, recv_payload[i]);
}
//debugSerial.println("");
// Send packet
uint8_t status = LoRaBee.send(1, send_payload, sizeof(send_payload));
if (status != NoError)
{
p("Error sending payload: %d\n", status);
return;
}
debugSerial.println("Sent packet");
for (uint8_t i = 0; i < 60; i++)
{
// Receive packet
uint8_t n = LoRaBee.receive(recv_payload, sizeof(recv_payload));
//p("Received %d bytes\n", n);
// Check if we got back our data
if ((n == sizeof(recv_payload)) &&
(memcmp(recv_payload, send_payload, payload_size/2) == 0))
{
debugSerial.println("Received matching packet");
break;
}
//debugSerial.println("No matching bytes received");
delay(1000);
}
}