Hi! im building a nrf24l01 network iot to display realtime charts with sensor's data, so im having trouble because i dont know what the best way to do this might be.
I need a group of sensors sending data to a base pc, and in the base pc it stamps the time when received, because it would be a mess to add time modules to each sensor's point, then all the data is sent at once, so it is received at once, not byte by byte sending.
Here is my first problem.
My approach to the solution was making a string so
String data;
data = "[";
for (int i=0; i < nsensors; i++){
if(i != nsensors-1) {
data += analogRead(i) + ",";
else {
data += analogRead(i) + "]"
}
}
In this way the format of the data can be received as [data,data,data] at the base pc, making it easier for me to format this in a list (python), as well as debug any wrong sent data, if i dont have "[", "]" this would be easy to know. Now the problem that i have is that i cannot send this as a string directly on the rf24Network library (GitHub - nRF24/RF24Network: OSI Layer 3 Networking for nRF24L01(+) Radios on Arduino and Raspberry Pi), not that i know at least.
So i can send a char array, but my problem with this would be transforming a non static size of that string, given that sensors values could change between 2 digits or 1 even to 3 or even 4, so char array would have variable size.
This is what i got know on the sender side
#include <RF24Network.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
int nsensors = 4;
String id = 1;
RF24 radio(10, 9); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 01; // Address of this node in Octal format ( 04,031, etc)
const uint16_t node00 = 00;
String data;
void setup() {
SPI.begin();
radio.begin();
network.begin(90, this_node); //(canal, dirección del nodo)
}
void loop() {
network.update();
data = "[" + id + ",";
for (int i=0; i<nsensores; i++){
if (i != nsensores-1){
data += analogRead(i) + ",";
} else {
data += analogRead(i) + "]";
}
}
//METHOD TO CONVERT VARIABLE STRING SIZE TO CHAR ARRAY (now data would be char array)
RF24NetworkHeader header(node00); // (Address where the data is going)
bool ok = network.write(header, &data, strlen(data)); // Send the data
}
and this on the receiver side
#include <RF24.h>
#include <RF24Network.h>
#include <SPI.h>
RF24 radio(10, 9); // nRF24L01 (CE,CSN)
RF24Network network(radio); // Include the radio in the network
const uint16_t this_node = 00; // Address of our node in Octal format ( 04,031, etc)
void setup() {
Serial.begin(9600);
radio.begin();
network.begin(90, this_node); //(channel, node address)
}
void loop() {
network.update();
while ( network.available() ) { // Is there any incoming data?
RF24NetworkHeader header;
len = network.getDyanamicPayloadSize();
char incomingData[len]; //or something like that, not sure tho
network.read(header, &incomingData, len); // Read the incoming data
Serial.println(incomingData);
}
}
Thank you for helping, if you want to know more info about the nrf24Network library here i read chunks of code. Would love to know the optimal way that you think how this should be done.