Examples > GSM library
This example shows you how to answer a HTTP request using a GSM shield attached to an Arduino. Specifically, it connects to xively.com, a free datalogging site. The example requires that you set up a xively.com account, as well as a xively feed (for more information on setting up an input feed, please click here). Your GSM shield will then connect to that feed and upload sensor data.
To use a data connection with the GSM shield, you'll need your provider's Access Point Name (APN), login, and password. To obtain this information, contact the network provider for the most up to date information. This page has some information about various carrier settings, but it may not be current.
First, import the GSM library
Declare the various settings for your Xively account; your API key, feed ID, and project name (the user agent).
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. This page has some information about various carrier settings, but it may not be current.
Initialize instances of the classes you're going to use. You're going to need the GSM, GPRS, GSMServer, and GSMClient classes.
Create some variables to store information you're going to need in the sketch; a char array to hold the URL, and some variables for setting timing intervals.
setup, open a serial connection to the computer.
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
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 and has attached itself to the GPRS network,
gsmAccess() will return
GSM_READY. Use this as a flag to set the
notConnected variable to
false. Once connected, you can close
You're going to use some custom functions in this sketch. The first will make a HTTP connection to the server and send any data. Declare a function named
sendData() that accepts an argument of type
Check to see if there is a successful connection to the server
When connected, send a HTTP PUT request with the information about your feed
You'll need to calculate the length of the data you're sending. The length will be 8 bytes for the term "sensor1," plus the number of digits of the data. You'll create a new function named
getLength() in a moment to handle this information. Once calculated, send the length to the server, and the last pieces of the PUT.
Last, send the data to the server.
If the connection attempt was not successful, send a status message to the serial monitor, and disconnect from the server.
Update the time that the last connection, or attempt, happened before closing the function.
Create another function, named
getLength(), for determining the length of the data being sent in bytes.
Create a variable named
digits to hold the number of bytes. You know that there will always be at least one digit being sent.
To find the total number of digits to send, continually divide the value by ten, adding one to the digit count each time you divide, until you reach 0.
Return the number of digits and close the function.
loop, read the value from the analog sensor and store it in a local variable
If there are bytes available, read them and send them to the serial port.
Check for an active network connection. If there isn't one, but there was one last time through
loop(), then stop the client:
If there is no connection, and ten seconds have passed since the last connection, or connection attempt, then connect again and send data
Store the state of the connection for next time through
loop() and close it up.
Once your code is uploaded, open the serial monitor for debugging, and check your xively.com feed to see updates.