使用SIM800L和Arduino Uno发送短信

时间:2018-02-02 17:50:52

标签: c++ arduino sim800

我一直在使用Arduino Uno,W5100以太网屏蔽和SIM800L模块。我想要做的是从MySQL数据库获取手机号码并将短信发送到该手机号码。

我到目前为止所做的是从mySQL数据库中获取手机号码并将其打印到串口显示器。

我的短信功能也有效,但只有当我在代码的顶部使用定义的移动(#define PHONE_NUMBER "+639056600152")号码时它才有效。我可以使用此接收短信,但是从数据库中传递的手机号码不起作用。

以下是我的代码。

#include <GPRS_Shield_Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Ethernet.h>

//SERIAL PIN ASSIGNMENT, BAUDRATE, PHONE NUMBER, MESSAGE
#define PIN_TX    3
#define PIN_RX    2
#define BAUDRATE  9600
#define PHONE_NUMBER "+639056600152"
#define MESSAGE  "Smoke was detected in your apartment unit"

String response = String(100);
byte mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
EthernetClient client;
char server[] = "192.168.2.100"; // change this to your server IP

GPRS GSMTEST(PIN_TX, PIN_RX, BAUDRATE); //RX,TX,BAUDRATE

void setup() {
  Serial.begin(9600);
  while (!GSMTEST.init()) {
    delay(1000);
    Serial.print("INIT ERROR\r\n");
  }
  Serial.println("GSM INIT SUCCESS");
  Ethernet.begin(mac);
  Serial.print("IP Address        : ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  if (client.connect(server, 80)) {
    Serial.println("-> Connected");
    // Make a HTTP request:
    client.print( "GET /apartment/getnum.php");
    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Connection: close" );
    client.println();
    client.println();
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //Serial.print(c);
        response = response + c;
      }
    }
    int contentBodyIndex = response.lastIndexOf('\n');
    if (contentBodyIndex > 0) {
      String str = response.substring(contentBodyIndex);
      int respLen = str.length() + 1;
      char charArr[respLen];
      str.toCharArray(charArr, respLen);
      Serial.print(charArr);
      if (GSMTEST.sendSMS(charArr, MESSAGE)) {
        Serial.print("Message SENT!");
      } else {
        Serial.print("Message NOT SENT!");
      }
    }
    delay(5000);
    client.stop();
  } else {
    Serial.println("--> connection failed/n");
  }
}

0 个答案:

没有答案