@alteriom/painlessmesh 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -0
- package/LICENSE +674 -0
- package/README.md +434 -0
- package/RELEASE_GUIDE.md +419 -0
- package/docs/DOCUMENTATION_MIGRATION_PLAN.md +176 -0
- package/docs/README.md +71 -0
- package/docs/alteriom/overview.md +508 -0
- package/docs/api/core-api.md +607 -0
- package/docs/architecture/mesh-architecture.md +379 -0
- package/docs/architecture/plugin-system.md +517 -0
- package/docs/getting-started/first-mesh.md +410 -0
- package/docs/getting-started/installation.md +275 -0
- package/docs/getting-started/quickstart.md +158 -0
- package/docs/improvements/README.md +69 -0
- package/docs/troubleshooting/common-issues.md +521 -0
- package/docs/troubleshooting/faq.md +473 -0
- package/docs/tutorials/basic-examples.md +718 -0
- package/docs/wiki/API-Reference.md +246 -0
- package/docs/wiki/Complete-Documentation.md +123 -0
- package/examples/alteriom/README.md +82 -0
- package/examples/alteriom/alteriom.ino +186 -0
- package/examples/alteriom/alteriom_sensor_node.ino +184 -0
- package/examples/alteriom/alteriom_sensor_package.hpp +128 -0
- package/examples/alteriom/improved_sensor_node.ino +246 -0
- package/examples/alteriom/platformio.ini +25 -0
- package/examples/basic/basic.ino +66 -0
- package/examples/basic/platformio.ini +25 -0
- package/examples/bridge/bridge.ino +51 -0
- package/examples/bridge/platformio.ini +25 -0
- package/examples/echoNode/echoNode.ino +33 -0
- package/examples/echoNode/platformio.ini +25 -0
- package/examples/logClient/logClient.ino +109 -0
- package/examples/logClient/platformio.ini +25 -0
- package/examples/logServer/logServer.ino +81 -0
- package/examples/logServer/platformio.ini +25 -0
- package/examples/mqttBridge/mqttBridge.ino +118 -0
- package/examples/mqttBridge/platformio.ini +26 -0
- package/examples/namedMesh/namedMesh.ino +97 -0
- package/examples/namedMesh/platformio.ini +25 -0
- package/examples/otaReceiver/otaReceiver.ino +79 -0
- package/examples/otaReceiver/platformio.ini +25 -0
- package/examples/otaSender/nodemcu32s_connections.JPG +0 -0
- package/examples/otaSender/otaSender.ino +151 -0
- package/examples/otaSender/platformio.ini +25 -0
- package/examples/startHere/platformio.ini +25 -0
- package/examples/startHere/startHere.ino +159 -0
- package/examples/webServer/platformio.ini +27 -0
- package/examples/webServer/webServer.ino +89 -0
- package/keywords.txt +49 -0
- package/library.json +34 -0
- package/library.properties +11 -0
- package/package.json +78 -0
- package/src/AlteriomPainlessMesh.h +98 -0
- package/src/arduino/wifi.hpp +365 -0
- package/src/boost/asynctcp.hpp +279 -0
- package/src/painlessMesh.h +70 -0
- package/src/painlessMeshSTA.cpp +236 -0
- package/src/painlessMeshSTA.h +58 -0
- package/src/painlessTaskOptions.h +4 -0
- package/src/painlessmesh/base64.hpp +111 -0
- package/src/painlessmesh/buffer.hpp +229 -0
- package/src/painlessmesh/callback.hpp +91 -0
- package/src/painlessmesh/configuration.hpp +77 -0
- package/src/painlessmesh/connection.hpp +192 -0
- package/src/painlessmesh/layout.hpp +188 -0
- package/src/painlessmesh/logger.hpp +158 -0
- package/src/painlessmesh/memory.hpp +120 -0
- package/src/painlessmesh/mesh.hpp +560 -0
- package/src/painlessmesh/metrics.hpp +323 -0
- package/src/painlessmesh/ntp.hpp +263 -0
- package/src/painlessmesh/ota.hpp +553 -0
- package/src/painlessmesh/plugin.hpp +188 -0
- package/src/painlessmesh/protocol.hpp +813 -0
- package/src/painlessmesh/router.hpp +322 -0
- package/src/painlessmesh/tcp.hpp +71 -0
- package/src/painlessmesh/validation.hpp +239 -0
- package/src/plugin/performance.hpp +214 -0
- package/src/plugin/remote.hpp +64 -0
- package/src/scheduler.cpp +10 -0
- package/src/wifi.cpp +2 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
# Core API Reference
|
|
2
|
+
|
|
3
|
+
This document provides a comprehensive reference for the painlessMesh core API. All functions and classes are part of the `painlessmesh` namespace unless otherwise specified.
|
|
4
|
+
|
|
5
|
+
## painlessMesh Class
|
|
6
|
+
|
|
7
|
+
The main class for mesh network functionality.
|
|
8
|
+
|
|
9
|
+
### Constructor
|
|
10
|
+
|
|
11
|
+
```cpp
|
|
12
|
+
painlessMesh mesh;
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
No parameters required. Creates a mesh instance ready for initialization.
|
|
16
|
+
|
|
17
|
+
### Core Methods
|
|
18
|
+
|
|
19
|
+
#### `init()`
|
|
20
|
+
|
|
21
|
+
Initialize the mesh network.
|
|
22
|
+
|
|
23
|
+
```cpp
|
|
24
|
+
void init(TSTRING meshPrefix, TSTRING meshPassword, uint16_t port);
|
|
25
|
+
void init(TSTRING meshPrefix, TSTRING meshPassword, Scheduler* userScheduler, uint16_t port);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**Parameters:**
|
|
29
|
+
- `meshPrefix` - Network name (SSID prefix)
|
|
30
|
+
- `meshPassword` - Network password
|
|
31
|
+
- `userScheduler` - Optional external TaskScheduler instance
|
|
32
|
+
- `port` - TCP port for mesh communication (default: 5555)
|
|
33
|
+
|
|
34
|
+
**Example:**
|
|
35
|
+
```cpp
|
|
36
|
+
Scheduler userScheduler;
|
|
37
|
+
mesh.init("MyMesh", "password123", &userScheduler, 5555);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### `update()`
|
|
41
|
+
|
|
42
|
+
Main mesh update loop. Call this in your `loop()` function.
|
|
43
|
+
|
|
44
|
+
```cpp
|
|
45
|
+
void update();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**Example:**
|
|
49
|
+
```cpp
|
|
50
|
+
void loop() {
|
|
51
|
+
mesh.update(); // Always call this
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Message Sending
|
|
56
|
+
|
|
57
|
+
#### `sendBroadcast()`
|
|
58
|
+
|
|
59
|
+
Send a message to all nodes in the mesh.
|
|
60
|
+
|
|
61
|
+
```cpp
|
|
62
|
+
bool sendBroadcast(TSTRING msg, bool includeSelf = false);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
**Parameters:**
|
|
66
|
+
- `msg` - Message to broadcast
|
|
67
|
+
- `includeSelf` - Whether to send to self (default: false)
|
|
68
|
+
|
|
69
|
+
**Returns:** `true` if message was queued successfully
|
|
70
|
+
|
|
71
|
+
**Example:**
|
|
72
|
+
```cpp
|
|
73
|
+
String msg = "Hello everyone!";
|
|
74
|
+
bool sent = mesh.sendBroadcast(msg);
|
|
75
|
+
if (!sent) {
|
|
76
|
+
Serial.println("Failed to send broadcast");
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### `sendSingle()`
|
|
81
|
+
|
|
82
|
+
Send a message to a specific node.
|
|
83
|
+
|
|
84
|
+
```cpp
|
|
85
|
+
bool sendSingle(uint32_t destId, TSTRING msg);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Parameters:**
|
|
89
|
+
- `destId` - Target node ID
|
|
90
|
+
- `msg` - Message to send
|
|
91
|
+
|
|
92
|
+
**Returns:** `true` if message was queued successfully
|
|
93
|
+
|
|
94
|
+
**Example:**
|
|
95
|
+
```cpp
|
|
96
|
+
uint32_t targetNode = 123456789;
|
|
97
|
+
String msg = "Hello specific node!";
|
|
98
|
+
bool sent = mesh.sendSingle(targetNode, msg);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Plugin System
|
|
102
|
+
|
|
103
|
+
#### `sendPackage()`
|
|
104
|
+
|
|
105
|
+
Send a custom package through the plugin system.
|
|
106
|
+
|
|
107
|
+
```cpp
|
|
108
|
+
bool sendPackage(const protocol::PackageInterface* pkg);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Parameters:**
|
|
112
|
+
- `pkg` - Pointer to package object
|
|
113
|
+
|
|
114
|
+
**Returns:** `true` if package was sent successfully
|
|
115
|
+
|
|
116
|
+
**Example:**
|
|
117
|
+
```cpp
|
|
118
|
+
SensorPackage sensor;
|
|
119
|
+
sensor.temperature = 25.0;
|
|
120
|
+
sensor.humidity = 60.0;
|
|
121
|
+
bool sent = mesh.sendPackage(&sensor);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### `onPackage()`
|
|
125
|
+
|
|
126
|
+
Register a handler for specific package types.
|
|
127
|
+
|
|
128
|
+
```cpp
|
|
129
|
+
void onPackage(int type, std::function<bool(protocol::Variant&)> function);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**Parameters:**
|
|
133
|
+
- `type` - Package type ID to handle
|
|
134
|
+
- `function` - Handler function (return `true` to stop propagation)
|
|
135
|
+
|
|
136
|
+
**Example:**
|
|
137
|
+
```cpp
|
|
138
|
+
mesh.onPackage(SENSOR_DATA, [](protocol::Variant& variant) {
|
|
139
|
+
SensorPackage sensor = variant.to<SensorPackage>();
|
|
140
|
+
Serial.printf("Temp: %.1f°C\n", sensor.temperature);
|
|
141
|
+
return false; // Don't stop propagation
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Node Information
|
|
146
|
+
|
|
147
|
+
#### `getNodeId()`
|
|
148
|
+
|
|
149
|
+
Get this node's unique identifier.
|
|
150
|
+
|
|
151
|
+
```cpp
|
|
152
|
+
uint32_t getNodeId();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Returns:** 32-bit node ID
|
|
156
|
+
|
|
157
|
+
**Example:**
|
|
158
|
+
```cpp
|
|
159
|
+
uint32_t myId = mesh.getNodeId();
|
|
160
|
+
Serial.printf("My node ID: %u\n", myId);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### `getNodeList()`
|
|
164
|
+
|
|
165
|
+
Get list of all connected nodes.
|
|
166
|
+
|
|
167
|
+
```cpp
|
|
168
|
+
std::list<uint32_t> getNodeList();
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Returns:** List of node IDs
|
|
172
|
+
|
|
173
|
+
**Example:**
|
|
174
|
+
```cpp
|
|
175
|
+
auto nodes = mesh.getNodeList();
|
|
176
|
+
Serial.printf("Connected to %d nodes\n", nodes.size());
|
|
177
|
+
for (auto nodeId : nodes) {
|
|
178
|
+
Serial.printf(" Node: %u\n", nodeId);
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### `subConnectionJson()`
|
|
183
|
+
|
|
184
|
+
Get mesh topology as JSON string.
|
|
185
|
+
|
|
186
|
+
```cpp
|
|
187
|
+
TSTRING subConnectionJson(bool pretty = false);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
**Parameters:**
|
|
191
|
+
- `pretty` - Whether to format JSON nicely (default: false)
|
|
192
|
+
|
|
193
|
+
**Returns:** JSON representation of mesh topology
|
|
194
|
+
|
|
195
|
+
**Example:**
|
|
196
|
+
```cpp
|
|
197
|
+
String topology = mesh.subConnectionJson(true);
|
|
198
|
+
Serial.println("Mesh topology:");
|
|
199
|
+
Serial.println(topology);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Time Synchronization
|
|
203
|
+
|
|
204
|
+
#### `getNodeTime()`
|
|
205
|
+
|
|
206
|
+
Get synchronized mesh time.
|
|
207
|
+
|
|
208
|
+
```cpp
|
|
209
|
+
uint32_t getNodeTime();
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
**Returns:** Time in microseconds since mesh start
|
|
213
|
+
|
|
214
|
+
**Example:**
|
|
215
|
+
```cpp
|
|
216
|
+
uint32_t timestamp = mesh.getNodeTime();
|
|
217
|
+
uint32_t seconds = timestamp / 1000000;
|
|
218
|
+
Serial.printf("Mesh time: %u seconds\n", seconds);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Task Management
|
|
222
|
+
|
|
223
|
+
#### `addTask()`
|
|
224
|
+
|
|
225
|
+
Add a task to the mesh scheduler.
|
|
226
|
+
|
|
227
|
+
```cpp
|
|
228
|
+
// Recurring task
|
|
229
|
+
std::shared_ptr<Task> addTask(Scheduler& scheduler, unsigned long interval,
|
|
230
|
+
long iterations, std::function<void()> callback);
|
|
231
|
+
|
|
232
|
+
// One-time task
|
|
233
|
+
std::shared_ptr<Task> addTask(Scheduler& scheduler, std::function<void()> callback);
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
**Parameters:**
|
|
237
|
+
- `scheduler` - Scheduler instance
|
|
238
|
+
- `interval` - Task interval in milliseconds
|
|
239
|
+
- `iterations` - Number of iterations (`TASK_FOREVER` for infinite)
|
|
240
|
+
- `callback` - Function to execute
|
|
241
|
+
|
|
242
|
+
**Returns:** Shared pointer to task
|
|
243
|
+
|
|
244
|
+
**Example:**
|
|
245
|
+
```cpp
|
|
246
|
+
// Recurring task every 30 seconds
|
|
247
|
+
auto task = mesh.addTask(userScheduler, 30000, TASK_FOREVER, [](){
|
|
248
|
+
Serial.println("Periodic task executed");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
// One-time task
|
|
252
|
+
mesh.addTask(userScheduler, [](){
|
|
253
|
+
Serial.println("One-time task executed");
|
|
254
|
+
});
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Callbacks
|
|
258
|
+
|
|
259
|
+
#### `onReceive()`
|
|
260
|
+
|
|
261
|
+
Set callback for receiving messages.
|
|
262
|
+
|
|
263
|
+
```cpp
|
|
264
|
+
void onReceive(std::function<void(uint32_t from, String& msg)> callback);
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
**Parameters:**
|
|
268
|
+
- `callback` - Function to call when message received
|
|
269
|
+
|
|
270
|
+
**Example:**
|
|
271
|
+
```cpp
|
|
272
|
+
mesh.onReceive([](uint32_t from, String& msg) {
|
|
273
|
+
Serial.printf("Received from %u: %s\n", from, msg.c_str());
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### `onNewConnection()`
|
|
278
|
+
|
|
279
|
+
Set callback for new node connections.
|
|
280
|
+
|
|
281
|
+
```cpp
|
|
282
|
+
void onNewConnection(std::function<void(uint32_t nodeId)> callback);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
**Example:**
|
|
286
|
+
```cpp
|
|
287
|
+
mesh.onNewConnection([](uint32_t nodeId) {
|
|
288
|
+
Serial.printf("New node connected: %u\n", nodeId);
|
|
289
|
+
});
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
#### `onDroppedConnection()`
|
|
293
|
+
|
|
294
|
+
Set callback for lost connections.
|
|
295
|
+
|
|
296
|
+
```cpp
|
|
297
|
+
void onDroppedConnection(std::function<void(uint32_t nodeId)> callback);
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
**Example:**
|
|
301
|
+
```cpp
|
|
302
|
+
mesh.onDroppedConnection([](uint32_t nodeId) {
|
|
303
|
+
Serial.printf("Lost connection to: %u\n", nodeId);
|
|
304
|
+
});
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
#### `onChangedConnections()`
|
|
308
|
+
|
|
309
|
+
Set callback for topology changes.
|
|
310
|
+
|
|
311
|
+
```cpp
|
|
312
|
+
void onChangedConnections(std::function<void()> callback);
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
**Example:**
|
|
316
|
+
```cpp
|
|
317
|
+
mesh.onChangedConnections([]() {
|
|
318
|
+
auto nodes = mesh.getNodeList();
|
|
319
|
+
Serial.printf("Topology changed. Now connected to %d nodes\n", nodes.size());
|
|
320
|
+
});
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
#### `onNodeTimeAdjusted()`
|
|
324
|
+
|
|
325
|
+
Set callback for time synchronization events.
|
|
326
|
+
|
|
327
|
+
```cpp
|
|
328
|
+
void onNodeTimeAdjusted(std::function<void(int32_t offset)> callback);
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
**Parameters:**
|
|
332
|
+
- `offset` - Time adjustment in microseconds
|
|
333
|
+
|
|
334
|
+
**Example:**
|
|
335
|
+
```cpp
|
|
336
|
+
mesh.onNodeTimeAdjusted([](int32_t offset) {
|
|
337
|
+
Serial.printf("Time adjusted by %d microseconds\n", offset);
|
|
338
|
+
});
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### Network Testing
|
|
342
|
+
|
|
343
|
+
#### `startDelayMeas()`
|
|
344
|
+
|
|
345
|
+
Measure network delay to a specific node.
|
|
346
|
+
|
|
347
|
+
```cpp
|
|
348
|
+
bool startDelayMeas(uint32_t nodeId);
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
**Parameters:**
|
|
352
|
+
- `nodeId` - Target node for delay measurement
|
|
353
|
+
|
|
354
|
+
**Returns:** `true` if measurement started successfully
|
|
355
|
+
|
|
356
|
+
**Example:**
|
|
357
|
+
```cpp
|
|
358
|
+
uint32_t targetNode = 123456789;
|
|
359
|
+
if (mesh.startDelayMeas(targetNode)) {
|
|
360
|
+
Serial.println("Delay measurement started");
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
#### `onNodeDelayReceived()`
|
|
365
|
+
|
|
366
|
+
Set callback for delay measurement results.
|
|
367
|
+
|
|
368
|
+
```cpp
|
|
369
|
+
void onNodeDelayReceived(std::function<void(uint32_t nodeId, int32_t delay)> callback);
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
**Parameters:**
|
|
373
|
+
- `nodeId` - Node that was measured
|
|
374
|
+
- `delay` - Round-trip delay in microseconds
|
|
375
|
+
|
|
376
|
+
**Example:**
|
|
377
|
+
```cpp
|
|
378
|
+
mesh.onNodeDelayReceived([](uint32_t nodeId, int32_t delay) {
|
|
379
|
+
Serial.printf("Delay to node %u: %d µs\n", nodeId, delay);
|
|
380
|
+
});
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Debug and Diagnostics
|
|
384
|
+
|
|
385
|
+
#### `setDebugMsgTypes()`
|
|
386
|
+
|
|
387
|
+
Control debug message output.
|
|
388
|
+
|
|
389
|
+
```cpp
|
|
390
|
+
void setDebugMsgTypes(uint16_t types);
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
**Parameters:**
|
|
394
|
+
- `types` - Bitwise combination of debug types
|
|
395
|
+
|
|
396
|
+
**Debug Types:**
|
|
397
|
+
```cpp
|
|
398
|
+
#define ERROR 0x0001
|
|
399
|
+
#define STARTUP 0x0002
|
|
400
|
+
#define CONNECTION 0x0004
|
|
401
|
+
#define SYNC 0x0008
|
|
402
|
+
#define COMMUNICATION 0x0010
|
|
403
|
+
#define GENERAL 0x0020
|
|
404
|
+
#define MSG_TYPES 0x0040
|
|
405
|
+
#define REMOTE 0x0080
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
**Example:**
|
|
409
|
+
```cpp
|
|
410
|
+
// Enable error, startup, and connection messages
|
|
411
|
+
mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION);
|
|
412
|
+
|
|
413
|
+
// Enable all debug messages
|
|
414
|
+
mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION | SYNC |
|
|
415
|
+
COMMUNICATION | GENERAL | MSG_TYPES | REMOTE);
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Advanced Methods
|
|
419
|
+
|
|
420
|
+
#### `stop()`
|
|
421
|
+
|
|
422
|
+
Stop the mesh and clean up resources.
|
|
423
|
+
|
|
424
|
+
```cpp
|
|
425
|
+
void stop();
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
**Example:**
|
|
429
|
+
```cpp
|
|
430
|
+
void shutdown() {
|
|
431
|
+
mesh.stop();
|
|
432
|
+
Serial.println("Mesh stopped");
|
|
433
|
+
}
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
## Type Definitions
|
|
437
|
+
|
|
438
|
+
### TSTRING
|
|
439
|
+
|
|
440
|
+
Cross-platform string type that works on both ESP32/ESP8266 and desktop platforms.
|
|
441
|
+
|
|
442
|
+
```cpp
|
|
443
|
+
#if defined(ESP32) || defined(ESP8266)
|
|
444
|
+
typedef String TSTRING;
|
|
445
|
+
#else
|
|
446
|
+
typedef std::string TSTRING;
|
|
447
|
+
#endif
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### Constants
|
|
451
|
+
|
|
452
|
+
```cpp
|
|
453
|
+
// Task scheduler constants
|
|
454
|
+
#define TASK_FOREVER -1
|
|
455
|
+
#define TASK_ONCE 1
|
|
456
|
+
|
|
457
|
+
// Common intervals
|
|
458
|
+
#define TASK_SECOND 1000
|
|
459
|
+
#define TASK_MINUTE (60 * TASK_SECOND)
|
|
460
|
+
#define TASK_HOUR (60 * TASK_MINUTE)
|
|
461
|
+
|
|
462
|
+
// Default values
|
|
463
|
+
#define MESH_DEFAULT_PORT 5555
|
|
464
|
+
#define MAX_CONNECTIONS 4 // Platform dependent
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
## Usage Patterns
|
|
468
|
+
|
|
469
|
+
### Basic Setup Pattern
|
|
470
|
+
|
|
471
|
+
```cpp
|
|
472
|
+
#include "painlessMesh.h"
|
|
473
|
+
|
|
474
|
+
#define MESH_PREFIX "MyMeshNetwork"
|
|
475
|
+
#define MESH_PASSWORD "secretPassword"
|
|
476
|
+
#define MESH_PORT 5555
|
|
477
|
+
|
|
478
|
+
Scheduler userScheduler;
|
|
479
|
+
painlessMesh mesh;
|
|
480
|
+
|
|
481
|
+
void setup() {
|
|
482
|
+
Serial.begin(115200);
|
|
483
|
+
|
|
484
|
+
// Set up callbacks
|
|
485
|
+
mesh.onReceive(&receivedCallback);
|
|
486
|
+
mesh.onNewConnection(&newConnectionCallback);
|
|
487
|
+
mesh.onChangedConnections(&changedConnectionCallback);
|
|
488
|
+
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
|
|
489
|
+
|
|
490
|
+
// Initialize mesh
|
|
491
|
+
mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
|
|
492
|
+
|
|
493
|
+
// Set debug level
|
|
494
|
+
mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
void loop() {
|
|
498
|
+
mesh.update();
|
|
499
|
+
}
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### Message Handling Pattern
|
|
503
|
+
|
|
504
|
+
```cpp
|
|
505
|
+
void receivedCallback(uint32_t from, String& msg) {
|
|
506
|
+
Serial.printf("Received from %u: %s\n", from, msg.c_str());
|
|
507
|
+
|
|
508
|
+
// Parse JSON if needed
|
|
509
|
+
DynamicJsonDocument doc(1024);
|
|
510
|
+
deserializeJson(doc, msg);
|
|
511
|
+
|
|
512
|
+
if (doc["type"] == "sensor") {
|
|
513
|
+
handleSensorData(doc);
|
|
514
|
+
} else if (doc["type"] == "command") {
|
|
515
|
+
handleCommand(doc);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
### Plugin Usage Pattern
|
|
521
|
+
|
|
522
|
+
```cpp
|
|
523
|
+
// Define custom package
|
|
524
|
+
class MyPackage : public painlessmesh::plugin::BroadcastPackage {
|
|
525
|
+
public:
|
|
526
|
+
float value = 0.0;
|
|
527
|
+
|
|
528
|
+
MyPackage() : BroadcastPackage(100) {} // Unique type ID
|
|
529
|
+
|
|
530
|
+
MyPackage(JsonObject jsonObj) : BroadcastPackage(jsonObj) {
|
|
531
|
+
value = jsonObj["value"];
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
JsonObject addTo(JsonObject&& jsonObj) const override {
|
|
535
|
+
jsonObj = BroadcastPackage::addTo(std::move(jsonObj));
|
|
536
|
+
jsonObj["value"] = value;
|
|
537
|
+
return jsonObj;
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// Register handler
|
|
542
|
+
mesh.onPackage(100, [](protocol::Variant& variant) {
|
|
543
|
+
MyPackage pkg = variant.to<MyPackage>();
|
|
544
|
+
Serial.printf("Received value: %.2f\n", pkg.value);
|
|
545
|
+
return false;
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
// Send package
|
|
549
|
+
MyPackage pkg;
|
|
550
|
+
pkg.value = 42.5;
|
|
551
|
+
mesh.sendPackage(&pkg);
|
|
552
|
+
```
|
|
553
|
+
|
|
554
|
+
## Error Handling
|
|
555
|
+
|
|
556
|
+
### Return Value Checking
|
|
557
|
+
|
|
558
|
+
```cpp
|
|
559
|
+
// Always check return values
|
|
560
|
+
if (!mesh.sendBroadcast(message)) {
|
|
561
|
+
Serial.println("Failed to send broadcast - check connections");
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (!mesh.sendSingle(nodeId, message)) {
|
|
565
|
+
Serial.println("Failed to send to specific node - node may be disconnected");
|
|
566
|
+
}
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
### Connection State Monitoring
|
|
570
|
+
|
|
571
|
+
```cpp
|
|
572
|
+
void monitorConnections() {
|
|
573
|
+
auto nodes = mesh.getNodeList();
|
|
574
|
+
if (nodes.empty()) {
|
|
575
|
+
Serial.println("Warning: No mesh connections!");
|
|
576
|
+
} else {
|
|
577
|
+
Serial.printf("Connected to %d nodes\n", nodes.size());
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
## Performance Considerations
|
|
583
|
+
|
|
584
|
+
### Memory Management
|
|
585
|
+
|
|
586
|
+
- Use `TSTRING::reserve()` for strings that will grow
|
|
587
|
+
- Implement `jsonObjectSize()` accurately in custom packages
|
|
588
|
+
- Monitor heap usage on ESP8266 (limited to ~80KB)
|
|
589
|
+
|
|
590
|
+
### Message Optimization
|
|
591
|
+
|
|
592
|
+
- Keep messages small to reduce network overhead
|
|
593
|
+
- Use binary data in JSON strings for large data
|
|
594
|
+
- Batch multiple values in single messages when possible
|
|
595
|
+
|
|
596
|
+
### Connection Limits
|
|
597
|
+
|
|
598
|
+
- ESP8266: Typically 2-4 connections maximum
|
|
599
|
+
- ESP32: Can handle 4-10 connections depending on memory
|
|
600
|
+
- Monitor connection count and implement backoff if needed
|
|
601
|
+
|
|
602
|
+
## Next Steps
|
|
603
|
+
|
|
604
|
+
- Explore [Plugin API](plugin-api.md) for advanced package handling
|
|
605
|
+
- Learn about [Configuration](configuration.md) options
|
|
606
|
+
- See [Callbacks](callbacks.md) for detailed event handling
|
|
607
|
+
- Check [Performance Optimization](../advanced/performance.md) guide
|