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.
