Storing SMS messages in an array.

Can you please give me an example of making an array of arrays? i’m thinking it would be like this:

char smsdata[160] // would hold the recieved message. with a maximum of 160 characters.

int inbox[inbox_no] //would store smsdata at index 0 and increment to store the next smsdata.

//smsdata would then clear and wait for the next message recieved.

if (inbox_no < 5), it would reset inbox_no = 0

i can’t seem to code this. i’ve tried using typedef and other methods, but i think i’m doing something wrong. any help please?

i would like to set the

Can you please give me an example of making an array of arrays?

What I think you want is an array of pointers to the individual messages.

char *msgPtr[10]; // hold 10 message pointers.

Then, you'd use strdup() to dynamically allocate memory, and copy the contents of smsdata to that location:

msgPtr[0] = strdup(smsdata);

Be aware that you have a limited amount of memory to be dealing with, and that it is necessary to free() memory when you are done with it.

Also, be aware that free() has a bug that corrupts memory occasionally.

A static array of messages of a known size is probably a better idea.

char smsdata[5][80]; // space for 5 80 character messages.

PaulS: What I think you want is an array of pointers to the individual messages.

char *msgPtr[10]; // hold 10 message pointers.

you Sir, are awesome. :) i'm gonna check this out. :D thanks a lot! i'll post about my progress. :D

Yeah! i did it. :slight_smile: Here’s a code that displays the recieved message and stores it in an array.

i only used 5 spaces and inbox messages. then it would replace the 1st inbox space when the 6th message comes.

#include <SIM900.h>
#include <sms.h>
#include <SoftwareSerial.h>

char smsdata[160];
char numberRx[20];
int i = 0;
int x = 0;
char *inbox[5];

void setup()
{
  Serial.begin(9600);
  gsm.begin(4800);
}

void loop()
{
  checkSMS();
}

void checkSMS()
{
  if(gsm.readSMS(smsdata, 160, numberRx, 20))
  {
    Serial.print("SMS Recieved: ");
    while (smsdata[i+1] != '\0')
    {
      Serial.print(smsdata[i]);
      i++;
    }
    i = 0;
    x++;
    Serial.println("");
    inbox[x] = strdup(smsdata);
    store();
  }
  else
  {
    Serial.println("No Messages");
  }
}

void store()
{
  int y;
    
  if (x == 5)
  {
    x = 0;
  }
  
  
  
  Serial.print("Inbox 1: ");
  Serial.println(inbox[0]);
  Serial.print("Inbox 2: ");
  Serial.println(inbox[1]);
  Serial.print("Inbox 3: ");
  Serial.println(inbox[2]);
  Serial.print("Inbox 4: ");
  Serial.println(inbox[3]);
  Serial.print("Inbox 5: ");
  Serial.println(inbox[4]); 

}

But i’m having difficulty in integrating it into my project, an SMS-based LED Matrix Display.

any ideas? thanks a lot!

Hi! i was able to store the text messages into an array. But i’m having problems looping them into the LED Matrix.

#include "cmap.h"
#include <SIM900.h>
#include <sms.h>
#include <SoftwareSerial.h>

#define INTEN 6500 //scroll speed. 
                   //the higher the value, the slower the scroll

//-----------------LED MATRIX PINS------------------

#define CLK  5    // clock pin connected to digital pin 5
#define DAT  6    // Data pin connected to digital pin 6
#define STR  7    // Strobe pin connected to digital pin 7 
#define K0  A0    // Data 0 connected to analog pin 0
#define K1  A1    // Data 1 connected to analog pin 1
#define K2  A2    // Data 2 connected to analog pin 2
#define K3  A3    // Data 3 connected to analog pin 3
#define K4  8     // Data 4 connected to digital pin 8
#define K5  9     // Data 5 connected to digital pin 9
#define K6  10    // Data 6 connected to digital pin 10
#define K7  11    // Data 7 connected to digital pin 11

//-------------------GSM VARIABLES-----------------------

boolean MSG_RCVD    = false;            // no message recieved as initial value
boolean started     = false;            // set gsm as off initally 
const char regnum[] = "639339182267";   // admin number
const char passRx[] = "default#";       // holds password, must be 8 char only! and ends with #
char smsdata[160];                      // sms only accept 160 characters
char numberRx[20];                      // holds sender number


//-----------------LED MATRIX VARIABLES------------------

char Display_Ram[38];                   // locates the the ASCII Character Map Display
char count;                             // counter for 	
unsigned char char_pointer = 0x00;      // character pointer for smsdata and LED_DATA arrays
unsigned char mask;                     // for masking the display 
unsigned char shift_counter;            // for scrolling the message

int pins[]  = { CLK, DAT, STR, K0,      // pins for LED Matrix
                K1, K2, K3, K4, K5, 
                K6, K7 };
int mask_data[]  = {0x00, 0x00, 0x00,   // mask for LED Matrix Display
                    0x80, 0x40, 0x20, 
                    0x10, 0x08, 0x04, 
                    0x02, 0x01};
                    
unsigned int d;
int message_no;                            // message counter
char *LED_DATA = announcement(message_no); // LED_DATA pointer
char *inbox[5];
int x = -1;



void setup() 
{ 
  // ---------- serial setup ----------
  Serial.begin(9600);
  gsm.begin(4800); 
  pinsetup(); 
}

void loop()
{
  Display();
  shift(); 
}

void pinsetup()
{
  for (int x =0; x<=10; x++){  
    pinMode(pins[x], OUTPUT);     
  } 
  
  for (int y=3; y <=10; y++){
    digitalWrite(pins[y],HIGH);  
  }  

  for(d=0;d<32;d++){
    clock();
  }
  strobe();
  d = 0;
}

void checkSMS ()
// ---------- check if there is new message ----------
{
  int i;
  if(gsm.readSMS(smsdata, 160, numberRx, 20))
  {
    if (strncmp (numberRx, regnum, 11) == 0 && strncmp (smsdata, passRx, 8) == 0)
    {
      while (smsdata[i] != '\0')
        {
          smsdata[i] = smsdata[i+8];     
          i++;                 
        }
        MSG_RCVD = true;
        store();
    }
  }
  else
  {
    MSG_RCVD = false;
  }
}

void store()
{
  x++;
  if (x == 4)
  {
    x = 0;
  }
  inbox[x] = strdup(smsdata);
}

void Display (void)
{
  int i = 0;
  for (i = 10; i >=3 ; i--)
  {
    mask = mask_data[i] ;
    scan();
    digitalWrite(pins[i],LOW);
    for(d=0;d<INTEN;d++){}
    digitalWrite(pins[i],HIGH);
  }
}

void scan(void){
  for (count=32;count>(-1);count--){
    if ((Display_Ram[count] & mask) == mask)
      digitalWrite(DAT,LOW);
    else
      digitalWrite(DAT,HIGH);
    clock();
  }
  strobe();
}

void clock(void){
  digitalWrite(CLK,HIGH);			// clock hi
  digitalWrite(CLK,LOW);		        // clock low
}

void strobe(void){
  digitalWrite(STR,HIGH);			// strobe hi
  digitalWrite(STR,LOW);			// strobe low
}

void shift(void) 
{
  for (int val=0; val<37; val++)
  {
    Display_Ram[val] = Display_Ram[val+1];
  }
  
  shift_counter++;
  
  if(shift_counter == 6)
  {
    shift_counter = 0;
//    load_data();
    checkSMS();
    checker();
  }
}

void load_data(void)
{ 
  int counter1 = 0;
  
  char *LED_DATA = announcement(message_no); 
  
  if(LED_DATA[char_pointer+1]  == '\0')
  {
    char_pointer = 0;
    message_no++;
  }
    
  for(counter1 = 0; counter1 < 5; counter1++)
  {
    Display_Ram[32 + counter1] = pgm_read_byte(&(character_data[(LED_DATA[char_pointer] - 0x20)][counter1])); //read the byte at this location
  }
  
  Display_Ram[37] = 00;	
  char_pointer++;
  
}

char *announcement(int message_no2)
{
  char *LED_message[]={"    LCoES Announcements         ",
                       "    Hello Letranites!           ",
                          inbox[0] ,
                          inbox[1] ,
                          inbox[2] ,
                          inbox[3] ,
                          inbox[4] };
 
}

void checker()
{
  
  
  char *LED_message[]={"    LCoES Announcements         ",
                        "    Hello Letranites!           ",
                          inbox[0] ,
                          inbox[1] ,
                          inbox[2] ,
                          inbox[3] ,
                          inbox[4] };
  
  
  Serial.print("Message 1: ");
  Serial.println(LED_message[0]);
  Serial.print("Message 2: ");
  Serial.println(LED_message[1]);
  Serial.print("Message 3: ");
  Serial.println(LED_message[2]);
  Serial.print("Message 4: ");
  Serial.println(LED_message[3]);
  Serial.print("Message 5: ");
  Serial.println(LED_message[4]);
  Serial.print("Message 6: ");
  Serial.println(LED_message[5]);
  Serial.println("");
  
}

any ideas? i’ve checked the text messages by printing all the messages.

thanks! i really need this done by monday. :frowning:

PaulS: Be aware that you have a limited amount of memory to be dealing with, and that it is necessary to free() memory when you are done with it.

i noticed that once i filled up the last array, and it goes back to index 0, it suddenly stops accepting new messages. does this mean i need to use free()?

anyway, i'll try it out. :D