reading variables in struct{} and then transmit via nRF24L01

Hi all,

I am working on a AWS (Automatic Weather Station). Data is collected on site and then transmitted via a nRF24L01 to home base about 200 meters away.
I spent a fair bit of time setting up both nRF24L01 transmitter and receiver and got it nicely working.
However, so far I have only sent fixed values in a struct array and it works great.
Now I'm coming to the point that I want to read a sensor; put the reading in a variable and then transmit that variable to the receiver and this got me stumped.
The code compiles nicely but on the receiver end I can only see "0" as a value. all the other static values arrive in good order.

Here is my transmitter code (I have simplified the code to make it more readable):

/* nRF24L01 Transmitter code*/
#include <SPI.h>
#include <RF24.h>

int LDRvalue;

RF24 radio(9, 10); // CE, CSN
byte addresses[][6] = {"0"}; //Identify the transmitting and receiving bit

struct package
{int LDR = LDRvalue; float hum = 99.9; float DP2 = 99.9;};
typedef struct package Package;
Package data;
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void setup() {
  Serial.begin(9600); 
  radio.begin();  // Initiate the radio object
  radio.setChannel(124);  // 0 to 124, frequencies between 2.4 and 2.5 Ghz in chunks of 100 MHz. Use a channel unlikely to be used by Wifi, Microwave ovens etc 
  radio.setPALevel(RF24_PA_MIN);  // Set the transmit power to lowest available to prevent power supply related issues, can also be set to MAX  
  radio.setDataRate(RF24_2MBPS);  // Set the speed of the transmission to the quickest available, can be set to 250KB or 1MBPS to increase range  
  radio.openWritingPipe(addresses[0]);  // Open a writing and reading pipe on each radio, with opposite addresses
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void loop() {  
  LDRvalue = analogRead(A0);
  Serial.println(LDRvalue);
  
  radio.write( &data, sizeof(data));     
  delay(1000);
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

So basically I'm reading the value of an LDR sensor at pin A0. This is then stored as an integer variable and then I want to include the variable in the struct so that it can be transmitted to the receiver.
But no matter how hard I try I cannot have it working.
Any tips/ideas will be much appreciated. Also, does anybody know a good reference where I can familiarize myself with the struct command? (Sorry but the reference at arduino.cc is pretty much useless).
I am aware about the maximum size of 32 bytes that can be transmitted at once. So, the program is set up so that I can create multiple packages and send them individually.
Cheers!

Luc

You are getting the "variable" as Zero because that was its value at the time you created the structure.

A structure is simply a collection of variables (over simplified).

What you Need to do is Change the value within the structure.

You may or may not even Need the variable LDRvalue. To Change the value that is within the structure, use

package.LDR = 123; or package.LDR = LDRValue;

Hi Jaba,

I was excited when I received your reply and thought it was going to work. However the compiler doesn't like it.
Here is what I tried:

{package.LDR = LDRvalue; float hum = 99.9; float DP2 = 10.0; char text1[100] = "17-22-01";};
and then
{package1.LDR = LDRvalue; float hum = 99.9; float DP2 = 10.0; char text1[100] = "17-22-01";};
and then
{int package.LDR = LDRvalue; float hum = 99.9; float DP2 = 10.0; char text1[100] = "17-22-01";};
and then
{int package1.LDR = LDRvalue; float hum = 99.9; float DP2 = 10.0; char text1[100] = "17-22-01";};

The previous lines triggers following fault in the compiler:

expected';' at end of member declaration

Post your complete program as it is now

Hi UKHeliBob,

Here's the program as is:

/* nRF24L01 Transmitter code
Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
Connections for UNO: GND= GND, VCC= +3.3V, CE= pin7, CSN= pin8, MOSI= pin11, MISO= pin12, SCK= pin13, IRQ= not used
Connections for MEGA2560: GND= GND, VCC= +3.3V, CE= pin7, CSN= pin8, MISO= pin50, MOSI= pin51, SCK= pin52, IRQ= not used*/
#include <SPI.h>
#include <RF24.h>

int LDRvalue;

RF24 radio(9, 10); // CE, CSN
//pinMode(53,OUTPUT); //This line should be added when using a MEGA
byte addresses[][6] = {"0"}; //Identify the transmitting and receiving bit

struct package0
{int id=1; float temperature = 18.3; float humidity = 44.3; float DP = 15.0; char text0[100] = "2018-02-04 ";};
typedef struct package0 Package0;
Package0 data0;

struct package1
{int package1.LDR = LDRvalue; float hum = 99.9; float DP2 = 10.0; char text1[100] = "17-22-01";};
typedef struct package1 Package1;
Package1 data1;
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void setup() {
  Serial.begin(9600); 
  radio.begin();  // Initiate the radio object
  radio.setChannel(124);  // 0 to 124, frequencies between 2.4 and 2.5 Ghz in chunks of 100 MHz. Use a channel unlikely to be used by Wifi, Microwave ovens etc 
  radio.setPALevel(RF24_PA_MIN);  // Set the transmit power to lowest available to prevent power supply related issues, can also be set to MAX  
  radio.setDataRate(RF24_2MBPS);  // Set the speed of the transmission to the quickest available, can be set to 250KB or 1MBPS to increase range  
  radio.openWritingPipe(addresses[0]);  // Open a writing and reading pipe on each radio, with opposite addresses
  radio.openReadingPipe(1, addresses[0]); 
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
void loop() {  
  LDRvalue = analogRead(A0);
  Serial.println(LDRvalue);
  radio.write( &data0, sizeof(data0));
  data0.id = data0.id + 1;
  radio.write( &data1, sizeof(data1));     
  delay(1000);
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1 Like
  int package1.LDR = LDRvalue;

Looks like you are trying to use the name of the struct within the struct that you are declaring. That is never going to end well

Try
 int LDR = LDRvalue;in the struct declaration.

Later in the code you can then use

package1.LDR = someValue;

to change the value

As UKHeliBob already stated, that's the way it's done.

You Need to understand a bit more about structures.

The line: struct package0
{int id=1; float temperature = 18.3; float humidity = 44.3; float DP = 15.0; char text0[100] = "2018-02-04 "};

defines a structure named package0. The structure contains everything which is inside the braces {}. In your case, it contains an element named id which is of type int, an element named temperature which is of type float, etc.

The entire structure can be referred to using the structure Name, such as in send(package0);

Single elements within the structure are referred to as, for example, package0.temperature.

Really put over simply, think of the structure as a Family. The entire Family are the Johnsons. The individual elements of the Family are Johnson.Paul, Johnson.Sarah, Johnson.Bobby, etc.

Because the structure is usually only "declared" once. It is only useful to use the entire line "struct package0
{int id=1; float temperature = 18.3; float humidity = 44.3; float DP = 15.0; char text0[100] = "2018-02-04 ";};" once.

Though, as you have done, it is acceptable to initialize the individual elements at the time the structure is declared, it isn't necessary and is in fact most often not done because you seldom would know, for example, the current temperature at the time you are writing the program. In other words, you could use this shorter Version to declare the structure and later assign values to the individual elements.

struct package0
{int id; float temperature; float humidity; float DP; char text0[100]};

Does that help understand structures a bit more?

Hi Jaba,

That makes a lot of sense! It's becoming clear now how this works.
Currently I'm at work but will try it out tonight.
The analogy with the Johnson family makes sense. Sometimes, us novices, need to have simple explanations to understand what's going on and this is definitly a step forward.
I'll keep you in the loop!
Have a great day!

Luc