i am working on painless mess to create a mesh network with esp8266 and esp32
i have prepared a code for slave which will be used to routing data from master towards the receiver esp8266
i have use sendBroadcast function to send data from slave to esp which broadcast msg to all nodes even when some node didn't want that data, So now i want to send data to particular nodes like msg 1 to node1 only and msg 2 to node 2 only, i only know the function that will be needed which is sendSingle() but i really don't know how to use it and not much examples are available on it so it would be great help if someone guide me through
here is my code for slave
//Code for Node1_slave
#include "painlessMesh.h"
// WiFi Credentials
#define MESH_PREFIX "SSID"
#define MESH_PASSWORD "PASSWORD"
#define MESH_PORT 5555
bool S_Light_1 = 0;
bool S_Light_2 = 0;
Scheduler userScheduler;
painlessMesh mesh;
void sendMessage() ;
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void sendMessage()
{
// Serializing in JSON Format
DynamicJsonDocument doc(1024);
doc["L1"] = S_Light_1;
doc["L2"] = S_Light_2;
String msg ;
serializeJson(doc, msg);
mesh.sendBroadcast( msg );
Serial.println("\nSlave send");
Serial.println(msg);
taskSendMessage.setInterval((TASK_SECOND * 1));
}
void receivedCallback( uint32_t from, String &msg ) {
String json;
DynamicJsonDocument doc(1024);
json = msg.c_str();
DeserializationError error = deserializeJson(doc, json);
if (error)
{
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
}
S_Light_1= doc["M_Light_1"];//from master's send Doc
S_Light_2 = doc["M_Light_2"];//from master's send Doc
Serial.println("From Master");
}
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 setup() {
Serial.begin(115200);
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);
userScheduler.addTask( taskSendMessage );
taskSendMessage.enable();
}
void loop() {
mesh.update();
}