-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathserial.ino
68 lines (55 loc) · 1.86 KB
/
serial.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
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/*
* Serial Port over BLE
* Create UART service compatible with Nordic's *nRF Toolbox* and Adafruit's *Bluefruit LE* iOS/Android apps.
*
* BLESerial class implements same protocols as Arduino's built-in Serial class and can be used as it's wireless
* replacement. Data transfers are routed through a BLE service with TX and RX characteristics. To make the
* service discoverable all UUIDs are NUS (Nordic UART Service) compatible.
*
* Please note that TX and RX characteristics use Notify and WriteWithoutResponse, so there's no guarantee
* that the data will make it to the other end. However, under normal circumstances and reasonable signal
* strengths everything works well.
*/
// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>
#include "BLESerial.h"
//custom boards may override default pin definitions with BLESerial(PIN_REQ, PIN_RDY, PIN_RST)
BLESerial bleSerial;
void setup() {
// custom services and characteristics can be added as well
bleSerial.setLocalName("UART");
Serial.begin(115200);
bleSerial.begin();
}
void loop() {
bleSerial.poll();
forward();
// loopback();
// spam();
}
// forward received from Serial to BLESerial and vice versa
void forward() {
if (bleSerial && Serial) {
int byte;
while ((byte = bleSerial.read()) > 0) Serial.write((char)byte);
while ((byte = Serial.read()) > 0) bleSerial.write((char)byte);
}
}
// echo all received data back
void loopback() {
if (bleSerial) {
int byte;
while ((byte = bleSerial.read()) > 0) bleSerial.write(byte);
}
}
// periodically sent time stamps
void spam() {
if (bleSerial) {
bleSerial.print(millis());
bleSerial.println(" tick-tacks!");
delay(1000);
}
}