Losing char* data without visible reason

I am currently writing a datalogger program and wanted it not to overwrite old files on the SD card. So I have it look for the file basename Apollo, start with 0 and check to see if /Apollo#.txt exists. If it does, increment to the next number and so on until it doesn't find the file name and that becomes the new file to write to. However, my designated filename , char* logFile, appears to get corrupted leaving me puzzled. I have even stripped the file of all sensor data, server info, etc. to the bare bones to trace. In the set-up, I do the search and set the filename. I confirm this with Serial output. I then have it show me that name again in the loop() function. It does it once (most of the time). The second time, it glitches. I am not sure where to go from here. It is probably something simple, but I am missing it.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"

// Set these to your desired credentials.
const char *ssid = "Apollo1";
const char *password = "ghost12345";  // Must be at least 8 characters
String Measurement = "";
int logger;
int hh=0;
char* logFile;

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");
  // You can remove the password parameter if you want the AP to be open.
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  Serial.println("Server started: Apollo1");
  Serial.println("");
  Serial.println("SD Card check");
  if(!SD.begin()){
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();
  if(cardType == CARD_NONE){
    Serial.println("No SD card attached");
    return;
  }
  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
    Serial.println("MMC");
  }
  else if(cardType == CARD_SD){
    Serial.println("SDSC");
  }
  else if(cardType == CARD_SDHC){
    Serial.println("SDHC");
  }
  else {
    Serial.println("UNKNOWN");
  }
  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);
  char hhfilename[14];
  sprintf(hhfilename,"/Apollo%d.txt",hh);
  while(SD.exists(hhfilename)){
    Serial.print("Found data file: ");
    Serial.println(hhfilename);
    hh++;
    sprintf(hhfilename,"/Apollo%d.txt",hh);
  }
  logFile = hhfilename;
  Serial.print("Data file chosen: ");
  Serial.println(logFile);
  logger = 0;
}

void loop() {
  Serial.printf("Writing path: %s\n\n",logFile);
  delay(5000);
}

Serial Output:

18:16:14.144 -> SD Card Type: SDHC
18:16:14.144 -> SD Card Size: 7583MB
18:16:14.144 -> Data file chosen: /Apollo0.txt
Writing path: /A���?:
18:16:14.144 -> 
18:16:19.117 -> Writing path: �?
18:16:19.117 -> 

Global logFile is set to equal the value of locally set hhfilename. As a global variable once set, shouldn’t it remain as is, regardless of changes made to hhfilename?

If I had:
Int i=4;
Int j=i;
I++;

j would still remain 4, not increment to 5 as well.

You could also make this static so the memory it is occupying is not released when function setup() returns.
static char hhfilename[14];

Each solution has advantages and disadvantages.
I want to point on a different solution.

advantages:

  • don't have to care about boundary checking
  • don't have to care about zero-terminating strings
  • don't have to care about a pointer pointing to the right place
  • almost the same comfort as class Strings by avoiding the disadvantages of class String

disadvantage: needs some additional RAM

This is what the SafeString-library offers.
https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.