在Arduino Mega中使用SIM800发送数据时出错

时间:2017-09-13 10:36:15

标签: android arduino embedded tcp-ip

我正在开展一个大学项目,我必须使用带有GSM模块的Arduino mega(SIM800),通过GPRS在Android应用程序上使用TCP服务器发送传感器数据。

当我执行AT+CIPSEND时,我在HyperTerminal中收到错误(请参阅随附的屏幕截图和编码)。

Arduino代码:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

unsigned int k7, k = 0, a, b, r = 0;
unsigned long previousMillis = 0, currentMillis;
unsigned long time, i, k1;
const long interval = 5000;
double alpha = 0.75;
static double oldValue = 0;
static double oldChange = 0;
int hb = i; //heart beat sensor pin
int rs = 30; //respiration sensor input
int bz = 40; //buzzer output pin
int vi = 45; //vibration output pin

void gsm_init(void);
void Gprs_Init();
void gprs_Send_deat(unsigned int, unsigned int, unsigned int);
void value(int);

unsigned char num[12] = "7709751376";
void setup()
{
  lcd.begin(16, 2);
  Serial3.begin(9600);
  Serial1.begin(9600);
  lcd.setCursor(0, 0);
  lcd.print("  SLEEP APNEA   ");
  lcd.setCursor(0, 1);
  lcd.print("   USING IOT    ");
  delay(1000); delay(1000); delay(1000);
  delay(1000); delay(1000); delay(1000);
  delay(1000); delay(1000); delay(1000);
  delay(1000); delay(1000); delay(1000);
  pinMode(hb, INPUT);
  pinMode(rs, INPUT);
  pinMode(bz, INPUT);
  pinMode(vi, INPUT);
  digitalWrite(bz, LOW);
  digitalWrite(vi, LOW);
  lcd.clear();
  gsm_init();
  Gprs_Init();
}
void loop()
{
  a = analogRead(A0);   //ECG sensor input

  if (digitalRead(hb) == HIGH)
  {
    i++;
    if (i > 2500)
    {
      i = 0;
      r++;
    }
  }

  if (digitalRead(rs) == LOW)
  {
    k1++;
    if (k1 > 2000)
    {
      k1 = 0;
      k++;
    }
  }
  if (k > 20)
  {
    k = 12;
  }
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    r = (r * 5) + 10;
    k += 02;
    lcd.setCursor(0, 0);
    lcd.print("HB:");
    lcd.setCursor(3, 0);
    lcd.print(r);

    lcd.setCursor(0, 1);
    lcd.print("RES:");
    lcd.setCursor(4, 1);
    lcd.print("   ");
    lcd.setCursor(4, 1);
    lcd.print(k);

    lcd.setCursor(8, 0);
    lcd.print("ECG:");
    lcd.setCursor(12, 0);
    lcd.print(a);
    if ((k >= 8 && k <= 15) && (r >= 60 && r <= 80))
    {
      lcd.setCursor(7, 1);
      lcd.print("  NORMAL ");
      digitalWrite(bz, LOW);
      digitalWrite(vi, LOW);
      delay(3000);
    }
    if (((k >= 8 && k <= 15) && (r >= 60 && r <= 80)) != 1)
    {
      lcd.setCursor(7, 1);
      lcd.print("ABNORMAL ");
      digitalWrite(bz, HIGH);
      digitalWrite(vi, HIGH);
      delay(3000);
    }
    if (a > 999)
    {
      a = 565;
    }
    gprs_Send_deat(r, k, a);
    k = 0;
    r = 0;
    previousMillis = millis();
  }
}
void gsm_init()
{
  lcd.setCursor(0, 0);
  lcd.print("  gsm init....  ");
  Serial3.println("AT"); delay(1000);
  Serial3.println("AT+CMGF=1"); delay(1000);
  Serial3.println("AT+CNMI=2,2,0,0,0"); delay(1000);
  delay(1000); delay(1000); delay(1000);
}

void Gprs_Init()
{
  lcd.setCursor(0, 0);
  lcd.print("CONNECTING GPRS ");
  Serial3.print("AT+CGATT?"); //GET GPRS SERVICE STATUS
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CIPSHUT"); //CLOSE ALL CONNECTION
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CIPSTATUS"); //QUERY CURRENT STATUS
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CIPMUX=0");//SET TCPIP CONNECTION MODE TO 0 FOR SINGLE CONNECTION ,1 FOR MULTI CONNECTION
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CIPCSGP=1");
  Serial3.write(',');
  Serial3.write('"');
  Serial3.print("airtelgprs.pr");
  Serial3.write('"');
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CLPORT=");//Set Local UDP/TCP Port
  Serial3.write('"');
  Serial3.print("TCP");
  Serial3.write('"');
  Serial3.write(',');
  Serial3.write('"');
  Serial3.print("6001");
  Serial3.write('"');
  Serial3.write(0X0D);
  Serial3.write(0x0A);
  delay(1000); delay(1000); delay(1000); delay(1000);

  Serial3.print("AT+CSTT=");// START TASK AND SET APN
  Serial3.write('"');
  Serial3.print("airtelgprs.pr");
  Serial3.write('"');
  Serial3.write(',');
  Serial3.write('"');
  Serial3.write('"');
  Serial3.write(',');
  Serial3.write('"');
  Serial3.write('"');
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);
  while (Serial3.available())
    Serial3.read();
  Serial3.print("AT+CIICR"); //BRING UP GPRS CONNECTION
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000); delay(1000); delay(1000); delay(1000);
  Serial3.print("AT+CIFSR");//GET LOCAL IP
  delay(1000); delay(1000); delay(1000); delay(1000);
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  while (Serial.available())
    Serial3.read();
  Serial3.print("AT+CIPSERVER=1");//START SERVER IN SINGLE CONNECTION MODE OR GIVEN PORT
  Serial3.write(',');
  Serial3.print("8000");//SERVER PORT
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  while (Serial3.available())
    Serial3.read();
  delay(2000); delay(1000); delay(1000);
  delay(2000); delay(1000); delay(1000);
  delay(2000); delay(1000); delay(1000);
  static char buff[120];
  static int rx;
  while (Serial3.available())
    buff[rx++] = Serial3.read();

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(" IP ADDRESS,,,,,");
  for (k7 = 23; k7 < 38; k7++)
  {
    lcd.setCursor(0 + k7 - 23, 1);
    lcd.write(buff[k7]);
  }
  delay(3000);
  lcd.setCursor(0, 0);
  lcd.print("  SEND IP ADDR  ");
  Serial3.print("AT+CMGS=");//SEND IP ADDRESS ON GIVEN NUMBER
  Serial3.print("\"");
  for (k7 = 0; k7 < 10; k7++)
  {
    Serial3.write(num[k7]);
  }
  Serial3.print("\"");
  delay(1000);
  Serial3.write(0x0D);
  Serial3.write(0x0A);
  delay(1000);
  Serial3.println("your ip address:");
  for (k7 = 23; k7 < 38; k7++)
  {
    Serial3.write(buff[k7]);
  }
  delay(1000);
  Serial3.write(0x1A);
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("MSG SEND FINISHED");
  delay(3000);
  lcd.clear();
}
void gprs_Send_deat(unsigned int rat1, unsigned int k2, unsigned int a1)
{
  lcd.setCursor(7, 1);
  lcd.print("DATA SEND");
  delay(1000);
  Serial3.print("AT+CIPSEND"); //SEND DATA TO CONNECTION
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(1000);
  Serial3.write('*');
  Serial1.write('*');
  value(rat1);  //Heart beat
  value(k2);    //respiration
  value(a1);    //ECG
  Serial3.write(0X0D);
  Serial3.write(0X0A);
  delay(2000);
  Serial3.write(0x1A);
  delay(2000);
  lcd.setCursor(7, 1);
  lcd.print("Completed");
  delay(1000);
  lcd.setCursor(7, 1);
  lcd.print(" update  ");
}
void value(int e)
{
  int q, g, h, w;
  q = e / 100;
  g = e % 100;
  h = g / 10;
  w = g % 10;
  Serial3.write(q + 0x30);
  Serial3.write(h + 0x30);
  Serial3.write(w + 0x30);
  Serial1.write(q + 0x30);
  Serial1.write(h + 0x30);
  Serial1.write(w + 0x30);
  delay(500);
}

超级终端:

Hyper Terminal

1 个答案:

答案 0 :(得分:0)

首先,使用

使SIM800达到全部功能
set :default_env, { 'PASSENGER_INSTANCE_REGISTRY_DIR' => '/home/MYUSER/passenger_temp' }

然后按照以下顺序:

AT+CFUN=1

在此响应确定之后,您可以尝试将数据发送到服务器。 请确保您对以上所有内容都做出响应,否则请尝试打印错误代码并检查数据表中的详细错误。 您可以在第343页

获取Here