使用NeoGPS每秒写出GPS信息

时间:2019-05-22 09:41:00

标签: arduino gps

我试图将GPS输出每秒写入输出文件,但是输出中仅得到零。对于另一个传感器的输出,它可以工作...

最初,我使用了TinyGPS ++软件包。但是,不可能每秒输出GPS数据。

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <avr/wdt.h>
#include "BlueDot_BME680.h"
BlueDot_BME680 bme680 = BlueDot_BME680();

#include <NMEAGPS.h>
#include <GPSport.h>
#include <Streamers.h>


static const int RXPin = 2, TXPin = 3;
static const uint32_t GPSBaud = 9600;


NMEAGPS gps;
gps_fix fix;


File myFile;

void setup() {
//*********************** serielle kommunikation
  Wire.begin(); 
  Serial.begin(9600);
  gpsPort.begin(GPSBaud);

  if (!SD.begin(4)) {

    return;
  }

    bme680.parameter.I2CAddress = 0x76;                  //Choose I2C Address
    bme680.parameter.sensorMode = 0b01;                  //Default sensor mode
    bme680.parameter.IIRfilter = 0b100;                  //Setting IIR Filter coefficient (15 default,0 off, 127 max)
    bme680.parameter.humidOversampling = 0b101;          //Setting Humidity Oversampling factor (16 default,0 disable humidity meas.) 
    bme680.parameter.tempOversampling = 0b101;           //Setting Temperature Oversampling factor (16 default, 0 disable)
    bme680.parameter.pressOversampling = 0b101;          //Setting Pressure Oversampling factor (16 default,0 disable) 
    bme680.parameter.pressureSeaLevel = 1013.25;         //default value of 1013.25 hPa
    bme680.parameter.tempOutsideCelsius = 15;            //default 15°C, current average outside temp to calculate altitude
    bme680.parameter.target_temp = 320;                  // gas sensor hot plate temp (320C default, 200C min, 400C max)



  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  bme680.writeCTRLMeas();

  myFile = SD.open("sensor.txt", FILE_WRITE);


  if (myFile) {
    // print the headings for our data
    myFile.println("Time,Sat,Lat,Lng,Alt,Temp,Hum,Press");
  }
  myFile.close();
}

void sensor() {
  bme680.writeCTRLMeas();
  myFile = SD.open("sensor.txt", FILE_WRITE);
  if (myFile) {
  myFile.print(fix.dateTime);
  myFile.print(',');
  myFile.print(fix.satellites);
  myFile.print(',');
  myFile.print(fix.latitude(), 6);
  myFile.print(',');
  myFile.print(fix.longitude(), 6);
  myFile.print(',');
  myFile.print(fix.altitude());
  myFile.print(',');  
  myFile.print(bme680.readTempC());
  myFile.print(',');
  myFile.print(bme680.readHumidity());
  myFile.print(',');
  myFile.print(bme680.readPressure());
  myFile.println();
  }
  myFile.close();
}


//  This is the main GPS parsing loop.
static void GPSloop()
{
  while (gps.available( gpsPort )) {
    fix = gps.read();
    sensor();
  }
}

void loop()
{
  GPSloop();

  delay(1000);
}

目标是每秒检索一次GPS数据。此刻我只能得到零。

2 个答案:

答案 0 :(得分:0)

正如评论中所读,我认为不可能以1秒的频率获得所有gps数据,至少在位置和速度(GPRMC数据)上,它不能这么快地工作...这还需要时间。 (可能需要几分钟)以在开机时进行首次修复。尝试制作一个循环以获取第一个修复程序,然后每5秒更新一次,然后从那里开始尝试使其更快。

答案 1 :(得分:0)

首先,您需要将串行数据(字符)不断地馈送到fix对象,因此需要从循环中删除delay(1000)

第二,您需要检查该位置是否有效,然后将其写入SD。

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <avr/wdt.h>
#include "BlueDot_BME680.h"
BlueDot_BME680 bme680 = BlueDot_BME680();

#include <NMEAGPS.h>
#include <GPSport.h>
#include <Streamers.h>

static const int RXPin = 2, TXPin = 3;
static const uint32_t GPSBaud = 9600;

NMEAGPS gps;
gps_fix fix;

File myFile;

void setup()
{
    //*********************** serielle kommunikation
    Wire.begin();
    Serial.begin(9600);
    gpsPort.begin(GPSBaud);

    if (!SD.begin(4))
    {

        return;
    }

    bme680.parameter.I2CAddress = 0x76;          //Choose I2C Address
    bme680.parameter.sensorMode = 0b01;          //Default sensor mode
    bme680.parameter.IIRfilter = 0b100;          //Setting IIR Filter coefficient (15 default,0 off, 127 max)
    bme680.parameter.humidOversampling = 0b101;  //Setting Humidity Oversampling factor (16 default,0 disable humidity meas.)
    bme680.parameter.tempOversampling = 0b101;   //Setting Temperature Oversampling factor (16 default, 0 disable)
    bme680.parameter.pressOversampling = 0b101;  //Setting Pressure Oversampling factor (16 default,0 disable)
    bme680.parameter.pressureSeaLevel = 1013.25; //default value of 1013.25 hPa
    bme680.parameter.tempOutsideCelsius = 15;    //default 15°C, current average outside temp to calculate altitude
    bme680.parameter.target_temp = 320;          // gas sensor hot plate temp (320C default, 200C min, 400C max)

    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    bme680.writeCTRLMeas();

    myFile = SD.open("sensor.txt", FILE_WRITE);

    if (myFile)
    {
        // print the headings for our data
        myFile.println("Time,Sat,Lat,Lng,Alt,Temp,Hum,Press");
    }
    myFile.close();
}

void sensor()
{
    bme680.writeCTRLMeas();
    myFile = SD.open("sensor.txt", FILE_WRITE);
    if (myFile)
    {
        myFile.print(fix.dateTime);
        myFile.print(',');
        myFile.print(fix.satellites);
        myFile.print(',');
        myFile.print(fix.latitude(), 6);
        myFile.print(',');
        myFile.print(fix.longitude(), 6);
        myFile.print(',');
        myFile.print(fix.altitude());
        myFile.print(',');
        myFile.print(bme680.readTempC());
        myFile.print(',');
        myFile.print(bme680.readHumidity());
        myFile.print(',');
        myFile.print(bme680.readPressure());
        myFile.println();
    }
    myFile.close();
}

//  This is the main GPS parsing loop.
static void GPSloop()
{
    while (gps.available(gpsPort))
    {
        fix = gps.read();
        if (fix.valid.location)
            sensor();
    }
}

void loop()
{
    GPSloop();
}