Tutorial.GSMToolsTestModem History

Hide minor edits - Show changes to output

March 11, 2013, at 10:31 AM by Scott Fitzgerald -
Changed lines 185-188 from:
(:include GSMLibrarySeeAlsoIncludes :)
to:
* [[/Reference/GSMModemConstructor | GSMModem]] Constructor
* [[/Reference/GSMModemBegin | begin]]()
* [[/Reference/GSMModemGetIMEI | getIMEI]]()
February 04, 2013, at 08:22 AM by Scott Fitzgerald -
Changed lines 5-6 from:
This sketch gives you diagnostic information about the modem on the GSM shield.
to:
This sketch tests the modem on the GSM shield to see if it is working correctly. You do not need a SIM card for this example.
Deleted line 10:
* SIM card with a data connection
Changed lines 26-31 from:
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 ""@@

Define a number of constants that contain information about the GPRS network you're going to connect to. You'll need the Access Point Name (APN), login, and password. To obtain this information, contact your network provider for the most up to date information. [[http://forums.pinstack.com/f24/tcp_apn_wap_gateway_port_carrier_settings-360/ | This page]] has some information about various carrier settings, but it may not be current.
to:
Create an instance of the GSMModem class:

@@GSMModem modem;@@

Create a variable to hold the IMEI number of the modem
Changed lines 33-35 from:
#define GPRS_APN "GPRS_APN"
#define GPRS_LOGIN "login"
#define GPRS_PASSWORD "password"
to:
String IMEI = "";
Changed lines 36-37 from:
Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, GSMServer, and GSMClient classes. When you instantiate the GSMServer class, you'll need to tell it which port to listen for incoming connections. Port 80 is the default port for HTTP requests.
to:
In @@setup@@, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.
Changed lines 39-42 from:
GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSMServer server(80);
to:
void setup(){
Serial.begin(9600);
Serial.print("Starting modem test...");
Changed lines 44-45 from:
Create some variables to set a timeout period, flag incoming client messages, and a buffer to read incoming bytes. to hold the server, path, and port you wish to connect to.
to:
Call @@modem.begin()@@ to start the modem. Send a status message depending on the outcome, and end @@setup()@@.
Changed lines 47-49 from:
char server[] = "arduino.cc";
char path[] = "/";
int port = 80;
to:
if(modem.begin())
Serial.println("modem.begin() succeeded");
else
Serial.println("ERROR, no modem answer.");
}
Changed lines 54-55 from:
In @@setup@@, open a serial connection to the computer. After opening the connection, send a message indicating the sketch has started.
to:

Inside @@loop@@, use @@modem.getIMEI()@@ to return the IMEI number of the modem. This number is unique to your GSM shield.
Changed lines 58-60 from:
void setup(){
Serial.begin(9600);
Serial.println("Starting Arduino web client.");
to:
void loop()
{
// get modem IMEI
Serial.print("Checking IMEI...");
IMEI = modem.getIMEI();
Changed lines 65-66 from:
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 :
to:
If there is a valid response from @@getIMEI()@@, print it to the serial monitor and reset the modem with @@modem.begin()@@.
Changed lines 68-74 from:
boolean notConnected = true;
to:
if(IMEI != NULL)
{
// show IMEI in serial monitor
Serial.println("Modem's IMEI: " + IMEI);
// reset modem to check booting:
Serial.print("Reseting modem...");
modem.begin();
Changed lines 77-80 from:
Connect to the network by calling @@gsmAccess.begin()@@. It takes the SIM card's PIN as an argument. You'll also connect to the GPRS network using @@gprs.attachGPRS()@@. This requires the APN, login, and password you declared earlier. 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 nd had attached itself to the GPRS network, @@gsmAccess()@@ will return @@GSM_READY@@. Use this as a flag to set the @@notConnected@@ variable to @@true@@ or @@false@@. Once connected, the remainder of @@setup@@ will run.
to:
Once reset, check the IMEI again. If it is a valid return again, the modem is functioning as expected.
Changed lines 80-85 from:
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
to:
if(modem.getIMEI() != NULL)
Changed lines 82-83 from:
Serial.println("Not connected");
delay(1000);
to:
Serial.println("Modem is functoning properly");
Deleted line 83:
}
Changed lines 86-87 from:
Attempt to connect to the server using @@client.connect()@@. @@connect()@@ requires two arguments, the server and port. Once connected to the server, make a HTTP request by calling @@client.print()@@. A typical web request looks like "GET pathname HTTP/1.0". @@print@@ will send the message, just as a browser would.
to:
If, after resetting the modem, there is not a valid return from @@getIMEI()@@, report an error
Changed lines 89-99 from:
if (client.connect(server, port))
to:
else
{
Serial.println("Error: getIMEI() failed after modem.begin()");
}
(:sourceend:)

If you never received an IMEI after starting the sketch, report it, and end the program.

(:source lang=arduino tabwidth=4:)
}
else
Changed lines 101-106 from:
Serial.println("connected");
client.print("GET ");
client.print(path);
client.println(" HTTP/1.0");
client.println();
}
to:
Serial.println("Error: Could not get IMEI");
}
// do nothing:
while(true);
}
Changed lines 108-109 from:
If the connection to the server failed, make a note of it in the serial monitor, and close up @@setup@@.
to:
Once your code is uploaded, open the serial monitor. You should see the HTML of http://arduino.cc print out on screen when it is received.

(:div class=code :)
Added lines 112-142:
/*

This example test if your modem is working correctly.

Circuit:
* GSM shield attached (using digital pins 2, 3, and 7)

Created 12 Jun 2012
by David del Peral
modified 21 Nov 2012
by Tom Igoe
*/

// libraries
#include <GSM.h>

// modem verification object
GSMModem modem;

// IMEI variable
String IMEI = "";

void setup()
{
// initialize serial communications
Serial.begin(9600);

// start modem test (reset and check response)
Serial.print("Starting modem test...");
if(modem.begin())
Serial.println("modem.begin() succeeded");
Changed lines 144-146 from:
{
Serial.println("connection failed");
}
to:
Serial.println("ERROR, no modem answer.");
Changed lines 146-151 from:
(:sourceend:)

Inside @@loop@@, check to see if there are bytes returned from the server. If so, read them and print them to the serial monitor.

(:source lang=arduino tabwidth=4:)
if (client.available())
to:

void loop()
{
// get modem IMEI
Serial.print("Checking IMEI...");
IMEI = modem.getIMEI();

// check IMEI response
if(IMEI != NULL)
Changed lines 156-157 from:
char c = client.read();
Serial.print(c);
to:
// show IMEI in serial monitor
Serial.println("Modem's IMEI: " + IMEI);
// reset modem to check booting:
Serial.print("Reseting modem...");
modem.begin();
// get and check IMEI one more time
if(modem.getIMEI() != NULL)
{
Serial.println("Modem is functoning properly");
}
else
{
Serial.println("Error: getIMEI() failed after modem.begin()");
}
Changed lines 171-176 from:
(:sourceend:)

If the server disconnects, which it will typically do once it has completed the HTTP request, stop the client locally and close the @@loop@@.

(:source lang=arduino tabwidth=4:)
if (!client.available() && !client.connected())
to:
else
Changed lines 173-179 from:
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
for(;;)
;
to:
Serial.println("Error: Could not get IMEI");
Added lines 175-176:
// do nothing:
while(true);
Added line 178:
Deleted lines 179-365:


Once your code is uploaded, open the serial monitor. You should see the HTML of http://arduino.cc print out on screen when it is received.

(:div class=code :)
(:source lang=arduino tabwidth=4:)
/*
Pachube sensor client

This sketch connects an analog sensor to Pachube (http://www.pachube.com)
using a Telefonica GSM/GPRS shield.

This example has been updated to use version 2.0 of the Pachube.com API.
To make it work, create a feed with a datastream, and give it the ID
sensor1. Or change the code below to match your feed.

Circuit:
* Analog sensor attached to analog in 0
* GSM shield attached to pins 10, 11, 12, 13

created 4 March 2012
by Tom Igoe
and adapted for GSM shield by David Del Peral

This code is in the public domain.

*/

// libraries
#include <GSM.h>

// Pachube Client data
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password

// initialize the library instance:
GSMClient client;
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.pachube.com
char server[] = "api.pachube.com"; // name address for pachube API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

void setup()
{
// initialize serial communications
Serial.begin(9600);

// connection state
boolean notConnected = true;

// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
}

void loop()
{
// read the analog sensor:
int sensorReading = analogRead(A0);

// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected)
{
client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
{
sendData(sensorReading);
}

// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}

/*
This method makes a HTTP connection to the server.
*/
void sendData(int thisData)
{
// if there's a successful connection:
if (client.connect(server, 80))
{
Serial.println("connecting...");

// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.print("Host: api.pachube.com\n");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");

// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 8 + getLength(thisData);
client.println(thisLength);

// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close");
client.println();

// here's the actual content of the PUT request:
client.print("sensor1,");
client.println(thisData);
}
else
{
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted
lastConnectionTime = millis();
}

/*
This method calculates the number of digits in the
sensor reading. Since each digit of the ASCII decimal
representation is a byte, the number of digits equals
the number of bytes.
*/
int getLength(int someValue)
{
// there's at least one byte:
int digits = 1;

// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0)
{
dividend = dividend /10;
digits++;
}

// return the number of digits:
return digits;
}

(:sourceend:)
February 03, 2013, at 07:24 PM by Scott Fitzgerald -
Changed lines 1-6 from:
''Examples > Make Voice Call''

!!!Using the GSM Shield to Make Calls

This example shows how to use the GSM Shield and Library to make voice calls. Please [[/Reference/GSM | click here ]] for more information on the GSM library.
to:
''Examples > GSM library''

!!!GSM Test Modem

This sketch gives you diagnostic information about the modem on the GSM shield.
Changed line 11 from:
* SIM card
to:
* SIM card with a data connection
Changed lines 23-25 from:
The code below is configured for use with a GSM/GPRS shield. In the variable declaration area, you need to instantiate both the @@GSM@@ and @@GSMVoiceCall@@ classes. Inside @@setup()@@, call @@GSM.begin()@@, with the @@PINNUMBER@@ of your SIM card as parameter. @@GSM.begin()@@ will return whether the shield managed to connect to the GSM network or not.

In the @@loop()@@, the input from the Serial Monitor is used to tell the GSM Shield a phone number to call to. The code waits until the call is over before performing any other actions.
to:
First, import the GSM library

@@#include <GSM.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 ""@@

Define a number of constants that contain information about the GPRS network you're going to connect to. You'll need the Access Point Name (APN), login, and password. To obtain this information, contact your network provider for the most up to date information. [[http://forums.pinstack.com/f24/tcp_apn_wap_gateway_port_carrier_settings-360/ | This page]] has some information about various carrier settings, but it may not be current.

(:source lang=arduino tabwidth=4:)
#define GPRS_APN "GPRS_APN"
#define GPRS_LOGIN "login"
#define GPRS_PASSWORD "password"
(:sourceend:)

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

(:source lang=arduino tabwidth=4:)
GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSMServer server(80);
(:sourceend:)

Create some variables to set a timeout period, flag incoming client messages, and a buffer to read incoming bytes. to hold the server, path, and port you wish to connect to.

(:source lang=arduino tabwidth=4:)
char server[] = "arduino.cc";
char path[] = "/";
int port = 80;
(:sourceend:)

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

(:source lang=arduino tabwidth=4:)
void setup(){
Serial.begin(9600);
Serial.println("Starting Arduino web client.");
(:sourceend:)

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 :

(:source lang=arduino tabwidth=4:)
boolean notConnected = true;
(:sourceend:)

Connect to the network by calling @@gsmAccess.begin()@@. It takes the SIM card's PIN as an argument. You'll also connect to the GPRS network using @@gprs.attachGPRS()@@. This requires the APN, login, and password you declared earlier. 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 nd had attached itself to the GPRS network, @@gsmAccess()@@ will return @@GSM_READY@@. Use this as a flag to set the @@notConnected@@ variable to @@true@@ or @@false@@. Once connected, the remainder of @@setup@@ will run.

(:source lang=arduino tabwidth=4:)
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
(:sourceend:)

Attempt to connect to the server using @@client.connect()@@. @@connect()@@ requires two arguments, the server and port. Once connected to the server, make a HTTP request by calling @@client.print()@@. A typical web request looks like "GET pathname HTTP/1.0". @@print@@ will send the message, just as a browser would.

(:source lang=arduino tabwidth=4:)
if (client.connect(server, port))
{
Serial.println("connected");
client.print("GET ");
client.print(path);
client.println(" HTTP/1.0");
client.println();
}
(:sourceend:)

If the connection to the server failed, make a note of it in the serial monitor, and close up @@setup@@.

(:source lang=arduino tabwidth=4:)
else
{
Serial.println("connection failed");
}
}
(:sourceend:)

Inside @@loop@@, check to see if there are bytes returned from the server. If so, read them and print them to the serial monitor.

(:source lang=arduino tabwidth=4:)
if (client.available())
{
char c = client.read();
Serial.print(c);
}
(:sourceend:)

If the server disconnects, which it will typically do once it has completed the HTTP request, stop the client locally and close the @@loop@@.

(:source lang=arduino tabwidth=4:)
if (!client.available() && !client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
for(;;)
;
}
}
(:sourceend:)


Once your code is uploaded, open the serial monitor. You should see the HTML of http://arduino.cc print out on screen when it is received.
Changed lines 141-322 from:
(:source https://raw.github.com/arduino/Arduino/master/libraries/GSM/examples/MakeVoiceCall/MakeVoiceCall.ino lang=arduino tabwidth=4:)
to:
(:source lang=arduino tabwidth=4:)
/*
Pachube sensor client

This sketch connects an analog sensor to Pachube (http://www.pachube.com)
using a Telefonica GSM/GPRS shield.

This example has been updated to use version 2.0 of the Pachube.com API.
To make it work, create a feed with a datastream, and give it the ID
sensor1. Or change the code below to match your feed.

Circuit:
* Analog sensor attached to analog in 0
* GSM shield attached to pins 10, 11, 12, 13

created 4 March 2012
by Tom Igoe
and adapted for GSM shield by David Del Peral

This code is in the public domain.

*/

// libraries
#include <GSM.h>

// Pachube Client data
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN "GPRS_APN" // replace your GPRS APN
#define GPRS_LOGIN "login" // replace with your GPRS login
#define GPRS_PASSWORD "password" // replace with your GPRS password

// initialize the library instance:
GSMClient client;
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled

// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(216,52,233,121); // numeric IP for api.pachube.com
char server[] = "api.pachube.com"; // name address for pachube API

unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com

void setup()
{
// initialize serial communications
Serial.begin(9600);

// connection state
boolean notConnected = true;

// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
}

void loop()
{
// read the analog sensor:
int sensorReading = analogRead(A0);

// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected)
{
client.stop();
}

// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval))
{
sendData(sensorReading);
}

// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}

/*
This method makes a HTTP connection to the server.
*/
void sendData(int thisData)
{
// if there's a successful connection:
if (client.connect(server, 80))
{
Serial.println("connecting...");

// send the HTTP PUT request:
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.print("Host: api.pachube.com\n");
client.print("X-ApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);
client.print("Content-Length: ");

// calculate the length of the sensor reading in bytes:
// 8 bytes for "sensor1," + number of digits of the data:
int thisLength = 8 + getLength(thisData);
client.println(thisLength);

// last pieces of the HTTP PUT request:
client.print("Content-Type: text/csv\n");
client.println("Connection: close");
client.println();

// here's the actual content of the PUT request:
client.print("sensor1,");
client.println(thisData);
}
else
{
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted
lastConnectionTime = millis();
}

/*
This method calculates the number of digits in the
sensor reading. Since each digit of the ASCII decimal
representation is a byte, the number of digits equals
the number of bytes.
*/
int getLength(int someValue)
{
// there's at least one byte:
int digits = 1;

// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
while (dividend > 0)
{
dividend = dividend /10;
digits++;
}

// return the number of digits:
return digits;
}

(:sourceend:)
Changed line 328 from:
(:include GSMLibrarySeeAlsoIncludes :)
to:
(:include GSMLibrarySeeAlsoIncludes :)
November 16, 2012, at 07:58 PM by Scott Fitzgerald -
Added lines 1-34:
''Examples > Make Voice Call''

!!!Using the GSM Shield to Make Calls

This example shows how to use the GSM Shield and Library to make voice calls. Please [[/Reference/GSM | click here ]] for more information on the GSM library.

(:div class=BOM :)
!!!Hardware Required
* Arduino board
* Arduino + Telefonica GSM/GPRS Shield
* SIM card
(:divend:)

!!!Circuit
(:div class=circuit :)

%height=300px%[[Attach:GSMShield_ArduinoUno.jpg | Attach:GSMShield_ArduinoUno.jpg ]]

[-image of the Arduino GSM Shield on top of an Arduino Uno -]

(:divend:)

The code below is configured for use with a GSM/GPRS shield. In the variable declaration area, you need to instantiate both the @@GSM@@ and @@GSMVoiceCall@@ classes. Inside @@setup()@@, call @@GSM.begin()@@, with the @@PINNUMBER@@ of your SIM card as parameter. @@GSM.begin()@@ will return whether the shield managed to connect to the GSM network or not.

In the @@loop()@@, the input from the Serial Monitor is used to tell the GSM Shield a phone number to call to. The code waits until the call is over before performing any other actions.

(:div class=code :)
(:source https://raw.github.com/arduino/Arduino/master/libraries/GSM/examples/MakeVoiceCall/MakeVoiceCall.ino lang=arduino tabwidth=4:)
(:divend:)


!!!See Also:

(:include GSMLibrarySeeAlsoIncludes :)

Share