1

I'm currently trying to log data from a GPS shield onto a microSD card using a Sparkfun microSD shield. All my code will compile, but when I open the serial monitor the microSD will initialize, but then the log file itself won't be able to be opened. I have formatted the card and reset all the shields and the Arduino itself multiple times, but nothing seems to work.

When I upload the example sketch used to show the functionality of the data logging, provided in the SD.h library by Sparkfun, everything will function properly and it will be able to open the file correctly. I used this file to give me information on how to format my code, so it is very similar.

One thing that I think may be causing the issue is the size of my code. When uploaded, the code uses 80% of total memory and 84% of dynamic. I tried commenting out a lot of the code to see if more free memory would give a different result, but still got the same error.

Any help would be awesome, and thank you in advance! I've included my code below.

// Include FONA, Watchdog, SD card and declare pins
#include "SoftwareSerial.h"
#include "Adafruit_FONA.h"
#include "Adafruit_SleepyDog.h"
#include "SPI.h"
#include "SD.h"

#define FONA_RX 2 //Reset pin
#define FONA_TX 3 //FONA serial TX pin
#define FONA_RST 4 //FONA serial RX pin

//Include software serial
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

const int chipSelect = 8;

File dataFile = SD.open("data.log", FILE_WRITE);

//Set strings to hold all the data
  String Lat = "";
  String Long = "";
  String KPH = "";
  String Head = "";
  String Alt = "";

  float latitude, longitude, speed_kph, heading, altitude;

//Set integers for the data
  int GPSlat = latitude;
  int GPSlong = longitude;
  int GPSkph = speed_kph;
  int GPShead = heading;
  int GPSalt = altitude;

//Set counter and phone number for location messaging
  int Log_Counter = 0;

// Halt function called when an error occurs.  Will print an error and stop execution while
// doing a fast blink of the LED.  If the watchdog is enabled it will reset after 10 seconds.
//void halt(const __FlashStringHelper *error) {
//  Serial.println(error);
//}

//uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); //

//uint8_t type; //


void setup() {
  while (!Serial);

  //Initialize serial output
  Serial.begin(115200);
  Serial.println(F("Team Space Bread GPS - Zero-G-luten"));

  //Initialize SD card, then check if card is present
  Serial.print(F("Initializing SD card..."));
  pinMode(SS, OUTPUT);
  digitalWrite(SS, HIGH);
  if (!SD.begin(chipSelect)) {
    Serial.println(F("Card failed or is not present"));
    while (1);
  }
  Serial.println("Card initialized");

  //Open file to log data to
  if (! dataFile) {
    Serial.println(F("Error opening log file"));
    while (1);
  }

  //Initialize FONA module
  Serial.println(F("Initializing FONA.... (may take a couple seconds)"));
  fonaSerial->begin(4800);
  if (!fona.begin(*fonaSerial)) {
  // halt(F("Couldn't find FONA"));
  }
  fonaSS.println("AT+CMEE=2");
  Serial.println(F("FONA is functioning swimmingly"));

  //Enable watchdog timer; if code hangs and will not connect to GPS, WDT will run out after 10 seconds
  //and reset the code
  Watchdog.enable(10000);
  Watchdog.reset();

  //Wait for FONA to connect to cell network
  Serial.println(F("Checking for network..."));
  while (fona.getNetworkStatus() != 1) {
   delay(500);
  }

  //Enable GPS
  Watchdog.reset();
  Serial.println(F("Enabling GPS"));
  fona.enableGPS(true);
  delay(2000);
  Watchdog.reset();
  if (!fona.enableGPS(true)) {
  // halt(F("Failed to turn GPS on, resetting..."));
  }
  Serial.println(F("GPS connection successful"));

  // Start the GPRS data connection.
  Watchdog.reset();
  Serial.println(F("Enabling GPRS"));
  fona.enableGPRS(true);
  delay(2000);   
  Watchdog.reset();
  if (!fona.enableGPRS(true)) {
  //  halt(F("Failed to turn GPRS on, resetting..."));
  }
  Serial.println(F("Connected to cellular"));

  //Wait a bit for connection to stabilize
  Watchdog.reset();
  delay(5000);

}

void loop() {
//Reset Watchdog at start of loop - everything must work in less than 10 seconds!
 Watchdog.reset();

// Grab a GPS reading.

  bool gpsFix = fona.getGPS(&latitude, &longitude, &speed_kph, &heading, &altitude);

//Define strings as data intergers
  Lat += String(GPSlat);
  Long += String(GPSlong);
  KPH += String(GPSkph);
  Head += String(GPShead);
  Alt += String(GPSalt);
//Print all data onto SD card
  dataFile.print("GPS lat: ");
  dataFile.println(Lat);
  dataFile.print("GPS long: ");
  dataFile.println(Long);
  dataFile.print("GPS speed KPH: ");
  dataFile.println(KPH);
  dataFile.print("GPS heading: ");
  dataFile.println(Head);
  dataFile.print("GPS altitude: ");
  dataFile.println(Alt);
  dataFile.print(" ");

//Flush file to save data onto card
  dataFile.flush();

//Every loop, add 1 to Log_Counter. When Log_Counter==180 (every 15 minutes), send SMS to number with location
  Log_Counter++;
  if (Log_Counter==180) {
//There has to be a more elegant way to do this, but ¯\_(ツ)_/¯

//Buffer for location and phone number
  char LocationBuffer[48];
//  char SendTo[16];

//Make latitude and longitude into one string
  String(Location) = Lat+=Long+=Alt ;

//Set phone number to text to
//  String(PhoneNumber) = "18143803250";
                       //^^^^This^^^^ is where you put the number to send the location to every 15 minutes

//Location string to char that will be able to send 
  Location.toCharArray(LocationBuffer, sizeof(LocationBuffer));

////Set phone number to char
//  PhoneNumber.toCharArray(SendTo, sizeof(SendTo));

//Send SMS w/ location
fona.sendSMS("18143803250", LocationBuffer);
}

//Take mesaurement every 5 seconds
  delay(5000);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.