Pages: [1]   Go Down
Author Topic: Erratic behavior in setup()  (Read 374 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
  pinMode(GSMresetpin,OUTPUT);
 
  Serial.begin(57600);
  Serial.println("system startup");
 
  printf_begin();
  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:
    return;
  }
  Serial.println("card initialized.");
 
  // Setup and configure rf radio
  radio.begin();

  // optionally, increase the delay between retries & # of retries
  //radio.setRetries(15,15);
  radio.setChannel(2);
  radio.setDataRate(RF24_1MBPS);
  radio.setCRCLength(RF24_CRC_8);
  radio.enableDynamicPayloads();
  radio.enableAckPayload();
   
  //ACK Payload
  byte ack_no_cmd[5] = {0,0,0,0,0};
  radio.writeAckPayload(5,&ack_no_cmd,5);
  //Open up RX pipe(s)
  radio.openReadingPipe(5,rxpipes[4]);
  // Start listening
  radio.startListening();
  // Dump the configuration of the rf unit for debugging
  radio.printDetails();
 
  gsm.TurnOn(9600);              //module power on
  delay(5000);
  gsm.Echo(0);               //disable AT echo
  int reg = 0;
  while(reg!=REG_REGISTERED){
      reg=gsm.IsRegistered();
      Serial.print("Registration ");
      Serial.println(reg);
      Serial.println("Checking for valid network registration");
      reg=gsm.CheckRegistration();
      switch (reg){   
        case REG_NOT_REGISTERED:
          Serial.println("GSM module not registered with network");
          break;
        case REG_REGISTERED:
          Serial.println("GSM module is registered with network");     
          break;
        case REG_NO_RESPONSE:
          Serial.println("GSM module not responding");
          break;
        case REG_COMM_LINE_BUSY:
          Serial.println("COM line is not free");
          break;
      }
      delay(15000);
   }
   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);     
   Serial.print(temp);
   /*
   //attach to GPRS network
   temp = gsm.SendATCmdWaitResp("AT+CGATT=1",500,100,"OK",5);
   Serial.print(temp);
   temp = gsm.SendATCmdWaitResp("AT+CSTT=\"telargo.t-mobile.com\",\"\",\"\"",500,100,"OK",5);
   Serial.print(temp);
   temp = gsm.SendATCmdWaitResp("AT+CIICR",5000,5000,"OK",5);
   Serial.print(temp);
   num_rx_bytes = gsm.SendATCmdWaitSendResp("AT+CIFSR",500,100,5,gsm_rx_buffer);
   for (byte k=0; k<num_rx_bytes; k++){
     Serial.print(gsm_rx_buffer[k]);
   }
     
     Serial.print("\n\r"); 
 
     num_rx_bytes = gsm.SendATCmdWaitSendResp("AT+CCLK?",500,100,5,gsm_rx_buffer);
     for (byte k=0; k<num_rx_bytes; k++){
       Serial.print(gsm_rx_buffer[k]);
     }
     Serial.print("\n\r"); 
     */
     //turn off the GSM module...one second pulse to turn off, half second to turn on
    digitalWrite(GSMPWRpin, HIGH);
    delay(1500);
    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.

Thanks,

Andy

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Andy
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50431
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..."));
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Thanks,

Andy
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!

Andy
Logged

Pages: [1]   Go Up
Jump to: