i have used an sim900 gsm shield and programmed it,when i send message it is displayed on serial monitor .
when i try to save same message to string and display it on serial monitor this doesnt work.
i have attached my program file to this topic,Please help me asap
Thank You
CODE:
#include <SoftwareSerial.h>
//use TX to 9 and RX to 10
SoftwareSerial mySerial(9, 10);
//char def[]="#A.";
char str[15];
int i=0;
void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
delay(100);
RecieveMessage();
serialEvent();
mySerial.println(str);
}
void loop()
{
if (mySerial.available()>0)
Serial.write(mySerial.read());
}
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
mySerial.println("GSM SUCCESSFULLY CONFIGURED FOR RECEIVING");
}
void serialEvent()
{
while (Serial.available())
{
while (Serial.available())
{
char inChar = Serial.read();
str = inChar;
-
mySerial.print(str);*
-
i++;*
-
}*
-
}*
}
GSM_Receiving2.ino (879 Bytes)
Welcome to the forum!
Please use the code tags (</>) around your code so its easy to read.
Not sure why you have two while(Serial.available()) loops but I suggest the main problem is that you missed the index so each new character overwrites the previous one at the start of the string.
try something like
while (Serial.available())
{
char inChar = Serial.read();
str[i] = inChar; // add [i] to this line
mySerial.print(inChar);
i++;
}
Thanx for it but it doesn't work , Cant display the string on serial monitor
i have interface my gsm and arduino, communication between gsm and arduino is good but when I send message to gsm module it is displayed on serial monitor but when i try to save message in a string and display that string it doesn't work. can you please tell me where my message is stored in arduino you you receive it and how to extract that message .
thanks
Can you be specific about the exact line in your code where the problem occurs? What does your program do now and how you would like it to be different. At the moment your description of the problem is too vague.
Please use code tags (</>) when you post.
i think so in void serialevent() function there is error, it is not saving received message to string .
#include <SoftwareSerial.h>
//use TX to 9 and RX to 10
SoftwareSerial mySerial(9, 10);
char str[15];
int i=0;
void setup()
{
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
delay(100);
RecieveMessage();
serialEvent();
mySerial.println(str);
}
void loop()
{
if (mySerial.available()>0)
Serial.write(mySerial.read());
}
void RecieveMessage()
{
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
mySerial.println("GSM SUCCESSFULLY CONFIGURED FOR RECEIVING");
}
void serialEvent()
{
while (Serial.available())
{
char inChar = Serial.read();
str[i++] = inChar;
mySerial.print(inChar);
}
serialEvent() is only called once in setup(): it will exit if there are no characters to read and never be called again- is this what you meant? (There is also a missing } in that function)
I assume that the variable "str" is where you wish to save the data? It doesnt appear to be accessed anywhere else so not sure what your problem exactly is. It will also overflow with bad consequences if more than 15 characters are read.
hey can you tell me how can i save a message received in arduino. message that is received to arduino is an string or an character by character??. how can i directly save it to string
serial.read() gives one character at a time. The code snippet I gave you will store each character into an array of characters.