-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNuSEcho.ino
96 lines (87 loc) · 3.2 KB
/
NuSEcho.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
/**
* @file NusEcho.ino
* @author Ángel Fernández Pineda. Madrid. Spain.
* @date 2023-12-18
*
* @brief Example of a non-blocking communications stream
* based on the Nordic UART Service
*
* @note See examples/README.md for a description
*
* @copyright Creative Commons Attribution 4.0 International (CC BY 4.0)
*
*/
#include <Arduino.h>
#include "NuSerial.hpp"
#include "NimBLEDevice.h"
#define DEVICE_NAME "NuSerial Echo"
void setup()
{
// Initialize serial monitor
Serial.begin(115200);
Serial.println("*****************************");
Serial.println(" BLE echo server demo ");
Serial.println("*****************************");
Serial.println("--Initializing--");
// Initialize BLE stack and Nordic UART service
NimBLEDevice::init(DEVICE_NAME);
NimBLEDevice::getAdvertising()->setName(DEVICE_NAME);
NuSerial.begin(115200);
// Initialization complete
Serial.println("--Ready--");
}
void loop()
{
if (NuSerial.isConnected())
{
int serialMonitorChar = Serial.read();
if ((serialMonitorChar == 'E') || (serialMonitorChar == 'e'))
{
// Open the serial monitor in Arduino IDE
// Type "E" or "e" and press ENTER to drop the BLE connection
Serial.println("--Terminating connection from server side--");
NuSerial.end();
}
else
{
int processedCount = 0;
int availableCount = NuSerial.available();
if (availableCount)
Serial.printf("--Available %d bytes for processing--\n", availableCount);
while (NuSerial.available())
{
int bleChar = NuSerial.read();
if (bleChar < 0)
Serial.println("ERROR: NuSerial.read()<0, but NuSerial.available()>0. Should not happen.");
else
{
// Echo
if (NuSerial.write(bleChar) < 1)
Serial.println("ERROR: NuSerial.write() failed");
// Note: the following delay is required because we are sending data in a byte-by-byte basis.
// If we send bytes quicker than they are consumed by the peer,
// the internal buffer of NimBLE will overflow, thus losing some bytes.
// BLE is designed to transmit a larger chunk of bytes slowly rather than a single byte quickly.
// That's another reason to use NuPacket instead of NuSerial.
delay(30);
// log ASCII/ANSI codes
Serial.printf("%d.", bleChar);
processedCount++;
}
}
if (processedCount != availableCount)
Serial.printf("\nERROR: %d bytes were available, but %d bytes were processed.\n", availableCount, processedCount);
else if (processedCount)
{
Serial.printf("\n--Stream of %d bytes processed--\n", processedCount);
}
}
}
else
{
Serial.println("--Waiting for connection--");
while (!NuSerial.isConnected())
delay(500);
Serial.println("--Connected--");
}
}