Arduino与gsm板的串行通信

时间:2018-01-09 01:55:29

标签: geolocation arduino-uno gsm

我正在尝试编写一个程序,其中arduino从gsm模块接收文本。但是我的程序能够读取第一个文本并执行任务,但我希望它能够接收多个文本并为每个相应的文本执行单独的任务?任何帮助都会很棒。谢谢你。

#include <SoftwareSerial.h>
SoftwareSerial gsm(2, 3);

String inString[64];    // string to hold input coming from device 
String read;
int count = 0;
boolean verifyingDone = false;         // flags to determine the action completed and to perform next action 
boolean regist = false;
boolean registercomplete = false;
boolean location = false;


void setup()

{
  // put your setup code here, to run once:
  Serial.begin(9600);
  gsm.begin(4800);
  delay(100);
  gsm.print("ate0\r");  // removes the echo from the device
  delay(100);
  gsm.print("at+cnmi=1,2,0,0,0\r");  // to receive a text
  Serial.print("The Device has been Intiated");
}

void loop() {
  if (gsm.available())
  {
    while (gsm.available())
    {

      read = gsm.readString();
      verifyNum();   // verifying the number
      /* verifyingDone=true;

        if(verifyingDone){
         delay(1000);
         gsm.print("at+cgpsinfo\r");
         delay(1000);
         checkGps();
         verifyingDone=false;
      */

reading1: inString[count++] = read;
      Serial.print(String(read));
      if (count == 64) {
        break;
      }
    }

    delay(200);
    count = 0;
  }
}

void clearBufferArray()
{
  for (int i = 0; i < count; i++)
  {
    inString[i] = "";
  }
}

void verifyNum() {

  if (read.startsWith("\r\n+CMT: ")) {      // checking the incoming text and verifying the pre-defined number
    int index1 = read.indexOf('"');
    int index2 = read.indexOf('"', index1 + 1);
    String  Number = read.substring(index1 + 2, index2);
    char floatbuf[100];                    // make this at least big enough for the whole string
    Number.toCharArray(floatbuf, 100);
    int number = atoi(floatbuf);

    //Serial.println(number);
    delay(1000);
    if (strcmp(number, 24190) == 0)
    {
      Serial.println("The Sending number is " + Number);
      Serial.println("This is the registered number");
      regist = true;

      if (regist) {
        gsm.print("AT+CMGS=\"0434519166\"\r");           // Sending registeration confirmation via sms
        delay(100);
        gsm.print("Success this mobile number has been registered .\r");
        gsm.print("\r");
        delay(100);
        gsm.println((char)26);
        delay(10000);
        registercomplete = true;
        if (regist == true && registercomplete == true) {           //after complete registeration ,checking the gps and sending the co-ordinates
          checkGps();
          delay(500);

        }

      }
      else {
        gsm.print("AT+CMGSO=\"0434519166\" \,\"Someone tried to access your device\"\r");                     // When not a registered number


      }

    }
  }

}

void checkGps()
{
  Serial.print("Gps Search has started");
  delay(10000);//big delay to complete previous process           //Not able to do this process completely------>Coz not able to re-read the string obtained from the first read..have to find an alternate to read incoming signals efficiently and re-using it
  clearBufferArray();
  delay(1000);
  gsm.write("at+cgpsinfo\r");
  delay(10000);
  Serial.println();
  Serial.println();
  Serial.print("Locating.....Wait for a moment");
  /*int spacePosition = read.indexOf(':');
    if (read.charAt(spacePosition + 1) == ',' && regist==true )*/
  if (read.startsWith("\r\n+CGPINFO:,,,"))
  {
    Serial.println("GPS signal not found.Go outside"); //
    gsm.print("AT+CMGS=\"0434519166\"\r");            // Sending location as sms
    delay(100);
    gsm.print("GPS signal not found.Go outside ");
    gsm.print("\r");
    delay(100);
    gsm.println((char)26);
    delay(1000);
    regist = false;
  }
  else
  {

    Serial.println("Signal found");
    gsm.print("AT+CMGS=\"0434519166\"\r");            //Confirming signal found and sending location----->location finding programs can be obtained from previouses sketches
    delay(100);
    gsm.print("GPS found.Location will be sent soon ");
    gsm.print("\r");
    delay(100);
    gsm.println((char)26);
    delay(1000);



  }
}

谢谢,以上代码只是试用,欢迎任何帮助!

0 个答案:

没有答案