NB Web Server

This sketch turns the Arduino MKR NB 1500 into a web server. When the board receives a request from a connected client, it sends back the value of analog inputs 0-5.

Not all network operators allow incoming data requests from outside their network. This means you can create a web server with the MKR NB 1500, but you may not be able to connect to it from the public internet; only from another data enabled device from the same provider on the same network. You should check with your provider to see what specific policies they have in place regarding incoming data connections.

Hardware Required

  • Arduino MKR NB 1500
  • antenna
  • SIM card enable for Data
  • (optional) 6 potentiometers or other analog inputs attached to A0-A5

Circuit

Optional analog sensors like photoresistors, potentiometers and such may be connected, as explained elsewhere in our tutorials, to pins A0 - A5

Code

First, import the MKRNB library

#include <MKRNB.h>

SIM cards may have a PIN number that enables their functionality. Define the PIN for your SIM. If your SIM has no PIN, you can leave it blank :

#define PINNUMBER ""

Initialize instances of the classes you're going to use. You're going to need the NB, GPRS, and NBServer classes. When you instantiate the NBServer class, you'll need to tell it which port to listen for incoming connections. Port 80 is the default port for HTTP requests.

GPRS gprs;
NB nbAccess;
NBServer server(80);

In setup, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.

void setup(){
  Serial.begin(9600);
  Serial.println("Starting Arduino web client.");

Create a local variable to track the connection status. You'll use this to keep the sketch from starting until the SIM is connected to the network :

boolean connected = true;

Connect to the network by calling nbAccess.begin(). It takes the SIM card's PIN as an argument. You'll also connect to the GPRS network using gprs.attachGPRS(). By placing this inside a while() loop, you can continually check the status of the connection and wait for them to both become true before proceeding.

When the modem does connect and has attached itself to the GPRS network, nbAccess() will return NB_READY. Use this as a flag to set the connected variable to true or false. Once connected, the remainder of setup will run.

while(!connected)
 {
   if ((nbAccess.begin(PINNUMBER) == GSM_READY) &&
      (gprs.attachGPRS() == GPRS_READY)) {
    connected = true;
   else
   {
     Serial.println("Not connected");
     delay(1000);
   }
 }

Start the server using server.begin(). You can request the server's IP address with grps.getIPAddress() and end the setup.

server.begin();

  IPAddress LocalIP = gprs.getIPAddress();
  Serial.println("Server IP address=");
  Serial.println(LocalIP);
}

In loop, create an instance of NBClient and check if there are any active connections

void loop() {
  NBClient client = server.available();

  if (client)
  {

While the client is connected, and there is data waiting to be read, begin to read the request. Read through the available bytes until a newline character has been received.

In this instance, you won't actually do anything with the request, it's assumed that it is a HTTP request, and you'll serve up a web page.

while (client.connected())
    {
      if (client.available())
      {
        Serial.println("Receiving request!");
        bool sendResponse = false;
        while(char c=client.read()) {
          if (c == '\n') sendResponse = true;
        }

Once the request has been read, start to send a standard HTTP response header with client.print() and client.println().

if (sendResponse)
       {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<html>");

Read through the analog inputs and send the values to the client.

for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");      
          }

Send a closing tag for the webpage, and stop the client connection before closing the loop.

client.println("</html>");
          //necessary delay
          delay(1000);
          client.stop();
        }
      }
    }
  }
}

Once your code is uploaded, open the serial monitor. Once the IP address is printed to the serial monitor, enter it into a web browser. You should see a webpage that reports the analog input values on each the Arduino's six inputs.

if you cannot connect to the IP address, make sure your network operator enables incoming traffic.

The complete sketch is below.

SORRY, There is an error at our code repository, please inform to web@arduino.cc

See Also




Last revision 2018/11/21 by SM