-
Notifications
You must be signed in to change notification settings - Fork 3
/
STM32-LoRaGateway.ino
296 lines (246 loc) · 6.36 KB
/
STM32-LoRaGateway.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
A STM32F103C8T6 (Blue Pill) LoRa Gateway, but not a gateway like for the
The Things Network.
Please read the README.md for details
24 apr 2020: Arjan Wooning
*/
#include <SPI.h>
#include <LoRa.h>
#include <EEPROM.h>
#define SERIAL_BAUD 57600
// LoRa configuration
const long frequency = 866E6; // LoRa Frequency
const int csPin = PA4; // LoRa radio chip select
const int resetPin = PC14; // LoRa radio reset
const int irqPin = PA1; // change for your board; must be a hardware interrupt pin
// Gateway configuration
typedef struct {
byte nodeId; // Which Gateway NodeId 1...7
byte hex_output :2; // 0 = dec, 1 = hex, 2 = hex+ascii
byte rssi_output :1; // 0 = dec, 1 = hex, 2 = hex+ascii
byte spare_flags :5;
} GPIOconfig;
static GPIOconfig config;
#define MAX_PACKET_SIZE 10
static word value;
static byte stack[MAX_PACKET_SIZE + 4], top, sendLen, destNodeId;
static boolean sendMsg = false;
//Message max 31 bytes
typedef struct {
byte nodeId;
byte msg [MAX_PACKET_SIZE - 1];
} Payload;
void LoRa_rxMode(){
LoRa.disableInvertIQ(); // normal mode
LoRa.receive(); // set receive mode
}
void LoRa_txMode(){
LoRa.idle(); // set standby mode
LoRa.enableInvertIQ(); // active invert I and Q signals
}
void LoRa_sendMessage(Payload payload, byte payloadLen) {
LoRa_txMode(); // set tx mode
LoRa.beginPacket(); // start packet
LoRa.write((byte*) &payload, payloadLen); // add payload
LoRa.endPacket(true); // finish packet and send it
}
void onTxDone() {
// Serial.println("TxDone");
LoRa_rxMode();
}
static void printOneChar (char c) {
Serial.print(c);
}
static void showString (PGM_P s) {
for (;;) {
char c = pgm_read_byte(s++);
if (c == 0)
break;
if (c == '\n')
printOneChar('\r');
printOneChar(c);
}
}
static void saveConfig () {
EEPROM.write(0, (int)config.nodeId);
}
static void showNibble (byte nibble) {
char c = '0' + (nibble & 0x0F);
if (c > '9')
c += 7;
Serial.print(c);
}
static void showByte (byte value) {
if (config.hex_output) {
showNibble(value >> 4);
showNibble(value);
} else
Serial.print((word) value);
}
static void loadConfig () {
config.nodeId = EEPROM.read(0);
if ( config.nodeId > 7) {
Serial.println("Init EEPROM for first time");
config.nodeId = 1;
saveConfig();
}
}
const char helpText[] PROGMEM =
"\n"
"Available commands:\n"
" <nn>n - set Gateway node ID (standard node ids are 1..7)\n"
" ...,<nn>s - send data packet to node <nn>\n"
" <n>x - set reporting format (0: decimal, 1: hex, 2: hex+ascii)\n"
" <n>r - set reporting of RSSI signal strength (0:off, 1:on)\n"
" v - display board name and board id\n"
" h - this help\n"
;
static void showHelp () {
showString(helpText);
Serial.println();
Serial.print("nodeId=");
Serial.println(config.nodeId, DEC);
}
//Cmnd example: 10,3a
static void handleSerialInput (char c) {
byte stm32pinNr = 0;
if ('0' <= c && c <= '9') {
value = 10 * value + c - '0';
return;
}
if (c == ',') {
if (top < sizeof stack)
stack[top++] = value; // truncated to 8 bits
value = 0;
return;
}
if ('a' <= c && c <= 'z') {
Serial.print("> ");
for (byte i = 0; i < top; ++i) {
Serial.print((word) stack[i]);
printOneChar(',');
}
Serial.print(value);
Serial.println(c);
}
if (c > ' ') {
switch (c) {
case 's': // send packet to node ID N, no ack
sendMsg = true; // Send the message
sendLen = top; // Payload length
destNodeId = value; // Destination NodeId
break;
case 'n': // set node id
config.nodeId = value & 0x3F;
saveConfig();
break;
case 'x': // set reporting mode to decimal (0), hex (1), hex+ascii (2)
config.hex_output = value;
saveConfig();
break;
case 'r': // set reporting of RSSI signal strength
config.rssi_output = value;
saveConfig();
break;
case 'v': //display the interpreter version and configuration
Serial.print("[LORA-GATEWAY]");
break;
case 'h':
showHelp();
break;
// default:
// Serial.print("Unknown input: ");
// Serial.print(c);
// Serial.print(" (0x");
// Serial.print(c, HEX);
// Serial.println(")");
// showHelp();
}
}
value = top = 0;
}
boolean runEvery(unsigned long interval)
{
static unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
return true;
}
return false;
}
void setup() {
Serial.begin(SERIAL_BAUD); // initialize serial
while (!Serial);
Serial.println();
loadConfig();
Serial.println("[LORA-GATEWAY]");
LoRa.setPins(csPin, resetPin, irqPin);
if (!LoRa.begin(frequency)) {
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
// Serial.println("LoRa init succeeded.");
// Serial.println();
// Serial.println("Only receive messages from nodes");
// Serial.println("Tx: invertIQ enable");
// Serial.println("Rx: invertIQ disable");
// Serial.println();
//LoRa.setTxPower(20);
LoRa.enableCrc();
LoRa.onReceive(onReceive);
LoRa.onTxDone(onTxDone);
LoRa_rxMode();
}
void onReceive(int packetSize) {
if (packetSize > MAX_PACKET_SIZE)
Serial.print("ERROR packetSize too long: "); // Start received message
else
Serial.print("OK"); // Start received message
for (int i = 0; i < packetSize; i++) {
if (!config.hex_output)
printOneChar(' ');
showByte((char)LoRa.read());
}
if (config.rssi_output) {
Serial.print(" RSSI ");
Serial.print(LoRa.packetRssi());
}
Serial.println();
// String message = "";
//
// while (LoRa.available()) {
// message += (char)LoRa.read();
// }
//
// Serial.print("Gateway Receive: ");
// Serial.println(message);
}
void loop() {
if (Serial.available()) {
handleSerialInput(Serial.read());
} else {
if (sendMsg) {
sendMsg = false;
// Check received package
Payload txPayload;
// Serial.print("Send to NodeID: ");
// Serial.print(destNodeId, DEC);
// Serial.print(" - msgLen:");
// Serial.print(sendLen, DEC);
//
// Serial.print(" - data:");
txPayload.nodeId = destNodeId;
for (char i = 0; i < sendLen; i++) {
// Serial.print(' ');
// Serial.print(stack[i], DEC);
txPayload.msg[i] = stack[i];
}
// Serial.println();
LoRa_sendMessage(txPayload, sendLen + 1); // send message
//Serial.println("Send Message!");
}
delay(10); //10ms
}
}