将 AdafruitFona GSM shield 与微型 gps 库集成

integrating the AdafruitFona GSM shield with tiny gps library

我需要帮助来整合这两个库,以便我可以通过 GSM 发送 GPS 数据。需要有关使用两个特殊序列的信息,还需要代码帮助。

下面的 segmnet 包含 GPS 屏蔽的代码,必须使用它来生成位置,并且必须通过 gsm 将此数据发送到手机号码。

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 4, TXPin = 3;//was 4 and 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus gps;

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

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

  Serial.println(F("GPS GSM tracking system"));
  Serial.println(F("Sabdadon Presents"));
  Serial.print(F("Search and Rescue")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("Sabarish"));
  Serial.println();
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (ss.available() > 0)
    if (gps.encode(ss.read()))

      displayInfo();

  if (millis() > 500000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }
}

void displayInfo()
{

  delay(10000);

  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 5);
    Serial.print(F(","));

    Serial.print(gps.location.lng(), 5);

   // latitude=gps.location.lat();
    //longitude=gps.location.lng();


    //if(latitude && longitude)

  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {

    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
     ss.read();
    Serial.print(F("INVALID"));
  }

  Serial.println();
}

适用于 GSM

#include "Adafruit_FONA.h"
#define FONA_RX 2//2
#define FONA_TX 3//3
#define FONA_RST 4//4
char replybuffer[255];
#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
uint8_t type;
void setup()
{
   while (!Serial);
  Serial.begin(115200);
  Serial.println(F("FONA basic test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));

  fonaSerial->begin(4800);
  if (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  type = fona.type();
  Serial.println(F("FONA is OK"));
  Serial.print(F("Found "));
  switch (type) {
    case FONA800L:
      Serial.println(F("FONA 800L")); break;
    case FONA800H:
      Serial.println(F("FONA 800H")); break;
    case FONA808_V1:
      Serial.println(F("FONA 808 (v1)")); break;
    case FONA808_V2:
      Serial.println(F("FONA 808 (v2)")); break;
    case FONA3G_A:
      Serial.println(F("FONA 3G (American)")); break;
    case FONA3G_E:
      Serial.println(F("FONA 3G (European)")); break;
    default: 
      Serial.println(F("???")); break;
  }

  // Print module IMEI number.
  char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = fona.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("Module IMEI: "); Serial.println(imei);
  }
}
  void loop()
  { Serial.print(F("FONA> "));
  while (! Serial.available() ) {
    if (fona.available()) {
      Serial.write(fona.read());
    }
  }
  // send an SMS!
        char sendto[21], message[141];
        flushSerial();
        Serial.print(F("Send to #"));
        readline(sendto, 20);
        Serial.println(sendto);
        Serial.print(F("Type out one-line message (140 char): "));
        readline(message, 140);
        Serial.println(message);
        if (!fona.sendSMS(sendto, message)) {
          Serial.println(F("Failed"));
        } else {
          Serial.println(F("Sent!"));
        }

    }
void flushSerial() {
  while (Serial.available())
    Serial.read();
}

char readBlocking() {
  while (!Serial.available());
  return Serial.read();
}
uint16_t readnumber() {
  uint16_t x = 0;
  char c;
  while (! isdigit(c = readBlocking())) {
    //Serial.print(c);
  }
  Serial.print(c);
  x = c - '0';
  while (isdigit(c = readBlocking())) {
    Serial.print(c);
    x *= 10;
    x += c - '0';
  }
  return x;
}

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
  uint16_t buffidx = 0;
  boolean timeoutvalid = true;
  if (timeout == 0) timeoutvalid = false;

  while (true) {
    if (buffidx > maxbuff) {
      //Serial.println(F("SPACE"));
      break;
    }

    while (Serial.available()) {
      char c =  Serial.read();

      //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

      if (c == '\r') continue;
      if (c == 0xA) {
        if (buffidx == 0)   // the first 0x0A is ignored
          continue;

        timeout = 0;         // the second 0x0A is the end of the line
        timeoutvalid = true;
        break;
      }
      buff[buffidx] = c;
      buffidx++;
    }

    if (timeoutvalid && timeout == 0) {
      //Serial.println(F("TIMEOUT"));
      break;
    }
    delay(1);
  }
  buff[buffidx] = 0;  // null term
  return buffidx;
}

这是混合使用 GPS 输入设备和 GSM 输出设备的分步说明。

Remainder for Arduino principles:

The void setup() function is performed one time after startup.

The void loop() function is performed periodically after the setup().

第一步 - GPS 设备和序列号声明 link

// GPS and Serial link
static const int RXPin = 4, TXPin = 3;//was 4 and 3;
static const uint32_t GPSBaud = 9600;
// The TinyGPS++ object
TinyGPSPlus DeviceGPS;

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

第 2 步 - GSM/FONA 设备和序列号 link

的声明

Including the SendTo SMS number !!!

#define FONA_RX 2//2
#define FONA_TX 3//3
#define FONA_RST 4//4

// The serial connection to the GSM device
SoftwareSerial SerialFONA = SoftwareSerial(FONA_TX, FONA_RX);
// The FONA/GSM Cellular Module device
Adafruit_FONA DeviceFONA = Adafruit_FONA(FONA_RST);
// The destination SMS number
static const char *sSendTo = "<NUMBER>";

第 3 步 - setup() 函数(控制台、GPS 和 GSM)

It is possible to add some extra Init.

// only execute once
void setup()
{
    // Wait and Init Console
    while (!Serial); // Serial over USB
    Serial.begin(115200);

    // Init GPS link
    SerialGPS.begin(GPSBaud);
    Serial.print(F("TinyGPSPlus ver: "));
    Serial.println(TinyGPSPlus::libraryVersion());

    // Init GSM link
    SerialFONA.begin(4800);
    if (! DeviceFONA.begin(SerialFONA)) {
        Serial.println(F("Couldn't find FONA"));
        while (1); // Stop working
    }

    // Add some extra Init      
}

Step4 - loop() 函数等待 GPS 定位并发送短信

It is possible to use String() to create the SMS based on the acquired DeviceGPS.location.lng() and DeviceGPS.location.lat().

// executed periodicaly
void loop()
{
    // check until GPS message
    while (SerialGPS.available() > 0) {
        // get for a complete GPS message
        DeviceGPS.encode(SerialGPS.read());
    }

    // flush GSM serial link
    while (SerialFONA.available() > 0) {
        if (DeviceFONA.available()) {
            DeviceFONA.flush();
        }
    }

    // send an SMS!
    char sendto[21], message[141];

    // Wait for location (lng, lat, alt) is OK
    if (DeviceGPS.location.isValid()) {
        // ==> create SMS with longitude & latitude

    }   

}