GPS接收器+ Ublox NEO-6M-无法超时

时间:2019-07-10 13:48:27

标签: arduino gps nodemcu

我正在尝试编写一些代码,这些代码将打开gps模块并等待数据到达,以便我可以使用它进行处理。我不需要持续不断的交流;我只需要从gps装置收到的第一笔经纬度,然后就可以将其关闭。如果在一定时间内(例如20秒)找不到自己的位置,我还需要停止它

以下是一些我尝试过的简单代码,旨在使gps能够读取(稍后会出现带有超时的完整代码)

    #include <TinyGPS++.h>

    #include <SoftwareSerial.h>
    TinyGPSPlus gps;  // The TinyGPS++ object
    SoftwareSerial ss(D4, D3); // The serial connection to the GPS     
    float latitude , longitude;
    int count = 0;


    void setup()
    {

      Serial.begin(9600);
      ss.begin(9600);
      Serial.println();
      Serial.print("Starting");

    }

    void loop()
    {
      while (ss.available() > 0)
        if (gps.encode(ss.read()))
        {
          if (gps.location.isValid())
          {
            latitude = gps.location.lat();
            lat_str = String(latitude , 6);
            longitude = gps.location.lng();
            lng_str = String(longitude , 6);
          }

        }
      Serial.println(lat_str + ":" + lng_str);
      delay(100);
      count += 1;
      String Count = String(count);
      Serial.println(Count);
    }

当上述延迟为100时,一切正常,当延迟为1000时,突然没有数据通过。

这是我尝试过的完整代码,其中包括超时和突破条件,再次无法正常工作。截止线位于GPS_mode函数中:while(((stoploop <1)&&(上一个-startTime

    #include <SoftwareSerial.h>
    #include <TinyGPS++.h>
    TinyGPSPlus gps;  // The TinyGPS++ object
    SoftwareSerial ss(D4, D3); // The serial connection to the GPS d

    int stoploop = 0; //used for stopping gps once a signal lock or     timeout is reached
    unsigned long startTime = millis(); // timer used for gps timeout
    const int gpsCutoffPin = D1;

    void setup() {
      //initialise the serial monitor
      Serial.begin(9600);
      gps_ss.begin(9600);
      //initialise the transistor pins
      pinMode(gpsCutoffPin, OUTPUT);
      // Start the gps in an off state
      digitalWrite(gpsCutoffPin, LOW);
    }

    void loop() {
        Serial.println("Enter GPS mode");
        GPS_mode();
        Serial.println("Back to the main loop now...");
        Serial.println(stoploop);
        delay(100000);
    }

    void GPS_mode(){
      //turn on the gps
      digitalWrite(gpsCutoffPin, HIGH);
      stoploop = 0;
      startTime = millis();
      previous = millis();
      int TimeOut = 60*1000;
      gps_ss.listen();
      Serial.println(stoploop);
        while ((stoploop < 1) && (previous - startTime < TimeOut))
      {
        while (gps_ss.available() > 0)
        {
          if (gps.encode(gps_ss.read()))
          {
            logInfo();
          }
          yield();
        }

      }
      gpsLat = (gps.location.lat(), DEC);
      gpsLon = (gps.location.lng(), DEC);


      // turn off the gps
      digitalWrite(gpsCutoffPin, LOW);
    }

    void logInfo(){
      // Causes us to wait until we have satelite fix
      if(!gps.location.isValid())
      {
        Serial.println("Not a valid location. Waiting for satelite data.");

        //return;
      }
    else {
       //url += String(gps.location.lat(), DEC);

        //url += String(gps.location.lng(), DEC);
        Serial.println(gps.location.lat(), DEC);
        Serial.println(gps.location.lng(), DEC);
        stoploop = 2;
        //delay(1000);
    }
    previous = millis();
    }

我希望在屏幕上看到一些gps数据,但实际上什么也看不到。我知道GPS正在工作并且正在接收数据,因为闪烁的LED告诉我。

我不知道该如何解决。如果有人能帮助我,我将非常感谢。

谢谢

0 个答案:

没有答案
相关问题