-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathble_uart.ino
155 lines (132 loc) · 4.45 KB
/
ble_uart.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
/**
@file ble_uart.ino
@author Bernd Giesecke ([email protected])
@brief BLE example shows how to implement UART over BLE
@version 0.1
@date 2020-07-27
@copyright Copyright (c) 2020
@note RAK4631 GPIO mapping to nRF52840 GPIO ports
RAK4631 <-> nRF52840
WB_IO1 <-> P0.17 (GPIO 17)
WB_IO2 <-> P1.02 (GPIO 34)
WB_IO3 <-> P0.21 (GPIO 21)
WB_IO4 <-> P0.04 (GPIO 4)
WB_IO5 <-> P0.09 (GPIO 9)
WB_IO6 <-> P0.10 (GPIO 10)
WB_SW1 <-> P0.01 (GPIO 1)
WB_A0 <-> P0.04/AIN2 (AnalogIn A2)
WB_A1 <-> P0.31/AIN7 (AnalogIn A7)
*/
#include <Arduino.h>
#include <bluefruit.h>
#include <Wire.h>
// Forward declarations for functions
void ble_connect_callback(uint16_t conn_handle);
void ble_disconnect_callback(uint16_t conn_handle, uint8_t reason);
/**
@brief BLE UART service
@note Used for BLE UART communication
*/
BLEUart g_BleUart;
/** Flag if BLE UART client is connected */
bool g_BleUartConnected = false;
/**
@brief Arduino setup function. Called once after power on or reset
*/
void setup()
{
// Initialize built in green LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// Initialize Serial for debug output
Serial.begin(115200);
// Wait for USB Serial to be ready or terminal to be connected
time_t timeout = millis(); // Timeout in case the system runs on its own
// Waiting for Serial
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
// Blink the LED to show that we are alive
delay(100);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
else
{
// Timeout while waiting for USB Serial
break;
}
}
Serial.println("================================");
Serial.println("RAK4631 BLE UART example");
Serial.println("================================");
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
Bluefruit.begin(1, 0);
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
Bluefruit.setTxPower(4);
// Set the BLE device name
Bluefruit.setName("RAK4631_UART");
Bluefruit.Periph.setConnectCallback(ble_connect_callback);
Bluefruit.Periph.setDisconnectCallback(ble_disconnect_callback);
// Configure and Start BLE Uart Service
g_BleUart.begin();
// Set up and start advertising
// Advertising packet
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addName();
/* Start Advertising
- Enable auto advertising if disconnected
- Interval: fast mode = 20 ms, slow mode = 152.5 ms
- Timeout for fast mode is 30 seconds
- Start(timeout) with timeout = 0 will advertise forever (until connected)
For recommended advertising interval
https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
}
/**
@brief Callback when client connects
@param conn_handle: Connection handle id
*/
void ble_connect_callback(uint16_t conn_handle)
{
(void)conn_handle;
g_BleUartConnected = true;
Serial.println("BLE client connected");
}
/**
@brief Callback invoked when a connection is dropped
@param conn_handle: connection handle id
@param reason: disconnect reason
*/
void ble_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void)conn_handle;
(void)reason;
g_BleUartConnected = false;
Serial.println("BLE client disconnected");
}
/**
@brief Arduino loop. Runs forever until power off or reset
*/
void loop()
{
// Forward anything received from USB Serial to BLE UART
if (Serial.available() && g_BleUartConnected)
{
g_BleUart.print(Serial.readString());
}
// Forward anything received from BLE UART to USB Serial
if (g_BleUart.available())
{
Serial.print(g_BleUart.readString());
}
}