@@ -78,20 +78,12 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
78
78
79
79
debug (PSTR (" %s started, id %d\n " ), repeaterMode?" repeater" :" sensor" , nc.nodeId );
80
80
81
- // Open reading pipe for messages directed to this node (set write pipe to same)
82
- RF24::openReadingPipe (WRITE_PIPE, TO_ADDR (nc.nodeId ));
83
- RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR (nc.nodeId ));
84
-
85
- // Send presentation for this radio node (attach
86
- present (NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
87
-
88
- // Send a configuration exchange request to controller
89
- // Node sends parent node. Controller answers with latest node configuration
90
- // which is picked up in process()
91
- sendRoute (build (msg, nc.nodeId , GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, false ).set (nc.parentNodeId ));
92
-
93
- // Wait configuration reply.
94
- waitForReply ();
81
+ // If we got an id, set this node to use it
82
+ if (nc.nodeId != AUTO) {
83
+ setupNode ();
84
+ // Wait configuration reply.
85
+ waitForReply ();
86
+ }
95
87
}
96
88
97
89
void MySensor::setupRadio (rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
@@ -138,6 +130,20 @@ void MySensor::requestNodeId() {
138
130
waitForReply ();
139
131
}
140
132
133
+ void MySensor::setupNode () {
134
+ // Open reading pipe for messages directed to this node (set write pipe to same)
135
+ RF24::openReadingPipe (WRITE_PIPE, TO_ADDR (nc.nodeId ));
136
+ RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR (nc.nodeId ));
137
+
138
+ // Send presentation for this radio node (attach
139
+ present (NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
140
+
141
+ // Send a configuration exchange request to controller
142
+ // Node sends parent node. Controller answers with latest node configuration
143
+ // which is picked up in process()
144
+ sendRoute (build (msg, nc.nodeId , GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, false ).set (nc.parentNodeId ));
145
+ }
146
+
141
147
void MySensor::findParentNode () {
142
148
failedTransmissions = 0 ;
143
149
@@ -325,15 +331,14 @@ boolean MySensor::process() {
325
331
} else if (type == I_ID_RESPONSE) {
326
332
if (nc.nodeId == AUTO) {
327
333
nc.nodeId = msg.getByte ();
328
- // Write id to EEPROM
329
334
if (nc.nodeId == AUTO) {
330
335
// sensor net gateway will return max id if all sensor id are taken
331
336
debug (PSTR (" full\n " ));
332
337
while (1 ); // Wait here. Nothing else we can do...
333
- } else {
334
- RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR (nc.nodeId ));
335
- eeprom_write_byte ((uint8_t *)EEPROM_NODE_ID_ADDRESS, nc.nodeId );
336
338
}
339
+ setupNode ();
340
+ // Write id to EEPROM
341
+ eeprom_write_byte ((uint8_t *)EEPROM_NODE_ID_ADDRESS, nc.nodeId );
337
342
debug (PSTR (" id=%d\n " ), nc.nodeId );
338
343
}
339
344
} else if (type == I_CONFIG) {
0 commit comments