Skip to content

Commit 893fbc6

Browse files
committed
Ok, had to introduce a small delay on the ack-sensing side to let ack-recieving side have to to switch to listening mode.
1 parent f440982 commit 893fbc6

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

libraries/MySensors/Sensor.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,19 @@ boolean Sensor::sendWrite(uint8_t dest, message_s message, int length) {
226226
// int retry = WRITE_RETRY;
227227
RF24::stopListening();
228228
RF24::openWritingPipe(TO_ADDR(dest));
229-
ok = RF24::write(&message, min(MAX_MESSAGE_LENGTH, sizeof(message.header) + length), broadcast);
230-
debug(PSTR("Sensor::sendWrite() - return value = %s\n"), ok ? "true" : "false" );
229+
RF24::startWrite(&message, min(MAX_MESSAGE_LENGTH, sizeof(message.header) + length), broadcast);
231230
RF24::startListening();
232231
RF24::closeReadingPipe(WRITE_PIPE); // Stop listening to write-pipe after transmit
233232

234-
if (!broadcast && (radioId == GATEWAY_ADDRESS)) {
233+
if (!broadcast) {
235234
// ---------------- WAIT FOR ACK ------------------
236235
unsigned long startedWaiting = millis();
237236
bool timeout = false;
238-
// Wait max 100 ms
237+
// Wait for ack message maximum 50 ms
239238
while ( !RF24::available() && !timeout ) {
240-
if (millis() - startedWaiting > 100 ) {
239+
if (millis() - startedWaiting > ACK_MAX_WAIT ) {
241240
timeout = true;
242-
debug(PSTR("Transmit: timeout\n"));
241+
debug(PSTR("Ack: receive timeout\n"));
243242
ok = false;
244243
}
245244
}
@@ -248,12 +247,12 @@ boolean Sensor::sendWrite(uint8_t dest, message_s message, int length) {
248247
uint8_t idest;
249248
RF24::read( &idest, sizeof(uint8_t));
250249
if (dest != idest) {
251-
debug(PSTR("Transmit: size problem\n"));
250+
debug(PSTR("Ack: received ack from the wrong sensor\n"));
252251
ok = false;
253252
}
254253
} else {
255254
ok = false;
256-
debug(PSTR("Transmit: general fail\n"));
255+
debug(PSTR("Ack: received non ack msg.\n"));
257256
}
258257
//--------------------
259258
}
@@ -456,11 +455,13 @@ boolean Sensor::readMessage() {
456455
RF24::read(&msg, len);
457456

458457
if (!(msg.header.messageType==M_INTERNAL && msg.header.type == I_PING_ACK)) {
458+
delay(ACK_SEND_DELAY); // Small delay here to let other side switch to reading mode
459459
RF24::stopListening();
460460
RF24::openWritingPipe(TO_ADDR(msg.header.last));
461461
RF24::write(&radioId, sizeof(uint8_t));
462462
RF24::startListening();
463463
RF24::closeReadingPipe(WRITE_PIPE); // Stop listening to write-pipe after transmit
464+
debug(PSTR("Sent ack msg to %d\n"), msg.header.last);
464465
}
465466
uint8_t valid = validate(len-sizeof(header_s));
466467
boolean ok = valid == VALIDATE_OK;

libraries/MySensors/Sensor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
#define CURRENT_NODE_PIPE ((uint8_t)1)
5151
#define BROADCAST_PIPE ((uint8_t)2)
5252

53+
#define ACK_MAX_WAIT 50
54+
#define ACK_SEND_DELAY 5
5355

5456
#define WRITE_RETRY 5
5557
#define FIND_RELAY_RETRIES 20

0 commit comments

Comments
 (0)