GSM Test Web Server
This sketch creates a web server to accept incoming connections on the Arduino MKR GSM 1400. Some network providers only allow requests from inside their own network. You will need to check with your network provider to make sure your SIM card will accept incoming HTTP requests.
Hardware Required
antenna
SIM card enable for Data
Circuit
Code
The complete sketch is below.
/*
Basic Web Server
A simple web server that replies with nothing, but prints the client's request
and the server IP address.
Circuit:
* MKR GSM 1400 board
* Antenna
created
by David Cuartielles
modified 21 Nov 2012
by Tom Igoe
*/
#include <MKRGSM.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;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
// initialize the library instance
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSMServer server(80); // port 80 (http default)
// timeout
const unsigned long __TIMEOUT__ = 10 * 1000;
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("starting,..");
// connection state
bool connected = false;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("Connected to GPRS network");
// start server
server.begin();
//Get IP.
IPAddress LocalIP = gprs.getIPAddress();
Serial.println("Server IP address=");
Serial.println(LocalIP);
}
void loop() {
GSMClient client = server.available();
if (client) {
if (client.available()) {
Serial.write(client.read());
}
}
}
See Also
Arduino MKR GSM 1400 - Complete product description.
Getting started with the MKR GSM 1400 - Get everything set up in minutes.
MKRGSM library - Your reference for the GSM Library.
MKRGSMToolsTestGPRS - Tries to access the internet over GPRS with supplied APN and credentials.
GSMToolsBandManagement - Checks the band currently configured in the modem and allows you to change it.
MKRGSMToolsGsmScanNetworks - Scans the available networks and prints informations about IMEI and number of the SIM card.
MKRGSMToolsPinManagement - How to to change or remove the PIN number.
MKRGSMToolsTestModem - Tests to see if the modem of the GSM shield is working correctly.
MKRGSMExamplesMakeVoiceCall - How to make a voice call with mic and speaker.
Last revision 2017/11/29 by AG