在arduino uno上使用带有单独gps屏蔽的gsm / gprs屏蔽

时间:2014-01-05 23:31:50

标签: gps arduino

我一直试图从我的gps盾牌获取gps数据,然后使用arduino uno上的gprs shield发送到Web服务器无济于事。 我正在使用Seeduino的gprs shield v2.0b与sim 900和gps v1.1盾牌。 我能够单独获取gps数据并单独发送到我的Web服务器,但在两者结合后,它将完全停止工作。 两者都使用软件序列,但在不同的端口上。

我的整个代码位于

之下
#include "SIM900.h"
#include <SoftwareSerial.h>
//#include "inetGSM.h"
#include "sms.h"
//#include "call.h"
//#include "gps.h"
#include <TinyGPS.h>
//#include <String.h>


//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to start a connection as client.
/* This sample code demonstrates the normal use of a TinyGPS object.
 It requires the use of SoftwareSerial, and assumes that you have a
 4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
 */

SoftwareSerial ss(3, 4);
TinyGPS gps;

const char sid[]= "ARDUINO1";
unsigned long fix_age,time,date;
float vspeed,vcourse ;
float flat, flon;
unsigned long age;
long mycounter=0;
boolean gotgpsdata=false;


//InetGSM inet;
//CallGSM call;
SMSGSM sms;
//GPSGSM gpss;
 char* longg;
    char* latt;
   char* speddd;
   char* dateee;
   char* timeee;
   int count=0;
 char final[150]="A1,";
char stat;
char msg[50];
int numdata;
char inSerial[50];
int i=0;
boolean started=false;

void setup() 
{

  //Serial connection.
  Serial.begin(115200);
  Serial.println("GSM Shield testing.");  
  ss.begin(9600);  
  //gsm.begin(19200);
  delay(5000);  
  Serial.print("Simple TinyGPS library v. "); 
  Serial.println(TinyGPS::library_version());
  Serial.println();
  gotgpsdata=false;  
}

void sendsms(char data[])
{

  if (gsm.begin(19200)){
    Serial.println("\nstatus=READY");
    started=true;  
    gsm.forceON();
  }
  else Serial.println("\nstatus=IDLE");

  if(started){
    //Enable this two lines if you want to send an SMS.   
    if (sms.SendSMS("+234xxxxxxx", data))
      Serial.println("\nSMS sent OK");
      delay(3000);
  }  
}

void dogprsaction(char data[])
{ 
    if (gsm.begin(19200)){
    Serial.println("\nstatus=READY");
    started=true;  
    gsm.forceON();
  }
  else Serial.println("\nstatus=IDLE");
  delay(10000);

  if(started){
    //GPRS attach, put in order APN, username and password.
    //If no needed auth let them blank.
    if (inet.attachGPRS("internet.ng.airtel.com", "internet", "internet"))
      Serial.println("status=ATTACHED");
    else Serial.println("status=ERROR");
    delay(1000);

    //Read IP address.
    gsm.SimpleWriteln("AT+CIFSR");
    delay(5000);
    //Read until serial buffer is empty.
    gsm.WhileSimpleRead();

    //TCP Client GET, send a GET request to the server and
    //my server aaddress goes here
    numdata=inet.httpGET("www.google.com", 80,"/?q=hello", msg, 50);
    //Print the results.
    Serial.println("\nNumber of data received:");
    Serial.println(numdata);  
    Serial.println("\nData received:"); 
    Serial.println(msg); 
    //ss.flush();
    gotgpsdata=false;
  }
  //inet.dettachGPRS();

}

int getgpsdata()
{
 strcpy(final,"A1,");
 // ss.listen();
  //ss.begin(9600);//9600);
  delay(2000);  
 // Serial.println("starting gps");
  boolean newData = false;
  gotgpsdata=false; 
  unsigned long chars;
  unsigned short sentences, failed;  

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      // Serial.print(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
      {
        newData = true;
      }
      else
      {

      }
    }
  }

  if (newData)
  {

    gps.f_get_position(&flat, &flon, &age);
    Serial.print("SYSTEM ID = ");
    Serial.print(sid);
    Serial.print(" LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
    Serial.print(" LON=");
    Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
    Serial.print(" SAT=");
    Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
    Serial.print(" PREC=");
    Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
    Serial.print(" DATE=");
    gps.get_datetime(&date, &time, &fix_age);
    Serial.print(date);
    Serial.print(" TIME=");
    Serial.print(time);
    Serial.print(" SPEED=");
    vspeed=gps.f_speed_kmph();
    Serial.print(vspeed);
    Serial.print("km/hr");
    gotgpsdata=true;
     Serial.println();
     Serial.println(flon); 
     char longitude[30]="ARDUINO"; 
   char latitude[30]="ARDUINO"; 
 char speedd[30]="ARDUINO";  
char datee[30]="ARDUINO";  
char timee[30]="ARDUINO"; 
    longg= dtostrf(flon,1,4,&longitude[15]);
    latt= dtostrf(flat,1,4,&latitude[15]);
    speddd= dtostrf(vspeed,1,4,&speedd[15]);
    dateee= dtostrf(date,1,0,&datee[15]);
    timeee= dtostrf(time,1,0,&timee[15]);
   char* all="ARDUINO";
   Serial.print("LON:");
   Serial.print(longg);
   Serial.print(",LAT: ");
   Serial.print(latt);
    Serial.print(",SPEED: ");
   Serial.print(speddd);
    Serial.print(",DATE: ");
   Serial.print(dateee);
    Serial.print(",TIME: ");
   Serial.print(timeee);
   Serial.println();
   int longlength=strlen(longg);
   int latlength=strlen(latt);
   int spdlength=strlen(speddd);
   int datelength=strlen(dateee);
   int timelength=strlen(timeee);  

   int totallength=longlength+latlength+spdlength+datelength+timelength;  

   int counter=0;
   strcat(final,longg);
   strcat(final,",");
    strcat(final,latt);
   strcat(final,",");
    strcat(final,speddd);
   strcat(final,",");
    strcat(final,dateee);
   strcat(final,",");
    strcat(final,timeee);
  // strcat(final,",");

   Serial.println(final);

  }

  gps.stats(&chars, &sentences, &failed);
  Serial.print(" CHARS=");
  Serial.print(chars);
  Serial.print(" SENTENCES=");
  Serial.print(sentences);
  Serial.print(" CSUM ERR=");
  Serial.println(failed);
  Serial.println();
  delay(2000);
  return 12;
}


void loop() 
{
  ss.begin(9600);
  getgpsdata();
  ss.end(); 
  if(gotgpsdata==true)
  {    
    dogprsaction(final);
  }

  serialhwread();
  //Read for new byte on NewSoftSerial.
  serialswread();

};

void serialhwread(){
  i=0;
  if (Serial.available() > 0){            
    while (Serial.available() > 0) {
      inSerial[i]=(Serial.read());
      delay(10);
      i++;      
    }

    inSerial[i]='\0';
    if(!strcmp(inSerial,"/END")){
      Serial.println("_");
      inSerial[0]=0x1a;
      inSerial[1]='\0';
      gsm.SimpleWriteln(inSerial);
    }
    //Send a saved AT command using serial port.
    if(!strcmp(inSerial,"TEST")){
      Serial.println("SIGNAL QUALITY");
      gsm.SimpleWriteln("AT+CSQ");
    }
    //Read last message saved.
    if(!strcmp(inSerial,"MSG")){
      Serial.println(msg);
    }
    else{
      Serial.println(inSerial);
      gsm.SimpleWriteln(inSerial);
    }    
    inSerial[0]='\0';
  }
}

void serialswread(){
  gsm.SimpleRead();
}

1 个答案:

答案 0 :(得分:0)

我们遇到了同样的问题。我们通过切换到AltSoftSerial解决了这个问题。有一个工作跟踪器的演示,在我们的网站上有代码 http://www.mallinson-electrical.com/shop/gpsgsm 我希望这有帮助