Dezibot 4
Loading...
Searching...
No Matches
Communication.cpp
Go to the documentation of this file.
1#include "Communication.h"
2
3Scheduler userScheduler; // to control your personal task
4painlessMesh mesh;
5uint32_t Communication::groupNumber = 0;
6
7// User-defined callback function pointer
8void (*Communication::userCallback)(String &msg) = nullptr;
9
11{
12 String data = String(groupNumber) + "#" + msg;
13 mesh.sendBroadcast(data);
14}
15
16// Needed for painless library
17void Communication::receivedCallback(uint32_t from, String &msg)
18{
19 int separatorIndex = msg.indexOf('#');
20 if (separatorIndex != -1) {
21 String groupNumberStr = msg.substring(0, separatorIndex);
22 uint32_t num = groupNumberStr.toInt();
23 String restOfMsg = msg.substring(separatorIndex + 1);
24
25 Serial.printf("startHere: Received from %u groupNumber=%u msg=%s\n", from, num, restOfMsg.c_str());
26
27 if (groupNumber != num) return;
28
29 // Execute user-defined callback if it is set
30 if (userCallback) {
31 userCallback(restOfMsg);
32 }
33 }
34}
35
36void newConnectionCallback(uint32_t nodeId)
37{
38 Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
39}
40
42{
43 Serial.printf("Changed connections\n");
44}
45
46void nodeTimeAdjustedCallback(int32_t offset)
47{
48 Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
49}
50
51void vTaskUpdate(void *pvParameters)
52{
53 for (;;)
54 {
55 mesh.update();
56 }
57}
58
59void Communication::setGroupNumber(uint32_t number) {
60 groupNumber = number;
61}
62
63// Method to set the user-defined callback function
64void Communication::onReceive(void (*callbackFunc)(String &msg))
65{
66 userCallback = callbackFunc;
67}
68
70{
71 Serial.begin(115200);
72
73 // mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
74 mesh.setDebugMsgTypes(ERROR | STARTUP); // set before init() so that you can see startup messages
75
77 mesh.onReceive(&receivedCallback);
78 mesh.onNewConnection(&newConnectionCallback);
79 mesh.onChangedConnections(&changedConnectionCallback);
80 mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
81
82 static uint8_t ucParameterToPass;
83 TaskHandle_t xHandle = NULL;
84
85 xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle);
86 configASSERT(xHandle);
87};
Scheduler userScheduler
void vTaskUpdate(void *pvParameters)
void changedConnectionCallback()
void newConnectionCallback(uint32_t nodeId)
painlessMesh mesh
void nodeTimeAdjustedCallback(int32_t offset)
#define MESH_PORT
#define MESH_PREFIX
#define MESH_PASSWORD
void sendMessage(String msg)
void onReceive(void(*callbackFunc)(String &msg))
static void begin(void)
initialize the Mesh Compnent, must be called before the other methods are used.
void setGroupNumber(uint32_t number)