@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,246 @@
1
+ # API Reference
2
+
3
+ This page contains the complete API reference for the Alteriom painlessMesh Library.
4
+
5
+ ## Core Classes
6
+
7
+ ### painlessMesh
8
+
9
+ The main class for creating and managing mesh networks.
10
+
11
+ #### Initialization Methods
12
+
13
+ ```cpp
14
+ void init(String ssid, String password, Scheduler *baseScheduler, uint16_t port = 5555);
15
+ void setDebugMsgTypes(uint16_t types);
16
+ ```
17
+
18
+ #### Network Management
19
+
20
+ ```cpp
21
+ void update(); // Call in loop()
22
+ bool sendBroadcast(String &msg); // Send to all nodes
23
+ bool sendSingle(uint32_t &destId, String &msg); // Send to specific node
24
+ ```
25
+
26
+ #### Event Callbacks
27
+
28
+ ```cpp
29
+ void onReceive(std::function<void(uint32_t from, String &msg)> cb);
30
+ void onNewConnection(std::function<void(uint32_t nodeId)> cb);
31
+ void onChangedConnections(std::function<void()> cb);
32
+ void onNodeTimeAdjusted(std::function<void(int32_t offset)> cb);
33
+ ```
34
+
35
+ #### Node Information
36
+
37
+ ```cpp
38
+ uint32_t getNodeId(); // Get this node's ID
39
+ std::list<uint32_t> getNodeList(); // Get all connected nodes
40
+ uint32_t getNodeTime(); // Get synchronized time
41
+ size_t connectionCount(); // Number of direct connections
42
+ ```
43
+
44
+ ## Alteriom Extensions
45
+
46
+ ### SensorPackage (Type 200)
47
+
48
+ Environmental data collection package for sensor networks.
49
+
50
+ ```cpp
51
+ class SensorPackage : public painlessmesh::plugin::BroadcastPackage {
52
+ public:
53
+ uint32_t sensorId = 0; // Unique sensor identifier
54
+ uint32_t timestamp = 0; // Unix timestamp
55
+ double temperature = 0.0; // Temperature in Celsius
56
+ double humidity = 0.0; // Relative humidity (0-100%)
57
+ double pressure = 0.0; // Atmospheric pressure in hPa
58
+ uint8_t batteryLevel = 0; // Battery level (0-100%)
59
+
60
+ // Constructors
61
+ SensorPackage();
62
+ SensorPackage(JsonObject jsonObj);
63
+
64
+ // Serialization
65
+ JsonObject addTo(JsonObject&& jsonObj) const;
66
+ size_t jsonObjectSize() const;
67
+ };
68
+ ```
69
+
70
+ **Usage Example:**
71
+ ```cpp
72
+ SensorPackage sensor;
73
+ sensor.sensorId = mesh.getNodeId();
74
+ sensor.timestamp = mesh.getNodeTime();
75
+ sensor.temperature = 23.5;
76
+ sensor.humidity = 65.0;
77
+ sensor.pressure = 1013.25;
78
+ sensor.batteryLevel = 85;
79
+
80
+ // Send as broadcast
81
+ auto variant = painlessmesh::protocol::Variant(&sensor);
82
+ String message;
83
+ variant.printTo(message);
84
+ mesh.sendBroadcast(message);
85
+ ```
86
+
87
+ ### CommandPackage (Type 201)
88
+
89
+ Device control and automation commands for remote node management.
90
+
91
+ ```cpp
92
+ class CommandPackage : public painlessmesh::plugin::SinglePackage {
93
+ public:
94
+ uint8_t command = 0; // Command type identifier
95
+ uint32_t targetDevice = 0; // Target node ID
96
+ uint32_t commandId = 0; // Unique command identifier
97
+ TSTRING parameters = ""; // JSON parameters string
98
+
99
+ // Constructors
100
+ CommandPackage();
101
+ CommandPackage(JsonObject jsonObj);
102
+
103
+ // Serialization
104
+ JsonObject addTo(JsonObject&& jsonObj) const;
105
+ size_t jsonObjectSize() const;
106
+ };
107
+ ```
108
+
109
+ **Usage Example:**
110
+ ```cpp
111
+ CommandPackage cmd;
112
+ cmd.dest = targetNodeId;
113
+ cmd.command = 1; // LED control
114
+ cmd.targetDevice = targetNodeId;
115
+ cmd.commandId = millis();
116
+ cmd.parameters = "{\"state\":\"ON\",\"brightness\":75}";
117
+
118
+ // Send to specific node
119
+ auto variant = painlessmesh::protocol::Variant(&cmd);
120
+ String message;
121
+ variant.printTo(message);
122
+ mesh.sendSingle(targetNodeId, message);
123
+ ```
124
+
125
+ ### StatusPackage (Type 202)
126
+
127
+ Health monitoring and system status reporting for network diagnostics.
128
+
129
+ ```cpp
130
+ class StatusPackage : public painlessmesh::plugin::BroadcastPackage {
131
+ public:
132
+ uint8_t deviceStatus = 0; // Device status flags
133
+ uint32_t uptime = 0; // Uptime in seconds
134
+ uint16_t freeMemory = 0; // Free memory in KB
135
+ uint8_t wifiStrength = 0; // WiFi signal strength (0-100)
136
+ TSTRING firmwareVersion = ""; // Firmware version string
137
+
138
+ // Constructors
139
+ StatusPackage();
140
+ StatusPackage(JsonObject jsonObj);
141
+
142
+ // Serialization
143
+ JsonObject addTo(JsonObject&& jsonObj) const;
144
+ size_t jsonObjectSize() const;
145
+ };
146
+ ```
147
+
148
+ **Usage Example:**
149
+ ```cpp
150
+ StatusPackage status;
151
+ status.deviceStatus = 0x01; // Operational
152
+ status.uptime = millis() / 1000;
153
+ status.freeMemory = ESP.getFreeHeap() / 1024;
154
+ status.wifiStrength = 75;
155
+ status.firmwareVersion = "1.6.1-alteriom";
156
+
157
+ // Broadcast status
158
+ auto variant = painlessmesh::protocol::Variant(&status);
159
+ String message;
160
+ variant.printTo(message);
161
+ mesh.sendBroadcast(message);
162
+ ```
163
+
164
+ ## Message Handling
165
+
166
+ ### Receiving Messages
167
+
168
+ ```cpp
169
+ void receivedCallback(uint32_t from, String& msg) {
170
+ DynamicJsonDocument doc(1024);
171
+ deserializeJson(doc, msg);
172
+ JsonObject obj = doc.as<JsonObject>();
173
+ uint8_t msgType = obj["type"];
174
+
175
+ switch(msgType) {
176
+ case 200: // SensorPackage
177
+ handleSensorData(alteriom::SensorPackage(obj), from);
178
+ break;
179
+ case 201: // CommandPackage
180
+ handleCommand(alteriom::CommandPackage(obj), from);
181
+ break;
182
+ case 202: // StatusPackage
183
+ handleStatus(alteriom::StatusPackage(obj), from);
184
+ break;
185
+ default:
186
+ Serial.printf("Unknown message type: %d\n", msgType);
187
+ }
188
+ }
189
+ ```
190
+
191
+ ### Debug Message Types
192
+
193
+ ```cpp
194
+ // Debug levels (can be combined with |)
195
+ #define ERROR 0x00000001
196
+ #define STARTUP 0x00000002
197
+ #define CONNECTION 0x00000004
198
+ #define SYNC 0x00000008
199
+ #define COMMUNICATION 0x00000010
200
+ #define GENERAL 0x00000020
201
+ #define MSG_TYPES 0x00000040
202
+ #define REMOTE 0x00000080
203
+
204
+ // Usage
205
+ mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION);
206
+ ```
207
+
208
+ ## Platform Constants
209
+
210
+ ### Memory Constraints
211
+ - **ESP32**: ~320KB RAM available
212
+ - **ESP8266**: ~80KB RAM available
213
+ - **MAX_CONN**: Typically 4-10 connections depending on platform
214
+
215
+ ### Network Limits
216
+ - **Message Size**: Recommended max 4KB
217
+ - **String Length**: Recommended max 256 characters for compatibility
218
+ - **Network Delay**: ~50-200ms typical mesh routing delay
219
+
220
+ ## Error Handling
221
+
222
+ ### Common Return Values
223
+ - **`true`**: Operation successful
224
+ - **`false`**: Operation failed (check network/memory)
225
+
226
+ ### Best Practices
227
+ ```cpp
228
+ // Always check return values
229
+ if (!mesh.sendBroadcast(message)) {
230
+ Serial.println("Failed to send message");
231
+ // Implement retry logic or error handling
232
+ }
233
+
234
+ // Monitor connection changes
235
+ void changedConnectionCallback() {
236
+ auto nodeList = mesh.getNodeList();
237
+ Serial.printf("Network size: %d nodes\n", nodeList.size() + 1);
238
+ }
239
+ ```
240
+
241
+ ## See Also
242
+
243
+ - [Installation Guide](Installation)
244
+ - [Examples](Examples)
245
+ - [GitHub Repository](https://github.com/Alteriom/painlessMesh)
246
+ - [Release Notes](https://github.com/Alteriom/painlessMesh/releases)
@@ -0,0 +1,123 @@
1
+ # Complete Documentation
2
+
3
+ Welcome to the complete documentation for Alteriom painlessMesh Library!
4
+
5
+ ## 📖 Documentation Sections
6
+
7
+ ### Getting Started
8
+ - [Installation Guide](Installation) - Set up the library in your development environment
9
+ - [Quick Start](https://github.com/Alteriom/painlessMesh/blob/main/docs/getting-started/quickstart.md) - Your first mesh network
10
+ - [First Mesh Tutorial](https://github.com/Alteriom/painlessMesh/blob/main/docs/getting-started/first-mesh.md) - Step-by-step guide
11
+
12
+ ### API Documentation
13
+ - [API Reference](API-Reference) - Complete class and method documentation
14
+ - [Core API](https://github.com/Alteriom/painlessMesh/blob/main/docs/api/core-api.md) - painlessMesh core functionality
15
+ - [Plugin System](https://github.com/Alteriom/painlessMesh/blob/main/docs/architecture/plugin-system.md) - Extensible architecture
16
+
17
+ ### Examples and Tutorials
18
+ - [Examples](Examples) - Working code examples
19
+ - [Basic Examples](https://github.com/Alteriom/painlessMesh/blob/main/docs/tutorials/basic-examples.md) - Fundamental usage patterns
20
+ - [Alteriom Extensions](https://github.com/Alteriom/painlessMesh/blob/main/docs/alteriom/overview.md) - SensorPackage, CommandPackage, StatusPackage
21
+
22
+ ### Architecture
23
+ - [Mesh Architecture](https://github.com/Alteriom/painlessMesh/blob/main/docs/architecture/mesh-architecture.md) - How the mesh network works
24
+ - [Plugin System](https://github.com/Alteriom/painlessMesh/blob/main/docs/architecture/plugin-system.md) - Extensible package system
25
+
26
+ ### Troubleshooting
27
+ - [FAQ](https://github.com/Alteriom/painlessMesh/blob/main/docs/troubleshooting/faq.md) - Frequently asked questions
28
+ - [Common Issues](https://github.com/Alteriom/painlessMesh/blob/main/docs/troubleshooting/common-issues.md) - Solutions to common problems
29
+
30
+ ### Development
31
+ - [Contributing Guidelines](Contributing) - How to contribute to the project
32
+ - [Release Guide](https://github.com/Alteriom/painlessMesh/blob/main/RELEASE_GUIDE.md) - Release process documentation
33
+
34
+ ## 🚀 Quick Links
35
+
36
+ ### Installation
37
+ ```bash
38
+ # Arduino Library Manager
39
+ Tools → Manage Libraries → Search "Alteriom PainlessMesh"
40
+
41
+ # PlatformIO
42
+ lib_deps = alteriom/painlessMesh@^1.6.1
43
+
44
+ # NPM
45
+ npm install @alteriom/painlessmesh
46
+ ```
47
+
48
+ ### Basic Usage
49
+ ```cpp
50
+ #include "painlessMesh.h"
51
+
52
+ #define MESH_PREFIX "YourMeshName"
53
+ #define MESH_PASSWORD "YourPassword"
54
+ #define MESH_PORT 5555
55
+
56
+ Scheduler userScheduler;
57
+ painlessMesh mesh;
58
+
59
+ void setup() {
60
+ Serial.begin(115200);
61
+ mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
62
+ mesh.onReceive([](uint32_t from, String& msg) {
63
+ Serial.printf("Received: %s from %u\n", msg.c_str(), from);
64
+ });
65
+ }
66
+
67
+ void loop() {
68
+ mesh.update();
69
+ }
70
+ ```
71
+
72
+ ## 📦 Package Types
73
+
74
+ ### SensorPackage (Type 200)
75
+ For environmental sensor data collection:
76
+ - Temperature, humidity, pressure measurements
77
+ - Battery level monitoring
78
+ - Timestamp synchronization
79
+
80
+ ### CommandPackage (Type 201)
81
+ For device control and automation:
82
+ - Remote device commands
83
+ - Parameter passing via JSON
84
+ - Command acknowledgment tracking
85
+
86
+ ### StatusPackage (Type 202)
87
+ For system health monitoring:
88
+ - Device operational status
89
+ - Memory usage tracking
90
+ - Network connectivity metrics
91
+
92
+ ## 🌟 Key Features
93
+
94
+ - **Automatic Mesh Formation** - Nodes discover and connect automatically
95
+ - **JSON-Based Messaging** - Easy to use and extend
96
+ - **Time Synchronization** - Coordinated behaviors across nodes
97
+ - **Multi-Platform Support** - ESP32, ESP8266
98
+ - **Alteriom Extensions** - Enhanced packages for common use cases
99
+ - **Comprehensive Testing** - Full CI/CD pipeline with automated testing
100
+
101
+ ## 📚 External Resources
102
+
103
+ - **GitHub Repository**: https://github.com/Alteriom/painlessMesh
104
+ - **NPM Package**: https://www.npmjs.com/package/@alteriom/painlessmesh
105
+ - **PlatformIO Registry**: https://registry.platformio.org/libraries/alteriom/painlessMesh
106
+ - **Issue Tracker**: https://github.com/Alteriom/painlessMesh/issues
107
+ - **Discussions**: https://github.com/Alteriom/painlessMesh/discussions
108
+
109
+ ## 📝 License
110
+
111
+ This project is licensed under LGPL-3.0 - see the [LICENSE](https://github.com/Alteriom/painlessMesh/blob/main/LICENSE) file for details.
112
+
113
+ ## 🤝 Contributing
114
+
115
+ We welcome contributions! Please see our [Contributing Guidelines](Contributing) for details on:
116
+ - Code style and standards
117
+ - Pull request process
118
+ - Issue reporting
119
+ - Feature requests
120
+
121
+ ---
122
+
123
+ **Need Help?** Check the [FAQ](https://github.com/Alteriom/painlessMesh/blob/main/docs/troubleshooting/faq.md) or [open an issue](https://github.com/Alteriom/painlessMesh/issues).
@@ -0,0 +1,82 @@
1
+ # Alteriom painlessMesh Extensions
2
+
3
+ This directory contains Alteriom-specific extensions and examples for the painlessMesh library.
4
+
5
+ ## Package Types
6
+
7
+ ### SensorPackage (Type 200)
8
+ Broadcast package for sharing environmental sensor data across the mesh.
9
+
10
+ **Fields:**
11
+ - `temperature` - Temperature in Celsius
12
+ - `humidity` - Relative humidity percentage
13
+ - `pressure` - Atmospheric pressure in hPa
14
+ - `sensorId` - Unique sensor identifier
15
+ - `timestamp` - Unix timestamp of measurement
16
+ - `batteryLevel` - Battery level percentage (0-100)
17
+
18
+ ### CommandPackage (Type 201)
19
+ Single-destination package for sending commands to specific nodes.
20
+
21
+ **Fields:**
22
+ - `command` - Command type identifier
23
+ - `targetDevice` - Target device ID
24
+ - `parameters` - Command parameters as JSON string
25
+ - `commandId` - Unique command identifier for tracking
26
+
27
+ ### StatusPackage (Type 202)
28
+ Broadcast package for sharing device health and status information.
29
+
30
+ **Fields:**
31
+ - `deviceStatus` - Device status flags
32
+ - `uptime` - Device uptime in seconds
33
+ - `freeMemory` - Free memory in KB
34
+ - `wifiStrength` - WiFi signal strength (0-100)
35
+ - `firmwareVersion` - Current firmware version string
36
+
37
+ ## Examples
38
+
39
+ ### `alteriom_sensor_node.ino`
40
+ Complete Arduino sketch demonstrating:
41
+ - Periodic sensor data broadcasting
42
+ - Command handling
43
+ - Status reporting
44
+ - Message type discrimination
45
+ - Integration with painlessMesh
46
+
47
+ ## Usage
48
+
49
+ ```cpp
50
+ #include "alteriom_sensor_package.hpp"
51
+ using namespace alteriom;
52
+
53
+ // Create and send sensor data
54
+ SensorPackage sensor;
55
+ sensor.temperature = 25.0;
56
+ sensor.humidity = 60.0;
57
+ // ... set other fields
58
+ mesh.sendBroadcast(sensor.toJsonString());
59
+
60
+ // Handle incoming commands
61
+ void handleMessage(String& msg) {
62
+ auto doc = parseJson(msg);
63
+ if (doc["type"] == 201) {
64
+ CommandPackage cmd(doc.as<JsonObject>());
65
+ processCommand(cmd);
66
+ }
67
+ }
68
+ ```
69
+
70
+ ## Testing
71
+
72
+ Run the Alteriom package tests:
73
+ ```bash
74
+ ./bin/catch_alteriom_packages
75
+ ```
76
+
77
+ This validates:
78
+ - JSON serialization/deserialization
79
+ - Package type consistency
80
+ - Field preservation
81
+ - Edge case handling
82
+ - Integration with painlessMesh plugin system
@@ -0,0 +1,186 @@
1
+ //************************************************************
2
+ // AlteriomPainlessMesh Example
3
+ //
4
+ // This example demonstrates how to use AlteriomPainlessMesh
5
+ // with custom packages for sensor data, commands, and status
6
+ //
7
+ // This is the primary example that matches the folder name
8
+ // for Arduino Library Manager compatibility
9
+ //************************************************************
10
+
11
+ #include "AlteriomPainlessMesh.h"
12
+
13
+ #define MESH_PREFIX "AlteriomMesh"
14
+ #define MESH_PASSWORD "somethingSneaky"
15
+ #define MESH_PORT 5555
16
+
17
+ Scheduler userScheduler; // to control your personal task
18
+ painlessMesh mesh;
19
+
20
+ // Alteriom package handlers
21
+ using namespace alteriom;
22
+
23
+ // User stub
24
+ void sendSensorData();
25
+ void handleIncomingPackage(uint32_t from, String& msg);
26
+ void handleCommandPackage(CommandPackage& cmd);
27
+ void handleStatusRequest();
28
+
29
+ Task taskSendSensorData(30000, TASK_FOREVER, &sendSensorData);
30
+ Task taskSendStatus(60000, TASK_FOREVER, &handleStatusRequest);
31
+
32
+ void setup() {
33
+ Serial.begin(115200);
34
+
35
+ // Initialize mesh
36
+ mesh.setDebugMsgTypes(ERROR | MESH_STATUS | CONNECTION | SYNC |
37
+ COMMUNICATION | GENERAL | MSG_TYPES | REMOTE);
38
+ mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
39
+ mesh.onReceive(&handleIncomingPackage);
40
+ mesh.onNewConnection(&newConnectionCallback);
41
+ mesh.onChangedConnections(&changedConnectionCallback);
42
+ mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
43
+
44
+ // Add tasks
45
+ userScheduler.addTask(taskSendSensorData);
46
+ taskSendSensorData.enable();
47
+
48
+ userScheduler.addTask(taskSendStatus);
49
+ taskSendStatus.enable();
50
+
51
+ // Initialize sensors (placeholder)
52
+ Serial.println("Alteriom Sensor Node initialized");
53
+ }
54
+
55
+ void loop() { mesh.update(); }
56
+
57
+ void sendSensorData() {
58
+ // Create sensor package
59
+ SensorPackage sensorData;
60
+ sensorData.from = mesh.getNodeId();
61
+ sensorData.sensorId = mesh.getNodeId(); // Use node ID as sensor ID
62
+ sensorData.timestamp = mesh.getNodeTime();
63
+
64
+ // Read sensor values (placeholder - replace with actual sensor readings)
65
+ sensorData.temperature =
66
+ 23.5 + random(-50, 50) / 10.0; // Simulate temperature
67
+ sensorData.humidity = 45.0 + random(-100, 100) / 10.0; // Simulate humidity
68
+ sensorData.pressure = 1013.25 + random(-50, 50) / 10.0; // Simulate pressure
69
+ sensorData.batteryLevel = 85 + random(-20, 15); // Simulate battery
70
+
71
+ // Serialize and send
72
+ DynamicJsonDocument doc(sensorData.jsonObjectSize());
73
+ JsonObject obj = doc.to<JsonObject>();
74
+ sensorData.addTo(std::move(obj));
75
+
76
+ String msg;
77
+ serializeJson(doc, msg);
78
+ mesh.sendBroadcast(msg);
79
+
80
+ Serial.printf("Sent sensor data: T=%.1f, H=%.1f, P=%.1f, Bat=%d%%\n",
81
+ sensorData.temperature, sensorData.humidity,
82
+ sensorData.pressure, sensorData.batteryLevel);
83
+ }
84
+
85
+ void handleStatusRequest() {
86
+ StatusPackage status;
87
+ status.from = mesh.getNodeId();
88
+ status.deviceStatus = 0x01; // Device operational
89
+ status.uptime = millis() / 1000;
90
+ status.freeMemory = ESP.getFreeHeap() / 1024; // KB
91
+ status.wifiStrength = 75; // Placeholder
92
+ status.firmwareVersion = "1.0.0-alteriom";
93
+
94
+ // Serialize and send
95
+ DynamicJsonDocument doc(status.jsonObjectSize());
96
+ JsonObject obj = doc.to<JsonObject>();
97
+ status.addTo(std::move(obj));
98
+
99
+ String msg;
100
+ serializeJson(doc, msg);
101
+ mesh.sendBroadcast(msg);
102
+
103
+ Serial.printf("Sent status: uptime=%ds, mem=%dKB\n", status.uptime,
104
+ status.freeMemory);
105
+ }
106
+
107
+ void handleIncomingPackage(uint32_t from, String& msg) {
108
+ Serial.printf("Received from %u: %s\n", from, msg.c_str());
109
+
110
+ // Parse the JSON message
111
+ DynamicJsonDocument doc(1024);
112
+ deserializeJson(doc, msg);
113
+ JsonObject obj = doc.as<JsonObject>();
114
+
115
+ // Check message type
116
+ uint8_t msgType = obj["type"];
117
+
118
+ switch (msgType) {
119
+ case 200: // SensorPackage
120
+ {
121
+ SensorPackage receivedSensor(obj);
122
+ Serial.printf("Sensor data from %u: T=%.1f, H=%.1f\n",
123
+ receivedSensor.from, receivedSensor.temperature,
124
+ receivedSensor.humidity);
125
+ // Process sensor data (store, forward, analyze, etc.)
126
+ } break;
127
+
128
+ case 201: // CommandPackage
129
+ {
130
+ CommandPackage receivedCmd(obj);
131
+ if (receivedCmd.dest == mesh.getNodeId()) {
132
+ handleCommandPackage(receivedCmd);
133
+ }
134
+ } break;
135
+
136
+ case 202: // StatusPackage
137
+ {
138
+ StatusPackage receivedStatus(obj);
139
+ Serial.printf("Status from %u: uptime=%ds, mem=%dKB, fw=%s\n",
140
+ receivedStatus.from, receivedStatus.uptime,
141
+ receivedStatus.freeMemory,
142
+ receivedStatus.firmwareVersion.c_str());
143
+ } break;
144
+
145
+ default:
146
+ Serial.printf("Unknown message type: %d\n", msgType);
147
+ break;
148
+ }
149
+ }
150
+
151
+ void handleCommandPackage(CommandPackage& cmd) {
152
+ Serial.printf("Received command %d for device %u\n", cmd.command,
153
+ cmd.targetDevice);
154
+
155
+ // Process different command types
156
+ switch (cmd.command) {
157
+ case 1: // Reset command
158
+ Serial.println("Reset command received");
159
+ // Implement reset logic
160
+ break;
161
+
162
+ case 2: // Configuration update
163
+ Serial.printf("Config update: %s\n", cmd.parameters.c_str());
164
+ // Parse and apply configuration
165
+ break;
166
+
167
+ case 3: // Data request
168
+ Serial.println("Data request received");
169
+ sendSensorData(); // Send current sensor data
170
+ break;
171
+
172
+ default:
173
+ Serial.printf("Unknown command: %d\n", cmd.command);
174
+ break;
175
+ }
176
+ }
177
+
178
+ void newConnectionCallback(uint32_t nodeId) {
179
+ Serial.printf("New Connection, nodeId = %u\n", nodeId);
180
+ }
181
+
182
+ void changedConnectionCallback() { Serial.printf("Changed connections\n"); }
183
+
184
+ void nodeTimeAdjustedCallback(int32_t offset) {
185
+ Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
186
+ }