String issue

at_response = send_command("AT+CPMS=\"ME\"",1000);

The send_command function now requires three arguments, and does not return a value.

send_command("AT+CPMS=\"ME\"",1000, at_response);

huum, OK...now i undertood.
But, this comparison is not working anymore (the answer is always NOT OK):

  send_command("AT+CPMS=\"ME\"",1000, at_response);
  if (at_response.substring(at_response.length()-4) == ("OK\r\n")) {
    Serial.println("OK");
  }
  else {
    Serial.println("NOT OK");
       }

There is a debug to do with this code now?

thank you man!!

There is a debug to do with this code now?

Add

Serial.print("at_response: '");
Serial.print(at_response);
Serial.println("]");

after:

  send_command("AT+CPMS=\"ME\"",1000, at_response);

Show us the output.

the outpu is just:

at_response: ']

I tried the old code to make sure that the cell phone is ok, and it's ok!

Serial.print("at_response: '");

The ' should have been [.

If you copy those three lines into the send_command function, and change at_response to reply, what do you see?

PaulS:

Serial.print("at_response: '");

The ' should have been [.

If you copy those three lines into the send_command function, and change at_response to reply, what do you see?

i didn't undertand this.

PaulS, we can talk in another place (email or MSN) if you can, and after we write here the solution.

You can PM me.

I meant that

Serial.print("at_response: '");

should have been

Serial.print("at_response: [");

Now it's working the debug.

But it's with the same result of the old code:

[
+CPMS="ME"

+CPMS: 1,70,0,30,1,70

OK
]
[
+CMGR=1

+CMGR: 1,,24
0791551118312800240B811079186914F500001]

But it's with the same result of the old code:

Changing the way that the collected data is made known to the caller will not change the data.

The problem is here:

  while (Uart.available() > 0) 
  {
    incomingByte = (Uart.read());
    reply = reply + char(incomingByte);
  }

This expects all the data to have arrived by the time that the function is called. There is no guarantee that this will happen.

There is some end-of-message marker that signifies that the complete message has been received. You need to wait, in the send_command function, until that end character is received.

This is not working yet.
I still have this from the phone:

+CPMS="ME"

+CPMS: 1,70,0,30,1,70

OK

+CMGR=1

+CMGR: 1,,24
0791551118312800240B811079186914F500001

instead of:

+CPMS="ME"

+CPMS: 1,70,0,30,1,70

OK

+CMGR=1

+CMGR: 1,,24
0791551118312800240B811079186914F500001101313204812905CCF4392C07

OK, we can see the output, but what does the code look like?

The code now is:

#include <NewSoftSerial.h>

NewSoftSerial Uart(10, 11);

void send_command(String at_cmd, int dly, String &reply){

 // String msg;
  char incomingByte;
  
  Uart.println(at_cmd);
  delay(dly);
  while (Uart.available() > 0)
  {

    incomingByte = (Uart.read());
   // reply = reply + char(incomingByte);
reply += incomingByte;  
}  
}



void setup() {

  Uart.begin(4800);
  Serial.begin(9600);
  
//Coloca o pino 13 em Output onde será ligado o relé
  pinMode(rele, OUTPUT);
  pinMode(botao, INPUT);

  String at_response;
  String at_response2;

  

  send_command("AT+CPMS=\"ME\"",1000, at_response);
Serial.print(at_response);

  
  send_command("AT+CMGR=1", 1000, at_response2);
Serial.println(at_response2);
 
 
 
}
void loop() {}

The output is:
+CPMS="ME"

+CPMS: 1,70,0,30,1,70

OK
+CMGR=1

+CMGR: 1,,24
0791551118312800240B811079186914F500001

The last line is the SMS message I need to read and compare. So this is incomplete.
It had to be:
0791551118312800240B811079186914F500001101313204812905CCF4392C07

thanks!

+CPMS: 1,70,0,30,1,70

What do these numbers mean?

+CMGR: 1,,24

And these?

PaulS:

+CPMS: 1,70,0,30,1,70

What do these numbers mean?

+CMGR: 1,,24

And these?

they are some answer from the phone (AT COMMAND), i don't know exactly what they mean, but they are useless for me.

i don't know exactly what they mean, but they are useless for me.

If you don't know what they mean, how do you know that they are useless? What if one of the numbers indicated the message length? Wouldn't that be useful information?

PaulS:

i don't know exactly what they mean, but they are useless for me.

If you don't know what they mean, how do you know that they are useless? What if one of the numbers indicated the message length? Wouldn't that be useful information?

No because, the project is:
Some codes to see the serial connection with the phone, another command to change the memory to read (AT+CPMS="ME"), after I read the first message. If the message have the word "Ligar" but in PDU format (in PDU format the message content is the end of the line), the code turn a port HIGH. Else, do nothing.
If the message have the word "Desligar" but in PDU format, the code turn this same port, LOW.

because, I said this answer were useless for me. The line of the message is always with the same end for the messages I want to parse.

+CMGR: 1,,24
07 91 55 11 18 31 28 00 24 0B 81 10 79 18 69 14 F5 00 00 1
07 91 55 11 18 31 28 00 24 0B 81 10 79 18 69 14 F5 00 00 11 01 31 32 04 81 29 05 CC F4 39 2C 07

looks definitely like some hexdump

lets google...

AT +CMGR - SMS Tutorial: Using the +CMGR AT Command to Read an SMS Message from a Message Storage Area -
The AT command +CMGR (command name in text: Read Message) is used to read a message from a message storage area. The location of the message to be read from the message storage area is specified by an index number. The message to be retrieved by the AT command +CMGR does not necessarily have to be an SMS message. It can be of other message types such as status reports and cell broadcast messages

AT +CPMS - SMS Tutorial: Preferred Message Storage (AT+CPMS) -
The AT command +CPMS (command name in text: Preferred Message Storage) serves several purposes. It can be used to:
1. Select the message storage area that will be used when sending, receiving, reading, writing or deleting SMS messages.
2. Find the number of messages that are currently stored in the message storage area.
3. Find the maximum number of messages that can be stored in the message storage area.

robtillaart:

+CMGR: 1,,24
07 91 55 11 18 31 28 00 24 0B 81 10 79 18 69 14 F5 00 00 1
07 91 55 11 18 31 28 00 24 0B 81 10 79 18 69 14 F5 00 00 11 01 31 32 04 81 29 05 CC F4 39 2C 07

looks definitely like some hexdump

lets google...

AT +CMGR - SMS Tutorial: Using the +CMGR AT Command to Read an SMS Message from a Message Storage Area -
The AT command +CMGR (command name in text: Read Message) is used to read a message from a message storage area. The location of the message to be read from the message storage area is specified by an index number. The message to be retrieved by the AT command +CMGR does not necessarily have to be an SMS message. It can be of other message types such as status reports and cell broadcast messages

AT +CPMS - SMS Tutorial: Preferred Message Storage (AT+CPMS) -
The AT command +CPMS (command name in text: Preferred Message Storage) serves several purposes. It can be used to:
1. Select the message storage area that will be used when sending, receiving, reading, writing or deleting SMS messages.
2. Find the number of messages that are currently stored in the message storage area.
3. Find the maximum number of messages that can be stored in the message storage area.

yes, about the commands, they are what i said before.

I'm nearst now with this code:

char incomingByte;
#include <NewSoftSerial.h>

NewSoftSerial cell(10,11);
int rele = 12;

String response;


void setup() {
   cell.begin(9600);
   Serial.begin(9600);
   delay(500);
   
pinMode(rele, OUTPUT);   
digitalWrite(rele, LOW);  

   //Set memory to read sms from, ME = phone memory
 cell.println("AT+CPMS=\"ME\"");  
     delay(1000);
  
    cell.flush();
   Serial.flush(); 

     //Read received message
cell.println("AT+CMGR=1");
}
void loop(){
  


  
 if(cell.available() >0 && (!response.startsWith("079") && !response.endsWith("C07")))  {

   incomingByte = cell.read(), BYTE;
       // say what you got:       
       Serial.print(incomingByte);
       
       response = response + incomingByte;
 }
 

//se a mensagem tiver a palavra Ligar, ligar o rele.
if (response.substring(response.length()-12) == ("05CCF4392C07")) {

digitalWrite(rele, HIGH);

}

But the problem is with the loop.

After read the message, I have to delete it and continue verifying new messages to compare the content.

How can I do it?

thank all!