@@ -78,20 +78,12 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
7878
7979 debug (PSTR (" %s started, id %d\n " ), repeaterMode?" repeater" :" sensor" , nc.nodeId );
8080
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+ }
9587}
9688
9789void MySensor::setupRadio (rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
@@ -138,6 +130,20 @@ void MySensor::requestNodeId() {
138130 waitForReply ();
139131}
140132
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+
141147void MySensor::findParentNode () {
142148 failedTransmissions = 0 ;
143149
@@ -325,15 +331,14 @@ boolean MySensor::process() {
325331 } else if (type == I_ID_RESPONSE) {
326332 if (nc.nodeId == AUTO) {
327333 nc.nodeId = msg.getByte ();
328- // Write id to EEPROM
329334 if (nc.nodeId == AUTO) {
330335 // sensor net gateway will return max id if all sensor id are taken
331336 debug (PSTR (" full\n " ));
332337 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 );
336338 }
339+ setupNode ();
340+ // Write id to EEPROM
341+ eeprom_write_byte ((uint8_t *)EEPROM_NODE_ID_ADDRESS, nc.nodeId );
337342 debug (PSTR (" id=%d\n " ), nc.nodeId );
338343 }
339344 } else if (type == I_CONFIG) {
0 commit comments