@@ -22,15 +22,22 @@ void Sensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e
22
22
23
23
// Start up the radio library
24
24
RF24::begin ();
25
+ RF24::setAutoAck (1 );
26
+ RF24::enableAckPayload ();
27
+ if (!RF24::isPVariant ()) {
28
+ debug (PSTR (" Sorry, you'll need to use the P version of NRF24L01.\n " ));
29
+ while (1 );
30
+ }
31
+
25
32
RF24::setChannel (channel);
26
33
RF24::setPALevel (paLevel);
27
34
RF24::setDataRate (dataRate);
28
- RF24::setAutoAck (true );
29
- RF24::setRetries (2 ,15 );
35
+ RF24::setRetries (5 ,15 );
30
36
RF24::setCRCLength (RF24_CRC_16);
31
37
RF24::enableDynamicPayloads ();
32
38
33
39
40
+
34
41
// All repeater nodes and gateway listen to broadcast pipe (for PING messages)
35
42
if (isRelay) {
36
43
RF24::openReadingPipe (BROADCAST_PIPE, TO_ADDR (BROADCAST_ADDRESS));
@@ -44,6 +51,7 @@ void Sensor::begin(uint8_t _radioId, rf24_pa_dbm_e paLevel, uint8_t channel, rf2
44
51
45
52
debug (PSTR (" Started %s.\n " ), isRelay?" relay" :" sensor" );
46
53
54
+
47
55
setupRadio (paLevel, channel, dataRate);
48
56
49
57
// Fetch relay from EEPROM
@@ -58,7 +66,8 @@ void Sensor::begin(uint8_t _radioId, rf24_pa_dbm_e paLevel, uint8_t channel, rf2
58
66
59
67
initializeRadioId ();
60
68
61
- // Open reading pipe for messages directed to this node
69
+ // Open reading pipe for messages directed to this node (set write pipe to same)
70
+ RF24::openReadingPipe (WRITE_PIPE, TO_ADDR (radioId));
62
71
RF24::openReadingPipe (CURRENT_NODE_PIPE, TO_ADDR (radioId));
63
72
64
73
// Send presentation for this radio node
@@ -224,11 +233,16 @@ boolean Sensor::sendWrite(uint8_t dest, message_s message, int length) {
224
233
message.header .from ,message.header .to , message.header .last , dest, message.header .childId , message.header .messageType , message.header .type , message.header .crc , message.data );
225
234
226
235
bool broadcast = message.header .messageType == M_INTERNAL && message.header .type == I_PING;
236
+
227
237
RF24::stopListening ();
228
238
RF24::openWritingPipe (TO_ADDR (dest));
229
239
bool ok = RF24::write (&message, min (MAX_MESSAGE_LENGTH, sizeof (message.header ) + length), broadcast);
230
240
RF24::startListening ();
231
241
242
+
243
+ // RF24::closeReadingPipe(WRITE_PIPE); // Stop listening to write-pipe after transmit
244
+ // delay(50);
245
+
232
246
if (ok)
233
247
debug (PSTR (" Sent successfully\n " ));
234
248
else
@@ -351,11 +365,11 @@ char* Sensor::get(uint8_t nodeId, uint8_t childId, uint8_t sendType, uint8_t rec
351
365
}
352
366
353
367
char * Sensor::getStatus (uint8_t childId, uint8_t variableType) {
354
- return get (GATEWAY_ADDRESS, childId, M_REQ_VARIABLE, M_ACK_VARIABLE , variableType);
368
+ return get (GATEWAY_ADDRESS, childId, M_REQ_VARIABLE, M_SET_VARIABLE , variableType);
355
369
}
356
370
357
371
char * Sensor::getStatus (uint8_t nodeId, int8_t childId, uint8_t variableType) {
358
- return get (nodeId, childId, M_REQ_VARIABLE, M_ACK_VARIABLE , variableType);
372
+ return get (nodeId, childId, M_REQ_VARIABLE, M_SET_VARIABLE , variableType);
359
373
}
360
374
361
375
@@ -385,22 +399,33 @@ void Sensor::requestIsMetricSystem() {
385
399
boolean Sensor::messageAvailable () {
386
400
uint8_t pipe ;
387
401
boolean available = RF24::available (&pipe );
402
+ if (available && pipe <7 ) {
388
403
389
- if (available) {
390
- debug (PSTR (" Message available on pipe %d\n " ),pipe );
391
- }
404
+ uint8_t len = RF24::getDynamicPayloadSize ();
405
+ RF24::read (&msg, len);
406
+ RF24::writeAckPayload (pipe ,&pipe , 1 );
407
+
408
+ if (available) {
409
+ debug (PSTR (" Message available on pipe %d\n " ),pipe );
410
+ }
392
411
412
+ uint8_t valid = validate (len-sizeof (header_s));
413
+ boolean ok = valid == VALIDATE_OK;
393
414
394
- if (available && pipe <7 ) {
395
- boolean ok = readMessage ();
415
+ // Make sure string gets terminated ok for full sized messages.
416
+ msg.data [len - sizeof (header_s) ] = ' \0 ' ;
417
+ debug (PSTR (" Rx: fr=%d,to=%d,la=%d,ci=%d,mt=%d,t=%d,cr=%d(%s): %s\n " ),
418
+ msg.header .from ,msg.header .to , msg.header .last , msg.header .childId , msg.header .messageType , msg.header .type , msg.header .crc , valid==0 ?" ok" :valid==1 ?" ec" :" ev" , msg.data );
419
+
420
+ // boolean ok = readMessage();
396
421
if (ok && msg.header .to == radioId) {
397
422
// This message is addressed to this node
398
423
debug (PSTR (" Message addressed for this node.\n " ));
399
- if (msg.header .from == GATEWAY_ADDRESS &&
400
- // If this is variable message from sensor net gateway. Send ack back.
401
- msg.header .messageType == M_SET_VARIABLE) {
402
- // Send back ack message to sensor net gateway
424
+ // Send set-message back to sender if sender wants this
425
+ if (msg.header .messageType == M_SET_WITH_ACK) {
403
426
sendVariableAck ();
427
+ // The library user should not need to care about this ack request. Just treat it as a normal SET.
428
+ msg.header .messageType = M_SET_VARIABLE;
404
429
}
405
430
// Return message to waiting sketch...
406
431
return true ;
@@ -423,7 +448,7 @@ message_s Sensor::getMessage() {
423
448
}
424
449
425
450
426
- boolean Sensor::readMessage () {
451
+ /* boolean Sensor::readMessage() {
427
452
uint8_t len = RF24::getDynamicPayloadSize();
428
453
RF24::read(&msg, len);
429
454
@@ -435,7 +460,7 @@ boolean Sensor::readMessage() {
435
460
debug(PSTR("Rx: fr=%d,to=%d,la=%d,ci=%d,mt=%d,t=%d,cr=%d(%s): %s\n"),
436
461
msg.header.from,msg.header.to, msg.header.last, msg.header.childId, msg.header.messageType, msg.header.type, msg.header.crc, valid==0?"ok":valid==1?"ec":"ev", msg.data);
437
462
return ok;
438
- }
463
+ }*/
439
464
440
465
/*
441
466
* calculate CRC8 on message_s data taking care of data structure and protocol version
0 commit comments