Fix comment. Pseudo random.
This commit is contained in:
@@ -26,16 +26,16 @@ char* MyMessage::getString(char *buffer) {
|
|||||||
itoa(bValue, buffer, MAX_PAYLOAD);
|
itoa(bValue, buffer, MAX_PAYLOAD);
|
||||||
break;
|
break;
|
||||||
case P_INT16:
|
case P_INT16:
|
||||||
itoa(iValue, data, MAX_PAYLOAD);
|
itoa(iValue, buffer, MAX_PAYLOAD);
|
||||||
break;
|
break;
|
||||||
case P_UINT16:
|
case P_UINT16:
|
||||||
ltoa(uiValue, data, MAX_PAYLOAD);
|
ltoa(uiValue, buffer, MAX_PAYLOAD);
|
||||||
break;
|
break;
|
||||||
case P_LONG32:
|
case P_LONG32:
|
||||||
ltoa(lValue, data, MAX_PAYLOAD);
|
ltoa(lValue, buffer, MAX_PAYLOAD);
|
||||||
break;
|
break;
|
||||||
case P_ULONG32:
|
case P_ULONG32:
|
||||||
ltoa(ulValue, data, MAX_PAYLOAD);
|
ltoa(ulValue, buffer, MAX_PAYLOAD);
|
||||||
break;
|
break;
|
||||||
case P_CUSTOM:
|
case P_CUSTOM:
|
||||||
// TODO: Ok, what do we do here? We should probably convert this to hex
|
// TODO: Ok, what do we do here? We should probably convert this to hex
|
||||||
|
|||||||
@@ -22,8 +22,8 @@
|
|||||||
// Message types
|
// Message types
|
||||||
typedef enum {
|
typedef enum {
|
||||||
C_PRESENTATION = 0,
|
C_PRESENTATION = 0,
|
||||||
C_SET_VARIABLE = 1,
|
C_SET = 1,
|
||||||
C_REQ_VARIABLE = 2,
|
C_REQ = 2,
|
||||||
C_SET_WITH_ACK = 3,
|
C_SET_WITH_ACK = 3,
|
||||||
C_INTERNAL = 4,
|
C_INTERNAL = 4,
|
||||||
C_STREAM = 5 // For Firmaware and other larger chunks of data that need to be divided into pieces.
|
C_STREAM = 5 // For Firmaware and other larger chunks of data that need to be divided into pieces.
|
||||||
|
|||||||
@@ -111,11 +111,10 @@ void MySensor::findParentNode() {
|
|||||||
// Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to)
|
// Send ping message to BROADCAST_ADDRESS (to which all relaying nodes and gateway listens and should reply to)
|
||||||
build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING)
|
build(msg, s.nodeId, BROADCAST_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_PING)
|
||||||
sendWrite(BROADCAST_ADDRESS, msg, true);
|
sendWrite(BROADCAST_ADDRESS, msg, true);
|
||||||
// Wait for replies for max 10 seconds (or when buffer for all parent nodes have been filled up)
|
|
||||||
|
|
||||||
unsigned long enter = millis();
|
unsigned long enter = millis();
|
||||||
// Wait for ack responses 5 seconds
|
// Wait for ack responses slightly more than 1 second
|
||||||
while (millis() - enter < 5000) {
|
while (millis() - enter < 1200) {
|
||||||
process();
|
process();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +186,8 @@ bool MySensor::send(MyMessage message, bool enableAck) {
|
|||||||
message.sender = s.nodeId;
|
message.sender = s.nodeId;
|
||||||
if (enableAck) {
|
if (enableAck) {
|
||||||
mSetCommand(message,C_SET_WITH_ACK);
|
mSetCommand(message,C_SET_WITH_ACK);
|
||||||
|
} else {
|
||||||
|
mSetCommand(message,C_SET);
|
||||||
}
|
}
|
||||||
return sendRoute(message);
|
return sendRoute(message);
|
||||||
}
|
}
|
||||||
@@ -209,15 +210,14 @@ void MySensor::sketchInfo(const char *name, const char *version) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MySensor::request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) {
|
void MySensor::request(uint8_t childSensorId, uint8_t variableType, uint8_t destination) {
|
||||||
buildSend(msg, s.nodeId, destination, childSensorId, C_REQ_VARIABLE, variableType);
|
buildSend(msg, s.nodeId, destination, childSensorId, C_REQ, variableType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MySensor::requestTime() {
|
void MySensor::requestTime() {
|
||||||
buildSend(msg, s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_TIME);
|
buildSend(msg, s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MySensor::requestConfig() {
|
||||||
void MySensor::requestConfiguration() {
|
|
||||||
buildSend(msg, s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_CONFIG);
|
buildSend(msg, s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_CONFIG);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,7 +225,9 @@ boolean MySensor::process() {
|
|||||||
uint8_t pipe;
|
uint8_t pipe;
|
||||||
boolean available = RF24::available(&pipe);
|
boolean available = RF24::available(&pipe);
|
||||||
|
|
||||||
if (available && pipe<7) {
|
if (!available || pipe>6)
|
||||||
|
return false;
|
||||||
|
|
||||||
uint8_t len = RF24::getDynamicPayloadSize();
|
uint8_t len = RF24::getDynamicPayloadSize();
|
||||||
RF24::read(&msg, len);
|
RF24::read(&msg, len);
|
||||||
RF24::writeAckPayload(pipe,&pipe, 1 );
|
RF24::writeAckPayload(pipe,&pipe, 1 );
|
||||||
@@ -238,23 +240,24 @@ boolean MySensor::process() {
|
|||||||
debug(PSTR("rx: %d-%d-%d s=%d,c=%d,t=%d,cr=%s: %s\n"),
|
debug(PSTR("rx: %d-%d-%d s=%d,c=%d,t=%d,cr=%s: %s\n"),
|
||||||
msg.sender, msg.last, msg.destination, msg.sensor, mGetCommand(msg), msg.type, valid==0?"ok":valid==1?"ec":"ev", msg.getString());
|
msg.sender, msg.last, msg.destination, msg.sensor, mGetCommand(msg), msg.type, valid==0?"ok":valid==1?"ec":"ev", msg.getString());
|
||||||
|
|
||||||
if (ok) {
|
if (!ok)
|
||||||
|
return false;
|
||||||
|
|
||||||
uint8_t command = mGetCommand(msg);
|
uint8_t command = mGetCommand(msg);
|
||||||
uint8_t type = msg.type;
|
uint8_t type = msg.type;
|
||||||
uint8_t sender = msg.sender; //getSender();
|
uint8_t sender = msg.sender; //getSender();
|
||||||
uint8_t last = msg.last;
|
uint8_t last = msg.last;
|
||||||
uint8_t destination = msg.destination;
|
uint8_t destination = msg.destination;
|
||||||
|
|
||||||
if (relayMode && command == C_INTERNAL && type == I_PING && s.nodeId != AUTO) {
|
if (relayMode && command == C_INTERNAL && type == I_PING) {
|
||||||
// Relaying nodes should always answer ping messages
|
// Relaying nodes should always answer ping messages
|
||||||
// Wait a random delay of 0-2 seconds to minimize collision
|
// Wait a random delay of 0-2 seconds to minimize collision
|
||||||
// between ping ack messages from other relaying nodes
|
// between ping ack messages from other relaying nodes
|
||||||
randomSeed(millis());
|
delay(millis() & 0x3ff);
|
||||||
delay(random(2000));
|
|
||||||
uint8_t replyTo = msg.sender;
|
build(msg, s.nodeId, sender, NODE_CHILD_ID, C_INTERNAL, I_PING_ACK);
|
||||||
build(msg, s.nodeId, replyTo, NODE_CHILD_ID, C_INTERNAL, I_PING_ACK);
|
|
||||||
msg.set(s.distance);
|
msg.set(s.distance);
|
||||||
sendWrite(replyTo, msg);
|
sendWrite(sender, msg);
|
||||||
return false;
|
return false;
|
||||||
} else if (destination == s.nodeId) {
|
} else if (destination == s.nodeId) {
|
||||||
// This message is addressed to this node
|
// This message is addressed to this node
|
||||||
@@ -266,6 +269,7 @@ boolean MySensor::process() {
|
|||||||
|
|
||||||
if (command == C_INTERNAL) {
|
if (command == C_INTERNAL) {
|
||||||
if (type == I_PING_ACK) {
|
if (type == I_PING_ACK) {
|
||||||
|
// We've received a reply to our PING message looking for parent.
|
||||||
uint8_t distance = msg.getByte();
|
uint8_t distance = msg.getByte();
|
||||||
if (distance<s.distance-1) {
|
if (distance<s.distance-1) {
|
||||||
// Found a neighbor closer to GW than previously found
|
// Found a neighbor closer to GW than previously found
|
||||||
@@ -275,6 +279,7 @@ boolean MySensor::process() {
|
|||||||
eeprom_write_byte((uint8_t*)EEPROM_DISTANCE_ADDRESS, s.distance);
|
eeprom_write_byte((uint8_t*)EEPROM_DISTANCE_ADDRESS, s.distance);
|
||||||
debug(PSTR("p=%d, d=%d\n"), s.parentNodeId, s.distance);
|
debug(PSTR("p=%d, d=%d\n"), s.parentNodeId, s.distance);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
} else if (type == I_REQUEST_ID && sender == GATEWAY_ADDRESS) {
|
} else if (type == I_REQUEST_ID && sender == GATEWAY_ADDRESS) {
|
||||||
if (s.nodeId == AUTO) {
|
if (s.nodeId == AUTO) {
|
||||||
s.nodeId = msg.getByte();
|
s.nodeId = msg.getByte();
|
||||||
@@ -292,7 +297,7 @@ boolean MySensor::process() {
|
|||||||
return false;
|
return false;
|
||||||
} else if (relayMode && sender == GATEWAY_ADDRESS) {
|
} else if (relayMode && sender == GATEWAY_ADDRESS) {
|
||||||
if (type == I_RELAY_NODE && destination != GATEWAY_ADDRESS) {
|
if (type == I_RELAY_NODE && destination != GATEWAY_ADDRESS) {
|
||||||
// Someone at sensor net gateway side wants this node to refresh its relay node
|
// Gatewa/controller wants this node to refresh its parent node
|
||||||
findParentNode();
|
findParentNode();
|
||||||
// Send relay information back to sensor net gateway node device.
|
// Send relay information back to sensor net gateway node device.
|
||||||
buildSendPayload(msg,s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_RELAY_NODE,s.parentNodeId);
|
buildSendPayload(msg,s.nodeId, GATEWAY_ADDRESS, NODE_CHILD_ID, C_INTERNAL, I_RELAY_NODE,s.parentNodeId);
|
||||||
@@ -320,12 +325,12 @@ boolean MySensor::process() {
|
|||||||
// Check if sender requests an ack back.
|
// Check if sender requests an ack back.
|
||||||
if (command == C_SET_WITH_ACK) {
|
if (command == C_SET_WITH_ACK) {
|
||||||
ack = msg;
|
ack = msg;
|
||||||
mSetCommand(ack,C_SET_VARIABLE);
|
mSetCommand(ack,C_SET);
|
||||||
ack.sender = s.nodeId;
|
ack.sender = s.nodeId;
|
||||||
ack.destination = msg.sender;
|
ack.destination = msg.sender;
|
||||||
sendRoute(ack);
|
sendRoute(ack);
|
||||||
// The library user should not need to care about this ack request. Just treat it as a normal SET.
|
// The library user should not need to care about this ack request. Just treat it as a normal SET.
|
||||||
mSetCommand(msg,C_SET_VARIABLE);
|
mSetCommand(msg,C_SET);
|
||||||
}
|
}
|
||||||
// Call incoming message callback if available
|
// Call incoming message callback if available
|
||||||
if (msgCallback != NULL) {
|
if (msgCallback != NULL) {
|
||||||
@@ -396,8 +401,8 @@ boolean MySensor::process() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,6 @@
|
|||||||
#define CURRENT_NODE_PIPE ((uint8_t)1)
|
#define CURRENT_NODE_PIPE ((uint8_t)1)
|
||||||
#define BROADCAST_PIPE ((uint8_t)2)
|
#define BROADCAST_PIPE ((uint8_t)2)
|
||||||
|
|
||||||
#define SEND_RETRIES 3
|
|
||||||
#define FIND_RELAY_AFTER_FAILED_TRANSMISSIONS 5
|
#define FIND_RELAY_AFTER_FAILED_TRANSMISSIONS 5
|
||||||
|
|
||||||
#define build(_msg, _sender, _destination, _sensor, _cmd, _type) \
|
#define build(_msg, _sender, _destination, _sensor, _cmd, _type) \
|
||||||
@@ -127,10 +126,9 @@ class MySensor : public RF24
|
|||||||
*
|
*
|
||||||
* @param msg Message to send
|
* @param msg Message to send
|
||||||
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
|
* @param ack Set this to true if you want destination node to send ack back to this node. Default is not to request any ack.
|
||||||
* @param retry Number of attempts before giving up. Default is SEND_RETRIES
|
* @return true Returns true if message reached the first stop on its way to destination.
|
||||||
* @return true Retuns false if ack is enabled and no ack came back
|
|
||||||
*/
|
*/
|
||||||
bool send(MyMessage msg, bool ack=false/*, uint8_t retry=SEND_RETRIES*/);
|
bool send(MyMessage msg, bool ack=false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send this nodes battery level to gateway.
|
* Send this nodes battery level to gateway.
|
||||||
@@ -138,19 +136,6 @@ class MySensor : public RF24
|
|||||||
*/
|
*/
|
||||||
void sendBatteryLevel(uint8_t level);
|
void sendBatteryLevel(uint8_t level);
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches a value from gateway or some other sensor in the radio network. Resends request and waits for response.
|
|
||||||
* Note that if you have registered a callback in begin() it will also be called before message is returned
|
|
||||||
* by get()
|
|
||||||
*
|
|
||||||
* @param childSensorId The unique child id for the different sensors connected to this arduino. 0-254.
|
|
||||||
* @param variableType The variableType to fetch
|
|
||||||
* @param destination The nodeId of other node in radio network. Default is gateway
|
|
||||||
* @param number of attempts to fetch
|
|
||||||
* @return Message response
|
|
||||||
*/
|
|
||||||
// MyMessage get(uint8_t childSensorId, uint8_t variableType, uint8_t destination=GATEWAY_ADDRESS, uint8_t retry=SEND_RETRIES);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests a value from gateway or some other sensor in the radio network.
|
* Requests a value from gateway or some other sensor in the radio network.
|
||||||
* Make sure to add callback in begin() to handle request response.
|
* Make sure to add callback in begin() to handle request response.
|
||||||
@@ -162,7 +147,6 @@ class MySensor : public RF24
|
|||||||
*/
|
*/
|
||||||
void request(uint8_t childSensorId, uint8_t variableType, uint8_t destination=GATEWAY_ADDRESS);
|
void request(uint8_t childSensorId, uint8_t variableType, uint8_t destination=GATEWAY_ADDRESS);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests time from controller. Pick up response in callback.
|
* Requests time from controller. Pick up response in callback.
|
||||||
*
|
*
|
||||||
@@ -171,13 +155,13 @@ class MySensor : public RF24
|
|||||||
void requestTime();
|
void requestTime();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches configuration from controller. Returns true if metric system has been selected which means
|
* Requests configuration from controller. Returns true if metric system has been selected which means
|
||||||
* that sensor should report it's information in:
|
* that sensor should report it's information in:
|
||||||
* celsius, meter, cm, gram, km/h, m/s etc..
|
* celsius, meter, cm, gram, km/h, m/s etc..
|
||||||
* If false is returned the sensor should report data in imperial system which means
|
* If false is returned the sensor should report data in imperial system which means
|
||||||
* fahrenheit, feet, gallon, mph etc...
|
* fahrenheit, feet, gallon, mph etc...
|
||||||
*/
|
*/
|
||||||
void requestConfiguration();
|
void requestConfig();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user