From ace0a38c93be4bc622a998b6247e0499b8f29575 Mon Sep 17 00:00:00 2001 From: Marcelo Aquino Date: Sat, 7 Feb 2015 10:59:25 -0200 Subject: [PATCH] Node set up only after having an id --- libraries/MySensors/MySensor.cpp | 41 ++++++++++++++++++-------------- libraries/MySensors/MySensor.h | 1 + 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index f61a12d..d3d28a3 100755 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -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); - // 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)); - - // Wait configuration reply. - waitForReply(); + // If we got an id, set this node to use it + if (nc.nodeId != AUTO) { + setupNode(); + // Wait configuration reply. + waitForReply(); + } } void MySensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) { @@ -138,6 +130,20 @@ void MySensor::requestNodeId() { 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() { failedTransmissions = 0; @@ -325,15 +331,14 @@ boolean MySensor::process() { } else if (type == I_ID_RESPONSE) { if (nc.nodeId == AUTO) { nc.nodeId = msg.getByte(); - // Write id to EEPROM if (nc.nodeId == AUTO) { // sensor net gateway will return max id if all sensor id are taken debug(PSTR("full\n")); 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); } } else if (type == I_CONFIG) { diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index d96ec13..9d50b4b 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -276,6 +276,7 @@ class MySensor : public RF24 void waitForReply(); void requestNodeId(); + void setupNode(); void findParentNode(); uint8_t crc8Message(MyMessage &message); uint8_t getChildRoute(uint8_t childId);