从arduino接收和发送短信

时间:2016-08-27 11:36:33

标签: arduino

这是我从arduino发送消息并接收消息的代码。我试图单独运行两个代码,它们工作正常(我的意思是arduino可以单独接收和发送)但是当我合并两个代码时,arduino似乎只接收消息但不发送它。 请让我知道我在哪里犯了错误。

#include <TinyGPS.h>
#include <GSM.h>

long lat,lon;// create variable for latitude and longitude object

TinyGPS gps; // create gps object 
GSM_SMS sms;
char inchar;

void setup()
{
  Serial.begin(9600); // connect serial
  Serial1.begin(9600); // GSM connect
  Serial2.begin(9600); // connect gps sensor
  Serial.println("AT+CMGF=1");
  Serial.println("AT+CNMI=2,2,0,0,0");  
}

void loop()
{
  char one = receiveInfo();
  if(one=='1')
  {
    Serial.println("SMS received");
    Serial.println(one);
    //code works fine upto here and doesn't enter the loop below

    while(Serial2.available()>0)   // check for gps data
    {
      if(gps.encode(Serial2.read()))  // encode gps data
      { 
        gps.get_position(&lat,&lon); // get latitude and longitude
        displayInfo();
        sendInfo();
        delay(1000);
      }
    }
  }
} //end loop

void displayInfo()
{
  Serial.print("Position: ");
  Serial.print("lat: "); Serial.print(lat); Serial.print(" ");// print   latitude
  Serial.print("lon: "); Serial.println(lon); // print longitude
} //end displayInfo()

void sendInfo()
{  
  Serial1.print("AA");
  delay(1000); //delay of 1
  Serial1.println("AT");
  delay(1000); 
  Serial1.write("AT+CMGF=1\r\n");           //set GSM to text mode
  delay(1500); 
  Serial1.write("AT+CPMS=\"SM\"\r\n");         //Preferred SMS Message  Storage
  delay(1000); 
  Serial1.write("AT+CMGS=\"03360234233\"\r");           //set GSM to text   mode
  delay(1500);
  Serial1.print(lat); Serial1.print("  "); Serial1.print(lon);//set GSM to text mode
  delay(1500);
  Serial1.write(0x1A);           // sends ctrl+z end of message 
  delay(1500);
  Serial.println("sms sent ");
} //end sendInfo()

char receiveInfo()
{  
  if(Serial1.available()>0)
  {  
    inchar=Serial1.read();    
  }
  return inchar;
}

0 个答案:

没有答案