I think my brain is just burnt out at the moment...
I have the example from the NRFlite library for two way comms between nRF24L01 modules.
The hardware TX/RX example is working fine.
But, I need to add a lot more data into the comms and this is where I am getting stuck.
I want to add another 10 variables to the data that I am sending to the receiver.
And then a further 5 variables to the data coming back from the receiver.
Where to I put these additional bits of data? and where do I retrieve them?
Transmitter:
#include "SPI.h"
#include "NRFLite.h"
const static uint8_t RADIO_ID = 1;
const static uint8_t DESTINATION_RADIO_ID = 0;
const static uint8_t PIN_RADIO_CE = 9;
const static uint8_t PIN_RADIO_CSN = 10;
enum RadioPacketType
{
AcknowledgementData, // Produced by the primary receiver and provided to the transmitter via an acknowledgement data packet.
Heartbeat, // Sent by the primary transmitter.
BeginGetData, // Sent by the primary transmitter to tell the receiver it should load itself with an acknowledgement data packet.
EndGetData // Sent by the primary transmitter to receive the acknowledgement data packet from the receiver.
};
struct RadioPacket
{
RadioPacketType PacketType;
uint8_t FromRadioId;
uint32_t OnTimeMillis;
};
NRFLite _radio;
uint32_t _lastHeartbeatSendTime, _lastDataRequestTime;
void setup()
{
Serial.begin(115200);
if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN))
{
Serial.println("Cannot communicate with radio");
while (1); // Wait here forever.
}
Serial.println("Transmitter ONLINE");
}
void loop()
{
// Send a heartbeat once every second.
if (millis() - _lastHeartbeatSendTime > 999)
{
_lastHeartbeatSendTime = millis();
sendHeartbeat();
}
// Request data from the primary receiver once every 4 seconds.
if (millis() - _lastDataRequestTime > 3999)
{
_lastDataRequestTime = millis();
requestData();
}
}
void sendHeartbeat()
{
Serial.print("Sending heartbeat");
RadioPacket radioData;
radioData.PacketType = Heartbeat;
radioData.FromRadioId = RADIO_ID;
radioData.OnTimeMillis = millis();
if (_radio.send(DESTINATION_RADIO_ID, &radioData, sizeof(radioData)))
{
Serial.println("...Heartbeat Success");
}
else
{
Serial.println("...Heartbeat Failed");
}
}
void requestData()
{
Serial.println("Requesting data");
Serial.print(" Sending BeginGetData");
RadioPacket radioData;
radioData.PacketType = BeginGetData; // When the receiver sees this packet type, it will load an ACK data packet.
if (_radio.send(DESTINATION_RADIO_ID, &radioData, sizeof(radioData)))
{
Serial.println("...Success");
Serial.print(" Sending EndGetData");
radioData.PacketType = EndGetData; // When the receiver acknowledges this packet, we will get the ACK data.
if (_radio.send(DESTINATION_RADIO_ID, &radioData, sizeof(radioData)))
{
Serial.println("...Success");
while (_radio.hasAckData()) // Look to see if the receiver provided the ACK data.
{
RadioPacket ackData;
_radio.readData(&ackData);
if (ackData.PacketType == AcknowledgementData)
{
String msg = " Received ACK data from ";
msg += ackData.FromRadioId;
msg += ", ";
msg += ackData.OnTimeMillis;
msg += " ms";
Serial.println(msg);
}
}
}
else
{
Serial.println("...Failed");
}
}
else
{
Serial.println("...Failed");
}
}
Receiver:
#include "SPI.h"
#include "NRFLite.h"
const static uint8_t RADIO_ID = 0;
const static uint8_t DESTINATION_RADIO_ID = 1;
const static uint8_t PIN_RADIO_CE = 9;
const static uint8_t PIN_RADIO_CSN = 10;
enum RadioPacketType
{
AcknowledgementData, // Produced by the primary receiver and provided to the transmitter via an acknowledgement data packet.
Heartbeat, // Sent by the primary transmitter.
BeginGetData, // Sent by the primary transmitter to tell the receiver it should load itself with an acknowledgement data packet.
EndGetData // Sent by the primary transmitter to receive the acknowledgement data packet from the receiver.
};
struct RadioPacket
{
RadioPacketType PacketType;
uint8_t FromRadioId;
uint32_t OnTimeMillis;
};
NRFLite _radio;
void setup()
{
Serial.begin(115200);
if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN))
{
Serial.println("Cannot communicate with radio");
while (1); // Wait here forever.
}
}
void loop()
{
while (_radio.hasData())
{
RadioPacket radioData;
_radio.readData(&radioData);
if (radioData.PacketType == Heartbeat)
{
String msg = "Heartbeat from ";
msg += radioData.FromRadioId;
msg += ", ";
msg += radioData.OnTimeMillis;
msg += " ms";
Serial.println(msg);
}
else if (radioData.PacketType == BeginGetData)
{
Serial.println("Received data request, adding ACK data packet");
RadioPacket ackData;
ackData.PacketType = AcknowledgementData;
ackData.FromRadioId = RADIO_ID;
ackData.OnTimeMillis = millis();
// Add the data to send back to the transmitter into the radio.
// The next packet we receive will be acknowledged with this data.
_radio.addAckData(&ackData, sizeof(ackData));
}
else if (radioData.PacketType == EndGetData)
{
// The transmitter hopefully received the acknowledgement data packet at this point.
}
}
}