@@ -299,6 +299,12 @@ void MySensor::setupNode() {
299
299
}
300
300
301
301
void MySensor::findParentNode () {
302
+ static boolean findingParentNode = false ;
303
+
304
+ if (findingParentNode)
305
+ return ;
306
+ findingParentNode = true ;
307
+
302
308
failedTransmissions = 0 ;
303
309
304
310
// Set distance to max
@@ -313,6 +319,7 @@ void MySensor::findParentNode() {
313
319
314
320
// Wait for ping response.
315
321
wait (2000 );
322
+ findingParentNode = false ;
316
323
}
317
324
318
325
boolean MySensor::sendRoute (MyMessage &message) {
@@ -786,14 +793,17 @@ boolean MySensor::process() {
786
793
// If this node have an id, relay the message
787
794
788
795
if (command == C_INTERNAL && type == I_FIND_PARENT) {
789
- if (nc.distance == DISTANCE_INVALID) {
790
- findParentNode ();
791
- } else if (sender != nc.parentNodeId ) {
792
- // Relaying nodes should always answer ping messages
793
- // Wait a random delay of 0-2 seconds to minimize collision
794
- // between ping ack messages from other relaying nodes
795
- wait (hw_millis () & 0x3ff );
796
- sendWrite (sender, build (msg, nc.nodeId , sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false ).set (nc.distance ));
796
+ if (sender != nc.parentNodeId ) {
797
+ if (nc.distance == DISTANCE_INVALID)
798
+ findParentNode ();
799
+
800
+ if (nc.distance != DISTANCE_INVALID) {
801
+ // Relaying nodes should always answer ping messages
802
+ // Wait a random delay of 0-2 seconds to minimize collision
803
+ // between ping ack messages from other relaying nodes
804
+ wait (hw_millis () & 0x3ff );
805
+ sendWrite (sender, build (msg, nc.nodeId , sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false ).set (nc.distance ));
806
+ }
797
807
}
798
808
} else if (to == nc.nodeId ) {
799
809
// We should try to relay this message to another node
0 commit comments