|
| 1 | +# Device-to-Device Communication Without Manual Routes |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation eliminates the need for manually configured routes in Blues Notehub by using **fleet-based note targeting** and the Notecard's built-in device-to-device communication capabilities. |
| 6 | + |
| 7 | +## How It Works |
| 8 | + |
| 9 | +### Traditional Route-Based Approach (OLD) |
| 10 | +1. Client sends note to `telemetry.qo` |
| 11 | +2. Notehub route manually configured to forward `telemetry.qo` → server device `telemetry.qi` |
| 12 | +3. Server polls `telemetry.qi` for incoming data |
| 13 | +4. **Problem**: Requires manual route setup in Notehub web interface for each note file type |
| 14 | + |
| 15 | +### Fleet-Based API Approach (NEW) |
| 16 | +1. Client and server Notecards are organized into **Fleets** in Notehub |
| 17 | +2. Client sends notes to **fleet-scoped notefile** using special syntax |
| 18 | +3. Notes automatically delivered to all devices in target fleet |
| 19 | +4. **Benefit**: No manual route configuration needed - just fleet membership |
| 20 | + |
| 21 | +## Implementation Strategy |
| 22 | + |
| 23 | +### Method 1: Fleet-to-Fleet Communication (Recommended) |
| 24 | + |
| 25 | +Blues Notecard supports sending notes to a fleet using the format: `<fleet>:<notefile>`. |
| 26 | + |
| 27 | +**Client sends to server fleet:** |
| 28 | +```cpp |
| 29 | +// Instead of: |
| 30 | +// note.add to "telemetry.qo" with route configuration |
| 31 | + |
| 32 | +// Use fleet targeting: |
| 33 | +J *req = notecard.newRequest("note.add"); |
| 34 | +JAddStringToObject(req, "file", "fleet.server:telemetry.qi"); |
| 35 | +JAddItemToObject(req, "body", body); |
| 36 | +notecard.sendRequest(req); |
| 37 | +``` |
| 38 | +
|
| 39 | +**Server receives from any fleet member:** |
| 40 | +```cpp |
| 41 | +// Server just polls local notefile - Notehub delivers automatically |
| 42 | +J *req = notecard.newRequest("note.get"); |
| 43 | +JAddStringToObject(req, "file", "telemetry.qi"); |
| 44 | +JAddBoolToObject(req, "delete", true); |
| 45 | +J *rsp = notecard.requestAndResponse(req); |
| 46 | +``` |
| 47 | + |
| 48 | +### Method 2: Device-Specific Targeting |
| 49 | + |
| 50 | +For server-to-client config updates, target specific device UID: |
| 51 | + |
| 52 | +```cpp |
| 53 | +// Server sends config to specific client device |
| 54 | +J *req = notecard.newRequest("note.add"); |
| 55 | +char targetFile[80]; |
| 56 | +snprintf(targetFile, sizeof(targetFile), "device:%s:config.qi", clientDeviceUID); |
| 57 | +JAddStringToObject(req, "file", targetFile); |
| 58 | +JAddItemToObject(req, "body", configBody); |
| 59 | +notecard.sendRequest(req); |
| 60 | +``` |
| 61 | +
|
| 62 | +### Method 3: Project-Wide Notefiles |
| 63 | +
|
| 64 | +Use project-level notefiles that all devices can access: |
| 65 | +
|
| 66 | +```cpp |
| 67 | +// Client publishes to project-level notefile |
| 68 | +J *req = notecard.newRequest("note.add"); |
| 69 | +JAddStringToObject(req, "file", "project:telemetry"); |
| 70 | +// Add device UID in body so server knows source |
| 71 | +J *body = JCreateObject(); |
| 72 | +JAddStringToObject(body, "device", gDeviceUID); |
| 73 | +// ... add other data ... |
| 74 | +JAddItemToObject(req, "body", body); |
| 75 | +notecard.sendRequest(req); |
| 76 | +``` |
| 77 | + |
| 78 | +## Setup Requirements |
| 79 | + |
| 80 | +### In Blues Notehub: |
| 81 | + |
| 82 | +1. **Create Fleets:** |
| 83 | + - Fleet: `tankalarm-clients` (for all field devices) |
| 84 | + - Fleet: `tankalarm-server` (for base station) |
| 85 | + |
| 86 | +2. **Assign Devices to Fleets:** |
| 87 | + - Navigate to Devices |
| 88 | + - Select each client Notecard → assign to `tankalarm-clients` fleet |
| 89 | + - Select server Notecard → assign to `tankalarm-server` fleet |
| 90 | + |
| 91 | +3. **Done!** No route configuration needed. |
| 92 | + |
| 93 | +### In Device Configuration: |
| 94 | + |
| 95 | +**Client devices:** |
| 96 | +- Store the server fleet name (e.g., "tankalarm-server") in configuration |
| 97 | +- Use `fleet.tankalarm-server:<filename>` syntax when publishing |
| 98 | + |
| 99 | +**Server device:** |
| 100 | +- Poll local notefiles (`.qi` files) - Notehub automatically delivers |
| 101 | +- Store client fleet name for broadcasting config updates |
| 102 | + |
| 103 | +## Notefile Naming Convention |
| 104 | + |
| 105 | +| Communication | Old Route-Based | New Fleet-Based | |
| 106 | +|---------------|-----------------|-----------------| |
| 107 | +| Client → Server telemetry | `telemetry.qo` → route → server `telemetry.qi` | `fleet.tankalarm-server:telemetry.qi` | |
| 108 | +| Client → Server alarm | `alarm.qo` → route → server `alarm.qi` | `fleet.tankalarm-server:alarm.qi` | |
| 109 | +| Client → Server daily | `daily.qo` → route → server `daily.qi` | `fleet.tankalarm-server:daily.qi` | |
| 110 | +| Server → Client config | `config.qo` → route → client `config.qi` | `device:<uid>:config.qi` or `fleet.tankalarm-clients:config.qi` | |
| 111 | + |
| 112 | +## Benefits |
| 113 | + |
| 114 | +1. **Zero Route Configuration**: No manual route setup in Notehub web interface |
| 115 | +2. **Easier Deployment**: Just assign devices to fleets |
| 116 | +3. **Scalable**: Add new clients by assigning them to client fleet |
| 117 | +4. **Flexible**: Can target specific devices or broadcast to entire fleet |
| 118 | +5. **Resilient**: Fleet membership managed in Notehub, survives device replacement |
| 119 | + |
| 120 | +## Configuration Changes Needed |
| 121 | + |
| 122 | +### Client Configuration (config.json) |
| 123 | +```json |
| 124 | +{ |
| 125 | + "deviceLabel": "Tank01", |
| 126 | + "serverFleet": "tankalarm-server", |
| 127 | + // Remove: "serverRoute": "default-route" |
| 128 | + ... |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Server Configuration (server_config.json) |
| 133 | +```json |
| 134 | +{ |
| 135 | + "serverLabel": "TankAlarmServer", |
| 136 | + "clientFleet": "tankalarm-clients", |
| 137 | + ... |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Migration Path |
| 142 | + |
| 143 | +1. Create fleets in Notehub |
| 144 | +2. Assign devices to appropriate fleets |
| 145 | +3. Update client and server firmware with new fleet-based code |
| 146 | +4. Deploy updated firmware |
| 147 | +5. Verify communication works |
| 148 | +6. Delete old routes from Notehub (optional, for cleanup) |
| 149 | + |
| 150 | +## Troubleshooting |
| 151 | + |
| 152 | +**Notes not arriving at server:** |
| 153 | +- Verify server Notecard is in `tankalarm-server` fleet |
| 154 | +- Check client configuration has correct `serverFleet` name |
| 155 | +- Use Notehub Events view to see if notes are being sent |
| 156 | +- Confirm continuous mode enabled on both devices |
| 157 | + |
| 158 | +**Config updates not reaching clients:** |
| 159 | +- Verify clients are in correct fleet |
| 160 | +- Check server is using correct fleet name or device UID |
| 161 | +- For device-specific targeting, ensure UID is correct (use `card.uuid` response) |
| 162 | + |
| 163 | +**General debugging:** |
| 164 | +- Enable Notecard debug output: `notecard.setDebugOutputStream(Serial);` |
| 165 | +- Check Notehub Events for each device to see note traffic |
| 166 | +- Verify both devices have recent sync timestamps |
| 167 | + |
| 168 | +## Hardware Requirements |
| 169 | + |
| 170 | +- **Client**: Arduino Opta Lite + Blues Wireless for Opta adapter + analog extension (unchanged) |
| 171 | +- **Server**: Arduino Opta Lite + Blues Wireless for Opta adapter (unchanged) |
| 172 | + |
| 173 | +No additional hardware needed - this is purely a software/configuration change. |
| 174 | + |
| 175 | +## References |
| 176 | + |
| 177 | +- [Blues Wireless Fleet Documentation](https://dev.blues.io/reference/notehub-api/fleet-api/) |
| 178 | +- [Notecard note.add API](https://dev.blues.io/api-reference/notecard-api/note-requests/#note-add) |
| 179 | +- [Device-to-Device Communication](https://dev.blues.io/guides-and-tutorials/routing-data-to-cloud/device-to-device/) |
0 commit comments