Web Client with MKR NB 1500

Learn how to make a request to a web server over the NB-IoT / CAT-M1 network.

Introduction

In this tutorial, we will use the MKR NB 1500 and the MKRNB library to make a request to a web server, and print the content in the Serial Monitor.

Goals

The goals of this project are:

  • Make a request to a server.
  • Print the content in the Serial Monitor.

Hardware & Software Needed

Circuit

The circuit for this tutorial is easy: simply attach the dipole antenna to the board.

Simple circuit of board and antenna.
Simple circuit of board and antenna.

Creating the Program

We will now get to the programming part of this tutorial.

1. First, let's make sure we have the drivers installed. If we are using the Web Editor, we do not need to install anything. If we are using an offline editor, we need to install it manually. This can be done by navigating to Tools > Board > Board Manager.... Here we need to look for the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) and install it.

2. Now, we need to install the libraries needed. If we are using the Web Editor, there is no need to install anything. If we are using an offline editor, simply go to Tools > Manage libraries.., and search for MKRNB and install it.

3. We will now take a look at the main functions in this program:

  • const char PINNUMBER[]
    - stores the pin number of your card.
  • NB nbAccess
    - base class for all NB based functions.
  • GPRS gprs
    - base class for GPRS functions, e.g. Internet, server behaviors.
  • char server[] = "example.org"
    - server we are connecting to.
  • char path[] = "/"
    - additional path (in this case none).
  • int port = 80
    - port 80 is default for HTTP.
  • nbAccess.begin(pin)
    - starts the modem.
  • gprs.attachGPRS()
    - attach to the GPRS network.
  • client.print()
    - prints to the client. This is used to e.g. make the GET request.
  • client.available()
    - check if data is available.
  • client.read()
    - reads incoming data.

The code is available in the snippet below. Make sure you change the

PINNUMBER
variable to match the pin on your SIM card. This code can also be found in File > Examples > MKRNB > NBWebClient in the editor. Note that if you use that example, you need to enter your pin in the
arduino_secrets.h
file.

1// libraries
2#include <MKRNB.h>
3
4// PIN Number
5const char PINNUMBER[] = "0000";
6
7// initialize the library instance
8NBClient client;
9GPRS gprs;
10NB nbAccess;
11
12// URL, path and port (for example: example.org)
13char server[] = "example.org";
14char path[] = "/";
15int port = 80; // port 80 is the default for HTTP
16
17void setup() {
18 // initialize serial communications and wait for port to open:
19 Serial.begin(9600);
20 while (!Serial) {
21 ; // wait for serial port to connect. Needed for native USB port only
22 }
23
24 Serial.println("Starting Arduino web client.");
25 // connection state
26 boolean connected = false;
27
28 // After starting the modem with NB.begin()
29 // attach to the GPRS network with the APN, login and password
30 while (!connected) {
31 if ((nbAccess.begin(PINNUMBER) == NB_READY) &&
32 (gprs.attachGPRS() == GPRS_READY)) {
33 connected = true;
34 } else {
35 Serial.println("Not connected");
36 delay(1000);
37 }
38 }
39
40 Serial.println("connecting...");
41
42 // if you get a connection, report back via serial:
43 if (client.connect(server, port)) {
44 Serial.println("connected");
45 // Make a HTTP request:
46 client.print("GET ");
47 client.print(path);
48 client.println(" HTTP/1.1");
49 client.print("Host: ");
50 client.println(server);
51 client.println("Connection: close");
52 client.println();
53 } else {
54 // if you didn't get a connection to the server:
55 Serial.println("connection failed");
56 }
57}
58
59void loop() {
60 // if there are incoming bytes available
61 // from the server, read them and print them:
62 if (client.available()) {
63 Serial.print((char)client.read());
64 }
65
66 // if the server's disconnected, stop the client:
67 if (!client.available() && !client.connected()) {
68 Serial.println();
69 Serial.println("disconnecting.");
70 client.stop();
71
72 // do nothing forevermore:
73 for (;;)
74 ;
75 }
76}

Testing It Out

After the code has been successfully uploaded, we need to open the Serial Monitor to initialize the rest of the program. For these type of projects, we use the

while(!Serial)
command, so we can read any available information only after we open the Serial Monitor.

After we open the Serial Monitor, the board will start connecting to the network.

Starting the web client.
Starting the web client.

When it connects, it will then make a GET request to example.org. It basically downloads the contents, and prints it in the Serial Monitor. The result is that you can see the entire page in the Serial Monitor.

The HTML content of example.org printed in the Serial Monitor.
The HTML content of example.org printed in the Serial Monitor.

Troubleshoot

Note that the board does not always find a way to connect to the network. There can be several issues behind this.

  • Antenna is not working.
  • SIM card is not working.
  • We're not in range of a network.

Conclusion

In this tutorial, we have simply made a GET request to a server, received it, and printed it in the Serial Monitor. There are many different ways we can build on this, such as making requests to servers that has live data updated every so often.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.