Dezibot 4
Communication.cpp
Go to the documentation of this file.
1 #include "Communication.h"
2 
3 Scheduler userScheduler; // to control your personal task
4 painlessMesh mesh;
5 uint32_t Communication::groupNumber = 0;
6 
7 // User-defined callback function pointer
8 void (*Communication::userCallback)(String &msg) = nullptr;
9 
11 {
12  String data = String(groupNumber) + "#" + msg;
13  mesh.sendBroadcast(data);
14 }
15 
16 // Needed for painless library
17 void 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 
36 void 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 
46 void nodeTimeAdjustedCallback(int32_t offset)
47 {
48  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
49 }
50 
51 void vTaskUpdate(void *pvParameters)
52 {
53  for (;;)
54  {
55  mesh.update();
56  }
57 }
58 
59 void Communication::setGroupNumber(uint32_t number) {
60  groupNumber = number;
61 }
62 
63 // Method to set the user-defined callback function
64 void 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 };
newConnectionCallback
void newConnectionCallback(uint32_t nodeId)
Definition: Communication.cpp:36
changedConnectionCallback
void changedConnectionCallback()
Definition: Communication.cpp:41
Communication::sendMessage
void sendMessage(String msg)
Definition: Communication.cpp:10
MESH_PORT
#define MESH_PORT
Definition: Communication.h:10
Communication::onReceive
void onReceive(void(*callbackFunc)(String &msg))
Definition: Communication.cpp:64
MESH_PREFIX
#define MESH_PREFIX
Definition: Communication.h:8
MESH_PASSWORD
#define MESH_PASSWORD
Definition: Communication.h:9
Communication.h
mesh
painlessMesh mesh
Definition: Communication.cpp:4
vTaskUpdate
void vTaskUpdate(void *pvParameters)
Definition: Communication.cpp:51
nodeTimeAdjustedCallback
void nodeTimeAdjustedCallback(int32_t offset)
Definition: Communication.cpp:46
userScheduler
Scheduler userScheduler
Definition: Communication.cpp:3
Communication::setGroupNumber
void setGroupNumber(uint32_t number)
Definition: Communication.cpp:59
Communication::begin
static void begin(void)
initialize the Mesh Compnent, must be called before the other methods are used.
Definition: Communication.cpp:69