add mesh with painlessmesh

This commit is contained in:
Anton Jacker 2024-06-10 22:56:08 +02:00
parent 9e26a704aa
commit 5676ce46fd
No known key found for this signature in database
GPG Key ID: 6581AFF52DAA87AC
3 changed files with 121 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include "multiColorLight/MultiColorLight.h" #include "multiColorLight/MultiColorLight.h"
#include "motionDetection/MotionDetection.h" #include "motionDetection/MotionDetection.h"
#include "infraredLight/InfraredLight.h" #include "infraredLight/InfraredLight.h"
#include "mesh/Mesh.h"
class Dezibot { class Dezibot {
@ -29,6 +30,7 @@ public:
MultiColorLight multiColorLight; MultiColorLight multiColorLight;
MotionDetection motionDetection; MotionDetection motionDetection;
InfraredLight infraredLight; InfraredLight infraredLight;
Mesh mesh;
void begin(void); void begin(void);
/* /*
Display display Display display

87
src/mesh/Mesh.cpp Normal file
View File

@ -0,0 +1,87 @@
#include "Mesh.h"
Scheduler userScheduler; // to control your personal task
painlessMesh mesh;
uint32_t Mesh::groupNumber = 0;
// User-defined callback function pointer
void (*Mesh::userCallback)(uint32_t from, String &msg) = nullptr;
void Mesh::sendMessage(String msg)
{
String data = String(groupNumber) + "#" + msg;
mesh.sendBroadcast(data);
}
// Needed for painless library
void Mesh::receivedCallback(uint32_t from, String &msg)
{
int separatorIndex = msg.indexOf('#');
if (separatorIndex != -1) {
String groupNumberStr = msg.substring(0, separatorIndex);
uint32_t num = groupNumberStr.toInt();
String restOfMsg = msg.substring(separatorIndex + 1);
Serial.printf("startHere: Received from %u groupNumber=%u msg=%s\n", from, num, restOfMsg.c_str());
if (groupNumber != num) return;
// Execute user-defined callback if it is set
if (userCallback) {
userCallback(from, restOfMsg);
}
}
}
void newConnectionCallback(uint32_t nodeId)
{
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback()
{
Serial.printf("Changed connections\n");
}
void nodeTimeAdjustedCallback(int32_t offset)
{
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
}
void vTaskUpdate(void *pvParameters)
{
for (;;)
{
mesh.update();
}
}
void Mesh::setGroupNumber(uint32_t number) {
groupNumber = number;
}
// Method to set the user-defined callback function
void Mesh::onReceive(void (*callbackFunc)(uint32_t from, String &msg))
{
userCallback = callbackFunc;
}
void Mesh::begin(void)
{
Serial.begin(115200);
// mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes(ERROR | STARTUP); // set before init() so that you can see startup messages
mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
static uint8_t ucParameterToPass;
TaskHandle_t xHandle = NULL;
xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle);
configASSERT(xHandle);
};

32
src/mesh/Mesh.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef Mesh_h
#define Mesh_h
#include <stdint.h>
#include <Arduino.h>
#include <painlessMesh.h>
#define MESH_PREFIX "DEZIBOT_MESH"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
class Mesh{
public:
/**
* @brief initialize the Mesh Compnent, must be called before the other methods are used.
*
*/
static void begin(void);
void setGroupNumber(uint32_t number);
void sendMessage(String msg);
void onReceive(void (*callbackFunc)(uint32_t from, String &msg));
private:
static void (*userCallback)(uint32_t from, String &msg);
static void receivedCallback(uint32_t from, String &msg);
static uint32_t groupNumber;
};
#endif //Mesh_h