当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino MKRNB - attachGPRS()用法及代码示例


说明

连接到NarrowBand IoT 或 LTE Cat M1 以启动 GPRS 通信。

每个蜂窝提供商都有一个接入点名称 (APN),用作蜂窝网络和互联网之间的桥梁。有时,连接点有用户名和密码,但 NB 技术依赖其他信息来验证连接,因此您无需指定任何内容,因为您的 SIM 和网络将安全地建立连接,无需任何参数手动给的。

用法

gprs.attachGPRS()

参数

None

返回

错误、空闲、连接、NB_READY、GPRS_READY、TRANSPARENT_CONNECTED

示例

/*
  Web client

  This sketch connects to a website through a MKR NB 1500 board. Specifically,
  this example downloads the URL "http://example.org/" and
  prints it to the Serial monitor.

  Circuit:
   - MKR NB 1500 board
   - Antenna
   - SIM card with a data plan

  created 8 Mar 2012
  by Tom Igoe
*/

// libraries
#include <MKRNB.h>

#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = SECRET_PINNUMBER;

// initialize the library instance
NBClient client;
GPRS gprs;
NB nbAccess;

// URL, path and port (for example: example.org)
char server[] = "example.org";
char path[] = "/";
int port = 80; // port 80 is the default for HTTP

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting Arduino web client.");
  // connection state
  boolean connected = false;

  // After starting the modem with NB.begin()
  // attach to the GPRS network with the APN, login and password
  while (!connected) {
    if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
        (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    Serial.print((char)client.read());
  }

  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for (;;)
      ;
  }
}

相关用法


注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 MKRNB - attachGPRS()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。