0

我正在从事车辆遥测项目。为此,我试图从 GPS shield 获取 GPS 数据,并使用 Sim900 GSM/GPRS shield Get 命令将它们发送到thingpeak。我使用的开发板是 Arduino Uno。

问题是我使用的 GSM/GPRS 屏蔽在使用 http 时仅适用于 4800 波特率。我的 GPS 以 9600 波特率工作。我正在使用这两个防护罩,并且在单独的操作中(GSM/GPRS 和 GPS)设备都工作得很好。在集成测试中(将两个屏蔽连接到 Arduino Uno 时)GPS 数据不会通过 GSM/GPRS 屏蔽发送到 thingspeak。我也尝试在代码中获取 GPS 数据。它对我不起作用!代码如下。

PS - 我在同一主题下阅读了旧帖子,但 AltSoftSerial 对我不起作用。

#include <SoftwareSerial.h>
#include <String.h>
#include "TinyGPS++.h"

int fixed = 0;
int led = 13;
TinyGPSPlus gps;
SoftwareSerial mySerial(4, 5); //GSM
SoftwareSerial SoftSerial(2, 3); //GPS
float longi = gps.location.lng();
float lati = gps.location.lat();


void setup() {
  SoftSerial.begin(9600); //Start GPS
  mySerial.begin(4800); //Start GSM
  Serial.begin(4800); //Start Serial Monitor
  delay(10000);
}

void loop() {
  longi = gps.location.lng();
  lati = gps.location.lat();
  GPSCord();
  Send2Think();


  if(mySerial.available()){
    Serial.write(mySerial.read());
  }
}

void Send2Think(){



  mySerial.println("AT");
  delay(500);
  //SoftSerial.println("HELLO");

  mySerial.println("AT+CPIN?");
  delay(500);
  //SoftSerial.println("HELLO");

  mySerial.println("AT+CREG?");
  delay(500);

  mySerial.println("AT+CGATT?");
  delay(500);

  mySerial.println("AT+CIPSHUT");
  delay(500);

  mySerial.println("AT+CIPSTATUS");
  delay(500);

  mySerial.println("AT+CIPMUX=0");
  delay(500);

  ShowSerialData();

  mySerial.println("AT+CSTT=\"hutch3g\"");//start task and setting the APN,
  delay(1000);
  SoftSerial.println("HELLO");

  ShowSerialData();

  mySerial.println("AT+CIICR");//bring up wireless connection
  delay(3000);

  ShowSerialData();

  mySerial.println("AT+CIFSR");//get local IP adress
  delay(2000);

  ShowSerialData();

  mySerial.println("AT+CIPSPRT=0");
  delay(3000);

  ShowSerialData();

  mySerial.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000);

  ShowSerialData();

  mySerial.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  ShowSerialData();

  String str1="GET https://api.thingspeak.com/update?api_key=FF19NVWMPZEWX0XK&field2=" + String(lati)+ "&field3=" + String(longi);
  mySerial.println(str1);//begin send data to remote server

  delay(4000);
  ShowSerialData();

  mySerial.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet 
  mySerial.println();
  ShowSerialData();

  mySerial.println("AT+CIPSHUT");//close the connection
  delay(100);
  ShowSerialData();
}

void ShowSerialData()
{
  while(mySerial.available()!=0)
    Serial.write(mySerial.read());
}

void GPSCord(){
  while (SoftSerial.available() > 0)
  gps.encode(SoftSerial.read());

  if (gps.location.isUpdated())
  {
    fixed = 1;
    Serial.print("LAT="); Serial.print(gps.location.lat(), 6);
    Serial.print(" LNG="); Serial.println(gps.location.lng(), 6);
  }

  if(fixed ==0)
  {
     digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
     delay(300);               // wait for a second
     digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
     delay(300);  
  }
  if (gps.altitude.isUpdated())
  {
    Serial.print("Altitude:");
    Serial.println(gps.altitude.meters());
  }
}
4

0 回答 0