Skip to content

Commit fd58854

Browse files
committed
Add PingPong example for RAK4631
1 parent 1f42eee commit fd58854

File tree

12 files changed

+1760
-2
lines changed

12 files changed

+1760
-2
lines changed

examples/PingPong-RAK4631/PingPong-RAK4631.ino

Lines changed: 460 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# PingPong for PlatformIO
2+
3+
Example to be used with with PlatformIO as extension on Atom or Microsoft Visual Studio. It is a simple Ping - Pong between two LoRa nodes. One node sends a PING and the other node responds with a PONG. It is a simple application that can be usefull to test the range your LoRa setup can reach.
4+
Principal of function:
5+
- Listen for incoming packets.
6+
- If receive timeout (3s) occurs start a CAD (channel activity detection)
7+
- - If the CAD shows no activity, send a PING package
8+
- - Restart listening for incoming packets
9+
- If a PING package was received start a CAD (channel activity detection)
10+
- - If the CAD shows no activity, send a PONG response
11+
- - Restart listening for incoming messages
12+
- If a PONG package was received start a CAD (channel activity detection)
13+
- - If the CAD shows no activity, send the next PING package
14+
- - Restart the listening for incoming packets
15+
16+
BLE of is activated and the RAk4631 starts advertising with two services. One is for DFU, the OTA update service of Nordic to update the firmware on the chip. The second one is a simple BLE-UART service to send debug messages over BLE to a BLE-UART app like the [Serial Bluetooth Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) for Android
17+
18+
## Some explanation for the code
19+
20+
This code is made for the RAK4631.
21+
It uses the specific initialization routine for the RAK4631 and no pin definitions are required.
22+
23+
Initialize the LoRa HW
24+
```
25+
lora_rak4630_init();
26+
```
27+
Setup the callbacks for LoRa events
28+
```
29+
RadioEvents.TxDone = OnTxDone;
30+
RadioEvents.RxDone = OnRxDone;
31+
RadioEvents.TxTimeout = OnTxTimeout;
32+
RadioEvents.RxTimeout = OnRxTimeout;
33+
RadioEvents.RxError = OnRxError;
34+
RadioEvents.CadDone = OnCadDone;
35+
```
36+
Initialize the radio and set the TX and RX parameters
37+
```
38+
Radio.Init(&RadioEvents);
39+
40+
Radio.SetChannel(RF_FREQUENCY);
41+
42+
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
43+
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
44+
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
45+
true, 0, 0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
46+
47+
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
48+
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
49+
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
50+
0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
51+
```
52+
Start listening for packets
53+
```
54+
Radio.Rx(RX_TIMEOUT_VALUE);
55+
```
56+
57+
## Using semaphores for low power consumption'
58+
59+
Instead of running the loop endless, a semaphore is used to wake up the MCU on a LoRa event.
60+
The loop waits for the semaphore to released and then handles the event.

examples/PingPong-RAK4631/ble.ino

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifdef NRF52_SERIES
2+
#include <bluefruit.h>
3+
4+
void startAdv(void);
5+
void connect_callback(uint16_t conn_handle);
6+
void disconnect_callback(uint16_t conn_handle, uint8_t reason);
7+
8+
// OTA DFU service
9+
BLEDfu bledfu;
10+
// UART service
11+
BLEUart bleuart;
12+
13+
bool bleUARTisConnected = false;
14+
15+
void initBLE(void)
16+
{
17+
Bluefruit.begin();
18+
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
19+
Bluefruit.setTxPower(4);
20+
Bluefruit.setName("PPG_LORA_SX126x_TEST");
21+
22+
Bluefruit.autoConnLed(false);
23+
24+
Bluefruit.Periph.setConnectCallback(connect_callback);
25+
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
26+
27+
// To be consistent OTA DFU should be added first if it exists
28+
bledfu.begin();
29+
30+
// Configure and Start BLE Uart Service
31+
bleuart.begin();
32+
33+
// Set up and start advertising
34+
startAdv();
35+
}
36+
37+
void startAdv(void)
38+
{
39+
// Advertising packet
40+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
41+
Bluefruit.Advertising.addTxPower();
42+
Bluefruit.Advertising.addName();
43+
44+
/* Start Advertising
45+
* - Enable auto advertising if disconnected
46+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
47+
* - Timeout for fast mode is 30 seconds
48+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
49+
*
50+
* For recommended advertising interval
51+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
52+
*/
53+
Bluefruit.Advertising.restartOnDisconnect(true);
54+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
55+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
56+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
57+
}
58+
59+
// callback invoked when central connects
60+
void connect_callback(uint16_t conn_handle)
61+
{
62+
(void)conn_handle;
63+
bleUARTisConnected = true;
64+
}
65+
66+
/**
67+
* Callback invoked when a connection is dropped
68+
* @param conn_handle connection where this event happens
69+
* @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
70+
*/
71+
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
72+
{
73+
(void)conn_handle;
74+
(void)reason;
75+
bleUARTisConnected = false;
76+
}
77+
#endif

examples/PingPong/PingPong.ino

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,20 @@ extern BLEUart bleuart;
6161

6262
// Check if the board has an LED port defined
6363
#ifdef ESP32
64+
#ifndef LED_BUILTIN
6465
#define LED_BUILTIN 2
6566
#endif
67+
#endif
6668
#ifdef ESP8266
69+
#ifndef LED_BUILTIN
6770
#define LED_BUILTIN 2
6871
#endif
72+
#endif
6973
#ifdef NRF52_SERIES
74+
#ifndef LED_BUILTIN
7075
#define LED_BUILTIN 17
7176
#endif
77+
#endif
7278

7379
// Define LoRa parameters
7480
#define RF_FREQUENCY 868000000 // Hz
@@ -282,7 +288,12 @@ void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
282288
}
283289
#endif
284290
isMaster = false;
285-
Radio.Rx(RX_TIMEOUT_VALUE);
291+
// Check if our channel is available for sending
292+
Radio.Sleep();
293+
Radio.SetCadParams(LORA_CAD_08_SYMBOL, LORA_SPREADING_FACTOR + 13, 10, LORA_CAD_ONLY, 0);
294+
cadTime = millis();
295+
Radio.StartCad();
296+
// Sending next Pong will be started when the channel is free
286297
}
287298
else // valid reception but neither a PING or a PONG message
288299
{ // Set device as master and start again
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < https://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < https://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < https://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choose one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
# - platformio update
39+
#
40+
# script:
41+
# - platformio run
42+
43+
44+
#
45+
# Template #2: The project is intended to be used as a library with examples.
46+
#
47+
48+
# language: python
49+
# python:
50+
# - "2.7"
51+
#
52+
# sudo: false
53+
# cache:
54+
# directories:
55+
# - "~/.platformio"
56+
#
57+
# env:
58+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
59+
# - PLATFORMIO_CI_SRC=examples/file.ino
60+
# - PLATFORMIO_CI_SRC=path/to/test/directory
61+
#
62+
# install:
63+
# - pip install -U platformio
64+
# - platformio update
65+
#
66+
# script:
67+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# PingPong for PlatformIO
2+
3+
Example to be used with with PlatformIO as extension on Atom or Microsoft Visual Studio. It is a simple Ping - Pong between two LoRa nodes. One node sends a PING and the other node responds with a PONG. It is a simple application that can be usefull to test the range your LoRa setup can reach.
4+
Principal of function:
5+
- Listen for incoming packets.
6+
- If receive timeout (3s) occurs start a CAD (channel activity detection)
7+
- - If the CAD shows no activity, send a PING package
8+
- - Restart listening for incoming packets
9+
- If a PING package was received start a CAD (channel activity detection)
10+
- - If the CAD shows no activity, send a PONG response
11+
- - Restart listening for incoming messages
12+
- If a PONG package was received start a CAD (channel activity detection)
13+
- - If the CAD shows no activity, send the next PING package
14+
- - Restart the listening for incoming packets
15+
16+
BLE of is activated and the RAk4631 starts advertising with two services. One is for DFU, the OTA update service of Nordic to update the firmware on the chip. The second one is a simple BLE-UART service to send debug messages over BLE to a BLE-UART app like the [Serial Bluetooth Terminal](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) for Android
17+
18+
## Some explanation for the code
19+
20+
This code is made for the RAK4631.
21+
It uses the specific initialization routine for the RAK4631 and no pin definitions are required.
22+
23+
Initialize the LoRa HW
24+
```
25+
lora_rak4630_init();
26+
```
27+
Setup the callbacks for LoRa events
28+
```
29+
RadioEvents.TxDone = OnTxDone;
30+
RadioEvents.RxDone = OnRxDone;
31+
RadioEvents.TxTimeout = OnTxTimeout;
32+
RadioEvents.RxTimeout = OnRxTimeout;
33+
RadioEvents.RxError = OnRxError;
34+
RadioEvents.CadDone = OnCadDone;
35+
```
36+
Initialize the radio and set the TX and RX parameters
37+
```
38+
Radio.Init(&RadioEvents);
39+
40+
Radio.SetChannel(RF_FREQUENCY);
41+
42+
Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
43+
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
44+
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
45+
true, 0, 0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
46+
47+
Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
48+
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
49+
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
50+
0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
51+
```
52+
Start listening for packets
53+
```
54+
Radio.Rx(RX_TIMEOUT_VALUE);
55+
```
56+
57+
## Using semaphores for low power consumption'
58+
59+
Instead of running the loop endless, a semaphore is used to wake up the MCU on a LoRa event.
60+
The loop waits for the semaphore to released and then handles the event.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[platformio]
12+
default_envs =
13+
; esp32dev
14+
; nrf52
15+
; esp8266
16+
rak4631
17+
18+
[env:esp32dev]
19+
platform = espressif32
20+
board = esp32dev
21+
framework = arduino
22+
upload_port = COM6
23+
upload_speed = 921600
24+
build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_ERROR
25+
lib_deps =
26+
beegee-tokyo/SX126X-arduino
27+
28+
[env:nrf52]
29+
platform = nordicnrf52
30+
board = adafruit_feather_nrf52832
31+
framework = arduino
32+
build_flags = -DCFG_DEBUG=2
33+
lib_deps =
34+
beegee-tokyo/SX126X-arduino
35+
36+
[env:rak4631]
37+
platform = nordicnrf52
38+
board = wiscore_rak4631
39+
framework = arduino
40+
; build_flags = -D LIB_DEBUG=1
41+
; -DCFG_DEBUG=2
42+
lib_deps =
43+
beegee-tokyo/SX126X-arduino
44+
45+
[env:esp8266]
46+
platform = espressif8266
47+
board = nodemcuv2
48+
framework = arduino
49+
upload_port = COM10
50+
upload_speed = 921600
51+
board_build.f_cpu = 160000000L
52+
build_flags =
53+
-Wl,-Tesp8266.flash.4m1m.ld
54+
-Og
55+
lib_deps =
56+
beegee-tokyo/SX126X-arduino
57+

0 commit comments

Comments
 (0)