diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index d999617..a0ddcf2 100644 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -31,7 +31,7 @@ MySensor::MySensor(uint8_t _cepin, uint8_t _cspin) : RF24(_cepin, _cspin) { } -void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) { +void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId, rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate) { Serial.begin(BAUD_RATE); isGateway = false; repeaterMode = _repeaterMode; @@ -51,13 +51,20 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b cc.isMetric = 0x01; } + if (_parentNodeId != AUTO) { + nc.parentNodeId = _parentNodeId; + autoFindParent = false; + } else { + autoFindParent = true; + } + if (_nodeId != AUTO) { // Set static id nc.nodeId = _nodeId; } - // If no parent was found. Try to find one. - if (nc.parentNodeId == 0xff) { + // If no parent was found in eeprom. Try to find one. + if (autoFindParent && nc.parentNodeId == 0xff) { findParentNode(); } @@ -186,7 +193,7 @@ boolean MySensor::sendRoute(MyMessage &message) { if (!ok) { // Failure when sending to parent node. The parent node might be down and we // need to find another route to gateway. - if (failedTransmissions > SEARCH_FAILURES) { + if (autoFindParent && failedTransmissions > SEARCH_FAILURES) { findParentNode(); } failedTransmissions++; diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 2ff9cab..6e16071 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -95,12 +95,13 @@ class MySensor : public RF24 * @param incomingMessageCallback Callback function for incoming messages from other nodes or controller and request responses. Default is NULL. * @param nodeId The unique id (1-254) for this sensor. Default is AUTO(255) which means sensor tries to fetch an id from controller. * @param repeaterMode Activate repeater mode. This node will forward messages to other nodes in the radio network. Make sure to call process() regularly. Default in false + * @param parentNodeId Use this to force node to always communicate with a certain parent node. Default is AUTO which means node automatically tries to find a parent. * @param paLevel Radio PA Level for this sensor. Default RF24_PA_MAX * @param channel Radio channel. Default is channel 76 * @param dataRate Radio transmission speed. Default RF24_1MBPS */ - void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, rf24_pa_dbm_e paLevel=RF24_PA_LEVEL, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE); + void begin(void (* msgCallback)(const MyMessage &)=NULL, uint8_t nodeId=AUTO, boolean repeaterMode=false, uint8_t parentNodeId=AUTO, rf24_pa_dbm_e paLevel=RF24_PA_LEVEL, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE); /** * Return the nodes nodeId. @@ -237,6 +238,7 @@ class MySensor : public RF24 NodeConfig nc; // Essential settings for node to work ControllerConfig cc; // Configuration coming from controller bool repeaterMode; + bool autoFindParent; bool isGateway; MyMessage msg; // Buffer for incoming messages. MyMessage ack; // Buffer for ack messages.