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.
Optional analog sensors like photoresistors, potentiometers and such may be connected, as explained elsewhere in our tutorials, to pins A0 - A5
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.
In setup
, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.
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 :
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.
Start the server using server.begin()
. You can request the server's IP address with grps.getIPAddress()
and end the setup.
In loop
, create an instance of NBClient
and check if there are any active connections
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.
Once the request has been read, start to send a standard HTTP response header with client.print()
and client.println()
.
Read through the analog inputs and send the values to the client.
Send a closing tag for the webpage, and stop the client connection before closing the loop
.
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.
Last revision 2018/11/21 by SM