beginSMS()

描述

初始化簡訊並且指定簡訊要送至的電話號碼。要使用 write() 寫入簡訊內容前必須先呼叫 beginSMS()

語法


SMS.beginSMS(number)

參數

number:用來存放簡訊發送目的地的字元陣列

回傳

int:在非同步模式中回傳 0 表示最後呼叫的指令仍然在執行中,回傳 1 表示簡訊初始化成功,而回傳大於 1 的值代表有錯誤發生;在同步模式中回傳 1 表示簡訊初始化成功,回傳 0 表示簡訊初始化失敗

範例

#include <GSM.h>

#define PINNUMBER ""

// 初始化 GSM 函式庫
GSM gsmAccess;
GSM_SMS sms;

void setup()
{
  // 初始化 Serial
  Serial.begin(9600);

  Serial.println("SMS Messages Sender");

  // 連線狀態
  boolean notConnected = true;

  // 啟動 Arduino GSM shield,如果你的 SIM 卡有 PIN 碼,請當作 begin() 的參數
  while(notConnected)
  {
    if(gsmAccess.begin(PINNUMBER)==GSM_READY)
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
}

void loop()
{

  Serial.print("Enter a mobile number: ");
  // 用來存放簡訊目的地的電話號碼
  char remoteNumber[20];
  readSerial(remoteNumber);
  Serial.println(remoteNumber);

  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // 傳送簡訊
  sms.beginSMS(remoteNumber);
  sms.print(txtMsg);
  sms.endSMS(); 
  Serial.println("\nCOMPLETE!\n");
}

/*
  從序列埠監控視窗讀取輸入
 */
int readSerial(char result[])
{
  int i = 0;
  while(1)
  {
    while (Serial.available() > 0)
    {
      char inChar = Serial.read();
      if (inChar == '\n')
      {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if(inChar!='\r')
      {
        result[i] = inChar;
        i++;
      }
    }
  }
}

See also

GSM_SMS
ready()
endSMS()
available()
remoteNumber()
read()
write()
print()
peek()
flush()


函式庫參考主頁面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.