Hello!
I want to use the GSM module along with an ethernet one. I have changed my gsm pins to 4,5 to avoid the collision of pin 2 used also by the eth module. My webserver has a form in which the user inserts the number and text for the SMS. I can successfully send the 1st sms but after that the gsm module becomes unresponsively. Any ideas why is that going ? Below is my sketch .
#include <SPI.h>
#include <Ethernet.h>
#include <NewSoftSerial.h>
#define maxLength 255
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,5, 177 };
String receivedText = String(255);
Server server(80);
NewSoftSerial cell(4,5);
char incoming_char=0;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.println("WebOn");
cell.begin(9600);
delay(35000);
cell.print("AT+CMGF=1");
}
void loop()
{
Client client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (receivedText.length() < maxLength) {
receivedText += c;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><head><title>SMS Sender</title></head><body>");
client.println("<h1>SMS</h1>");
client.print("<form accept-charset=""ISO-8859-1"" method=get>Mesajul: <input type=text size=255 name=the_text> <input type=submit></form>");
client.println("</body></html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
int firstPos = receivedText.indexOf("?");
if (firstPos > -1) {
int lastPos = receivedText.indexOf(" ", firstPos);
String mesaj = receivedText.substring(firstPos+20, lastPos);
String nr = receivedText.substring(firstPos+10, firstPos+20);
for(int i=0;i<mesaj.length();i++)if(mesaj[i]=='+')mesaj[i]=' ';
cell.print("AT+CMGS="); // now send message...
cell.print(34,BYTE); // ASCII equivalent of "
cell.print(nr);
cell.print(34,BYTE); // ASCII equivalent of "
delay(500); // give the module some thinking time
cell.print(mesaj); // our message to send
cell.println(26,BYTE); // ASCII equivalent of Ctrl-Z
delay(15000); // the SMS module needs time to return to OK status
}
receivedText = "";
client.stop();
}
}
My ethernet shield does not have an SDcard slot, so I think it does not use pin 4. After the 2nd attempt i have the variables in good format, checked with Serial.print the nr and message Strings. Any other ideas ?
Does the cell phone return any data in response to any of the AT commands sent? I don't see anywhere where you read any possible response. Perhaps it does, and there is a clue in the unread data.
As PaulS mentioned you're not checking responses from the AT commands. You might start replacing this section of code:
cell.print(34,BYTE); // ASCII equivalent of "
cell.print(nr);
cell.print(34,BYTE); // ASCII equivalent of "
delay(500); // give the module some thinking time
cell.print(mesaj); // our message to send
cell.println(26,BYTE); // ASCII equivalent of Ctrl-Z
with something like this (untested):
cell.print(34,BYTE); // ASCII equivalent of "
cell.print(nr);
cell.println(34,BYTE); // ASCII equivalent of "
uint8_t sendOK = 0;
unsigned long timeOutAT = millis();
while (millis() - timeOutAT <= 2000)
{
if(cell.available())
{
if(cell.read() == '>')
{
cell.print(mesaj); // our message to send
cell.println(26,BYTE); // ASCII equivalent of Ctrl-Z
sendOK = 1;
break;
}
}
}
if(!sendOK)
{
cell.println(0x1B,BYTE); //do not send message
} //There was an error waiting for the >
AT commands have different maximum time outs associated with them and I believe the maximum time out while waiting for the '>' after sending the phone number is 2 seconds. Once you receive the '>' character then you can print your SMS message text. One major problem that comes into play with these GSM modules is that if your delay is not long enough and you attempt to send the message before the '>' you'll be stuck indefinitely. The GSM modules I've worked with will not timeout and sit there indefinitely waiting for either the send command (CTRL Z) or the cancel command (ESCAPE). The code I posted should take care of that problem.
But after the 1st text i must set the module again with cell.println("AT+CMGF=1"); ?
How do you know you need to set this again? You should only have to set this once and it should be done in the setup section and not in the loop section. Are you saying that after the first SMS is sent the unit is reverting back to PDU mode from text mode?
I do not know that, i presume maybe it is good to do like this. To be something like a reset. I observed that if I send the 1st sms and wait like 5 minutes, I can send another one. How can i reset the module, is there any AT command for this ?
It may be the GSM module is not ready to send an SMS. A quick test you could do is simply send the AT command "AT" and check the response before sending an SMS. A big problem with your code is that you're not checking the response of any of your AT commands after you send them. There are several commands that have different maximum timeout lengths, so by not checking/waiting for the response you'll never be able to pinpoint where the problem is. I thought I had a document somewhere that showed the maximum time out values for each AT command but I can't seem to find it.
If I use the sketch for sms sending SM5100B | tronixstuff.com it works without any problem, and the timeout is 15 seconds between sms. I have the same timeout in my sketch.
...but that sketch only sends a single SMS. I found the document, it's for a Telit module, and it lists the maximum time out for sending an SMS as 60 seconds after sending the CTRL-Z.
I have modified that sketch deleting the last loop and it sends sms continuously without any problem. Can you spot any mistake in my code compared to that one ? So after I send a sms after 60 seconds I can send another one ? When I try to send the 2nd sms I get. +CMGS: is not the number where it should send the text message ?
Can you spot any mistake in my code compared to that one ?
No. I'm surprised the other one works and yours does not.
So after I send a sms after 60 seconds I can send another one ?
The 60 seconds is the maximum timeout before you receive a response acknowledging the AT command. So I guess the answer is yes, if you wait 60 seconds you should be able to send another one.
When I try to send the 2nd sms I get. +CMGS: is not the number where it should send the text message ?
I didn't fully understand this statement. If your returned value is +CMGS that usually means the message was successfully transmitted.
This is my current sketch. As usual the 1st message works ok, but at the other ones i try to send until i get the ">" and then send the message. At the end it fails and i get an +CMS ERROR: 305 . Any idea here?
My first recommendation is to remove the CMGF from your main program and place it in the setup section as it only needs to run once.
Secondly, what is the point of the citirecell() function and why are you calling it so many times? When the function is called it will return immediately if there is nothing in the receive buffer. In other words it only works if the AT command executes immediately with no delay.
I call it to check for the ">" char when the modules sends it to me. As I see now that function should be only after the second cell.println(34,BYTE); because there that char should appear. I will test 2morrow with the recommandation you gave me.