Question :
I’m having trouble saving an SMS message to 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 not save the SMS message. Whenever I try to save it, it assumes the value of:
" ÿ "
.
I’m using a sim900 GSM module with Arduino.
Here is 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 I print the message on the serial, its value is ÿ
, why this?
I researched and found that before telling Arduino to store the SMS, we have to tell him to “listen” to the serial that receives the message and this is related to:
gsm.listen();
But I did not understand the relationship very well.
Answer :
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]='
#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]='%pre%'; } 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]='%pre%'; Serial.println(data[1]); temp=0; temp=data[7]; Serial.println(temp);
';
}
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]='%pre%';
Serial.println(data[1]);
temp=0;
temp=data[7];
Serial.println(temp);
I think 'ÿ'
has the value -1
.
According to the documentation for SoftwareSerial: read
Returns
the character read, or -1 if none is available
If the function returns -1
it means that there are no characters available.
msg = cell.read();
if (msg == -1) /* nao ha caracteres disponiveis */;