|
| 1 | +# NimBLE Stream Examples |
| 2 | + |
| 3 | +This document describes the new Stream-based examples that demonstrate using BLE characteristics with the familiar Arduino Stream interface. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The NimBLE Stream classes (`NimBLEStreamServer` and `NimBLEStreamClient`) provide a simple way to use BLE characteristics like serial ports. You can use familiar methods like `print()`, `println()`, `read()`, and `available()` just like you would with `Serial`. |
| 8 | + |
| 9 | +## Available Examples |
| 10 | + |
| 11 | +### 1. NimBLE_Stream_Echo |
| 12 | +**Difficulty:** Beginner |
| 13 | +**Purpose:** Introduction to the Stream API |
| 14 | + |
| 15 | +This is the simplest example, perfect for getting started. It creates a BLE server that echoes back any data it receives. Uses default UUIDs for quick setup. |
| 16 | + |
| 17 | +**Key Features:** |
| 18 | +- Minimal setup code |
| 19 | +- Simple echo functionality |
| 20 | +- Good starting point for learning |
| 21 | + |
| 22 | +**Use Case:** Testing BLE connectivity, learning the basics |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +### 2. NimBLE_Stream_Server |
| 27 | +**Difficulty:** Intermediate |
| 28 | +**Purpose:** Full-featured server implementation |
| 29 | + |
| 30 | +A complete BLE server example that demonstrates all the major features of `NimBLEStreamServer`. Shows connection management, bidirectional communication, and proper server setup. |
| 31 | + |
| 32 | +**Key Features:** |
| 33 | +- Custom service and characteristic UUIDs |
| 34 | +- Connection callbacks |
| 35 | +- Periodic message sending |
| 36 | +- Echo functionality |
| 37 | +- MTU negotiation |
| 38 | +- Connection parameter updates |
| 39 | + |
| 40 | +**Use Case:** Building custom BLE services, wireless data logging, remote monitoring |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### 3. NimBLE_Stream_Client |
| 45 | +**Difficulty:** Intermediate |
| 46 | +**Purpose:** Full-featured client implementation |
| 47 | + |
| 48 | +Demonstrates how to scan for, connect to, and communicate with a BLE server using `NimBLEStreamClient`. Pairs with the NimBLE_Stream_Server example. |
| 49 | + |
| 50 | +**Key Features:** |
| 51 | +- Automatic server discovery |
| 52 | +- Connection management |
| 53 | +- Reconnection on disconnect |
| 54 | +- Bidirectional communication |
| 55 | +- Serial passthrough (type in Serial monitor to send via BLE) |
| 56 | + |
| 57 | +**Use Case:** Building BLE client applications, data collection from BLE devices |
| 58 | + |
| 59 | +## Quick Start |
| 60 | + |
| 61 | +### Running the Echo Example |
| 62 | + |
| 63 | +1. Upload `NimBLE_Stream_Echo` to your ESP32 |
| 64 | +2. Open Serial monitor (115200 baud) |
| 65 | +3. Use a BLE app (like nRF Connect) to connect |
| 66 | +4. Find service UUID `0xc0de`, characteristic `0xfeed` |
| 67 | +5. Subscribe to notifications |
| 68 | +6. Write data to see it echoed back |
| 69 | + |
| 70 | +### Running Server + Client Examples |
| 71 | + |
| 72 | +1. Upload `NimBLE_Stream_Server` to one ESP32 |
| 73 | +2. Upload `NimBLE_Stream_Client` to another ESP32 |
| 74 | +3. Open Serial monitors for both (115200 baud) |
| 75 | +4. Client will automatically find and connect to server |
| 76 | +5. Observe bidirectional communication |
| 77 | +6. Type in either Serial monitor to send data |
| 78 | + |
| 79 | +## Common Use Cases |
| 80 | + |
| 81 | +- **Wireless Serial Communication:** Replace physical serial connections with BLE |
| 82 | +- **Data Logging:** Stream sensor data to a mobile device or another microcontroller |
| 83 | +- **Remote Control:** Send commands between devices using familiar print/println |
| 84 | +- **Debugging:** Use BLE as a wireless alternative to USB serial debugging |
| 85 | +- **IoT Applications:** Simple data exchange between ESP32 devices |
| 86 | + |
| 87 | +## Stream Interface Methods |
| 88 | + |
| 89 | +The examples demonstrate these familiar methods: |
| 90 | + |
| 91 | +**Output (Sending Data):** |
| 92 | +- `print(value)` - Print data without newline |
| 93 | +- `println(value)` - Print data with newline |
| 94 | +- `printf(format, ...)` - Formatted output |
| 95 | +- `write(data)` - Write raw bytes |
| 96 | + |
| 97 | +**Input (Receiving Data):** |
| 98 | +- `available()` - Check if data is available |
| 99 | +- `read()` - Read a single byte |
| 100 | +- `peek()` - Preview next byte without removing it |
| 101 | + |
| 102 | +**Status:** |
| 103 | +- `hasSubscriber()` - Check if a client is connected (server only) |
| 104 | +- `ready()` or `operator bool()` - Check if stream is ready |
| 105 | + |
| 106 | +## Configuration |
| 107 | + |
| 108 | +Both server and client support configuration before calling `begin()`: |
| 109 | + |
| 110 | +```cpp |
| 111 | +bleStream.setTxBufSize(2048); // TX buffer size |
| 112 | +bleStream.setRxBufSize(2048); // RX buffer size |
| 113 | +bleStream.setTxTaskStackSize(4096); // Task stack size |
| 114 | +bleStream.setTxTaskPriority(1); // Task priority |
| 115 | +``` |
| 116 | + |
| 117 | +## Compatibility |
| 118 | + |
| 119 | +These examples work with: |
| 120 | +- ESP32 (all variants: ESP32, ESP32-C3, ESP32-S3, etc.) |
| 121 | +- Nordic chips (with n-able Arduino core) |
| 122 | +- Any BLE client supporting GATT characteristics with notifications |
| 123 | + |
| 124 | +## Additional Resources |
| 125 | + |
| 126 | +- [Arduino Stream Reference](https://www.arduino.cc/reference/en/language/functions/communication/stream/) |
| 127 | +- [NimBLE-Arduino Documentation](https://h2zero.github.io/NimBLE-Arduino/) |
| 128 | +- Each example includes a detailed README.md file |
| 129 | + |
| 130 | +## Troubleshooting |
| 131 | + |
| 132 | +**Client can't find server:** |
| 133 | +- Check that both devices are powered on |
| 134 | +- Verify UUIDs match between server and client |
| 135 | +- Ensure server advertising is started |
| 136 | + |
| 137 | +**Data not received:** |
| 138 | +- Verify client has subscribed to notifications |
| 139 | +- Check that buffers aren't full (increase buffer sizes) |
| 140 | +- Ensure both `init()` and `begin()` were called |
| 141 | + |
| 142 | +**Connection drops:** |
| 143 | +- Check for interference |
| 144 | +- Reduce connection interval if needed |
| 145 | +- Ensure devices are within BLE range |
0 commit comments