attachGPRS()
描述
連線到指定的接入點(Access Point Name, APN)並初始化 GPRS 連線。
電信提供商會給使用者接入點並且扮演 GPRS 網路和網際網路間的橋樑。有些接入點並不需要使用者名稱和密碼。
這個網頁列出了一些電信提供商的接入點資訊,但可能有一段時間沒有更新了。
語法
grps.attachGPRS(APN, user, password)
參數
APN:存放電信提供商網路接入點的字元陣列
user:存放接入點使用者名稱的字元陣列
password:存放接入點密碼的字元陣列
回傳
char array:ERROR, IDLE, CONNECTING, GSM_READY, GPRS_READY, TRANSPARENT_CONNECTED
範例
#include <GSM.h>
// PIN 碼
#define PINNUMBER ""
// APN 資料
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password
// 初始化函式庫
GPRS gprs;
GSM gsmAccess;
GSMServer server(80);
// 時限
const unsigned long __TIMEOUT__ = 10*1000;
void setup()
{
// 初始化 Serial
Serial.begin(9600);
// 連線狀態
boolean notConnected = true;
// 啟動 Arduino GSM shield,如果你的 SIM 卡有 PIN 碼,請當作 begin() 的參數
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("Connected to GPRS network");
// 啟動 server
server.begin();
// 取得 IP 位址
IPAddress LocalIP = gprs.getIPAddress();
Serial.println("Server IP address=");
Serial.println(LocalIP);
}
void loop() {
// 監聽是否有客戶端要求
GSM3MobileClientService client = server.available();
if (client)
{
while (client.connected())
{
if (client.available())
{
Serial.println("Receiving request!");
bool sendResponse = false;
while(char c=client.read()) {
if (c == '\n') sendResponse = true;
}
// 判斷是否讀到換行字元
if (sendResponse)
{
// 送出標準的 HTTP 標頭檔
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
// 把每一個類比腳位的值寫入封包
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(analogRead(analogChannel));
client.println("<br />");
}
client.println("</html>");
// 需要延遲
delay(1000);
client.stop();
}
}
}
}
}
See also
- GPRS
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.
