将AdafruitFona GSM屏蔽与微型gps库集成

时间:2016-11-17 06:15:00

标签: arduino gps tracking gsm adafruit

我需要帮助整合两个库,以便我可以通过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();
}

FOR 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;
}

1 个答案:

答案 0 :(得分:0)

以下是将GPS输入设备与GSM输出设备混合的逐步步骤。

  

Arduino原则的剩余:

     

void setup()功能在启动后执行一次。

     

void loop()函数定期执行   setup()

第1步 - 宣布GPS设备和串行链接

// 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设备和串行链接

  

包括SendTo短信号码!!!

#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>";

Step3 - (控制台,GPS和GSM)的setup()功能

  

可以添加一些额外的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定位并发送短信

  

可以使用String()创建基于的SMS   获得DeviceGPS.location.lng()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

    }   

}