ARDUINO:如何将GPS数据发送到Firebase?

时间:2019-03-01 09:33:08

标签: arduino gps arduino-uno android-gps

我有一个UBLOX Neo 6M模块来获取设备的经度和纬度。我打算将这些数据发送到Firebase。根据我的研究,它需要Wifi模块才能将数据传递到Firebase。我打算使用可以提供互联网数据的SIM卡。

#include <FirebaseArduino.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define FIREBASE_HOST "FIREBASE URL"
#define FIREBASE_AUTH "DB SECRET"

//SIM
#define APN "internet"

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

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

int n = 0;

void loop(){
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0){
    gps.encode(ss.read());
    if (gps.location.isUpdated()){
      Serial.print("Latitude= "); 
      Serial.print(gps.location.lat(), 6);
      Serial.print(" Longitude= "); 
      Serial.println(gps.location.lng(), 6);
    }
  }
}
  delay(1000);

  // set bool value
  Firebase.setBool("truth", false);
  // handle error
  if (Firebase.failed()) {
      Serial.print("setting /truth failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  // append a new value to /logs
  String name = Firebase.push("location", gps);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /location failed:");
      Serial.println(Firebase.error());  
      return;
  }
  Serial.print("pushed: /location/");
  Serial.println(name);
  delay(1000);
}

-

我确定此代码块不会使我进入工作系统。但是我想知道这个顺序是否可能将我带到某个地方。请就我所缺少的内容发表想法,我应该进行搜索。谢谢。

1 个答案:

答案 0 :(得分:0)

我不确定Firebase的工作方式,但是您需要做的是首先获取有效位置,然后将其传递给Firebase,如下所示:

#include <FirebaseArduino.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#define FIREBASE_HOST "FIREBASE URL"
#define FIREBASE_AUTH "DB SECRET"

//SIM
#define APN "internet"

float lat, lng;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

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

    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
    unsigned long last_millis = millis();
}

void feed_gps()
{
    unsigned long last_millis = millis();
    if (ss.available() > 0)
    {
        //feed gps for 1 seconds
        while (millis() - last_millis < 1000)
        {
            gps.encode(ss.read());
            if (gps.location.isValid())
            {
                lat = gps.location.lat();
                lng = gps.location.lng();
                Serial.print("Latitude= ");
                Serial.print(lat, 6);
                Serial.print(" Longitude= ");
                Serial.println(lng, 6);
            }
            else
            {
                Serial.print("GPS still unavailable, Chars processed: ");
                Serial.println(gps.charsProcessed());
            }
        }
    }
}

void loop()
{
    feed_gps();
    // set bool value
    Firebase.setBool("truth", false);
    // handle error
    if (Firebase.failed())
    {
        Serial.print("setting /truth failed:");
        Serial.println(Firebase.error());
        return;
    }

    char location_str[25] = {0};
    // store string of lat,lng
    sprintf(location_str, "%f, %f", lat, lng);

    String name = Firebase.push("location", location_str);
    // handle error
    if (Firebase.failed())
    {
        Serial.print("pushing /location failed:");
        Serial.println(Firebase.error());
        return;
    }
    Serial.print("pushed: /location/");
    Serial.println(name);
}