RF24Mesh_SerialConfig.ino¶

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/** RF24Mesh_SerialConfig.ino by TMRh20

   This example sketch shows how the same sketch can be written to a large number of devices, which are
   configured later via Serial input.

 **/

#include "RF24Network.h"
#include "RF24.h"
#include "RF24Mesh.h"
#include <SPI.h>
#include <printf.h>

/** Configure the nrf24l01 CE and CS pins */
RF24 radio(7, 8);
RF24Network network(radio);
RF24Mesh mesh(radio, network);

/**
   User Configuration: NodeID - A unique identifier for each radio. Allows addressing
   to change dynamically with physical changes to the mesh.

   In this example, user configuration of the node takes place via Serial input.
   Input a unique decimal value from 1-255 OR simply use characters 0-9, a-z, A-Z etc.
   The nodeID will be set/changed and saved in EEPROM upon user input via Serial, and stored
   between power loss, etc. Configuration only needs to be done once.

 **/


void setup() {

  Serial.begin(115200);
  while (!Serial) {
    // some boards need this because of native USB capability
  }

  // If this is a new node, the nodeID will return 0. Once the node is configured with an ID other than 0, this
  // bit will no longer run.
  while (!mesh.getNodeID()) {
    // Wait for the nodeID to be set via Serial
    if (Serial.available()) {
      mesh.setNodeID(Serial.read());
      Serial.print("Set NodeID: ");
      Serial.println(mesh.getNodeID());
    }
  }

  // Now that this node has a unique ID, connect to the mesh
  Serial.println(F("Connecting to the mesh..."));
  if (!mesh.begin()) {
    if (radio.isChipConnected()) {
      do {
        // mesh.renewAddress() will return MESH_DEFAULT_ADDRESS on failure to connect
        Serial.println(F("Could not connect to network.\nConnecting to the mesh..."));
      } while (mesh.renewAddress() == MESH_DEFAULT_ADDRESS);
    } else {
      Serial.println(F("Radio hardware not responding."));
      while (1) {
        // hold in an infinite loop
      }
    }
  }
}

unsigned long displayTimer = 0;

void loop() {

  mesh.update();

  // Send an update in every second
  if (millis() - displayTimer >= 1000) {
    displayTimer = millis();
    // Send the current millis() value to the master node as an 'M' type message
    mesh.write(&displayTimer, 'M', sizeof(displayTimer));
  }
}