json stop working with nrf24l01

Hi am trying to use to 2 arduino UNO each is connected to nrf24l01 and am trying to send an array of char from other to the other
and on the other am trying to (ajson) to get the value for (red, green , blue).

The problem was at the beginning it work correct but after while the json stop working.

code for transmitter

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

static char message[28] = {"{\"Blue\":255,\"Red\":255}"};
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 1;

// Address of the other node
const uint16_t other_node = 0;


void setup() {
Serial.begin(9600);
  Serial.println("RF24Network/examples/helloworld_tx/");
 
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
}

void loop() {
// Pump the network regularly
  network.update();

    Serial.print("Sending...");
  
    RF24NetworkHeader header(/*to node*/ other_node);
    bool ok = network.write(header,&message,sizeof(message));
    if (ok){
      Serial.println(message);  
       Serial.println("ok");  

}
  
    else{
      Serial.println("failed.");
    }

        delay(1000);
  }

Code for Recevier

#include <Adafruit_NeoPixel.h>
#include <MirfHardwareSpiDriver.h>
#include <aJSON.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#define PIN 6
#include "pitches.h"
#include <avr/pgmspace.h>


//aJsonObject* root = aJson.createObject();

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

 int noteDurations[] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
 int melody[] = { NOTE_A3};
//aJsonObject* AfterReceive;
aJsonObject* AfterReceive = aJson.createObject();
//Red
aJsonObject* query;
//Green
aJsonObject* query1;

static char message[30];

// CE, CSN
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 0;

// Address of the other node
const uint16_t other_node = 1;


void setup()
{

  Serial.begin(9600);
  Serial.println("Hello");
 //--- Setup  ALL rf radios
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
  strip.begin();
  strip.show();
}

void loop()
{

  // Pump the network regularly
  network.update();

  // Is network available?
  while(network.available())
  {


    RF24NetworkHeader header;
    network.read(header,message,sizeof(message));
    Serial.println("Received: ");
    Serial.println(message);  





  AfterReceive = aJson.parse(message);
Serial.println("After Json parse");




if (AfterReceive != NULL){
    Serial.println("Inside if");

          query  = aJson.getObjectItem(AfterReceive , "Red");
          int string =int( aJson.print(query));
          Serial.print(query->valueint);
          Serial.println("");
strip.setPixelColor(0,int(query),0,0);

          query1  = aJson.getObjectItem(AfterReceive , "Blue");
          char* string1 = aJson.print(query1);
          Serial.print(query1->valueint);
          aJson.deleteItem(query1);


strip.setPixelColor(1,0,0,int(query1));
strip.setPixelColor(2,int(query),0,int(query1));
          strip.show();
          query = NULL;
          query1 = NULL;
delay(1000);
Serial.println("");


break;
delay(2000);
  }}}

Thanks in advanced

The delay() 's in the code can corrupt - or cause overflow - of serial buffers.

Hallo robtillaart thanks for your replay. i have tried it but it still did not work.
also i have tried freeRam () method in the beginning it print around 600 after that it decrease around 240.

Transmitter code

#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>

static char message[25] = {"{\"Red\":255,\"Blue\":255}"};
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 1;

// Address of the other node
const uint16_t other_node = 0;


void setup() {
  
  Serial.begin(9600);
  Serial.println("RF24Network/examples/helloworld_tx/");
 
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);

  }

void loop() {

  // Pump the network regularly
  network.update();

    Serial.print("Sending...");
  
    RF24NetworkHeader header(/*to node*/ other_node);
    
    bool ok = network.write(header,&message,sizeof(message));
    
    if (ok){
      
      Serial.println(message);  
       Serial.println("ok");  

}
  
      else{
    
        Serial.println("failed.");
      
      }


  }

Receiver code

#include <Adafruit_NeoPixel.h>
#include <aJSON.h>
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
#define PIN 6
//#include "pitches.h"
//#include <avr/pgmspace.h>


//aJsonObject* root = aJson.createObject();

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

 //int noteDurations[] = {1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3,2,1};
 //int melody[] = { NOTE_A3};

aJsonObject* AfterReceive;
//Red

//Green

/*Blue
aJsonObject* query2;
//
aJsonObject* query3;
//NumOfRow
aJsonObject* query4;
// The user choose the value that if the value of the sensor reach it to alarm him
aJsonObject* query5;
// The value of the sensor
aJsonObject* query6;
//Oparation Mode
aJsonObject* query7;
*/// The received message from the master
static char message[26];

// CE, CSN
RF24 radio(9,10);

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 0;

// Address of the other node
const uint16_t other_node = 1;


void setup()
{

  Serial.begin(9600);
  Serial.println(F("Hello"));
  Serial.println();

Serial.println(freeRam());
 //--- Setup  ALL rf radios
  SPI.begin();
  radio.begin();
  network.begin(/*channel*/ 90, /*node address*/ this_node);
  strip.begin();
  strip.show();
  
}

void loop()
{

  // Pump the network regularly
  network.update();

  // Is there anything ready?
  while(network.available())
  {


    RF24NetworkHeader header;
    network.read(header,message,sizeof(message));
    Serial.println(F("Received: "));
    Serial.println(message);  

//aJson.deleteItem(AfterReceive);

      AfterReceive = NULL;
      
      if (AfterReceive == NULL){
      AfterReceive = aJson.parse(message);
      Serial.println(F("After Json parse"));

}


if (AfterReceive != NULL){
    Serial.println(F("Inside if"));

          aJsonObject* query  = aJson.getObjectItem(AfterReceive , "Red");
          int string =int( aJson.print(query));
          Serial.print(query->valueint);
          strip.setPixelColor(0,int(query),0,0);
  aJson.deleteItem(query);

Serial.println();

Serial.println(freeRam());

aJsonObject* query1  = aJson.getObjectItem(AfterReceive , "Blue");
          char* string1 = aJson.print(query1);
          Serial.print(query1->valueint);
          aJson.deleteItem(query1);
  aJson.deleteItem(query1);
/*          query2  = aJson.getObjectItem(AfterReceive , "Green");
          char* string2 = aJson.print(query2);
          Serial.print(query2->valueint);
          aJson.deleteItem(query2);
          strip.setPixelColor(3,0,int(query2),0);*/


strip.setPixelColor(1,0,0,int(query1));

strip.setPixelColor(2,int(query),0,int(query1));
strip.show();


Serial.println();

Serial.println(freeRam());
          


Serial.println();

Serial.println(freeRam());

break;

  }}
  Serial.println();

Serial.println(freeRam());

}

int freeRam () 
{
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

[code]

[/code]

If you go back to the basics and just do a send char receive char - does it work?
Often stripping parts of the code can help identify the buggy part.

yes it work. The problem if it goes inside Json part, the memory start decreasing.

Hello robtillaart thank you i solve it :slight_smile:

Maybe you can post the working code for when others have similar problems?

sure but i need few days to add comment on it and add more things :wink:

Hello robtillaart

here is the code but it can not read each massage (take one and forget one) so maybe you can help me

RX.txt (11 KB)

TX.txt (2.5 KB)

Please can anybody help

the code is work but it receive one message but the next one does not receive it

the indentation of your code makes it hard to read.
Usage of the String class if discouraged as it fragments the memory. So replace it with fixed size char arrays instead.

Thank you robtillaart

I tried it but it still the same receive one message, miss one message. =(

can it be that processing takes too much time?

Thanks i tried it before and also now i increase the delay on the TX (powerDown and powerUp) to 30000 and still same