Go Down

Topic: nRF24L01+ Module with the RF24Mesh Library - Char Array Receiving Problem (Read 203 times) previous topic - next topic

carlos14

I am using the nRF24L01+ module with the RF24Mesh library. I am able to transmit the data I want except for one character. Obviously, I am missing something but I can't figure out what. Can you please help me?

Here is the problem

I transmit:

2
5
1.00
2.00
3.00
4.00
2017-09-06 01:02:03


and I receive:

2
5
1.00
2.00
3.00
4.00
⸮017-09-06 01:02:03


I am getting instead of 2.


Here is the abbreviated version of the transmitter code:

Code: [Select]

/*** Tx ***/
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
#include <SPI.h>
#include "printf.h"

RF24 radio(9, 10); //Nano
RF24Network network(radio);
RF24Mesh mesh(radio, network);

long previousMillis = 0;
long interval = 30000;

struct payload_t
{
  int node;
  float v1;
  float v2;
  float v3;
  float v4;
  char dt[20];
};

int node_;
float v1_;
float v2_;
float v3_;
float v4_;
char dt_[20];

void setPayloadValues()
{
  node_ = 2;
  v1_ = 1.00;
  v2_ = 2.00;
  v3_ = 3.00;
  v4_ = 4.00;

  String sdt = "2017-09-06 01:02:03";
  sdt.toCharArray(dt_, sdt.length() + 1);
}

void setup()
{
  Serial.begin(115200);
  printf_begin();
  mesh.setNodeID(2);
  mesh.begin(97, RF24_250KBPS, 60000);
  radio.printDetails();
}

void loop()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;
    
    setPayloadValues();

    payload_t payload = {node_, v1_, v2_, v3_, v4_, strcpy(payload.dt, dt_)};

    if (!mesh.write(&payload, 'M', sizeof(payload)))
    {
      //Problem
    }
  }
}



and this is the receiver

Code: [Select]

/*** Rx ***/
#include <RF24Network.h>
#include <RF24.h>
#include <RF24Mesh.h>
#include <SPI.h>
#include <EEPROM.h>
#include "printf.h"

RF24 radio(9, 10); //Nano
RF24Network network(radio);
RF24Mesh mesh(radio, network);

struct payload_t
{
  int node;
  float v1;
  float v2;
  float v3;
  float v4;
  char dt[20];
};

void setup() {
  Serial.begin(115200);
  printf_begin();
  mesh.setNodeID(0);
  mesh.begin(97, RF24_250KBPS, 60000);
  radio.printDetails();
}


void loop() {
  mesh.update();
  mesh.DHCP();

  if (network.available()) {
    RF24NetworkHeader header;
    network.peek(header);

    payload_t payload;

    switch (header.type) {
      case 'M':
        network.read(header, &payload, sizeof(payload));

        Serial.println(String(payload.node));
        Serial.println(String(header.from_node));
        Serial.println(String(payload.v1));
        Serial.println(String(payload.v2));
        Serial.println(String(payload.v3));
        Serial.println(String(payload.v4));
        Serial.println(payload.dt);
        break;

      default:
        network.read(header, 0, 0);
        Serial.println(header.type);
        break;
    }
  }
}

PaulS

Code: [Select]

        Serial.println(String(payload.node));
        Serial.println(String(header.from_node));
        Serial.println(String(payload.v1));
        Serial.println(String(payload.v2));
        Serial.println(String(payload.v3));
        Serial.println(String(payload.v4));

Do you REALLY believe that the println() function doesn't know how to print an int without your "help" converting it to a string embedded in a String?

Code: [Select]
  sdt.toCharArray(dt_, sdt.length() + 1);
The second argument to toCharArray() is NOT the amount of data to extract. It is the size of the array to copy do.

Of course, there was NO reason to store the string in a String in the first place.

Code: [Select]
    payload_t payload = {node_, v1_, v2_, v3_, v4_, strcpy(payload.dt, dt_)};
You are trying to create an initialize an instance of a struct, populating it with an element of the struct being created. Why?

Code: [Select]
    payload_t payload = {node_, v1_, v2_, v3_, v4_, ""};
    strcpy(payload.dt, dt_);


The art of getting good answers lies in asking good questions.

carlos14

@PaulS,

Thank you for your reply. Obviously, I don't know what I am doing. Even though your insight is greatly appreciated you still did not answer the original question, I am correct?

No problem, I did find an answer, I'll be posting a solution to the problem later today.


Thank you,

Go Up