-
Notifications
You must be signed in to change notification settings - Fork 0
/
BastWAN_I2C_LoRa_Bridge.ino
133 lines (127 loc) · 3.56 KB
/
BastWAN_I2C_LoRa_Bridge.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <SPI.h>
#include <LoRa.h>
#include <LoRandom.h>
#include "aes.c"
#include "helper.h"
#include "Commands.h"
#include <Wire.h>
#define RFM_TCXO (40u)
void setup() {
time_t timeout = millis();
while (!Serial) {
// on nRF52840, Serial is not available right away.
// make the MCU wait a little
if ((millis() - timeout) < 5000) {
delay(100);
} else {
break;
}
}
uint8_t x = 5;
while (x > 0) {
Serial.print(" ");
Serial.print(x--);
Serial.print(" ");
delay(500);
} // Just for show
Serial.println("0!");
SerialUSB.println("SerialUSB started.");
Wire.begin(0x18);
delay(1000);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
SerialUSB.println("Wire started.");
SerialUSB.println("\n\nLoRa Bridge by BastWAN, RAKwireless and Kongduino");
Serial.println("------------------------------------------------------");
pinMode(RFM_TCXO, OUTPUT);
pinMode(RFM_SWITCH, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
//pinMode(PIN_PA28, OUTPUT);
//digitalWrite(RFM_TCXO, LOW);
//digitalWrite(PIN_PA28, HIGH);
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
if (!LoRa.begin(868.125E6)) {
SerialUSB.println("Starting LoRa failed!\nNow that's disappointing...");
while (1);
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125e3);
LoRa.setCodingRate4(5);
LoRa.setPreambleLength(8);
LoRa.setTxPower(20, PA_OUTPUT_PA_BOOST_PIN);
digitalWrite(RFM_SWITCH, HIGH);
LoRa.writeRegister(REG_LNA, 0x23); // TURN ON LNA FOR RECEIVE
if (!needEncryption) memset(SecretKey, 0, 32);
digitalWrite(RFM_SWITCH, LOW);
digitalWrite(RFM_TCXO, HIGH);
LoRa.receive();
cmdCount = sizeof(cmds) / sizeof(myCommand);
handleHelp("");
}
void loop() {
int incoming = LoRa.parsePacket();
if (incoming) {
SerialUSB.print("Received packet:\n");
memset(currentPacket, 0, 256);
packetSize = 0;
while (LoRa.available()) {
char c = (char)LoRa.read();
delay(10);
currentPacket[packetSize++] = c;
}
if (needEncryption) {
Serial.println("Decrypting [" + String(packetSize) + "]...");
hexDump((uint8_t *)currentPacket, packetSize);
SerialUSB.println("With key:");
hexDump((uint8_t *)SecretKey, 32);
decryptECB((uint8_t*)currentPacket, packetSize);
hexDump((uint8_t *)encBuf, packetSize / 2);
} else {
SerialUSB.print(currentPacket);
SerialUSB.print("RSSI: ");
SerialUSB.println(LoRa.packetRssi());
}
// digitalWrite(RFM_SWITCH, LOW);
// digitalWrite(RFM_TCXO, HIGH);
// LoRa.receive();
}
if (SerialUSB.available()) {
memset(incomingBuff, 0, 256);
int ix = 0;
while (SerialUSB.available()) {
char c = SerialUSB.read();
delay(10);
incomingBuff[ix++] = c;
}
SerialUSB.println("Incoming from USB:");
SerialUSB.println(incomingBuff);
handleCommands(incomingBuff);
}
}
void receiveEvent(int bytes) {
if (!Wire.available()) return;
uint16_t ix = 0;
memset(incomingBuff, 0, 256);
while (ix < bytes) incomingBuff[ix++] = Wire.read();
SerialUSB.println("Incoming from Wire:");
SerialUSB.println(incomingBuff);
handleCommands(incomingBuff);
if (strcmp(incomingBuff, "msg?") == 0) {
// Do we have a packet available?
msgLen = packetSize;
memcpy(msg, currentPacket, msgLen);
deletePacket = true;
return;
}
}
void requestEvent() {
Serial.print("Request from Master. Sending:\n");
hexDump((uint8_t *)msg, msgLen);
Wire.write(msg, msgLen);
msgLen = 0;
if (deletePacket) {
memset(currentPacket, 0, 256);
packetSize = 0;
}
memset(msg, 0, 256);
}