I am having a lot of trouble getting this to work. I have the transceivers attached to a Nano clone (master) and a WEMOS (ESP8266) D1 mini (slave). I googled and stumbled across Robin2's tutorial here. I incorporated the writeAckPayload into my larger process (which involves Blynk and much code -- however, this appeared to be an isolated issue to the rx/tx ack).
I could tell that the message was rx successfully, but the ack was not showing available.
I decided to try to simply upload the simple on-way TX and RX pgms from the link above (so I won't post them here -- only change is to the CE,CSN pins on the WEMOS). I discovered that ack seemed to be recognized on the first two messages, but then lost thereafter. Here is excerpt of the output from serial on the slave (notice Message 3 stuck infinitely):
Data received Message 0
ackPayload sent 103, -4006
Data received Message 1
ackPayload sent 102, -4007
Data received Message 2
ackPayload sent 101, -4008
Data received Message 3
ackPayload sent 100, -4009
Data received Message 3
ackPayload sent 109, -4000
Data received Message 3
ackPayload sent 108, -4001
Data received Message 3
ackPayload sent 107, -4002
Data received Message 3
ackPayload sent 106, -4003
Here is excerpt of output from serial on the Master:
Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino
SimpleTxAckPayload Starting
Data Sent Message 0 Acknowledge data 103, 0
Data Sent Message 1 Acknowledge data 103, 0
Data Sent Message 2 Acknowledge data 103, 0
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
Data Sent Message 3 Tx failed
This problem is repeatable and consistent. I have searched fruitlessly for a similar issue. Any ideas?
Thanks in advance.
Edit: Problem actually occurs on and after fourth tx, as zero is included. I was tired last night...
Robin2:
Have you got two regular Arduinos to test the program? I don't have a Wemos so if the problem is specific to it then I may not be able to help.
I'll try to switch the Wemos out for another Nano clone and see if that makes a difference. Good idea.
Robin2:
It would be a good idea to post both of the program that YOU have uploaded and tell us which on the Nano and which is on the Wemos.
The reported problem is with the Simple tutorial programs. Once/if I get that resolved, I'll see if my original programs continue to have an issue. If so, I'll definitely post them. Thank you.
Whandall:
How is the Nano NRF powered?
Does it have at least a capacitor?
What type of modules are used?
Right now I am prototyping, so both Wemos and Nano are using these. Power is currently supplied to both via USB from my laptop.
Whandall:
You will not manage to run the used high power modules from the Nano 3.3V rail,
get a 3.3V regulator for the NRF.
I'm confused by this. I am able to reliably write and read. It is simply the Ack data reply portion that seems to be omitted. If power is the issue, would you expect such a pattern? Ack works for three tx, then stops working. Every time.
(If my questions sound facetious, they honestly are not. I am really trying to understand.)
I have never seen high power NRFs running reliable from a Nano 3.3V rail.
If your symptom persists after running the Nano NRF from an adequate power supply,
we have to search elsewhere, but even standard NRFs have a hard time running from the Nanos 3.3v.
Whandall:
I have never seen high power NRFs running reliable from a Nano 3.3V rail.
If your symptom persists after running the Nano NRF from an adequate power supply,
we have to search elsewhere, but even standard NRFs have a hard time running from the Nanos 3.3v.
Thanks for clarifying. I will definitely keep this in mind. Your experience is appreciated.
Robin2:
I understand that but it is still a good idea to post the code as YOU have uploaded it. Just to be sure to be sure
...R
Fair enough.
Master - Nano code:
// SimpleTxAckPayload - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
//const byte slaveAddress[5] = {'R','x','A','A','A'};
const uint64_t slaveAddress = 0xABCDABCD71LL;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
char txNum = '0';
int ackData[2] = {-1, -1}; // to hold the two values coming from the slave
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
//===============
void setup() {
Serial.begin(9600);
Serial.println(F("Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino"));
Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
showData();
}
//================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
Serial.print(dataToSend);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
updateMessage();
}
else {
Serial.println(" Tx failed");
}
prevMillis = millis();
}
//=================
void showData() {
if (newData == true) {
Serial.print(" Acknowledge data ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.println(ackData[1]);
Serial.println();
newData = false;
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
Slave - Wemos code:
// SimpleRxAckPayload- the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 4
#define CSN_PIN 15
//const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
const uint64_t thisSlaveAddress = 0xABCDABCD71LL;
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
int ackData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;
//==============
void setup() {
Serial.begin(9600);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
radio.startListening();
}
//==========
void loop() {
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
updateReplyData();
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
Serial.print(" ackPayload sent ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.println(ackData[1]);
newData = false;
}
}
//================
void updateReplyData() {
ackData[0] -= 1;
ackData[1] -= 1;
if (ackData[0] < 100) {
ackData[0] = 109;
}
if (ackData[1] < -4009) {
ackData[1] = -4000;
}
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}
Robin2:
Have you got two regular Arduinos to test the program?
Robin2 nailed it. When I replaced the Wemos with Nano clone and set the CE/CSN pin values back, all works wonderfully. Now I am trying to remember where I got the CE/CSN values for the Wemos. I'm not sure I have confidence in them now. I am reading elsewhere that it should be CE/CSN pins as 0,16.
Will continue research, test, and report back. Thank you all.
EDIT: Multiple relatively reputable sources show the GPIO4/GPIO15 connections for CE/CSN on Wemos.
EDIT^2: I now understand the CE/CSN are configurable; hence, the designation in constructing the RF24. Testing with different CE/CSN pins garnered same results - ack works for first three tx, then no more. Back to the drawing board.
Problem resolved. Changed int ackdata[2] to int16_t ackdata[2].
Per TMRh20,
On the 32 bit device, it is 8 bytes, and on the nano it is 4.
If this is not accounted for, the incoming data is not cleared from the radio, (because the entire payload is not read out) and the RX buffer fills up, leading to the erroneous behavior after 3 payloads.