2013-03-11 12:17:08 +01:00
|
|
|
/*
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
This sketch test the GSM shield's ability to connect to a
|
2013-10-21 09:58:40 +02:00
|
|
|
GPERS network. It asks for APN information through the
|
2013-03-11 12:17:08 +01:00
|
|
|
serial monitor and tries to connect to arduino.cc.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Circuit:
|
|
|
|
* GSM shield attached
|
|
|
|
* SIM card with data plan
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Created 18 Jun 2012
|
|
|
|
by David del Peral
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
This example code is part of the public domain
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-05-20 17:10:06 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/GSMToolsTestGPRS
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
// libraries
|
|
|
|
#include <GSM.h>
|
|
|
|
|
|
|
|
// PIN Number
|
|
|
|
#define PINNUMBER ""
|
|
|
|
|
|
|
|
// initialize the library instance
|
|
|
|
GSM gsmAccess; // GSM access: include a 'true' parameter for debug enabled
|
|
|
|
GPRS gprsAccess; // GPRS access
|
|
|
|
GSMClient client; // Client service for TCP connection
|
|
|
|
|
|
|
|
// messages for serial monitor response
|
|
|
|
String oktext = "OK";
|
|
|
|
String errortext = "ERROR";
|
|
|
|
|
|
|
|
// URL and path (for example: arduino.cc)
|
|
|
|
char url[] = "arduino.cc";
|
2015-05-20 17:10:06 +02:00
|
|
|
char urlproxy[] = "http://www.arduino.cc";
|
2013-03-11 12:17:08 +01:00
|
|
|
char path[] = "/";
|
|
|
|
|
|
|
|
// variable for save response obtained
|
|
|
|
String response = "";
|
|
|
|
|
|
|
|
// use a proxy
|
|
|
|
boolean use_proxy = false;
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
use_proxy = false;
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// start GSM shield
|
|
|
|
// if your SIM has PIN, pass it as a parameter of begin() in quotes
|
|
|
|
Serial.print("Connecting GSM network...");
|
2013-10-21 09:58:40 +02:00
|
|
|
if (gsmAccess.begin(PINNUMBER) != GSM_READY)
|
2013-03-11 12:17:08 +01:00
|
|
|
{
|
|
|
|
Serial.println(errortext);
|
2013-10-21 09:58:40 +02:00
|
|
|
while (true);
|
2013-03-11 12:17:08 +01:00
|
|
|
}
|
|
|
|
Serial.println(oktext);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// read APN introduced by user
|
|
|
|
char apn[50];
|
|
|
|
Serial.print("Enter your APN: ");
|
|
|
|
readSerial(apn);
|
|
|
|
Serial.println(apn);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// Read APN login introduced by user
|
|
|
|
char login[50];
|
|
|
|
Serial.print("Now, enter your login: ");
|
|
|
|
readSerial(login);
|
|
|
|
Serial.println(login);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// read APN password introduced by user
|
|
|
|
char password[20];
|
|
|
|
Serial.print("Finally, enter your password: ");
|
|
|
|
readSerial(password);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// attach GPRS
|
|
|
|
Serial.println("Attaching to GPRS with your APN...");
|
2013-10-21 09:58:40 +02:00
|
|
|
if (gprsAccess.attachGPRS(apn, login, password) != GPRS_READY)
|
2013-03-11 12:17:08 +01:00
|
|
|
{
|
|
|
|
Serial.println(errortext);
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
else {
|
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println(oktext);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// read proxy introduced by user
|
|
|
|
char proxy[100];
|
|
|
|
Serial.print("If your carrier uses a proxy, enter it, if not press enter: ");
|
|
|
|
readSerial(proxy);
|
|
|
|
Serial.println(proxy);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// if user introduced a proxy, asks him for proxy port
|
|
|
|
int pport;
|
2013-10-21 09:58:40 +02:00
|
|
|
if (proxy[0] != '\0') {
|
2013-03-11 12:17:08 +01:00
|
|
|
// read proxy port introduced by user
|
|
|
|
char proxyport[10];
|
|
|
|
Serial.print("Enter the proxy port: ");
|
|
|
|
readSerial(proxyport);
|
|
|
|
// cast proxy port introduced to integer
|
|
|
|
pport = (int) proxyport;
|
|
|
|
use_proxy = true;
|
|
|
|
Serial.println(proxyport);
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// connection with arduino.cc and realize HTTP request
|
|
|
|
Serial.print("Connecting and sending GET request to arduino.cc...");
|
|
|
|
int res_connect;
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// if use a proxy, connect with it
|
2013-10-21 09:58:40 +02:00
|
|
|
if (use_proxy)
|
2013-03-11 12:17:08 +01:00
|
|
|
res_connect = client.connect(proxy, pport);
|
|
|
|
else
|
|
|
|
res_connect = client.connect(url, 80);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
if (res_connect)
|
|
|
|
{
|
|
|
|
// make a HTTP 1.0 GET request (client sends the request)
|
|
|
|
client.print("GET ");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// if use a proxy, the path is arduino.cc URL
|
2013-10-21 09:58:40 +02:00
|
|
|
if (use_proxy)
|
2013-03-11 12:17:08 +01:00
|
|
|
client.print(urlproxy);
|
|
|
|
else
|
|
|
|
client.print(path);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
client.println(" HTTP/1.0");
|
|
|
|
client.println();
|
|
|
|
Serial.println(oktext);
|
2013-10-21 09:58:40 +02:00
|
|
|
}
|
2013-03-11 12:17:08 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// if you didn't get a connection to the server
|
|
|
|
Serial.println(errortext);
|
|
|
|
}
|
|
|
|
Serial.print("Receiving response...");
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
boolean test = true;
|
2013-10-21 09:58:40 +02:00
|
|
|
while (test)
|
2013-03-11 12:17:08 +01:00
|
|
|
{
|
2013-10-21 09:58:40 +02:00
|
|
|
// if there are incoming bytes available
|
2013-03-11 12:17:08 +01:00
|
|
|
// from the server, read and check them
|
|
|
|
if (client.available())
|
|
|
|
{
|
|
|
|
char c = client.read();
|
|
|
|
response += c;
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// cast response obtained from string to char array
|
2013-10-21 09:58:40 +02:00
|
|
|
char responsechar[response.length() + 1];
|
|
|
|
response.toCharArray(responsechar, response.length() + 1);
|
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// if response includes a "200 OK" substring
|
2013-10-21 09:58:40 +02:00
|
|
|
if (strstr(responsechar, "200 OK") != NULL) {
|
2013-03-11 12:17:08 +01:00
|
|
|
Serial.println(oktext);
|
|
|
|
Serial.println("TEST COMPLETE!");
|
|
|
|
test = false;
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2013-03-11 12:17:08 +01:00
|
|
|
// if the server's disconnected, stop the client:
|
|
|
|
if (!client.connected())
|
|
|
|
{
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("disconnecting.");
|
|
|
|
client.stop();
|
|
|
|
test = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Read input serial
|
|
|
|
*/
|
|
|
|
int readSerial(char result[])
|
|
|
|
{
|
|
|
|
int i = 0;
|
2013-10-21 09:58:40 +02:00
|
|
|
while (1)
|
2013-03-11 12:17:08 +01:00
|
|
|
{
|
|
|
|
while (Serial.available() > 0)
|
|
|
|
{
|
|
|
|
char inChar = Serial.read();
|
|
|
|
if (inChar == '\n')
|
|
|
|
{
|
|
|
|
result[i] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
2013-10-21 09:58:40 +02:00
|
|
|
if (inChar != '\r')
|
2013-03-11 12:17:08 +01:00
|
|
|
{
|
|
|
|
result[i] = inChar;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|