Skip to content

Commit 23e5d5f

Browse files
committed
Merge pull request arduino#56 from mapnull/hotfix-setupNode
Node set up only after having an id
2 parents 576c092 + ace0a38 commit 23e5d5f

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

libraries/MySensors/MySensor.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

9789
void 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+
141147
void 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) {

libraries/MySensors/MySensor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ class MySensor : public RF24
276276

277277
void waitForReply();
278278
void requestNodeId();
279+
void setupNode();
279280
void findParentNode();
280281
uint8_t crc8Message(MyMessage &message);
281282
uint8_t getChildRoute(uint8_t childId);

0 commit comments

Comments
 (0)