Arduino connect GPS module

Arduino connect GPS module

The Punctual Atomic ATK-1218-BD Beidou GPS module was used in this experiment .

According to the instructions and software provided with the module, after I tested it without any problems, the baud rate of the serial communication of the module was set to 9600, only GNGGA information was displayed, and the refresh rate was 1Hz (that is, positioning once a second).

Module connected to Arduino:

● The module GND pin is connected to the Arduino's GND pin

● Module RX pin is connected to Arduino pin 3
● Module TX pin is connected to Arduino pin 4

● The module VCC pin is connected to the Arduino's 5V pin

I am using an Arduino Uno. On the Arduino Uno, all pins can be set as RX receivers. But on Arduino MEGA the pins that can be set as RX are 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69. The pins that can be set as RX on the Arduino Leonardo are 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
So you need to pay attention when using Arduino MEGA and Arduino Leonardo boards.

Write a program (using soft serial communication):
#include <SoftwareSerial.h>
 
// The serial connection to the GPS module
SoftwareSerial ss(4, 3);  // RX,TX
 
void setup(){
  Serial.begin(9600);
  ss.begin(9600);
}
 
void loop(){
  if (ss.available() > 0){
    // get the byte data from the GPS
    byte gpsData = ss.read();  //read是剪切,所以不加延迟。加延迟反而会影响数据读取
    Serial.write(gpsData);
  }
}
output:
$GNGGA,120644.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*76
$GNGGA,120645.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*77
$GNGGA,120646.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*74
$GNGGA,120647.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*75
$GNGGA,120648.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*7A
$GNGGA,120649.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*7B
Process GPS data
The analysis of GNGGA data is shown in the following figure:
image-20201014162100710

To parse the data, the first step is to split the GNGGA string. Arduino does not have a string splitting function that comes with languages ​​such as Java, so it can only write a program to split it by itself.

Programming:
#include <SoftwareSerial.h>

SoftwareSerial ss(4, 3); // RX,TX

String gpgga = "";

void setup(){
  Serial.begin(9600);
  ss.begin(9600);
}
 
void loop(){
  gpgga = "";
  while (ss.available() > 0){
    gpgga += char(ss.read());  
    delay(10);  // 延时,不延时的话就会跳出while循环,因为Arduino处理速度比串口传输快多了
  }
  
  if(gpgga.length() >0){
    //Serial.println(gpgga);
    //在这里进行数据的解析
    for (int i = 0; i < 15; i++) {
      commaPosition = gngga.indexOf(',');
      if (commaPosition != -1)
      {
        Serial.println(gngga.substring(0, commaPosition));
        gngga = gngga.substring(commaPosition + 1, gngga.length());
      }
      else {
        if (gngga.length() > 0) {  // 最后一个会执行这个
          Serial.println(gngga);
        }
      }
  }
}

The split string is as follows:

$GNGGA,115955.000,0000.0000,N,00000.0000,E,0,00,0.0,0.0,M,0.0,M,,0000*F
$GNGGA
115955.000
0000.0000
N
00000.0000
E
0
00
0.0
0.0
M
0.0
M

0000*F

I wanted to do data analysis in Arduino, but the type conversion of Arduino is really troublesome, so let’s convert the data such as latitude and longitude in other languages.

The final procedure is as follows:

#include <SoftwareSerial.h>

SoftwareSerial ss(4, 3);  // RX,TX

// 变量声明
String gngga = "";  // 读取到的GNGGA信息
String info[15];  // 用字符数组存储
int commaPosition = -1;

//函数声明
String getTime(); // 获取北京时间
String getLat(); // 获取纬度dd.mmssss
String getLng(); // 获取经度dd.mmssss
String getStatus(); // 获取当前定位状态,0=未定位,1 = 非差分定位,2=差分定位

void setup() {
  Serial.begin(9600);
  ss.begin(9600);
}

void loop() {
  gngga = "";
  while (ss.available() > 0) {
    gngga += char(ss.read());
    delay(10);  // 延时,不延时的话就会跳出while循环
  }

  if (gngga.length() > 0) {
    //Serial.println(gngga);
    //在这里进行数据的解析
    for (int i = 0; i < 15; i++) {
      commaPosition = gngga.indexOf(',');
      if (commaPosition != -1)
      {
        //Serial.println(gngga.substring(0, commaPosition));
        info[i] = gngga.substring(0, commaPosition);
        gngga = gngga.substring(commaPosition + 1, gngga.length());
      }
      else {
        if (gngga.length() > 0) {  // 最后一个会执行这个
          info[i] = gngga.substring(0, commaPosition);
          //Serial.println(gngga);
        }
      }
    }
    Serial.println("time: " + getTime());
    Serial.println("lat: " + getLat());
    Serial.println("lng: " + getLng());
    Serial.println("status: " + getStatus());
  }
}

String getTime(){
   return info[1];
}

String getLat(){
   return info[2];  
}

String getLng(){
   return info[4];  
}

String getStatus(){
   return info[6]; 
}

Guess you like

Origin blog.csdn.net/xiaoshihd/article/details/109361932