Merge pull request #56 from mapnull/hotfix-setupNode

Node set up only after having an id
This commit is contained in:
Henrik Ekblad
2015-02-07 16:52:54 +01:00
2 changed files with 24 additions and 18 deletions
+23 -18
View File
@@ -78,20 +78,12 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId); debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId);
// Open reading pipe for messages directed to this node (set write pipe to same) // If we got an id, set this node to use it
RF24::openReadingPipe(WRITE_PIPE, TO_ADDR(nc.nodeId)); if (nc.nodeId != AUTO) {
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId)); setupNode();
// Wait configuration reply.
// Send presentation for this radio node (attach waitForReply();
present(NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE); }
// Send a configuration exchange request to controller
// Node sends parent node. Controller answers with latest node configuration
// which is picked up in process()
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, false).set(nc.parentNodeId));
// Wait configuration reply.
waitForReply();
} }
void MySensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) { void MySensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) {
@@ -138,6 +130,20 @@ void MySensor::requestNodeId() {
waitForReply(); waitForReply();
} }
void MySensor::setupNode() {
// Open reading pipe for messages directed to this node (set write pipe to same)
RF24::openReadingPipe(WRITE_PIPE, TO_ADDR(nc.nodeId));
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
// Send presentation for this radio node (attach
present(NODE_SENSOR_ID, repeaterMode? S_ARDUINO_REPEATER_NODE : S_ARDUINO_NODE);
// Send a configuration exchange request to controller
// Node sends parent node. Controller answers with latest node configuration
// which is picked up in process()
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CONFIG, false).set(nc.parentNodeId));
}
void MySensor::findParentNode() { void MySensor::findParentNode() {
failedTransmissions = 0; failedTransmissions = 0;
@@ -325,15 +331,14 @@ boolean MySensor::process() {
} else if (type == I_ID_RESPONSE) { } else if (type == I_ID_RESPONSE) {
if (nc.nodeId == AUTO) { if (nc.nodeId == AUTO) {
nc.nodeId = msg.getByte(); nc.nodeId = msg.getByte();
// Write id to EEPROM
if (nc.nodeId == AUTO) { if (nc.nodeId == AUTO) {
// sensor net gateway will return max id if all sensor id are taken // sensor net gateway will return max id if all sensor id are taken
debug(PSTR("full\n")); debug(PSTR("full\n"));
while (1); // Wait here. Nothing else we can do... while (1); // Wait here. Nothing else we can do...
} else {
RF24::openReadingPipe(CURRENT_NODE_PIPE, TO_ADDR(nc.nodeId));
eeprom_write_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, nc.nodeId);
} }
setupNode();
// Write id to EEPROM
eeprom_write_byte((uint8_t*)EEPROM_NODE_ID_ADDRESS, nc.nodeId);
debug(PSTR("id=%d\n"), nc.nodeId); debug(PSTR("id=%d\n"), nc.nodeId);
} }
} else if (type == I_CONFIG) { } else if (type == I_CONFIG) {
+1
View File
@@ -276,6 +276,7 @@ class MySensor : public RF24
void waitForReply(); void waitForReply();
void requestNodeId(); void requestNodeId();
void setupNode();
void findParentNode(); void findParentNode();
uint8_t crc8Message(MyMessage &message); uint8_t crc8Message(MyMessage &message);
uint8_t getChildRoute(uint8_t childId); uint8_t getChildRoute(uint8_t childId);