startHere.ino 4.77 KB
Newer Older
BlackEdder's avatar
BlackEdder committed
1
2
3
4
5
//************************************************************
// this is a simple example that uses the easyMesh library
//
// 1. blinks led once for every node on the mesh
// 2. blink cycle repeats every BLINK_PERIOD
6
7
// 3. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds
// 4. prints anything it receives to Serial.print
8
//
BlackEdder's avatar
BlackEdder committed
9
10
11
12
//
//************************************************************
#include <painlessMesh.h>

13
// some gpio pin that is connected to an LED...
BlackEdder's avatar
BlackEdder committed
14
// on my rig, this is 5, change to the right number of your LED.
Germán Martín's avatar
Germán Martín committed
15
#define   LED             2       // GPIO number of connected LED, ON ESP-12 IS GPIO2
BlackEdder's avatar
BlackEdder committed
16

17
18
#define   BLINK_PERIOD    3000 // milliseconds until cycle repeat
#define   BLINK_DURATION  100  // milliseconds LED is on for
BlackEdder's avatar
BlackEdder committed
19
20
21
22
23

#define   MESH_SSID       "whateverYouLike"
#define   MESH_PASSWORD   "somethingSneaky"
#define   MESH_PORT       5555

24
25
26
27
28
29
30
31
// Prototypes
void sendMessage(); 
void receivedCallback(uint32_t from, String & msg);
void newConnectionCallback(uint32_t nodeId);
void changedConnectionCallback(); 
void nodeTimeAdjustedCallback(int32_t offset); 
void delayReceivedCallback(uint32_t from, int32_t delay);

32
Scheduler     userScheduler; // to control your personal task
BlackEdder's avatar
BlackEdder committed
33
painlessMesh  mesh;
34

Germán Martín's avatar
Germán Martín committed
35
36
bool calc_delay = false;
SimpleList<uint32_t> nodes;
37
38

void sendMessage() ; // Prototype
39
Task taskSendMessage( TASK_SECOND * 1, TASK_FOREVER, &sendMessage ); // start with a one second interval
BlackEdder's avatar
BlackEdder committed
40

41
42
43
44
// Task to blink the number of nodes
Task blinkNoNodes;
bool onFlag = false;

BlackEdder's avatar
BlackEdder committed
45
void setup() {
46
  Serial.begin(115200);
Germán Martín's avatar
Germán Martín committed
47

48
  pinMode(LED, OUTPUT);
BlackEdder's avatar
BlackEdder committed
49

BlackEdder's avatar
BlackEdder committed
50
  mesh.setDebugMsgTypes(ERROR | DEBUG);  // set before init() so that you can see error messages
BlackEdder's avatar
BlackEdder committed
51

52
  mesh.init(MESH_SSID, MESH_PASSWORD, &userScheduler, MESH_PORT);
53
54
55
56
57
  mesh.onReceive(&receivedCallback);
  mesh.onNewConnection(&newConnectionCallback);
  mesh.onChangedConnections(&changedConnectionCallback);
  mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  mesh.onNodeDelayReceived(&delayReceivedCallback);
BlackEdder's avatar
BlackEdder committed
58

59
60
  userScheduler.addTask( taskSendMessage );
  taskSendMessage.enable();
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  blinkNoNodes.set(BLINK_PERIOD, (mesh.getNodeList().size() + 1) * 2, []() {
      // If on, switch off, else switch on
      if (onFlag)
        onFlag = false;
      else
        onFlag = true;
      blinkNoNodes.delay(BLINK_DURATION);

      if (blinkNoNodes.isLastIteration()) {
        // Finished blinking. Reset task for next run 
        // blink number of nodes (including this node) times
        blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
        // Calculate delay based on current mesh time and BLINK_PERIOD
        // This results in blinks between nodes being synced
        blinkNoNodes.enableDelayed(BLINK_PERIOD - 
            (mesh.getNodeTime() % (BLINK_PERIOD*1000))/1000);
      }
  });
80
  userScheduler.addTask(blinkNoNodes);
81
82
  blinkNoNodes.enable();

83
  randomSeed(analogRead(A0));
BlackEdder's avatar
BlackEdder committed
84
85
86
}

void loop() {
Doanh in Windowns's avatar
Doanh in Windowns committed
87
  mesh.update();
88
  digitalWrite(LED, !onFlag);
89
90
91
92
93
}

void sendMessage() {
  String msg = "Hello from node ";
  msg += mesh.getNodeId();
94
  msg += " myFreeMemory: " + String(ESP.getFreeHeap());
BlackEdder's avatar
BlackEdder committed
95
  mesh.sendBroadcast(msg);
Germán Martín's avatar
Germán Martín committed
96

97
98
99
100
101
  if (calc_delay) {
    SimpleList<uint32_t>::iterator node = nodes.begin();
    while (node != nodes.end()) {
      mesh.startDelayMeas(*node);
      node++;
Germán Martín's avatar
Germán Martín committed
102
    }
103
104
    calc_delay = false;
  }
105
106

  Serial.printf("Sending message: %s\n", msg.c_str());
107
  
BlackEdder's avatar
BlackEdder committed
108
  taskSendMessage.setInterval( random(TASK_SECOND * 1, TASK_SECOND * 5));  // between 1 and 5 seconds
BlackEdder's avatar
BlackEdder committed
109
110
}

111
112
113

void receivedCallback(uint32_t from, String & msg) {
  Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
BlackEdder's avatar
BlackEdder committed
114
115
}

Germán Martín's avatar
Germán Martín committed
116
void newConnectionCallback(uint32_t nodeId) {
117
118
119
120
121
  // Reset blink task
  onFlag = false;
  blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD*1000))/1000);
 
122
  Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
BlackEdder's avatar
BlackEdder committed
123
  Serial.printf("--> startHere: New Connection, %s\n", mesh.subConnectionJson(true).c_str());
Germán Martín's avatar
Germán Martín committed
124
125
126
}

void changedConnectionCallback() {
127
  Serial.printf("Changed connections\n");
128
129
130
131
132
  // Reset blink task
  onFlag = false;
  blinkNoNodes.setIterations((mesh.getNodeList().size() + 1) * 2);
  blinkNoNodes.enableDelayed(BLINK_PERIOD - (mesh.getNodeTime() % (BLINK_PERIOD*1000))/1000);
 
133
  nodes = mesh.getNodeList();
Germán Martín's avatar
Germán Martín committed
134

135
136
  Serial.printf("Num nodes: %d\n", nodes.size());
  Serial.printf("Connection list:");
Germán Martín's avatar
Germán Martín committed
137

138
139
140
141
142
143
144
  SimpleList<uint32_t>::iterator node = nodes.begin();
  while (node != nodes.end()) {
    Serial.printf(" %u", *node);
    node++;
  }
  Serial.println();
  calc_delay = true;
Germán Martín's avatar
Germán Martín committed
145
146
}

Germán Martín's avatar
Germán Martín committed
147
void nodeTimeAdjustedCallback(int32_t offset) {
148
  Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
Germán Martín's avatar
Germán Martín committed
149
150
151
}

void delayReceivedCallback(uint32_t from, int32_t delay) {
152
  Serial.printf("Delay to node %u is %d us\n", from, delay);
BlackEdder's avatar
BlackEdder committed
153
}