Go Down

Topic: Erratic behavior in setup() (Read 628 times) previous topic - next topic


Hi everyone,

I'm trying to use a Gboard with an nRF24L01 board to log wireless sensor data to an SD card and then periodically transfer the data via GSM/GPRS.  I have the wireless sensor logging and the GSM upload working independently in separate skteches but I'm running onto problems when combining them.  The problem appears to be in the setup() function. 

Code: [Select]
#include "GSM_Shield.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

//for enable disable debug rem or not the string       #define DEBUG_PRINT
// definition of instance of GSM class

// Set up nRF24L01 radio on SPI bus plus pins 8 & 9
RF24 radio(8,9);

char gsm_rx_buffer[201];
GSM gsm;
int GSMPWRpin = 6;
int GSMresetpin = 7;

// Radio pipe addresses
const uint64_t rxpipes[5] = { 0xC2C2C2C2C2LL, 0xC2C2C2C2C3LL, 0xC2C2C2C2C4LL, 0xC2C2C2C2C5LL, 0xC2C2C2C2C6LL };
const uint64_t txpipe = 0xE7E7E7E7E7LL;

const int min_payload_size = 4;
const int max_payload_size = 32;
const int payload_size_increments_by = 2;
const int SD_CS = 10;

int next_payload_size = min_payload_size;
char receive_payload[max_payload_size+1]; // +1 to allow room for a terminating NULL char
unsigned int adval = 0;
unsigned int battval = 0;
byte ack_command = 0;

void setup(void) {
  pinMode(GSMPWRpin, OUTPUT);
  Serial.println("system startup");
  printf("Receiver Starting\n\r");
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(SD_CS)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
  // Setup and configure rf radio

  // optionally, increase the delay between retries & # of retries
  //ACK Payload
  byte ack_no_cmd[5] = {0,0,0,0,0};
  //Open up RX pipe(s)
  // Start listening
  // Dump the configuration of the rf unit for debugging
  gsm.TurnOn(9600);              //module power on
  gsm.Echo(0);               //disable AT echo
  int reg = 0;
      Serial.print("Registration ");
      Serial.println("Checking for valid network registration");
      switch (reg){   
        case REG_NOT_REGISTERED:
          Serial.println("GSM module not registered with network");
        case REG_REGISTERED:
          Serial.println("GSM module is registered with network");     
        case REG_NO_RESPONSE:
          Serial.println("GSM module not responding");
        case REG_COMM_LINE_BUSY:
          Serial.println("COM line is not free");
   byte num_rx_bytes = 0;
   //enable device to get local time from cell tower
   byte temp;
   temp = gsm.SendATCmdWaitResp("AT+CLTS=1",500,100,"OK",5);     
   //attach to GPRS network
   temp = gsm.SendATCmdWaitResp("AT+CGATT=1",500,100,"OK",5);
   temp = gsm.SendATCmdWaitResp("AT+CSTT=\"telargo.t-mobile.com\",\"\",\"\"",500,100,"OK",5);
   temp = gsm.SendATCmdWaitResp("AT+CIICR",5000,5000,"OK",5);
   num_rx_bytes = gsm.SendATCmdWaitSendResp("AT+CIFSR",500,100,5,gsm_rx_buffer);
   for (byte k=0; k<num_rx_bytes; k++){
     num_rx_bytes = gsm.SendATCmdWaitSendResp("AT+CCLK?",500,100,5,gsm_rx_buffer);
     for (byte k=0; k<num_rx_bytes; k++){
     //turn off the GSM module...one second pulse to turn off, half second to turn on
    digitalWrite(GSMPWRpin, HIGH);
    digitalWrite(GSMPWRpin, LOW);


void loop()

You can see that in the setup function there is a chunk of code that is commented out.  As long as I leave this commented, the setup() function runs ok.  However, as I uncomment these additional lines I start running into very strange behavior upon startup.  In some cases it seems like the board is resetting itself over and over again, as I get the first few print statements in setup() over and over again in my terminal.  In other cases I'll get those statements repeated for awhile and then it seems to start working.  In other cases, I'll get nothing.  The symptoms seem to be very repeatable depending on which lines I have commented out and which lines I leave in.  Sketch size is just under 20kbytes, and for an Uno board (328 processor) I think that should be ok.

I'm baffled as to what's going on...any assistance would be appreciated.




It should be 200 as defined by the COMM_BUF_LEN in GSM_Shield.h

I think I see where you're headed with that question, but keep in mind that even if I just uncomment the lines prior to using num_rx_bytes, I still run into problems.



Between the SD class and your large array, and all the Serial.print() constant strings, you are using up a lot of memory. Perhaps even more than you have.

The F() macro would help:
  Serial.print(F("Initializing SD card..."));
The art of getting good answers lies in asking good questions.


Between the SD class and your large array, and all the Serial.print() constant strings, you are using up a lot of memory. Perhaps even more than you have.

The F() macro would help:
  Serial.print(F("Initializing SD card..."));

I think you may be right, that could very well be the issue.  Any way to know for sure?  Any other tips to reduce memory usage?




After doing a little more testing I think you hit the nail on the head.  By reducing the number of strings I can get farther through the setup() function before getting the strange behavior.  I still have more optimization to do, but at least I know what the problem is.  Thanks!


Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131