Go Down

Topic: SMS command processing (Read 171 times) previous topic - next topic

BeWayne

May 19, 2018, 09:24 am Last Edit: May 19, 2018, 02:09 pm by BeWayne
Okay, struggling a little.

after getting to grips with using serial and being able to submit Telelphone  numbers in the format

SMS1, SMS2 etc and saving to EEPROM  I am able to do this for as many as I can store.

however, I now want to send an SMS command read the number and store it.



Currently I can read the SMS command that's sent in and store it to the same address allocation, whether be number for SMS1 or SMS2, SMS etc etc

command gets sents as SMS107xxxxxxxxx
we find SMS and the process it

I could if out each scenario,
ie
Code: [Select]
if(mySerial.find("SMS1")){
 

and

Code: [Select]
if(mySerial.find("SMS1")){
  etc

but must be a smarter way?

as you can see, want to use i to provide the int to calculate where to store the data, although it will be i-1, if I take the value 1 from the SMS1, 2 if SMS2 etc

current code for the recieved message processing is:-

Code: [Select]
void RecieveMessage(){


mySerial.println("AT+CMGF=1\r");
mySerial.println("AT+CNMI=2,2,0,0,0\r"); // AT Command to recieve a live SMS

 while(1){if(mySerial.find("SMS")){
    no1 = mySerial.readString();
    Serial.println(no1);
  Serial.print("Saving variable string to address: ");
  Serial.println(i*11);
  myString = no1.substring(1);
   Serial.println(myString);
  myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
  strcpy(buf, myStringChar);
  eeprom_write_string((i*11), buf);
  Serial.print("Reading string from address : ");
  Serial.println(i*11);
  eeprom_read_string((i*11), buf, BUFSIZE);
  Serial.println(buf);

 }}


Any guidance apprciated even if it is to look up a tutorial or reference material so i can try and understand.


6v6gt

You are combining a serial parse function with a eeprom store/retrieve function.
There may be more elegant ways to parse the result of your AT commands.
Google for "arduino parse sms data" to see other examples.

Regarding the eeprom, this is used normally for persistent storage which survives a shutdown.
Are you simply trying to accumulate all the incoming callers ?
Are you going to store the value of the index so that at the next system start, it does not overwrite the numbers from the last session ?

Code: [Select]
// these functions can be replaced by EEPROM.get() and EEPROM.put()
eeprom_read_string((i * 11), buf, BUFSIZE);
eeprom_write_string((i * 11), buf);

// this loops forever
while (1) { . . . }

lastchancename

OP, you have 25 posts.
By now, you should know how to post in <code> tags.
Try reading the stickies.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

BeWayne

Thanks for taking the time to reply.

@code posting, yeah that was a mistake these things happen, now corrected when I last checked.


In theory the power shouldn't be lost once the module is setup, certain data will need to remain after a power cycle, if it does occur. In theory once setup it will rarely ever change.

Currently the module is work in progress.

first objective get it working                           // can live with an untidy but functional system
second objective get it working smarter          // learning
third objective get the code clean.                  // ultimate goal


I'll be honest been through many examples and had trouble getting some working full stop.


At the moment, I am only storing 3 numbers, in my original code i was able to enter them from the Serial monitor and prompted for each one in turn.

recalling the numbers start at 0, 11 and 22 so the indexs are not saved as i know where they are starting from so they are hard coded, I say hard coded.

I will revisit some the searches and have another look at my options.




below is what i was using for the Serial Monitor entries.
Code: [Select]
void Number(){
  int i;
  for (i = 0; i < 3; i++) {
    Serial.print("Input Mobile Number: ");
    Serial.println(i+1);
  Serial.println("Awaiting Input"+i);
    while(Serial.available()==0){}//wait for user input
   
    no1 = Serial.readString(); //read input
    //Serial.println(i+1);
    Serial.println(no1);
    if(no1.startsWith("SMS",0)){ 
    Serial.println(i+1);
   
    Serial.print("Saving variable string to address: ");
    Serial.println(i*11);
    no1s = no1.substring(4);
    myString=no1s;
   
    myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
    strcpy(buf, myStringChar);

    eeprom_write_string((i*11), buf);

    Serial.print("Reading string from address : ");
    Serial.println(i*11);
    eeprom_read_string((i*11), buf, BUFSIZE);
    Serial.println(buf);
 
    }
  }
}

BeWayne

Okay.

haven't got  smart way of handling the telephone numbers as yet when I send them in via a text message.


1. send in the SMS as SMS107xxxxxxxx and SMS207xxxxxxxx etc etc
2. the system see the text come in and searchs for SMS
Code: [Select]
while(1){if(mySerial.find("SMS")){
3. then This is read to String no1 and passed to the function NumberProcessing
4. this then based on the starts with 1,2  etc uses an if statement to process and store it in EEPROM


Code: [Select]
void RecieveMessage(){
 
  mySerial.println("AT+CMGF=1\r");
  mySerial.println("AT+CNMI=2,2,0,0,0\r"); // AT Command to recieve a live SMS

   while(1){if(mySerial.find("SMS")){
      no1 = mySerial.readString();
       NumberProcess();
    }}


and

Code: [Select]
  Serial.println(no1);
  if(no1.startsWith("1")){
  Serial.print("Saving variable string to address: ");
  Serial.println(0);
  myString = no1;
  Serial.println(myString);
  myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
  strcpy(buf, myStringChar);
  eeprom_write_string((0), buf);
  Serial.print("Reading string from address : ");
  Serial.println(0);
  eeprom_read_string((0), buf, BUFSIZE);
  Serial.println(buf);}
else{ if(no1.startsWith("2")){
  Serial.print("Saving variable string to address: ");
  Serial.println(11);
  myString = no1;
  Serial.println(myString);
  myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
  strcpy(buf, myStringChar);
  eeprom_write_string((11), buf);
  Serial.print("Reading string from address : ");
  Serial.println(11);
  eeprom_read_string((11), buf, BUFSIZE);
  Serial.println(buf);}
 }}



There will be other  messages being received to determine the strings stored, this can be processed in a similar way, but don't think its smart.

at least 2 messages for pins activated and  deactivated (2 lots of data for each Pin), cant see there ever being more than 10 pins (20 messages), but with this method I could only done 9 pins.

However 1  number may be stored, or upto 9 this way, as starts with 10 would end up with the SMS1 and SMS10 being confused, as  SMS107xxxxx  starts 10.  Can't see me ver needing 9 number in the project but its a fair point.




lastchancename

#5
May 19, 2018, 07:30 pm Last Edit: May 19, 2018, 07:31 pm by lastchancename
Apart from losing the String variables, you're on the right track.
You're already using arrays.

Next step, make your storage a 2D array of numbers
e.g.
char myStorage[4][11]:
Then you have instant access to [4+1] strings of [11-1] characters each - with the trailing NULL.
(Careful - if your BUFSIZE is 11,  fully qualified phone numbers can have more than 10 digits...)
e.g.  +61402123456

Your 'save' function can become
eeprom_write_string(myStorage[index], buf);
where index is the index into your phone list.

Pulling numbers back from eeprom is the reverse.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

BeWayne

Thanks for your reply.

I will look at this later, not sure its making any sense at the minute, need some time out to recharge.



Go Up