The GSM/GPRS & GPS Shield: some Http connections examples
After having shown several examples regarding the use the GSM/GPRS & GPS shield with calls and text messages we are now going to present some applications that involve GPRS data.
Thanks to the inet.h class the shield can indeed connect to the internet and its various applications. We ‘ll see how to use the Arduino as a client requesting a web page, or as a server for receiving commands and, finally, how to send emails
EXAMPLE 1
Before describing this example, let’s recall what happens when you open the browser and request a page.
As a result of this, the browser establishes a TCP/IP connection with a web server (that can be identified by its IP address or by a string such as www.open-electronics.org thanks to the DNS service) and sends a request for the related page. This request is called ‘http request’ contains basic information as well as the path where you the page that we want to display is stored. Usually the path coincides with everything that comes after the domain suffix (in the case you wanted to access our about page, the path is www.open-electronics.org/about).
The server receiving the request checks for the page you requested and returns a first response with the outcome of the research (in case of page not found you have the classic ‘404 Error‘) and further information such as the GMT time set on the server or the last change to the page.
Following this response, if the research had positive results, the server will pass on the HTML code of the browser to display: the browser will then interpret the code and generate the page as we are used to see that.
You can replicate this process (except for the HTML rendering) thanks to our shield and its associated library.
In this sketch we will deal with a specific page request (Google’s search engine home), with analyzing the response, and extrapolate GMT.
Let’s look at the code.
For the sake of simplicity we chosen to include the data connection activation phase in the sketch setup.
Such a choice is not mandatory, but was preferred for clarity reasons, emphasizing the real part of the program within the loop function.
So in the setup, after initializing the shield, it will run the initialization routine of the mobile data network. For this operation, we simply invoke the following function:
inet.attachGPRS(char* APN, char* username, char* password)
In the examples you will find the function setup as if you were using the Italian operator called Wind.
Also the connection request to the server, being performed only once, has been included in the setup.
That call asks the server the page on the path defined by the third parameter through the specified port. The last two parameters are used to store inside the string passed with the fourth parameter, the entire response or any part of it, the length of which is defined by the last parameter.
The function returns the number of characters stored.
inet.httpGET(“www.google.com”, 80, “/”, msg, 50);
In this way, the first 50 characters of the answer will be saved in the msg variable. This has no purpose in this example and is performed only for demonstration purposes. You can set this parameter to 0.
It’s important the portion of the text that we want to analyze (the one containing the date and time) does not fall between characters saved, because we will control the remaining ones. In this case the first 50 characters are used by the server to communicate further information.
Let’s now try to understand what happens to the remaining part of the response that is not saved.
The library involves the use of a buffer of a size, defined within the library, that can be set at will. This buffer is used to temporarily store any communication from the SIM900 on the serial software port module, including server responses. Through the gsm.read () command you can read (if any) the first data (the oldest in the arrival order) stored on the buffer.
If the buffer is empty, it will return the 0 value (note that it is different from the value ‘0’ in ASCII encoded with the number 48).
So at each loop cycle we will read the first value in the buffer.
data=gsm read();
if(data>0){ […] }
Once verified that within the given variable a non-zero exists, we will proceed with the search for the string. The string containing the date and time is preceded by the text “Date:“. We’ll then take care of finding the value, and once identified, store the remaining characters, up to the ‘n’ character .
The string recognition algorithm is very simple: each character is compared with the first of the searched string (in this a capital D), and in case of correspondence we increasing a counter variable, and communicate that to the next iteration of the algorithm that will no longer compare with the first character, but with the second instead. Similarly, for the next, until the number of positive matches (contained within the varaiabile counter k) is not equal to the length of the word you’re waiting for.
In case of a different intermediate character, the algorithm will reset starting again to search for the first value of the expected string. If the required word is found, the algorithm is reset and a boolean variable named found, is set to true. At this point in time we just have to print to screen all subsequent values of the response, until ‘n’ (end of the line).
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
InetGSM inet;
int k=0;
int j=0;
char str[]="Date: ";
char msg[50];
boolean found=false;
char data;
int numdata;
char inSerial[50];
int i=0;
boolean started=false;
void setup()
{
//Serial connection.
Serial.begin(9600);
if (gsm.begin(2400)){
Serial.println("nstatus=READY");
started=true;
}
else Serial.println("nstatus=IDLE");
if(started){
if (inet.attachGPRS("internet.wind", "", ""))
Serial.println("status=ATTACHED");
else Serial.println("status=ERROR");
delay(1000);
numdata=inet.httpGET("www.google.com", 80, "/", msg, 50);
}
};
void loop()
{
data=gsm.read();
if(data>0){
//Serial.print(data);
if(data==str[k]){
k++;
if(k+1==strlen(str)){
found=true;
msg[0]='

