Strange code behaviour with nrf24, sim800l

Good morning all,
i'm trying to develop a base station tracking different spot in my house with node sensors that sleep most of the time and then wake up, make the readings (temp, humidity ecc.) and send them trough a nrf24 to a base station within range.

This station basically is waken up by the nrf24 (interrupt pin IRQ), writes the data to a file on a microSD and then send the data packet on my server via Gprs with a Sim800L module.

So far i've been doing some test and everything seems to work flawlessy on my nodes but i've experienced some problems in the receiving node. I think they are related to power supply problems (i had a lot of trouble in running the sim800l and i solved by adding a 470uf cap just before it).

The whole system is powered by 4 18650 lipos in parallel charged by a solar panel and tp4056. I got some random reboot (and yes i already solved my memory issues by replacing serial.print with the F macro). The weirdest thing is that the system works i would say 80% good without the solar panel and it struggles a lot with the panel. If i replace the 4 lipos with a 1s battery and solar panel it works better but still very unstable and not reliable.

Now i managed to make it run for a whole night and it hasnt crashed yet but i hope you could give me a hint. I'll let you see my code and i am ready to answer all your questions. I know it's really difficult without seeing the actual hardware but i hope you could come up with a hint or something. Thanks in advance.

Usually it gets stuck at the PostData() method.

Here is the code.

void setup()   /****** SETUP: RUNS ONCE ******/
{

  //change to 10k for bootup and gsm connection
  delay(4000); //wait for everything to start up
    
  Serial.begin(115200);
  
  //inizializza la micro SD
  if (!SD.begin(2)) {
    Serial.println(F("Inizializzazione della micro SD fallita"));
    return;
  }
  else{
  Serial.println(F("SD Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
  }

  
  //inizializza la sim800L
  serialSIM800.begin(9600);
  delay(1000);
  Serial.println(F("SIM800L Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
    
  printf_begin(); //inizializza printf per il debug del nrf24l01
  myRadio.begin();  //inizializza nrf24l01

  myRadio.enableAckPayload(); //SetACK payload
  myRadio.setAutoAck(true);  //set autoACK
    
  myRadio.setChannel(108);  //Set channel
  myRadio.setPALevel(RF24_PA_MAX);  // Set the PA Level   
  myRadio.setDataRate(RF24_250KBPS); //set data rate

  myRadio.maskIRQ(1,1,0);               //mask all IRQ triggers except for receive (1 is mask, 0 is no mask)
   
  myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  myRadio.startListening();
 
  myRadio.printDetails();
  Serial.println(F(""));
    
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{ 
   

  //wait untill a packet arrive
     
  if ( myRadio.available()) // Check for incoming data from transmitter
  {
    
    while (myRadio.available())  // While there is data ready
    {

      Serial.println(F(""));
      Serial.println(F("WAKE UP, PACKET RECEIVED"));        
      delay(1000);
      
      myRadio.read( &radioData, sizeof(radioData) ); // read the incoming data
      // DO something with the data, like print it
      Serial.println(F("OK, dati ricevuti"));  
      myRadio.writeAckPayload( 1, &ACK, sizeof(ACK) );
      printf("\nWriting ACK: %d", ACK);
            
    }

    delay(2000);    
      
    //leggo la Vbat della regina e l'assegno alla variabile
    VbatRegina = readBattery();  

    delay(2000);
        
     myFile = SD.open("data.txt", FILE_WRITE);

  // if the file opened okay, write to it:
    if (myFile) {
    Serial.println(F("Writing to DATA.txt..."));
    myFile.println();
    myFile.print(proprietario);
    myFile.print(",");
    delay(10);
    myFile.print(IdRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.IdArnia);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.packetN);
    myFile.print(",");
    delay(10);
    myFile.print(VbatRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.vbat);
    myFile.print(",");   
    myFile.print(radioData.tin);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.hum);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.tex);
    myFile.print(",");
    delay(10);   
    myFile.print(radioData.peso);
    myFile.print(",");
    delay(10);   
    // close the file:
    myFile.close();
    Serial.println(F("done."));
    
  } else {
    // if the file didn't open, print an error:
    Serial.println(F("ERROR opening the file"));
  }

    
    delay(3000);

    //post data on server
    postData();          
   
  } //END Radio available

  delay(3000);
    
  Serial.println(F("Sleeping Till Interrupt"));
 
  delay(100); //delay to allow serial to fully print before sleep
  
  sleep.pwrDownMode(); //set sleep mode
  
  //Sleep till interrupt pin equals a particular state. 
  //In this case "falling" change from 1 to 0.
  sleep.sleepInterrupt(digitalPinToInterrupt(InterruptPin),FALLING); //(interrupt Number, interrupt State)
  
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
float readBattery(){

  String str = "";
  
  Serial.println(F("Lettura dati batteria: "));

  int percentuale;
  float volt;

  delay(1000);
  
  serialSIM800.println(F("AT+CBC"));
  delay(400);
  while(serialSIM800.available()){
    str += (char)serialSIM800.read();
  }
  Serial.println(str);
  if(str.indexOf("+CBC") >= 0){
  percentuale = str.substring(str.indexOf("+CBC:")+8).toInt();
  volt = str.substring(str.indexOf("+CBC:")+11).toInt();

  volt= volt/1000;

  /*
  Serial.println(F("percentuale batteria: "));
  Serial.println(percentuale);
  Serial.println(F("Volt : "));
  Serial.println(volt);  
  */
  
  return volt;
  
  }
}


void postData(){ 

  serialSIM800.println(F("AT+SAPBR=3,1,\"APN\",\"web.coopvoce.it\""));//setting the APN, the second need you fill in your local apn server
  delay(1000);
  ShowSerialData();

  serialSIM800.println(F("AT+SAPBR=1,1"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+SAPBR=2,1"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+HTTPINIT"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+HTTPPARA=\"CID\",1"));
  delay(3000);
  ShowSerialData();

   serialSIM800.print(F("AT+HTTPPARA=\"URL\",\"bekeeper.altervista.org/insert.php?Proprietario="));
   serialSIM800.print(proprietario);
   serialSIM800.print(F("&IdRegina="));
   serialSIM800.print(IdRegina);
   serialSIM800.print(F("&IdArnia="));
   serialSIM800.print(radioData.IdArnia);
   serialSIM800.print(F("&packetn="));
   serialSIM800.print(radioData.packetN);
   serialSIM800.print(F("&VbatRegina="));
   serialSIM800.print(VbatRegina);
   serialSIM800.print(F("&VbatArnia="));
   serialSIM800.print(radioData.vbat);
   serialSIM800.print(F("&TIn="));
   serialSIM800.print(radioData.tin);
   serialSIM800.print(F("&Hum="));
   serialSIM800.print(radioData.hum);
   serialSIM800.print(F("&TEx="));
   serialSIM800.print(radioData.tex);
   serialSIM800.print(F("&Peso="));
   serialSIM800.print(radioData.peso);
   serialSIM800.println(F("\""));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPACTION=0"));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPREAD"));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPTERM"));
   delay(3000);
   ShowSerialData();


   serialSIM800.println(F("AT+SAPBR=0,1"));
   delay(3000);
   ShowSerialData();  

   
  
 }


void ShowSerialData(){


      while(serialSIM800.available()!=0)
      {
      Serial.write(serialSIM800.read());
      }
  
}

Why are there all those delay()s when writing to the file?

Why are there ANY delay()s in the code?

  delay(100); //delay to allow serial to fully print before sleep

Bullshit. Use Serial.flush() (the ONLY time it should be used) which will block only as long as needed to make sure the outgoing serial data buffer is empty.

cant you set everything to the same baud rate

PaulS:
Why are there all those delay()s when writing to the file?

Why are there ANY delay()s in the code?

  delay(100); //delay to allow serial to fully print before sleep

Bullshit. Use Serial.flush() (the ONLY time it should be used) which will block only as long as needed to make sure the outgoing serial data buffer is empty.

Hi, i used the delays because i found out that on the sketches that i found on the internet there were the delays here and there and i thought they were helpful to smooth out the code.

That is a good point but will it also improve the overall stability?

joeblogs:
cant you set everything to the same baud rate

Hi, can you elaborate more on that? I mean i have seen that the sim800l with software serial struggles at 115200 and i just left the default baud rate for the nrf24 which was 115200. How can i overcome this problem? do you think this could generate instability in the arduino itself?

travis^__^:
and i just left the default baud rate for the nrf24 which was 115200. How can i overcome this problem? do you think this could generate instability in the arduino itself?

You have not posted a complete program in your Original Post.

The nRF24 module uses SPI so the idea of a baud rate is meaningless.

When you get rid of all the delay()s and post a revised version of your program I will look at it.

If you do need to control the timing of something have a look at how millis() is used to manage timing without blocking in Several things at a time

...R
Simple nRF24L01+ Tutorial

Do you mean that you also want to see the transmitter code?

I tried removing all the delays but what i got was this mess.

SD Inizializzata corettamente
-----------------------------
SIM800L Inizializzata corettamente
-----------------------------
STATUS		 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	 = 0xe7e7e7e7e7 0x65646f4e31
RX_ADDR_P2-5	 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR		 = 0xe7e7e7e7e7
RX_PW_P0-6	 = 0x00 0x20 0x00 0x00 0x00 0x00
EN_AA		 = 0x3f
EN_RXADDR	 = 0x02
RF_CH		 = 0x6c
RF_SETUP	 = 0x27
CONFIG		 = 0x3f
DYNPD/FEATURE	 = 0x03 0x06
Data Rate	 = 250KBPS
Model		 = nRF24L01+
CRC Length	 = 16 bits
PA Power	 = PA_MAX

Sleeping Till Interrupt

WAKE UP, PACKET RECEIVED
OK, dati ricevuti

Writing ACK: 13Lettura dati batteria: 

Writing to DATA.txt...
done.
AT+CBC

+CBC: 0,79,4038

OK
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt
Sleeping Till Interrupt

I had a look at your sketch but i couldn't understant where and how to apply the millis() function to my code. Any hint appreciated.

travis^__^:
I tried removing all the delays but what i got was this mess.

You have not posted your revised program and you have not explained what parts of it need to be timed. Without that I have nothing to go on.

...R

/*-----( Import needed libraries )-----*/
#include <SPI.h>   // Comes with Arduino IDE
#include <SD.h>
#include "RF24.h"  // Download and Install (See above)
#include "printf.h"
#include <Sleep_n0m1.h>
#include <SoftwareSerial.h>

/*-----( Declare Constants and Pin Numbers )-----*/
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8 
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7
#define InterruptPin 3
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
File myFile;
RF24 myRadio (10, 9);
Sleep sleep;
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int ACK = 13;
String proprietario = "Franco";
int IdRegina = 1;
float VbatRegina = 0;

struct RadioData {
  int IdArnia;
  int packetN;
  float tin;
  float tex;
  float hum;
  float peso;
  float vbat;
};

RadioData radioData;


void setup()   /****** SETUP: RUNS ONCE ******/
{

  //change to 10k for bootup and gsm connection
  delay(4000); //wait for everything to start up
    
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Receive data Test"));
  Serial.println(F("Questions: terry@yourduino.com"));
  Serial.println(F(""));
  
  //inizializza la micro SD
  if (!SD.begin(2)) {
    Serial.println(F("Inizializzazione della micro SD fallita"));
    return;
  }
  else{
  Serial.println(F("SD Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
  }

  
  //inizializza la sim800L
  serialSIM800.begin(9600);
  delay(1000);
  Serial.println(F("SIM800L Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
    
  printf_begin(); //inizializza printf per il debug del nrf24l01
  myRadio.begin();  //inizializza nrf24l01

  myRadio.enableAckPayload(); //SetACK payload
  myRadio.setAutoAck(true);  //set autoACK
    
  myRadio.setChannel(108);  //Set channel
  myRadio.setPALevel(RF24_PA_MAX);  // Set the PA Level   
  myRadio.setDataRate(RF24_250KBPS); //set data rate

  myRadio.maskIRQ(1,1,0);               //mask all IRQ triggers except for receive (1 is mask, 0 is no mask)
   
  myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  myRadio.startListening();
 
  myRadio.printDetails();
  Serial.println(F(""));
    
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{ 
   

  //wait untill a packet arrive
     
  if ( myRadio.available()) // Check for incoming data from transmitter
  {
    
    while (myRadio.available())  // While there is data ready
    {

      Serial.println(F(""));
      Serial.println(F("WAKE UP, PACKET RECEIVED"));        
      delay(1000);
      
      myRadio.read( &radioData, sizeof(radioData) ); // read the incoming data
      // DO something with the data, like print it
      Serial.println(F("OK, dati ricevuti"));  
      myRadio.writeAckPayload( 1, &ACK, sizeof(ACK) );
      printf("\nWriting ACK: %d", ACK);
            
    }

    delay(2000);    
      
    //leggo la Vbat della regina e l'assegno alla variabile
    VbatRegina = readBattery();  

    delay(2000);
        
     myFile = SD.open("data.txt", FILE_WRITE);

  // if the file opened okay, write to it:
    if (myFile) {
    Serial.println(F("Writing to DATA.txt..."));
    myFile.println();
    myFile.print(proprietario);
    myFile.print(",");
    delay(10);
    myFile.print(IdRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.IdArnia);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.packetN);
    myFile.print(",");
    delay(10);
    myFile.print(VbatRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.vbat);
    myFile.print(",");   
    myFile.print(radioData.tin);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.hum);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.tex);
    myFile.print(",");
    delay(10);   
    myFile.print(radioData.peso);
    myFile.print(",");
    delay(10);   
    // close the file:
    myFile.close();
    Serial.println(F("done."));
    
  } else {
    // if the file didn't open, print an error:
    Serial.println(F("ERROR opening the file"));
  }

    
    delay(3000);

    //post data on server
    postData();          
   
  } //END Radio available

  delay(3000);
    
  Serial.println(F("Sleeping Till Interrupt"));
 
  delay(100); //delay to allow serial to fully print before sleep
  
  sleep.pwrDownMode(); //set sleep mode
  
  //Sleep till interrupt pin equals a particular state. 
  //In this case "falling" change from 1 to 0.
  sleep.sleepInterrupt(digitalPinToInterrupt(InterruptPin),FALLING); //(interrupt Number, interrupt State)
  
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
float readBattery(){

  String str = "";
  
  Serial.println(F("Lettura dati batteria: "));

  int percentuale;
  float volt;

  delay(1000);
  
  serialSIM800.println(F("AT+CBC"));
  delay(400);
  while(serialSIM800.available()){
    str += (char)serialSIM800.read();
  }
  Serial.println(str);
  if(str.indexOf("+CBC") >= 0){
  percentuale = str.substring(str.indexOf("+CBC:")+8).toInt();
  volt = str.substring(str.indexOf("+CBC:")+11).toInt();

  volt= volt/1000;

  /*
  Serial.println(F("percentuale batteria: "));
  Serial.println(percentuale);
  Serial.println(F("Volt : "));
  Serial.println(volt);  
  */
  
  return volt;
  
  }
}


void postData(){ 

  serialSIM800.println(F("AT+SAPBR=3,1,\"APN\",\"web.coopvoce.it\""));//setting the APN, the second need you fill in your local apn server
  delay(1000);
  ShowSerialData();

  serialSIM800.println(F("AT+SAPBR=1,1"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+SAPBR=2,1"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+HTTPINIT"));
  delay(3000);
  ShowSerialData();

  serialSIM800.println(F("AT+HTTPPARA=\"CID\",1"));
  delay(3000);
  ShowSerialData();

   serialSIM800.print(F("AT+HTTPPARA=\"URL\",\"bekeeper.altervista.org/insert.php?Proprietario="));
   serialSIM800.print(proprietario);
   serialSIM800.print(F("&IdRegina="));
   serialSIM800.print(IdRegina);
   serialSIM800.print(F("&IdArnia="));
   serialSIM800.print(radioData.IdArnia);
   serialSIM800.print(F("&packetn="));
   serialSIM800.print(radioData.packetN);
   serialSIM800.print(F("&VbatRegina="));
   serialSIM800.print(VbatRegina);
   serialSIM800.print(F("&VbatArnia="));
   serialSIM800.print(radioData.vbat);
   serialSIM800.print(F("&TIn="));
   serialSIM800.print(radioData.tin);
   serialSIM800.print(F("&Hum="));
   serialSIM800.print(radioData.hum);
   serialSIM800.print(F("&TEx="));
   serialSIM800.print(radioData.tex);
   serialSIM800.print(F("&Peso="));
   serialSIM800.print(radioData.peso);
   serialSIM800.println(F("\""));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPACTION=0"));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPREAD"));
   delay(3000);
   ShowSerialData();

   serialSIM800.println(F("AT+HTTPTERM"));
   delay(3000);
   ShowSerialData();


   serialSIM800.println(F("AT+SAPBR=0,1"));
   delay(3000);
   ShowSerialData();  

   
  
 }


void ShowSerialData(){


      while(serialSIM800.available()!=0)
      {
      Serial.write(serialSIM800.read());
      }
  
}

This is my whole code that is working randomly.

I need to do the following operations in order:

-sleep
-being waken up by incoming packet on nrf24 (interrupt pin)
-write data to microSD card
-post data on DB online
-go back to sleep

i am really struggling with this code because sometimes it is working and sometimes it is not. i have read tons of docs and posts but no answer was found.

Any help appreciated

travis

Does the code work better if you take out all references to the SD card? Some SD Card modules don't like sharing SPI with other things - even though they are supposed to?

I think if this was my project I would start without any sleep code and make sure it can do the rest of the stuff first.

I would also move almost all of the code that is in loop() into small single-purpose functions that can each be tested separately. Have a look at Planning and Implementing a Program

This is my whole code that is working randomly.

It would be a great help if you explain in some detail what actually happens

...R

Robin2:
Does the code work better if you take out all references to the SD card? Some SD Card modules don't like sharing SPI with other things - even though they are supposed to?

I already had problems with the microSd card and i found that this SOLVED. Nrf24 (Mirf lib) + Micro SD-card works OK together - Storage - Arduino Forum solved it pretty well. So i would exclude it.

Robin2:
I think if this was my project I would start without any sleep code and make sure it can do the rest of the stuff first.

I tried it before withour sleep code and it works as randomly as with it.

Robin2:
I would also move almost all of the code that is in loop() into small single-purpose functions that can each be tested separately. Have a look at Planning and Implementing a Program

That is something that i realy need to do in order to clean the code such as the postData() function.

Robin2:

This is my whole code that is working randomly.

It would be a great help if you explain in some detail what actually happens

...R

The thing is that i am currently testing it with only one transmitter even if in the final version there should be many more of them tranmitting non simoultaneously.(later on i will need them also to work simoultaneously but i really don't know how to to that since the received packet trigger an interrupt there is the possibility of breaking the execution of the code before it uploaded the data to the website).

The randomness comes from the fact that some time the postData() function doesn't complete and consequently leaves my sim800l module in a incorrect state or that the code reach the point in which the ip address is assigned and then the arduino reboots.

I am powering it with 4 18650 batteries in parallel and i put a 470uf cap on the 800l module.
The arduino instead receive the same power but it served by a step up 5v converter.

Further testing made me think of the power supply but then i tried it with a wall adapter rated 2A and i got the same random rebooting and/or errors.

I think i could improve the situation by wiring the reset pin of the sim800l and resetting it at the beginning of every cycle to prevent it stop in a incorrect state. But this doesn't solve the problem of the rebooting.

travis^__^:
The randomness comes from the fact that some time the postData() function doesn't complete and consequently leaves my sim800l module in a incorrect state or that the code reach the point in which the ip address is assigned and then the arduino reboots.

I don't have a SIM module so I can't test your program myself.

The only recommendation I can make is to tidy up the code so you have parts that can be tested separately. Asking for help with a specific part will also make it easier to get advice.

...R

OK, i'm going to try wrap up all the code in separate functions this wekeend and i'll let you know.
Thanks

Here i am again.

After cleaning a bit the code and splitting it into separate functions i isolated the problem into the AT+HTTPPARA instruction and more specifically in the fact that the SRAM is being saturated by the string or something like this. Googling a bit i found that this is a common issue with many GSM modules.

So my question now is: How can i manage more efficiently all those variables without saturating the SRAM?

travis^__^:
So my question now is: How can i manage more efficiently all those variables without saturating the SRAM?

I had rather hoped that by now you would know automatically to include the latest version of your code along with your question.

...R

/* YourDuinoStarter Example: Simple nRF24L01 Receive
  - WHAT IT DOES: Receives simple fixed data with nRF24L01 radio
  - SEE the comments after "//" on each line below
   Start with radios about 4 feet apart.
  - SEE the comments after "//" on each line below
  - CONNECTIONS: nRF24L01 Modules See:
  http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
  Uses the RF24 Library by TMRH2o here:
  https://github.com/TMRh20/RF24
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 10
   4 - CSN to Arduino pin 9
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - IRQ to Arduino pin 3

   V1.02 02/06/2016
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>   // Comes with Arduino IDE
#include <SD.h>
#include "RF24.h"  // Download and Install (See above)
#include "printf.h"
#include <Sleep_n0m1.h>
#include <SoftwareSerial.h>

/*-----( Declare Constants and Pin Numbers )-----*/
//SIM800 TX is connected to Arduino D8
#define SIM800_TX_PIN 8 
//SIM800 RX is connected to Arduino D7
#define SIM800_RX_PIN 7
#define InterruptPin 3
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
File myFile;
RF24 myRadio (10, 9);
Sleep sleep;
//Create software serial object to communicate with SIM800
SoftwareSerial serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN);
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int ACK = 13;
String proprietario = "Franco";
int IdRegina = 1;
float VbatRegina = 0;

struct RadioData {
  int IdArnia;
  int packetN;
  float tin;
  float tex;
  float hum;
  float peso;
  float vbat;
};

RadioData radioData;


void setup()   /****** SETUP: RUNS ONCE ******/
{

  //change to 10k for bootup and gsm connection
  delay(4000); //wait for everything to start up
    
  Serial.begin(115200);
  Serial.println(F("Queen Receiver STARTED"));
  Serial.println(F("Written by Pietro Rosso"));
  Serial.println(F(""));
  
  //inizializza la micro SD
  if (!SD.begin(2)) {
    Serial.println(F("Inizializzazione della micro SD fallita"));
    return;
  }
  else{
  Serial.println(F("SD Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
  }

  
  //inizializza la sim800L
  serialSIM800.begin(9600);
  delay(1000);
  Serial.println(F("SIM800L Inizializzata corettamente"));
  Serial.println(F("-----------------------------"));
    
  printf_begin(); //inizializza printf per il debug del nrf24l01
  myRadio.begin();  //inizializza nrf24l01

  myRadio.enableAckPayload(); //SetACK payload
  myRadio.setAutoAck(true);  //set autoACK
    
  myRadio.setChannel(108);  //Set channel
  myRadio.setPALevel(RF24_PA_MAX);  // Set the PA Level   
  myRadio.setDataRate(RF24_250KBPS); //set data rate

  myRadio.maskIRQ(1,1,0);               //mask all IRQ triggers except for receive (1 is mask, 0 is no mask)
   
  myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  myRadio.startListening();
 
  myRadio.printDetails();
  Serial.println(F(""));
    
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{ 
   

  //wait untill a packet arrive
     
    while (myRadio.available())  // While there is data ready
    {

      Serial.println(F(""));
      Serial.println(F("WAKE UP, PACKET RECEIVED"));        
      delay(1000);
      
      myRadio.read( &radioData, sizeof(radioData) ); // read the incoming data
      // DO something with the data, like print it
      Serial.println(F("OK, dati ricevuti"));  
      myRadio.writeAckPayload( 1, &ACK, sizeof(ACK) );
      printf("\nWriting ACK: %d", ACK);
      Serial.println();
            
         
    
    //leggo la Vbat della regina e l'assegno alla variabile
    VbatRegina = readBattery();  

    //scrivo i dati su sd
    if(!writeDataToSd()){
      // if the file didn't open, print an error:
    Serial.println(F("ERROR opening the file"));      
    }    

    
    delay(1000);

    //post data on server
    if(!postData()){      
      Serial.println("Data NOT posted online");          
    }else{

      Serial.println("done.");
    }
   
   //END Radio available
    }

    Serial.println(F("Sleeping Till Interrupt"));
   
    Serial.flush();
    
    sleep.pwrDownMode(); //set sleep mode
    
    //Sleep till interrupt pin equals a particular state. 
    //In this case "falling" change from 1 to 0.
    sleep.sleepInterrupt(digitalPinToInterrupt(InterruptPin),FALLING); //(interrupt Number, interrupt State)
  
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
float readBattery(){

  String str = "";
  
  Serial.println(F("Lettura dati batteria: "));

  int percentuale;
  float volt;

  delay(1000);
  
  serialSIM800.println(F("AT+CBC"));
  delay(400);
  while(serialSIM800.available()){
    str += (char)serialSIM800.read();
  }
  Serial.println(str);
  if(str.indexOf("+CBC") >= 0){
  percentuale = str.substring(str.indexOf("+CBC:")+8).toInt();
  volt = str.substring(str.indexOf("+CBC:")+11).toInt();

  volt= volt/1000;

  /*
  Serial.println(F("percentuale batteria: "));
  Serial.println(percentuale);
  Serial.println(F("Volt : "));
  Serial.println(volt);  
  */
  
  return volt;
  
  }
}


bool postData(){ 

  Serial.println("Posting data to Server...");
    
  serialSIM800.println(F("AT+SAPBR=3,1,\"APN\",\"web.coopvoce.it\""));//setting the APN, the second need you fill in your local apn server
  delay(1000);
  
  serialSIM800.println(F("AT+SAPBR=1,1"));
  delay(1000);


  serialSIM800.println(F("AT+SAPBR=2,1"));
 delay(1000);

 

  serialSIM800.println(F("AT+HTTPINIT"));
delay(1000);

  
  serialSIM800.println(F("AT+HTTPPARA=\"CID\",1"));
delay(1000);
 
   serialSIM800.print(F("AT+HTTPPARA=\"URL\",\"bekeeper.altervista.org/insert.php?Proprietario="));
   serialSIM800.print(proprietario);
   serialSIM800.print(F("&IdRegina="));
   serialSIM800.print(IdRegina);
   serialSIM800.print(F("&IdArnia="));
   serialSIM800.print(radioData.IdArnia);
   serialSIM800.print(F("&packetn="));
   serialSIM800.print(radioData.packetN);
   serialSIM800.print(F("&VbatRegina="));
   serialSIM800.print(VbatRegina);
   serialSIM800.print(F("&VbatArnia="));
   serialSIM800.print(radioData.vbat);
   serialSIM800.print(F("&TIn="));
   serialSIM800.print(radioData.tin);
   serialSIM800.print(F("&Hum="));
   serialSIM800.print(radioData.hum);
   serialSIM800.print(F("&TEx="));
   serialSIM800.print(radioData.tex);
   serialSIM800.print(F("&Peso="));
   serialSIM800.print(radioData.peso);
   serialSIM800.println(F("\""));
   delay(10000);
 
 

   serialSIM800.println(F("AT+HTTPACTION=0"));
   delay(3000);


   serialSIM800.println(F("AT+HTTPREAD"));
   delay(3000);



   serialSIM800.println(F("AT+HTTPTERM"));
   delay(3000);
  

   serialSIM800.println(F("AT+SAPBR=0,1"));
   delay(3000);

   return true;
  
 }


void ShowSerialData(){


      while(serialSIM800.available()!=0)
      {
      Serial.write(serialSIM800.read());
      }
  
}

bool writeDataToSd(){

 myFile = SD.open("data.txt", FILE_WRITE);

  // if the file opened okay, write to it:
    if (myFile) {
    Serial.println(F("Writing to DATA.txt..."));
    myFile.println();
    myFile.print(proprietario);
    myFile.print(",");
    delay(10);
    myFile.print(IdRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.IdArnia);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.packetN);
    myFile.print(",");
    delay(10);
    myFile.print(VbatRegina);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.vbat);
    myFile.print(",");   
    myFile.print(radioData.tin);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.hum);
    myFile.print(",");
    delay(10);
    myFile.print(radioData.tex);
    myFile.print(",");
    delay(10);   
    myFile.print(radioData.peso);
    myFile.print(",");
    delay(10);   
    // close the file:
    myFile.close();
    Serial.println(F("done."));

    return true;
   
    }
}
//None yet
//*********( THE END )***********

This is it.

I tried also printing with ShowSerialData() the output of the AT commands.

If i print all the commands output it works only if the serialmonitor is open (WTF?) and without the print i got back at the random crashes.

The problem could be around the long AT+HTTPPARA command because i've seen many crashes there and reading on google i found that many people experience it with overflow problems.

Any other suggestion?

I don't know anything about using a SIM card so there is probably nothing more I can suggest.

It may help to include some Serial.print() statements in different places in your program so that you can follow the progress through the program. For example put one before you start that long series of prints and another one after it.

...R

I will try.

What about the different behaviour with and without the serial monitor open?

I mean in the second case i only used battery power with boost converter to 5v. And yes i already tried swapping battery.

Btw i am getting a 2560 too so that i wil try with higher memory Sram and hope to solve this.

Ok, this is getting weird.

This is the function i call in the loop

bool postData(){ 

 // Serial.println("Posting data to Server...");

  
  serialSIM800.println(F("AT+SAPBR=3,1,\"APN\",\"web.coopvoce.it\""));//setting the APN, the second need you fill in your local apn server
  delay(1000);              // wait for a second

    //Serial.println("SO far so good");
 
  
  serialSIM800.println(F("AT+SAPBR=1,1"));
  delay(1000);


  
   // Serial.println("SO far so good1");

  
  serialSIM800.println(F("AT+SAPBR=2,1"));
 delay(1000);


  
   // Serial.println("SO far so good2");
 
 
  serialSIM800.println(F("AT+HTTPINIT"));
delay(1000);



   // Serial.println("SO far so good3");


  serialSIM800.println(F("AT+HTTPPARA=\"CID\",1"));
delay(1000);



   // Serial.println("SO far so good4");

  
   serialSIM800.print(F("AT+HTTPPARA=\"URL\",\"bekeeper.altervista.org/insert.php?Proprietario="));
   serialSIM800.print(proprietario);
   serialSIM800.print(F("&IdRegina="));
   serialSIM800.print(IdRegina);
   serialSIM800.print(F("&IdArnia="));
   serialSIM800.print(radioData.IdArnia);
   serialSIM800.print(F("&packetn="));
   serialSIM800.print(radioData.packetN);
   serialSIM800.print(F("&VbatRegina="));
   serialSIM800.print(VbatRegina);
   serialSIM800.print(F("&VbatArnia="));
   serialSIM800.print(radioData.vbat);
   serialSIM800.print(F("&TIn="));
   serialSIM800.print(radioData.tin);
   serialSIM800.print(F("&Hum="));
   serialSIM800.print(radioData.hum);
   serialSIM800.print(F("&TEx="));
   serialSIM800.print(radioData.tex);
   serialSIM800.print(F("&Peso="));
   serialSIM800.print(radioData.peso);
   serialSIM800.println(F("\""));
   delay(2000);


 
   // Serial.println("SO far so good5");
 
  
   serialSIM800.println(F("AT+HTTPACTION=0"));
   delay(2000);


   
   // Serial.println("SO far so good6");

/*
   serialSIM800.println(F("AT+HTTPREAD"));
   delay(3000);



    Serial.println("SO far so good7");

*/

   serialSIM800.println(F("AT+HTTPTERM"));
   delay(2000);


   
    //Serial.println("SO far so good8");
  

   serialSIM800.println(F("AT+SAPBR=0,1"));
   delay(2000);



   // Serial.println("SO far so good9");

   return true;
  
 }

If i connect the pro mini to the usb and i open the serial monitor it works flawlessy.

If not it get stuck.

what's happening there?