@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.
Files changed (80) hide show
  1. package/CHANGELOG.md +144 -0
  2. package/LICENSE +674 -0
  3. package/README.md +434 -0
  4. package/RELEASE_GUIDE.md +419 -0
  5. package/docs/DOCUMENTATION_MIGRATION_PLAN.md +176 -0
  6. package/docs/README.md +71 -0
  7. package/docs/alteriom/overview.md +508 -0
  8. package/docs/api/core-api.md +607 -0
  9. package/docs/architecture/mesh-architecture.md +379 -0
  10. package/docs/architecture/plugin-system.md +517 -0
  11. package/docs/getting-started/first-mesh.md +410 -0
  12. package/docs/getting-started/installation.md +275 -0
  13. package/docs/getting-started/quickstart.md +158 -0
  14. package/docs/improvements/README.md +69 -0
  15. package/docs/troubleshooting/common-issues.md +521 -0
  16. package/docs/troubleshooting/faq.md +473 -0
  17. package/docs/tutorials/basic-examples.md +718 -0
  18. package/docs/wiki/API-Reference.md +246 -0
  19. package/docs/wiki/Complete-Documentation.md +123 -0
  20. package/examples/alteriom/README.md +82 -0
  21. package/examples/alteriom/alteriom.ino +186 -0
  22. package/examples/alteriom/alteriom_sensor_node.ino +184 -0
  23. package/examples/alteriom/alteriom_sensor_package.hpp +128 -0
  24. package/examples/alteriom/improved_sensor_node.ino +246 -0
  25. package/examples/alteriom/platformio.ini +25 -0
  26. package/examples/basic/basic.ino +66 -0
  27. package/examples/basic/platformio.ini +25 -0
  28. package/examples/bridge/bridge.ino +51 -0
  29. package/examples/bridge/platformio.ini +25 -0
  30. package/examples/echoNode/echoNode.ino +33 -0
  31. package/examples/echoNode/platformio.ini +25 -0
  32. package/examples/logClient/logClient.ino +109 -0
  33. package/examples/logClient/platformio.ini +25 -0
  34. package/examples/logServer/logServer.ino +81 -0
  35. package/examples/logServer/platformio.ini +25 -0
  36. package/examples/mqttBridge/mqttBridge.ino +118 -0
  37. package/examples/mqttBridge/platformio.ini +26 -0
  38. package/examples/namedMesh/namedMesh.ino +97 -0
  39. package/examples/namedMesh/platformio.ini +25 -0
  40. package/examples/otaReceiver/otaReceiver.ino +79 -0
  41. package/examples/otaReceiver/platformio.ini +25 -0
  42. package/examples/otaSender/nodemcu32s_connections.JPG +0 -0
  43. package/examples/otaSender/otaSender.ino +151 -0
  44. package/examples/otaSender/platformio.ini +25 -0
  45. package/examples/startHere/platformio.ini +25 -0
  46. package/examples/startHere/startHere.ino +159 -0
  47. package/examples/webServer/platformio.ini +27 -0
  48. package/examples/webServer/webServer.ino +89 -0
  49. package/keywords.txt +49 -0
  50. package/library.json +34 -0
  51. package/library.properties +11 -0
  52. package/package.json +78 -0
  53. package/src/AlteriomPainlessMesh.h +98 -0
  54. package/src/arduino/wifi.hpp +365 -0
  55. package/src/boost/asynctcp.hpp +279 -0
  56. package/src/painlessMesh.h +70 -0
  57. package/src/painlessMeshSTA.cpp +236 -0
  58. package/src/painlessMeshSTA.h +58 -0
  59. package/src/painlessTaskOptions.h +4 -0
  60. package/src/painlessmesh/base64.hpp +111 -0
  61. package/src/painlessmesh/buffer.hpp +229 -0
  62. package/src/painlessmesh/callback.hpp +91 -0
  63. package/src/painlessmesh/configuration.hpp +77 -0
  64. package/src/painlessmesh/connection.hpp +192 -0
  65. package/src/painlessmesh/layout.hpp +188 -0
  66. package/src/painlessmesh/logger.hpp +158 -0
  67. package/src/painlessmesh/memory.hpp +120 -0
  68. package/src/painlessmesh/mesh.hpp +560 -0
  69. package/src/painlessmesh/metrics.hpp +323 -0
  70. package/src/painlessmesh/ntp.hpp +263 -0
  71. package/src/painlessmesh/ota.hpp +553 -0
  72. package/src/painlessmesh/plugin.hpp +188 -0
  73. package/src/painlessmesh/protocol.hpp +813 -0
  74. package/src/painlessmesh/router.hpp +322 -0
  75. package/src/painlessmesh/tcp.hpp +71 -0
  76. package/src/painlessmesh/validation.hpp +239 -0
  77. package/src/plugin/performance.hpp +214 -0
  78. package/src/plugin/remote.hpp +64 -0
  79. package/src/scheduler.cpp +10 -0
  80. package/src/wifi.cpp +2 -0
@@ -0,0 +1,379 @@
1
+ # Mesh Architecture
2
+
3
+ This document explains how painlessMesh works internally, its design principles, and architectural decisions.
4
+
5
+ ## Overview
6
+
7
+ painlessMesh creates a self-organizing, self-healing wireless mesh network using ESP8266 and ESP32 devices. The mesh automatically handles:
8
+
9
+ - **Node Discovery**: Devices find each other automatically
10
+ - **Routing**: Messages find optimal paths through the network
11
+ - **Topology Management**: Network adapts to nodes joining/leaving
12
+ - **Time Synchronization**: All nodes maintain synchronized clocks
13
+ - **Connection Management**: Automatic reconnection and load balancing
14
+
15
+ ## Core Architecture
16
+
17
+ ```
18
+ ┌─────────────────────────────────────────────────────────────┐
19
+ │ Application Layer │
20
+ ├─────────────────────────────────────────────────────────────┤
21
+ │ Plugin System │
22
+ ├─────────────────────────────────────────────────────────────┤
23
+ │ Mesh Layer │
24
+ ├─────────────────────────────────────────────────────────────┤
25
+ │ Protocol Layer │
26
+ ├─────────────────────────────────────────────────────────────┤
27
+ │ Network Layer (TCP) │
28
+ ├─────────────────────────────────────────────────────────────┤
29
+ │ WiFi Layer (ESP32/ESP8266) │
30
+ └─────────────────────────────────────────────────────────────┘
31
+ ```
32
+
33
+ ### Layer Responsibilities
34
+
35
+ **Application Layer**
36
+ - User code and application logic
37
+ - Custom message handling
38
+ - Business logic implementation
39
+
40
+ **Plugin System**
41
+ - Type-safe message packaging
42
+ - Custom package types (sensor data, commands, etc.)
43
+ - Message serialization/deserialization
44
+
45
+ **Mesh Layer**
46
+ - Node discovery and connection management
47
+ - Mesh topology maintenance
48
+ - Time synchronization across nodes
49
+
50
+ **Protocol Layer**
51
+ - Message routing algorithms
52
+ - Package type identification
53
+ - Connection lifecycle management
54
+
55
+ **Network Layer**
56
+ - TCP connection handling
57
+ - Message queuing and transmission
58
+ - Connection multiplexing
59
+
60
+ **WiFi Layer**
61
+ - Low-level ESP32/ESP8266 WiFi management
62
+ - Access point and station mode handling
63
+ - Radio frequency management
64
+
65
+ ## Network Topology
66
+
67
+ painlessMesh creates a **dynamic tree topology** that automatically reorganizes based on network conditions.
68
+
69
+ ### Tree Structure
70
+
71
+ ```
72
+ Root Node
73
+ / | \
74
+ Node1 Node2 Node3
75
+ / \ |
76
+ Node4 Node5 Node6
77
+ ```
78
+
79
+ ### Key Properties
80
+
81
+ - **No Single Point of Failure**: If any node fails, the mesh reorganizes
82
+ - **Shortest Path Routing**: Messages take optimal routes to destinations
83
+ - **Load Distribution**: Connections balanced across available nodes
84
+ - **Scalable**: Can handle dozens of nodes (limited by ESP memory)
85
+
86
+ ### Node Roles
87
+
88
+ **Root Node**
89
+ - Acts as the network coordinator
90
+ - Maintains network topology information
91
+ - Can be any node in the network
92
+ - Role automatically transfers if root fails
93
+
94
+ **Intermediate Nodes**
95
+ - Forward messages between other nodes
96
+ - Maintain connections to parent and children
97
+ - Participate in topology discovery
98
+
99
+ **Leaf Nodes**
100
+ - End devices with minimal connections
101
+ - Typically sensor or actuator nodes
102
+ - Minimal memory and processing overhead
103
+
104
+ ## Message Routing
105
+
106
+ painlessMesh implements multiple routing strategies:
107
+
108
+ ### 1. Broadcast Routing
109
+ Messages sent to all nodes in the network.
110
+
111
+ ```cpp
112
+ // Sender
113
+ mesh.sendBroadcast("Hello everyone!");
114
+
115
+ // All nodes receive the message
116
+ void receivedCallback(uint32_t from, String &msg) {
117
+ // Handle broadcast message
118
+ }
119
+ ```
120
+
121
+ **Algorithm**:
122
+ 1. Message originates at source node
123
+ 2. Each node forwards to all connected neighbors (except sender)
124
+ 3. Duplicate detection prevents loops
125
+ 4. Message reaches all nodes in network
126
+
127
+ ### 2. Single Node Routing
128
+ Messages sent to a specific node by ID.
129
+
130
+ ```cpp
131
+ // Send to specific node
132
+ mesh.sendSingle(targetNodeId, "Hello specific node!");
133
+ ```
134
+
135
+ **Algorithm**:
136
+ 1. Source looks up target in routing table
137
+ 2. Message forwarded to next hop toward target
138
+ 3. Each intermediate node repeats until target reached
139
+ 4. Automatic route discovery if target unknown
140
+
141
+ ### 3. Neighbor Routing
142
+ Messages sent only to directly connected neighbors.
143
+
144
+ ```cpp
145
+ // Plugin system example
146
+ NeighbourPackage pkg;
147
+ mesh.sendPackage(&pkg);
148
+ ```
149
+
150
+ **Use Cases**:
151
+ - Topology discovery
152
+ - Local status updates
153
+ - Connection management
154
+
155
+ ## Connection Management
156
+
157
+ ### Connection Lifecycle
158
+
159
+ ```
160
+ Disconnected → Discovering → Connecting → Connected → Disconnected
161
+ ↑ ↓
162
+ └────────── Connection Lost ←───────────┘
163
+ ```
164
+
165
+ ### Discovery Process
166
+
167
+ 1. **Scan Phase**: Node scans for available mesh networks
168
+ 2. **Evaluation Phase**: Evaluates signal strength and load
169
+ 3. **Connection Phase**: Establishes TCP connection
170
+ 4. **Handshake Phase**: Exchanges node information
171
+ 5. **Integration Phase**: Updates routing tables
172
+
173
+ ### Connection Parameters
174
+
175
+ ```cpp
176
+ // Maximum connections per node
177
+ #define MAX_CONN 4
178
+
179
+ // Connection timeout
180
+ #define CONNECTION_TIMEOUT 30000
181
+
182
+ // Reconnection interval
183
+ #define RECONNECT_INTERVAL 10000
184
+ ```
185
+
186
+ ### Load Balancing
187
+
188
+ - Nodes prefer connections with fewer existing connections
189
+ - Signal strength influences connection preference
190
+ - Automatic connection redistribution when topology changes
191
+
192
+ ## Time Synchronization
193
+
194
+ painlessMesh maintains synchronized time across all nodes using a distributed algorithm.
195
+
196
+ ### Synchronization Process
197
+
198
+ 1. **Root Election**: Node with most connections becomes time root
199
+ 2. **Time Distribution**: Root broadcasts its time periodically
200
+ 3. **Offset Calculation**: Each node calculates offset from root
201
+ 4. **Propagation**: Time updates propagate through mesh hierarchy
202
+
203
+ ### Time API
204
+
205
+ ```cpp
206
+ // Get synchronized mesh time
207
+ uint32_t meshTime = mesh.getNodeTime();
208
+
209
+ // Convert to milliseconds since startup
210
+ uint32_t meshTimeMs = meshTime / 1000;
211
+
212
+ // Time adjustment callback
213
+ void nodeTimeAdjustedCallback(int32_t offset) {
214
+ Serial.printf("Time adjusted by %d microseconds\n", offset);
215
+ }
216
+ ```
217
+
218
+ ### Use Cases
219
+
220
+ - **Synchronized Actions**: All nodes can execute actions at same time
221
+ - **Data Timestamping**: Consistent timestamps across sensors
222
+ - **Event Correlation**: Match events across different nodes
223
+ - **Scheduling**: Coordinate scheduled tasks
224
+
225
+ ## Memory Management
226
+
227
+ ### Memory Layout (ESP32)
228
+
229
+ ```
230
+ ┌─────────────────────┐ 320KB Total RAM
231
+ │ Application │
232
+ ├─────────────────────┤
233
+ │ Mesh Buffers │ ~50-100KB
234
+ ├─────────────────────┤
235
+ │ TCP Buffers │ ~20-40KB
236
+ ├─────────────────────┤
237
+ │ WiFi Stack │ ~30-50KB
238
+ ├─────────────────────┤
239
+ │ System/FreeRTOS │ ~50-100KB
240
+ └─────────────────────┘
241
+ ```
242
+
243
+ ### Memory Optimization
244
+
245
+ **Message Queuing**
246
+ - Outbound message queue per connection
247
+ - Configurable queue sizes
248
+ - Automatic message dropping under memory pressure
249
+
250
+ **Connection Limits**
251
+ - Maximum connections limit prevents memory exhaustion
252
+ - Dynamic connection management based on available memory
253
+
254
+ **Buffer Management**
255
+ - Shared buffer pools for message processing
256
+ - Automatic garbage collection of unused buffers
257
+
258
+ ### ESP8266 Considerations
259
+
260
+ - **Limited RAM**: ~80KB total, requires careful memory management
261
+ - **Fewer Connections**: Typically 2-4 max connections
262
+ - **Smaller Buffers**: Reduced message queue sizes
263
+ - **Conservative Limits**: More aggressive connection limits
264
+
265
+ ## Error Handling
266
+
267
+ ### Connection Failures
268
+
269
+ ```cpp
270
+ void droppedConnectionCallback(uint32_t nodeId) {
271
+ Serial.printf("Lost connection to node %u\n", nodeId);
272
+ // Mesh automatically attempts reconnection
273
+ }
274
+ ```
275
+
276
+ ### Message Delivery
277
+
278
+ - **Best Effort**: No guaranteed delivery by default
279
+ - **Automatic Retry**: Connection-level retries for TCP
280
+ - **Route Recovery**: Automatic routing table updates
281
+ - **Graceful Degradation**: Network continues with reduced connectivity
282
+
283
+ ### Network Partitions
284
+
285
+ When the mesh splits into separate networks:
286
+
287
+ 1. **Detection**: Nodes detect missing connections
288
+ 2. **Recovery Attempts**: Try to reconnect to lost nodes
289
+ 3. **Partition Handling**: Each partition operates independently
290
+ 4. **Reunification**: Automatic merge when connectivity restored
291
+
292
+ ## Performance Characteristics
293
+
294
+ ### Throughput
295
+
296
+ - **Per Connection**: ~100-500 KB/s depending on ESP model
297
+ - **Network Wide**: Scales with number of parallel paths
298
+ - **Bottlenecks**: Root node can become bottleneck in star topology
299
+
300
+ ### Latency
301
+
302
+ - **Direct Connection**: 10-50ms typical
303
+ - **Multi-hop**: +20-50ms per hop
304
+ - **Factors**: Network load, message size, ESP processing speed
305
+
306
+ ### Scalability
307
+
308
+ - **Node Count**: 10-50 nodes typical (limited by memory and connections)
309
+ - **Network Diameter**: 5-7 hops maximum recommended
310
+ - **Geographic Range**: 50-200m typical WiFi range
311
+
312
+ ## Design Decisions
313
+
314
+ ### Why Tree Topology?
315
+
316
+ **Advantages**:
317
+ - Simple routing algorithms
318
+ - No loops (prevents broadcast storms)
319
+ - Efficient bandwidth utilization
320
+ - Predictable behavior
321
+
322
+ **Trade-offs**:
323
+ - Single points of failure (mitigated by automatic reorganization)
324
+ - Not optimal for all traffic patterns
325
+ - Root node may become bottleneck
326
+
327
+ ### Why TCP?
328
+
329
+ **Advantages**:
330
+ - Reliable delivery within connections
331
+ - Flow control prevents buffer overflow
332
+ - Standard protocol with good tools
333
+
334
+ **Trade-offs**:
335
+ - Higher overhead than UDP
336
+ - Connection setup latency
337
+ - Memory overhead for connection state
338
+
339
+ ### Why JSON Messages?
340
+
341
+ **Advantages**:
342
+ - Human-readable debugging
343
+ - Flexible schema evolution
344
+ - Good library support
345
+ - Cross-platform compatibility
346
+
347
+ **Trade-offs**:
348
+ - Higher bandwidth usage than binary
349
+ - Parsing overhead
350
+ - Larger memory footprint
351
+
352
+ ## Security Considerations
353
+
354
+ ### Current Security
355
+
356
+ - **Network Password**: Simple WPA2 network password
357
+ - **No Message Encryption**: Messages sent in plaintext over WiFi
358
+ - **No Authentication**: Nodes with correct password can join
359
+
360
+ ### Security Limitations
361
+
362
+ - Vulnerable to network sniffing
363
+ - No node authentication beyond network password
364
+ - No message integrity verification
365
+ - Susceptible to man-in-the-middle attacks
366
+
367
+ ### Future Enhancements
368
+
369
+ - Message-level encryption
370
+ - Node certificates and authentication
371
+ - Perfect forward secrecy
372
+ - Secure key distribution
373
+
374
+ ## Next Steps
375
+
376
+ - Learn about the [Plugin System](plugin-system.md)
377
+ - Understand [Message Routing](routing.md) in detail
378
+ - Explore [Time Synchronization](time-sync.md) mechanisms
379
+ - See [Performance Optimization](../advanced/performance.md) techniques