Need ideas how to store Strings in arrays

Thanks for taking a look!

also this will be ran on a esp8266 if that makes a differance

So I built a controller that receives commands from a server.

at the moment the commands are ran as soon as they are received.

I want to be able to store some of these commands and execute them at a specific time (device can loose connection for up to hours at a time)

the commands are alphanumeric and are 14 char long

What I would like to do, and tried just to be sure it didn't work is this....

long   myTimeID; // numeric ID  1412        [2:12PM] time of day

String myTimmerArray; // Array of arrays   lets say there needs to be multiple commands at 1200 noon                

byte numberOfElements = (sizeof(myTimmerArray[myTimeID]) / 14);

myTimmerArray[myTimeID][numberOfElements + 1] = myCMDs.substring(0, 14);

So basically.... something that would work like this...

Issue is I do not know how many commands will be stored, the server generates them, so it could be two or MAX out the ESP's Memory

myTimmerArray[1200][0] = "JH000000110056"; // trigger at 12 noon
myTimmerArray[1200][1] = "ZH000220110000"; // trigger at 12 noon
myTimmerArray[1200][2] = "JH0CBVK0110000"; // trigger at 12 noon
  myTimmerArray[145][0] = "JH00936JHDI00A";  // trigger at 1:45AM

C is not my primary language so when it comes to situations like this my creativity is limited :confused:

Thanks again for taking a look!!!! :slight_smile:

Do not use String.
Use char arrays.

I agree: do not use Strings.

I do not know how many commands will be stored, the server generates them, so it could be two or MAX out the ESP's Memory

You have to make a decision about a sensible number to store, and reject additional commands.

One approach would be to have two arrays, one for commands and one for the time of execution (minutes past midnight is a useful approach).

Example program and initialization that stores two 14 character (zero terminated) commands and two times:

void setup() {
  Serial.begin(9600);
  char commands[2][15] = {"JH000000110056", "ZH000220110000"};
  int command_times[2] = {60 * 1 + 45, 60 * 12 + 0}; //1:45 AM and noon
  for (int i = 0; i < 2; i++) {
    Serial.print("At ");
    Serial.print(command_times[i]);
    Serial.print(" minutes past midnight, execute command ");
    Serial.println(commands[i]);
  }
}

void loop() {}
}

Output:

At 105 minutes past midnight, execute command JH000000110056
At 720 minutes past midnight, execute command ZH000220110000

The Arduino Time library can format and make sense out of any date/day time you like.

I agree that you should stay away from String; it might be extremely difficult to fully prevent the use of String on an ESP as it seems to be used a lot in its libraries. But your own code can store data as char arrays.

Nate_Bro:
Issue is I do not know how many commands will be stored, the server generates them, so it could be two or MAX out the ESP's Memory

You will either need to set a limit as indicated above or look into e.g. linked lists.

You are asking how to implement a FIFO queue (First In First Out). There are a number of ways to do this (look it up as it is a commonly used technique), but one way is to use linked lists. You may find a FIFO library somewhere.

A liked list can be thought of liked a string of beads, generally implemented as structs (beads) that contain the data you need to process and a pointer (thread) to the next struct (bead) in the list.

To add an item you allocated the memory, set the data and append the item to the end of the list. To use up an item you disconnect the structure from the list, use the data and then free up the memory.

The only limit to the size is the amount of memory available.