ARDUINO:使用 GSM SIM800L 将 GPS 数据发送到 Firebase

问题描述 投票:0回答:2

我正在尝试使用 GSM SIM800L 将 GPS 模块中获得的数据发送到 Firebase 实时数据库,但遗憾的是纬度和经度总是得到零结果。我已经测试了 GPS 模块并且它可以工作。如何解决这个问题...如果您知道解决方案,请帮助我..您的帮助非常有帮助..

这是我得到的结果:

POST:/Jeepney1.json?auth=yzZb6JRN6pHMoDxdtUPHhyhrCL0R1WtP9TPBWKM3
Data:{"Latitude":0.000000,"Longitude":0.000000}
Status code: 200
Response: {"Latitude":0.Longitude":0.0}
O
POST:/Jeepney1.json?auth=yzZb6JRN6pHMoDxdtUPHhyhrCL0R1WtP9TPBWKM3
Data:{"Latitude":0.000000,"Longitude":0.000000}
Status code: 200
Response: {"Latitude":0.Longitude":0.0}

以及我使用的代码:

#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_RX_BUFFER 256
 
#include <TinyGsmClient.h> 
#include <ArduinoHttpClient.h>  
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 4, TXPin = 3;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

#define rxPin 7
#define txPin 8
SoftwareSerial sim800(txPin, rxPin);
 
const char FIREBASE_HOST[]  = "famous-segment-319603-default-rtdb.firebaseio.com";
const String FIREBASE_AUTH  = "yzZb6JRN6pHMoDxdtUPHhyhrCL0R1WtP9TPBWKM3";
const String FIREBASE_PATH  = "Jeepney1";
const int SSL_PORT          = 443;
 
char apn[]  = "internet.globe.com.ph";
char user[] = "";
char pass[] = "";
 
 
TinyGsm modem(sim800);


TinyGsmClientSecure gsm_client_secure_modem(modem, 0);
HttpClient http_client = HttpClient(gsm_client_secure_modem, FIREBASE_HOST, SSL_PORT);
 
unsigned long previousMillis = 0;
 
 
void setup()
{
  Serial.begin(9600);
  ss.begin(96000);
  Serial.println(F("device serial initialize"));
 
  sim800.begin(9600);
  Serial.println(F("SIM800L serial initialize"));
 
  Serial.println(F("Initializing modem..."));
  modem.restart();
  String modemInfo = modem.getModemInfo();
  Serial.print(F("Modem: "));
  Serial.println(modemInfo);
 
  http_client.setHttpResponseTimeout(10 * 1000); //^0 secs timeout
}
 
void loop()
{
 
  Serial.print(F("Connecting to "));
  Serial.print(apn);
  if (!modem.gprsConnect(apn, user, pass))
  {
    Serial.println(F(" fail"));
    //delay(1000);
    return;
  }
  Serial.println(F(" OK"));
 
  http_client.connect(FIREBASE_HOST, SSL_PORT);
 
  while (true) {
    if (!http_client.connected())
    {
      Serial.println();
      http_client.stop();// Shutdown
      Serial.println(F("HTTP  not connected"));
      break;
    }
    else
    {
      gps_loop();
    }
 
  }
 
}

void PostToFirebase(const char* method, const String & path , const String & data, HttpClient* http)
{
  String response;
  int statusCode = 0;
  http->connectionKeepAlive(); 
  String url;
  if (path[0] != '/')
  {
    url = "/";
  }
  url += path + ".json";
  url += "?auth=" + FIREBASE_AUTH;
  Serial.print("POST:");
  Serial.println(url);
  Serial.print("Data:");
  Serial.println(data);
 
  String contentType = "application/json";
  http->put(url, contentType, data);
 
  statusCode = http->responseStatusCode();
  Serial.print(F("Status code: "));
  Serial.println(statusCode);
  response = http->responseBody();
  Serial.print(F("Response: "));
  Serial.println(response);
  
  if (!http->connected())
  {
    Serial.println();
    http->stop();// Shutdown
    Serial.println(F("HTTP POST disconnected"));
  }
 
}
 
void gps_loop()
{

    String  latitude = String(gps.location.lat(),6);
    String  longitude = String(gps.location.lng(), 6);
    
  String Data = "{";
  Data += "\"Latitude\":" + latitude + ",";
  Data += "\"Longitude\":" + longitude + ""; 
  Data += "}";
   
  PostToFirebase("PATCH", FIREBASE_PATH, Data, &http_client);
   
   
}
arduino gps gsm arduino-ide
2个回答
0
投票

可能是因为您的 GPS 模块没有提供 GPS 坐标,


0
投票

因为您正在使用两个模块与 arduino 进行软件串行通信。 Arduino 感到困惑。我的解决方案是要么对其中一个模块使用硬件串行,要么在与两个模块软件串行通信时,您想一次实现逻辑只有一个软件串行开始第二个停止启动第二个软件串行并首先停止

我的意见是你应该为其中一个模块使用硬件串行

TinyGPSPlus gps;
HardwareSerial& gpsSerial = Serial; // HardwareSerial for GPS module
© www.soinside.com 2019 - 2024. All rights reserved.