2016-11-17 8 views
0

Мне нужна помощь в интеграции двух библиотек, чтобы я мог отправлять данные GPS через GSM. Необходима информация об использовании двух специальных Serial, а также необходима помощь с кодом.интеграция экрана 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(); 
} 

ДЛЯ 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

Почему вы добавите точно такой же исходный код в «FOR GSM», чем первый? Пожалуйста, объясните, что вы пытаетесь и какой результат или ошибка вы получите? –

+0

извините, что это была ошибка – Sabarish

+0

только один из девиев работает одновременно. Мне нужна помощь, чтобы использовать их одновременно – Sabarish

ответ

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 устройства и серийный ссылка

В том числе количества SMS 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)

можно добавить некоторые дополнительные 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 - петля() функция ждать местоположения GPS и отправить SMS

можно использовать 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 

    } 

}