通过 GSM 模块发送 GPS 位置

Sending GPS location via GSM module

我正在使用 SIM900A GSM 模块和 NEO-6m GPS 模块。我想在GSM模块收到消息时,通过GSM模块发送位置。

当我在 GSM 模块的帮助下接收和发送任何消息时,我的代码可以正常工作,但是当我加入这两个模块(即 GPS 和 GSM 模块)时,它就不起作用了。

这是我的两个代码。

#include <SoftwareSerial.h>
// GPS
#include <TinyGPS.h>

// GSM
SoftwareSerial SIM900A(9, 10);
SoftwareSerial mySerial(5, 6);
TinyGPS gps;

void setup() {
  Serial.begin(9600);
  SIM900A.begin(9600);
  SIM900A.println("AT+CNMI=2,2,0,0,0");
  mySerial.begin(9600);
  delay(1000);
}

void loop() {
  bool newdata = false;
  String buffer = readSIM900A();
  if(SIM900A.available() > 0)
    Serial.println(SIM900A.read());
  if (buffer.startsWith("\r\n+CMT: ")) {
    // printing the number
    Serial.println(buffer.substring(9, 22));
    // Remove first 51 characters
    // buffer.remove(0, 51);
    int len = buffer.length();
    // printing message
    Serial.println(buffer.substring(51, len-2));
    if (buffer.substring(51, len-2) == "location") {
      Serial.println("Sending location");
      // GPS
      if (mySerial.available()) {
        char c = mySerial.read();
        if (gps.encode(c)) {
          newdata = true;
        }
      }
      if (newdata) {
        long int lat, lon;
        unsigned long age, age1, date, time, chars;
        gps.get_position(&lat, &lon, &age);
        gps.get_datetime(&date, &time, &age);
        Serial.print("Lat/Long(10^-5 deg): ");
        Serial.print(lat);
        Serial.print(", ");
        Serial.print(lon); 
        Serial.print(" Fix age: "); 
        Serial.print(age); Serial.println("ms.");
        Serial.print("Date(ddmmyy): "); Serial.print(date);
        Serial.print(" Time(hhmmsscc): ");
        Serial.print(time);
        Serial.print(" Fix age: "); Serial.print(age); 
        Serial.println("ms.");
        Serial.print("Alt(cm): "); Serial.print(gps.altitude());
        Serial.print(" Speed(mps): "); Serial.print(gps.f_speed_mps());
        // setting GSM module
        SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
        delay(1000); // Delay of 1000 milli seconds or 1 second
        // sending location from which code word had come
        SIM900A.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
        // Replace x with mobile number
        Serial.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
        delay(1000);
        SIM900A.print("Lat/Long(10^-5 deg): ");
        SIM900A.print(lat);
        SIM900A.print(", ");
        SIM900A.print(lon); 
        SIM900A.print(" Fix age: "); 
        SIM900A.print(age); SIM900A.println("ms.");
        SIM900A.print("Date(ddmmyy): "); SIM900A.print(date);
        SIM900A.print(" Time(hhmmsscc): ");
        SIM900A.print(time);
        SIM900A.print(" Fix age: "); SIM900A.print(age); 
        SIM900A.println("ms.");
        SIM900A.print("Alt(cm): "); SIM900A.print(gps.altitude());
        SIM900A.print(" Speed(mps): "); SIM900A.print(gps.f_speed_mps());
        SIM900A.println((char)26);// ASCII code of CTRL+Z
        delay(1000);
      }
    }
  }
  delay(100);
}

String readSIM900A() {
  String buffer;
  while (SIM900A.available()) {
    char c = SIM900A.read();
    buffer.concat(c);
    delay(10);
  }
  return buffer;
}

以上是我在GPS和GSM模块一起处理时的最终代码。当它们分开处理时,它们工作得很好。

连接数:

将SIM900A的Tx、Rx引脚分别接到Arduino Nano的9、10,将GPS模块的Tx、Rx分别接到5、6。我还与 Arduino 建立了共同点。

#include <NeoSWSerial.h>
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
// GPS
#include <TinyGPS.h>

// GSM
static const int RXPin = 8, TXPin = 9;
AltSoftSerial SIM900A(RXPin, TXPin);

NeoSWSerial mySerial(5, 6);
TinyGPS gps;

void setup()
{
Serial.begin(9600);
SIM900A.begin(9600);
SIM900A.println("AT+CNMI=2,2,0,0,0");
mySerial.begin(9600);
delay(1000);
}

void loop()
{
bool newdata = false;
String buffer = readSIM900A();
if(SIM900A.available() > 0)
  Serial.println(SIM900A.read());
if (buffer.startsWith("\r\n+CMT: "))
{
    // printing the number
    Serial.println(buffer.substring(9, 22)); 

    // Remove first 51 characters
    // buffer.remove(0, 51);
    int len = buffer.length();
    // Remove \r\n from tail
    // buffer.remove(len - 2, 2);
    // printing message
    Serial.println(buffer.substring(51, len-2));
    if (buffer.substring(51, len-2) == "location")
    {
      Serial.println("Sending location");

      // GPS
      if (mySerial.available()) 
      {
         char c = mySerial.read();
         if (gps.encode(c)) 
         {
            newdata = true;
         }
      }
      if (newdata)
      {
        long int lat, lon;
        unsigned long age, age1, date, time, chars;

        gps.get_position(&lat, &lon, &age);
        gps.get_datetime(&date, &time, &age);
        Serial.print("Lat/Long(10^-5 deg): ");
        Serial.print(lat);
        Serial.print(", ");
        Serial.print(lon); 
        Serial.print(" Fix age: "); 
        Serial.print(age); Serial.println("ms.");

        Serial.print("Date(ddmmyy): "); Serial.print(date); Serial.print(" 
        Time(hhmmsscc): ");
        Serial.print(time);
        Serial.print(" Fix age: "); Serial.print(age); 
        Serial.println("ms.");

        Serial.print("Alt(cm): "); Serial.print(gps.altitude());
        Serial.print(" Speed(mps): "); Serial.print(gps.f_speed_mps());

        // setting GSM module
        SIM900A.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
        delay(1000);  // Delay of 1000 milli seconds or 1 second
        // sending location from which code word had come
        SIM900A.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r"); // 
         Replace x with mobile number
        Serial.println("AT+CMGS=\"" + buffer.substring(9, 22) + "\"\r");
        delay(1000);

        SIM900A.print("Lat/Long(10^-5 deg): ");
        SIM900A.print(lat);
        SIM900A.print(", ");
        SIM900A.print(lon); 
        SIM900A.print(" Fix age: "); 
        SIM900A.print(age); SIM900A.println("ms.");

        SIM900A.print("Date(ddmmyy): "); SIM900A.print(date); 
        SIM900A.print(" Time(hhmmsscc): ");
        SIM900A.print(time);
        SIM900A.print(" Fix age: "); SIM900A.print(age); 
        SIM900A.println("ms.");

        SIM900A.print("Alt(cm): "); SIM900A.print(gps.altitude());
        SIM900A.print(" Speed(mps): "); SIM900A.print(gps.f_speed_mps());

        SIM900A.println((char)26);// ASCII code of CTRL+Z
        delay(1000);
      }
    }
    }
    delay(100);
   }

 String readSIM900A()
{
String buffer;

while (SIM900A.available())
{
    char c = SIM900A.read();
    buffer.concat(c);
    delay(10);
}

return buffer;
}

P.S : 我已经更改了 GSM 的引脚,其余部分相同。除了添加 AltSoftSerial 它是一个与 SoftwareSerial 相同的库(从管理库下载它,它需要引脚 8 和 9)。不要更改草图中的引脚号更改连接。