1
votes

I have a strange problem with my project. I am able to send and receive SMS messages but for some reason not from the same project.

This little one sends a message, and is working properly:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+38762701893";
String message = "this is some message";
void setup() {
  SIM900.begin(19200);
  delay(20000);
}
void sendSMS(String number, String mess) {
  SIM900.print("AT+CMGF=1\r");
  delay(100);
  SIM900.println("AT + CMGS = \"" + number + "\"");
  delay(100);
  SIM900.println(mess);
  delay(100);
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  delay(5000);
}
void loop() {
  sendSMS(no, message);
  do {} while (1);
}

However, if I want this same function to work under a little bigger program which receives SMS messages and responds to them, it doesn't work.

This is the complete code of the not working example:

#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+122333444";           //phone number
String message = "";
char inchar;
int b = 13;
bool state = 0;

void setup()
{
  Serial.begin(19200);
  SIM900.begin(19200);
  delay(5000);
  SIM900.print("AT+CMGF=1\r");
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
  Serial.println("Ready...");
  SIM900.begin(19200);
  delay(500);
  pinMode(b, OUTPUT);
}

void sendSMS(String number, String mess) {
  SIM900.print("AT+CMGF=1\r");
  delay(200);
  SIM900.println("AT+CMGS=\"" + number + "\"");
  delay(100);
  SIM900.println(mess);
  delay(100);
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  delay(8000);
}

void loop()
{
  if (state) {
    sendSMS(no, message);
    state = 0;
  }
  if (SIM900.available() > 0)  {
    inchar = SIM900.read();
    Serial.print(inchar);
    if (inchar == '#') {
      delay(10);
      inchar = SIM900.read();
      Serial.print(inchar);
      if (inchar == 'a') {
        delay(10);
        inchar = SIM900.read();
        Serial.print(inchar);
        if (inchar == '0') {
          digitalWrite(b, LOW);
          message = "something is off";
        } else  if (inchar == '1') {
          digitalWrite(b, HIGH);
          message = "something is on";
        }
      }
      state = 1;
    }
  }
}

This program receives the SMS, turns on the LED, or turns it off, depending of the SMS content, but it won't reply for some reason. Any help is greatly appreciated.

2

2 Answers

1
votes

Your code for sending SMS is incorrect.I would suggest using a library like gsmlib for arduino it will handle all the tasks appropriately.

Firstly

SIM900.print("AT+CMGF=1\r");

This should be done once in setup().

After sending SIM900.println("AT+CMGS=\"" + number + "\""); you need to wait for the modem to respond with > after which you can send the text. In your case you are assuming that the modem would have sent it within 200ms.

There are several gsm libs available where you can just use a function like sendSMS(number,text) and it will handle all the other stuff.

0
votes

I did that in C#, but you can convert to Arduino easily:

private void sendSMS_GSM()
{
    if (serialPort.IsOpen)
    {
        strResponseSim = "";
        serialPort.WriteLine("AT+CMGF=1\r\n");
        while (strResponseSim != "AT+CMGF=1\r\r\nOK\r\n") ;
        strResponseSim = "";
        serialPort.WriteLine("AT+CSCS=\"GSM\"\r\n");
        while (strResponseSim != "AT+CSCS=\"GSM\"\r\r\nOK\r\n") ;
        strResponseSim = "";
        serialPort.WriteLine("AT+CMGS=\"" + txtPhone.Text + "\"\r\n");
        serialPort.WriteLine(txtMessage.Text);
        serialPort.Write(new byte[] { 26 }, 0, 1);
        while (strResponseSim == "OK") ;
        strResponseSim = "";
    }
}