I use this gsm shield
http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0
on a Leonardo and hardware serial
I have a sketch that does not work anymore.
It should read a page on the Web
#include <Time.h>
#include <String.h>
char data[1024];
int data_size;
char aux;
int x = 0;
int ledPin = 13;
void setup()
{
Serial1.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
delay(500);
//initialize pins as outputs
pinMode(ledPin, OUTPUT);
// sæt time
setTime(1347697800);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
if (Serial.available())
switch(Serial.read())
{
case 't':
SendTextMessage();
break;
case 'r':
ReadTextMessage();
break;
case 'h':
SubmitHttpRequest();
break;
}
}
///SendTextMessage()
///this function is to send a sms message
void SendTextMessage()
{
Serial1.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
delay(100);
Serial1.println("AT + CMGS = \"+45xxxxxxxx\"");//send sms message, be careful need to add a country code before the cellphone number
delay(100);
Serial1.println("A test message!");//the content of the message
delay(100);
Serial1.println((char)26);//the ASCII code of the ctrl+z is 26
delay(100);
Serial1.println();
}
///ReadTextMessage()
///this function is to send a sms message
void ReadTextMessage()
{
Serial1.println("AT+CMGF=1"); //Because we want to send the SMS in text mode
delay(100);
Serial1.println("AT+CMGL=\"ALL\"");//send sms message, be careful need to add a country code before the cellphone number
ShowSerialData();
}
///SubmitHttpRequest()
///this function is submit a http request
///attention:the time of delay is very important, it must be set enough
void SubmitHttpRequest()
{
Serial1.println("AT+CSQ");
delay(100);
ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
Serial1.println("AT+CGATT?");
delay(100);
ShowSerialData();
Serial1.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
ShowSerialData();
Serial1.println("AT+SAPBR=3,1,\"APN\",\"internet\"");//setting the APN, the second need you fill in your local apn server
delay(4000);
ShowSerialData();
Serial1.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);
ShowSerialData();
Serial1.println("AT+HTTPINIT"); //init the HTTP request
delay(2000);
ShowSerialData();
Serial1.println("AT+HTTPPARA=\"URL\",\"http://stenbjergbryghus.dk/bi/time.php\"");// setting the httppara, the second parameter is the website you want to access
delay(1000);
ShowSerialData();
Serial.println("_1");
Serial1.println("AT+HTTPACTION=0");//submit the request
delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
ShowSerialData();
Serial.println("_2");
Serial1.flush();
Serial.println("_3");
Serial1.println("AT+HTTPREAD");// read the data from the website you access
// delay(1000);
Serial.println("_4");
do{
while(Serial1.available()==0);
}while(Serial1.read()!=':');
data_size=0;
while(Serial1.available()==0);
aux=Serial1.read();
do{
data_size*=10;
data_size+=(aux-0x30);
while(Serial1.available()==0);
aux=Serial1.read();
Serial.print(aux);
}while(aux!='\r');
do{
while(Serial1.available()==0);
}while(Serial1.read()!='\n');
for(x=0;x<=data_size;x++){
while(Serial1.available()==0);
Serial.print(Serial1.read());
data[x]=Serial1.read();
}
Serial.print(data); //Shows data
ShowSerialData();
Serial1.println("");
delay(100);
}
void ShowSerialData()
{
while(Serial1.available()!=0)
Serial.write(Serial1.read());
}
Here, the output
+CGATT: 1
OK
AT+SAPBR=3,1,"CONTYPE","GPRS"
OK
AT+SAPBR=3,1,"APN","internet"
OK
AT+SAPBR=1,1
OK
AT+HTTPINIT
OK
AT+HTTPPARA="URL","http://stenbjergbryghus.dk/bi/time.php"
O_1
AT+HTTPACTION=0
OK
+HTTPACTION:0,601,0
_2
_3
_4