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.