2016-11-17 57 views
0

我需要幫助整合兩個庫,以便我可以通過GSM發送GPS數據。需要關於使用兩個特殊串行的信息,並且還需要代碼的幫助。將AdafruitFona 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; 
} 
+0

你爲什麼加起來正是在了「GSM」相同的源代碼比第一個?請解釋你嘗試了什麼,你得到了什麼輸出或錯誤? –

+0

對不起,這是一個錯誤 – Sabarish

+0

只有一個devie正在一次工作。我需要幫助來同時使用它們 – Sabarish

回答

0

這裏是一個一步一步混合您的GPS輸入設備和GSM輸出設備。

餘爲Arduino的原則:

void setup()功能啓動後執行一次。

void loop()函數定期在 setup()之後執行。

第一步 - 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); 

第二步 - GSM/FONA裝置的聲明和串行鏈路

包括的SendTo SMS號碼!! !

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

第三步 - 設置()函數(控制檯,GPS和GSM)

有可能添加一些額外的初始化。

// 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  
} 

第四步 - 循環()函數來等待GPS位置和發送短信

它可以使用String()基礎上, 收購DeviceGPS.location.lng()DeviceGPS.location.lat()創建SMS。

// 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 

    } 

} 
相關問題