Arduino Leonardo在几圈后窒息

时间:2018-04-20 20:04:11

标签: arduino

我正在制作一个将数据发送到服务器的GPS追踪器,但是在1-2个循环完美工作(它发送包裹)之后,它会窒息(停止输出到串行并且不发送任何内容)。

为什么会发生这种情况以及如何解决? GPS通过UART通过HardwareSerial连接,GPRS也通过UART通过SoftwareSerial连接。

#include <GPRS_Shield_Arduino.h>
#include <TroykaGPS.h>
#include <SoftwareSerial.h>
#define IMEI "IMEI" //I removed the real IMEI 
#define INTERVAL 30000
#define LEN 370
#define MAX_SIZE_MAS 16
char tcpBuffer[LEN];
SoftwareSerial GPRSSerial(10, 11);
GPS gps(Serial1);
GPRS gprs(GPRSSerial);

void setup() {
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println("Serial init OK");
  delay(100);
  Serial1.begin(115200);
  Serial.println("GPS serial init OK");
  GPRSSerial.begin(9600);
  Serial.println("GPRS serial init OK");
  delay(1000);
  Serial.println("Waiting for network");
  while (1) {
    delay(1000);
    if (gps.available()) {
      gps.readParsing();
      if (gps.getState() == GPS_OK) break;
    } else {
      Serial.println("GPS not available");
    }
  }
  Serial.println("Network found!");
}

void loop() {
  gprs.powerOn();
  while (!gprs.init()) {
    Serial.println("GPRS Init error");
  }
  Serial.println("GPRS init success");
  delay(3000);
  while (!gprs.join("internet.beeline.ru", "beeline", "beeline")) {
    Serial.println("GPRS JOINING NETWORK ERROR");
    delay(1000);
  }
  Serial.println("GPRS OK");
  Serial.print("IP Address is ");
  Serial.println(gprs.getIPAddress());
  while (!gprs.connect(TCP, "*ip address*", 80)) {
    //I replaced the real IP
    Serial.println("Connect error");
    delay(1000);
  }
  Serial.println("Connect success");
  tcpSend();
  gprs.close();
  gprs.disconnect();
  delay(100);
  Serial.println("Sent");
  gprs.powerOff();
  delay(15000);
}

void tcpSend() {
  tcpBufferForm();
  gprs.send(tcpBuffer);
  clearTcpBuffer();
}

void tcpBufferForm() {
  //strcat(tcpBuffer, "GET /recvdata.php?filename=ard.txt&data=123 HTTP/1.0\r\n\r\n");
  char buf[100];
  if (gps.available()) {
    gps.readParsing();
    switch (gps.getState()) {
      case GPS_OK:
        char lon[16], lat[16], date[16], time[16];
        gps.getTime(time, MAX_SIZE_MAS);
        gps.getDate(date, MAX_SIZE_MAS);
        dtostrf(gps.getLatitudeBase10(), 9, 6, lat);
        dtostrf(gps.getLongitudeBase10(), 9, 6, lon);
        sprintf(tcpBuffer, "GET /recvdata.php?filename=%s_%s.txt&data=%s_%s_%s_%s_%s HTTP/1.0\r\n\r\n", date, time, IMEI, date, time, lat, lon);
        Serial.print("formed: ");
        Serial.println(tcpBuffer);
        Serial.print("pending to send...");
        break;
      case GPS_ERROR_DATA:
        gprs.getDateTime(buf);
        sprintf(tcpBuffer, "GET /recvdata.php?filename=%s_err.txt&data=ERROR_DATA_%s_%s HTTP/1.0\r\n\r\n", buf, buf, IMEI);
        Serial.println("Sending GPS_ERROR_DATA");
        Serial.print("formed: ");
        Serial.println(tcpBuffer);
        Serial.print("pending to send...");
        break;
      case GPS_ERROR_SAT:
        gprs.getDateTime(buf);
        sprintf(tcpBuffer, "GET /recvdata.php?filename=%s_err.txt&data=ERROR_SAT_%s_%s HTTP/1.0\r\n\r\n", buf, buf, IMEI);
        Serial.println("Sending GPS_ERROR_SAT");
        Serial.print("formed: ");
        Serial.println(tcpBuffer);
        Serial.print("pending to send...");
        break;
    }
  }
}

void clearTcpBuffer() {
  for (int t = 0; t < LEN; t++) {
    tcpBuffer[t] = 0;
  }
}

1 个答案:

答案 0 :(得分:0)

将GPRS设置进程移至设置()帮助