im trying to use a nrf24 card to send some data and the data will be floats/ints/unsigned longs. for example
int var1 = 2222
float var2 = 19.99
unsigned long var3 = 9999999
i dont use data structures very often and now im thinking this may be the only way to go. some type of array?
not sure what to do ive looked at a lot of different sample code but none of them really seem to lean towards what im going for. whats the easiest way to approach this? im using the RF24 library and nRF24L01
heres some clips of sample code ive thrown together trying to figure something out. trying to send theData structure throws compiler error "no matching function for call to 'RF24::write(Payload*)'"
// SimpleTx - 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'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "message0";
char txNum = '0';
unsigned long var = 100012;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
byte sendSize=0;
typedef struct {
int nodeId; //store this nodeId
unsigned long uptime; //uptime in ms
float temp; //temperature maybe?
} Payload;
Payload theData;
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write(&theData), sizeof(theData));
// 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) {
Serial.println(" Acknowledge received");
updateMessage();
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
and here is some sample code for another type of rf card i was trying to learn from the way the structure in this code was setup
i have new code that puts the float "temp" into a array and the receiving code is displaying the correct data but how can i send temp and humidity at the same time?
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
float temp = 76.00;
float humidity = 49.01;
int examplestate = 4;
unsigned long currentRunMillis = 12345678;
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
char buf2[16];
dtostrf(temp, 0, 2, buf2);
rslt = radio.write( &buf2, sizeof(buf2));
// Always use sizeof() as it gives the size as the number of bytes.
}
some one please help me figure out how to turn this array back into individual variables my code send a array of float variables but i need to compare these variables on the receive side and so ar all i can get is the same variable printed 4 times.
Please just make one Post and wait for an answer to that question before posting another Reply. It makes it much easier to help.
If you want to send a mix of variable types then just create a struct and send that.
You can create a struct like this (taken from something I am working on)
struct TrainMoveStruct {
byte curPos;
char moveState; // S tarting M oving L dr triggered D one
unsigned long startMillis;
};
TrainMoveStruct trainMove;