Skip to content

Instantly share code, notes, and snippets.

@gohai
Last active June 1, 2023 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gohai/fa5db01f33be24e4d25daf621e9d5159 to your computer and use it in GitHub Desktop.
Save gohai/fa5db01f33be24e4d25daf621e9d5159 to your computer and use it in GitHub Desktop.
// For Kat Park's PESCA
// Based on the basic example of the Painless Mesh library
#include "painlessMesh.h"
// GH: I had to manually install the AsyncTCP library also to make this compile
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define MAX_PEERS 10 // GH: how many peers maximum
#define MAX_INCOMMUNICADO_MS 15000 // GH: after now many milliseconds of silence we consider a peer gone
Scheduler userScheduler;
painlessMesh mesh;
void sendMessage();
Task taskSendMessage(TASK_SECOND * 1, TASK_FOREVER, &sendMessage);
// GH: the peers array holds entries for each nodeId enountered,
// GH: and the time we last heard from them
struct peer {
uint32_t nodeId;
uint64_t lastSeen;
};
struct peer peers[MAX_PEERS];
void sendMessage() {
String msg = "PING";
mesh.sendBroadcast(msg);
taskSendMessage.setInterval(TASK_SECOND * 1);
}
void receivedCallback(uint32_t from, String &msg) {
// GH: add or update the record for the node just heard
bool foundNode = false;
for (int i = 0; i < MAX_PEERS; i++) {
if (peers[i].nodeId == from) {
peers[i].lastSeen = millis();
foundNode = true;
break;
}
}
if (!foundNode) {
for (int i = 0; i < MAX_PEERS; i++) {
if (peers[i].nodeId == 0) {
peers[i].nodeId = from;
peers[i].lastSeen = millis();
foundNode = true;
Serial.printf("Arrived: %u\n", from);
break;
}
}
}
if (!foundNode) {
Serial.printf("No space to store %u, this should not happen\n", from);
}
Serial.printf("Heard from: %u\n", from);
}
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 | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE );
mesh.setDebugMsgTypes(ERROR | STARTUP);
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();
// GH: go through the list of peers and check if we haven't heard from
// GH: some in a while
uint64_t now = millis();
for (int i = 0; i < MAX_PEERS; i++) {
if (peers[i].nodeId != 0 && now - peers[i].lastSeen > MAX_INCOMMUNICADO_MS) {
Serial.printf("Left: %u\n", peers[i].nodeId);
peers[i].nodeId = 0;
peers[i].lastSeen = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment