-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeeeduinoLoRaWan.h
467 lines (400 loc) · 15.3 KB
/
SeeeduinoLoRaWan.h
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
SeeeduinoLoRaWan.h
2013 Copyright (c) Seeed Technology Inc. All right reserved.
Author: Wayne Weng
Date: 2016-10-17
add rgb backlight fucnction @ 2013-10-15
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.1 USA
Modified for LoRa@VSB by Ondřej Knebl, 20. 12. 2023
*/
#ifndef _SEEEDUINOLORAWAN_H_
#define _SEEEDUINOLORAWAN_H_
#include <Arduino.h>
#define SerialLoRa Serial1
#define PRINT_TO_SERIAL_MONITOR
#define _DEBUG_SERIAL_ 1
#define DEFAULT_TIMEOUT 5 // second
#define DEFAULT_TIMEWAIT 100 // millisecond
#define DEFAULT_DEBUGTIME 100 // millisecond
#define BATTERY_POWER_PIN A4
#define CHARGE_STATUS_PIN A5
#define BEFFER_LENGTH_MAX 256
enum _class_type_t { CLASS_A = 0, CLASS_B, CLASS_C };
enum _physical_type_t { EU433 = 0, EU868 };
enum _device_mode_t { LWABP = 0, LWOTAA, TEST };
enum _otaa_join_cmd_t { JOIN = 0, FORCE };
enum _window_delay_t { RECEIVE_DELAY1 = 0, RECEIVE_DELAY2, JOIN_ACCEPT_DELAY1, JOIN_ACCEPT_DELAY2 };
enum _band_width_t { BW125 = 125, BW250 = 250, BW500 = 500 };
enum _spreading_factor_t { SF12 = 12, SF11 = 11, SF10 = 10, SF9 = 9, SF8 = 8, SF7 = 7 };
enum _data_rate_t { DR0 = 0, DR1, DR2, DR3, DR4, DR5, DR6, DR7 };
/*****************************************************************
Type DataRate Configuration BitRate| TxPower Configuration
EU433 0 SF12/125 kHz 250 | 0 10dBm
1 SF11/125 kHz 440 | 1 7 dBm
2 SF10/125 kHz 980 | 2 4 dBm
3 SF9 /125 kHz 1760 | 3 1 dBm
4 SF8 /125 kHz 3125 | 4 -2dBm
5 SF7 /125 kHz 5470 | 5 -5dBm
6 SF7 /250 kHz 11000 | 6:15 RFU
7 FSK:50 kbps 50000 |
8:15 RFU |
******************************************************************
Type DataRate Configuration BitRate| TxPower Configuration
EU868 0 SF12/125 kHz 250 | 0 20dBm
1 SF11/125 kHz 440 | 1 14dBm
2 SF10/125 kHz 980 | 2 11dBm
3 SF9 /125 kHz 1760 | 3 8 dBm
4 SF8 /125 kHz 3125 | 4 5 dBm
5 SF7 /125 kHz 5470 | 5 2 dBm
6 SF7 /250 kHz 11000 | 6:15 RFU
7 FSK:50 kbps 50000 |
8:15 RFU |
*******************************************************************/
class LoRaWanClass
{
public:
LoRaWanClass(void);
/**
* \brief Initialize the conmunication interface
*
* \return Return null
*/
void init(void);
/**
* \brief Set frequency plan Europe 433 MHz (ITU region 1)
*
* \return Return null.
*/
void setEU433(void);
/**
* \brief Set frequency plan Europe 863-870 MHz (SF9 for RX2 - recommended)
*
* \return Return null.
*/
void setEU868(void);
/**
* \brief Read the version from device
*
* \param [in] *buffer The output data cache
* \param [in] length The length of data cache
* \param [in] timeout The over time of read
*
* \return Return null.
*/
void getVersion(void);
/**
* \brief Read the ID from device
*
* \param [in] *buffer The output data cache
* \param [in] length The length of data cache
* \param [in] timeout The over time of read
*
* \return Return null.
*/
void getId(void);
/**
* \brief Set the OTAA keys
*
* \param [in] *AppEUI The application identifier
* \param [in] *DevEUI The end-device identifier
* \param [in] *AppKey The Application key
*
* \return Return null.
*/
void setKeysOTAA(char *AppEUI, char *DevEUI, char *AppKey);
/**
* \brief Set the ABP keys
*
* \param [in] *DevAddr The end-device address
* \param [in] *NwkSKey The network session key
* \param [in] *AppSKey The application session key
*
* \return Return null.
*/
void setKeysABP(char *DevAddr, char *NwkSKey, char *AppSKey);
/**
* \brief Set the data rate
*
* \param [in] physicalType The type of ISM
*
* \return Return null.
*/
void setDataRate(_physical_type_t physicalType = EU433);
/**
* \brief Set the output power
*
* \param [in] power The output power value
*
* \return Return null.
*/
void setPower(short power);
/**
* \brief Set the port number
*
* \param [in] port The port number, range from 1 to 255
*
* \return Return null.
*/
void setPort(unsigned char port);
/**
* \brief ON/OFF adaptive data rate mode
*
* \param [in] command The date rate of encoding
*
* \return Return null.
*/
void setAdaptiveDataRate(bool command);
/**
* \brief Set the channel parameter
*
* \param [in] channel The channel number, range from 0 to 71
* \param [in] frequency The frequency value
*
* \return Return null.
*/
/**
* \brief Set the channel parameter
*
* \param [in] channel The channel number, range from 0 to 71
* \param [in] frequency The frequency value
* \param [in] dataRataMin The minimum date rate of channel
* \param [in] dataRataMax The maximum date rate of channel
*
* \return Return null.
*/
void setChannel(unsigned char channel, float frequency, _data_rate_t dataRataMin, _data_rate_t dataRataMax);
/**
* \brief Transmit the data
*
* \param [in] *buffer The transmit data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : transmit done, false : transmit failed
*/
bool transmitPacket(char *buffer, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Transmit the data
*
* \param [in] *buffer The transmit data cache
* \param [in] length The length of data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : transmit done, false : transmit failed
*/
bool transmitPacket(unsigned char *buffer, unsigned char length, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Transmit the packet data
*
* \param [in] *buffer The transmit data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : Confirmed ACK, false : Confirmed NOT ACK
*/
bool transmitPacketWithConfirmed(char *buffer, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Transmit the data
*
* \param [in] *buffer The transmit data cache
* \param [in] length The length of data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : Confirmed ACK, false : Confirmed NOT ACK
*/
bool transmitPacketWithConfirmed(unsigned char *buffer, unsigned char length, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Receive the data
*
* \param [in] *buffer The receive data cache
* \param [in] length The length of data cache
* \param [in] *rssi The RSSI cache
*
* \return Return Receive data number
*/
short receivePacket(char *buffer, short length, short *rssi);
/**
* \brief Transmit the proprietary data
*
* \param [in] *buffer The transmit data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : transmit done, false : transmit failed
*/
bool transmitProprietaryPacket(char *buffer, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Transmit the proprietary data
*
* \param [in] *buffer The transmit data cache
* \param [in] length The length of data cache
* \param [in] timeout The over time of transmit
*
* \return Return bool. Ture : transmit done, false : transmit failed
*/
bool transmitProprietaryPacket(unsigned char *buffer, unsigned char length, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Set device mode
*
* \param [in] mode The mode of device
*
* \return Return null
*/
void setActivation(_device_mode_t mode);
/**
* \brief Set device join a network
*
* \param [in] command The type of join
* \param [in] timeout The over time of join
*
* \return Return bool. True : join OK, false : join NOT OK
*/
bool setOTAAJoin(_otaa_join_cmd_t command, unsigned char timeout = DEFAULT_TIMEOUT);
/**
* \brief Set message unconfirmed repeat time
*
* \param [in] time The repeat time, range from 1 to 15
*
* \return Return null
*/
void setUnconfirmedMessageRepeatTime(unsigned char time);
/**
* \brief Set message retry times time
*
* \param [in] time The retry time, range from 0 to 254
*
* \return Return null
*/
void setConfirmedMessageRetryTime(unsigned char time);
/**
* \brief ON/OFF receice window 1
*
* \param [in] command The true : ON, false OFF
*
* \return Return null
*/
void setReceiveWindowFirst(bool command);
/**
* \brief Set receice window 1 channel mapping
*
* \param [in] channel The channel number, range from 0 to 71
* \param [in] frequency The frequency value of channel
*
* \return Return null
*/
void setReceiveWindowFirst(unsigned char channel, float frequency);
/**
* \brief Set receice window 2 channel mapping
*
* \param [in] frequency The frequency value of channel
* \param [in] dataRate The date rate value
*
* \return Return null
*/
void setReceiveWindowSecond(float frequency, _data_rate_t dataRate);
/**
* \brief Set receice window 2 channel mapping
*
* \param [in] frequency The frequency value of channel
* \param [in] spreadingFactor The spreading factor value
* \param [in] bandwidth The band width value
*
* \return Return null
*/
void setReceiveWindowSecond(float frequency, _spreading_factor_t spreadingFactor, _band_width_t bandwidth);
/**
* \brief ON/OFF duty cycle limitation
*
* \param [in] command The true : ON, false OFF
*
* \return Return null
*/
void setDutyCycle(bool command);
/**
* \brief ON/OFF join duty cycle limitation
*
* \param [in] command The true : ON, false OFF
*
* \return Return null
*/
void setJoinDutyCycle(bool command);
/**
* \brief Set receice window delay
*
* \param [in] command The delay type
* \param [in] _delay The delay value(millisecond)
*
* \return Return null
*/
void setReceiveWindowDelay(_window_delay_t command, unsigned short _delay);
/**
* \brief Set LoRaWAN class type
*
* \param [in] type The class type
*
* \return Return null
*/
void setClassType(_class_type_t type);
/**
* \brief Set device into low power mode
*
* \return Return null
*/
void setDeviceLowPower(void);
/**
* \brief Wake up device from low power mode
*
* \return Return null
*/
void setDeviceLowPowerWakeUp(void);
/**
* \brief Reset device
*
* \return Return null
*/
void setDeviceReset();
/**
* \brief Setup device default
*
* \return Return null
*/
void setDeviceDefault(void);
/**
* \brief Read battery voltage
*
* \return Return battery voltage
*/
float getBatteryVoltage(void);
/**
* \brief Read battery status
*
* \return Return false while charging, return true while charge done or no battery insert
*/
bool getBatteryStatus(void);
/**
* \brief Read module temperature
*
* \return Return module temperature
*/
float getModuleTemperatureC(void);
bool containsSubstring(const char* buffer, const char* substring);
void loraPrint(unsigned char timeout);
private:
void sendCommand(char *command);
short readBuffer(char* buffer, short length, unsigned char timeout = DEFAULT_TIMEOUT);
char _buffer[256];
};
extern LoRaWanClass lora;
#endif