0
votes

Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.

    #include <SoftwareSerial.h>
    #include <String.h>
    char inchar[255];
    SoftwareSerial cell(2,3); 
    int led1 = 22;
    #define powerOn 4
    int i;
    //char comparestring[160];
    char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command                                               ended with null terminator
    void setup()
    {
      // ilagay sa loob ng setup
      digitalWrite(powerOn, HIGH);  
      delay(1500);
      digitalWrite(powerOn, LOW);
      delay(5000);

      pinMode(led1, OUTPUT);
      digitalWrite(led1, LOW);

      Serial.begin(9600);
      cell.b-egin(9600);
      delay(30000);
      cell.println("AT+CMGF=1"); // set SMS mode to text
      delay(200);
      cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out         upon receipt
      delay(200);
      Serial.println("GSM SHIELD IS NOW OK AND READY");

    }
    void loop()
    {
      while(cell.available() >0)
      {
          inchar[i]=cell.read();
          i++;
          inchar[i] = '\0';
          Serial.print(inchar);
        if (inchar==command)
        {
          digitalWrite(led1, HIGH);
          cell.write("AT+CMGS=\"");
          cell.write("09267955775");
          cell.write("\"\r");
          delay(1000);
          cell.write("\nTerminal Monitoring System");
          delay(1000);
          cell.write(0x1A); // End the SMS with a control-z
        }
        else
        {
          Serial.println("\nInvalid Keyword! Type ?");
  dig-italWrite(led1, LOW);
        }
        }
        }

these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!

      while(cell.available() >0)
      {
          inchar[i]=cell.read();
          i++;
          inchar[i] = '\0';
          Serial.print(inchar);
        if (inchar==command)
1
Does "inchar" look like it ought to? If it does, are you sure you can compare to arrays just with "=="? (I don't know, I am just a bit surprised to see it like that)MortenSickel
@MortenSickel i realized that its wrong. But i still cant find a way to make this work. I hope you can help me.jpvisto
What does it look like? - is it just a random character mix, or can you somehow recognice the string you want to see?MortenSickel
No actually I did not. :( maybe you can help me modify my codes? thanks.jpvisto
Can you post an example - was it the same each time or did it change?MortenSickel

1 Answers

0
votes

inchar is a string so try using strcmp(inchar,command) for comparing the two.