Skip to content

Instantly share code, notes, and snippets.

@costyn
Last active March 14, 2021 10:57
Show Gist options
  • Save costyn/25aa9a2d570e459c89e3cb271f992dbd to your computer and use it in GitHub Desktop.
Save costyn/25aa9a2d570e459c89e3cb271f992dbd to your computer and use it in GitHub Desktop.
painlessMesh example basic rewritten with TaskScheduler
//************************************************************
// this is a simple example that uses the painlessMesh library
//
// 1. sends a silly message to every node on the mesh at a random time betweew 1 and 5 seconds
// 2. prints anything it recieves to Serial.print
//
//
//************************************************************
// #define _TASK_MICRO_RES uncomment for weirdness
#include "painlessMesh.h"
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
void sendMessage() ; // Prototype so PlatformIO doesn't complain
painlessMesh mesh;
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}
void newConnectionCallback(uint32_t nodeId) {
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
void changedConnectionCallback() {
Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str());
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
void setup() {
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, MESH_PORT );
mesh.onReceive(&receivedCallback);
mesh.onNewConnection(&newConnectionCallback);
mesh.onChangedConnections(&changedConnectionCallback);
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
mesh.scheduler.addTask( taskSendMessage );
taskSendMessage.enable() ;
}
void loop() {
mesh.update();
}
void sendMessage() {
String msg = "Hello from node ";
msg += mesh.getNodeId();
mesh.sendBroadcast( msg );
taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
}
@DGAD7
Copy link

DGAD7 commented Aug 10, 2020

why cant we use _TASK_MICRO_RES in painlessmesh?

@costyn
Copy link
Author

costyn commented Aug 11, 2020

Hello,

Since painlessMesh also uses TaskScheduler, using this directive would change the timing settings for painlessMesh itself too. But this was many years ago, perhaps it's been updated to support MICRO RES. Please check the code/documentation.

@DGAD7
Copy link

DGAD7 commented Aug 11, 2020

Hello,

Since painlessMesh also uses TaskScheduler, using this directive would change the timing settings for painlessMesh itself too. But this was many years ago, perhaps it's been updated to support MICRO RES. Please check the code/documentation.

Thank you for the answer.

@halukmy
Copy link

halukmy commented Mar 14, 2021

can we use pwm with this task? i want to fade dim but its get starts others after finish @costyn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment