Store SMS message in variable

Asked

Viewed 329 times

4

I’m having trouble saving a text message in a variable. The idea is to send an SMS with a command and later use a conditional operator to activate or deactivate a relay.

But I can’t save the SMS message. Whenever I try to save it, it takes on the value of:

" ÿ ".

I’m using a GSM sim900 module with the Arduino.

Follow the code I’m trying to use:

#include <SoftwareSerial.h>
SoftwareSerial cell(2,3); 
void setup()
{
    cell.begin(19200);
    cell.println("AT+CMGF=1"); 
    delay(200);
    cell.println("AT+CNMI=3,2,0,0");
    delay(200);
}
void loop() 
{
    if(cell.available() >0)    // se o shild resceber uma msg
    {          
        delay(10);
        msg=cell.read();     // salve ela na variavel  msg
        Serial.println(msg);// mostra o valor do "msg" NESSE ponto ela esta valendo ÿ 
        if (msg=='a')     // se o primeiro caracter for "a"
        {
            delay(10);
            msg=cell.read();
            if (msg=='0')   // se o segundo caracter for "0"
            {
                delay(10);
                msg=cell.read();
                if (msg=='e')   // se o terceiro caracter for "e"
                {
                }
            }
        }
    }
}

When printing the message in the serial, its value is ÿ, why that?

I researched and found that before telling the Arduino to store the SMS, we have to tell him to "listen" the serial that receives the message and this is related to:

gsm.listen(); 

But I didn’t understand the relationship very well.

  • @brasofilo the language of Arduino is called Arduino ;)

  • Arduino’s language is C!

  • I’m studying some four or five programming language, it was bad not to have put... to Moh lost, thank you

  • @Trxplz0 I think the OP code is in C, but the official language of Arduino is really called Arduino.

  • you are printing msg before reading msg.

  • áh eé... the error of vc this printing msg before reading, was from now on at the time of posting... ops

  • discovered a few more things, before entering the loop we should tell where the module should save the text with the code AT+CPMS=?

Show 2 more comments

2 answers

2

I think that 'ÿ' be of value -1.

According to the documentation of SoftwareSerial: read

Returns
the Character read, or -1 if None is available

if the function returns -1 means that no characters are available.

    msg = cell.read();
    if (msg == -1) /* nao ha caracteres disponiveis */;

0


this code solves my problem, I can edit the sms. using vectors :)

#include <SoftwareSerial.h>
SoftwareSerial SIM(2, 3);
int x =0;
float temp=0;
char data [256];

void loop()
{
  Serial.println("ler mensagem");
  delay(1000); 
  //mySerial.flush();
  for (x=0;x < 255;x++)
  {
    data[x]='\0';
  }
  x=0;
  do{
while(SIM.available()==0);
data[x]=SIM.read();
x++;
if(data[x-1]==0x0D&&data[x-2]=='"') 
{ 
 x=0;
}
}
while(!(data[x-1]=='K'&&data[x-2]=='O')); 
  data[x-3]='\0'; 
  Serial.println(data[1]);
 temp=0;
 temp=data[7];
Serial.println(temp); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.