[code]#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
int8_t answer;
int onModulePin= 2;
char aux_str[50];
char ip_data[]=“GET / HTTP/1.1\r\nHost: google.[c]om\r\nConnection: close\r\n\r\n”;
void setup(){
mySerial.begin(19200); // SOFTWARE COM
Serial.begin(19200); // HUMAN COM
delay(500);
pinMode(onModulePin, OUTPUT);
mySerial.begin(19200);
Serial.println("Starting...");
power_on();
delay(3000);
Serial.println("Connecting to the network...");
while( sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 1000)== 0 ){
}
}
void loop()
{
if (Serial.available())
switch(Serial.read())
{
case ‘h’:
SubmitHttpRequest();
break;
case ‘i’:
// Closes the socket
sendATcommand2(“AT+CIPCLOSE”, “CLOSE OK”, “ERROR”, 10000);
sendATcommand2(“AT+CIPSHUT”, “OK”, “ERROR”, 10000);
delay(10000);
break;
}
if (mySerial.available()) {
Serial.write(mySerial.read());
}
}
void SubmitHttpRequest() {
// Selects Single-connection mode
if (sendATcommand2("AT+CIPMUX=0", "OK", "ERROR", 1000) == 1)
{
// Waits for status IP INITIAL
while(sendATcommand2("AT+CIPSTATUS", "INITIAL", "", 500) == 0 );
delay(5000);
// Sets the APN, user name and password
if (sendATcommand2("AT+CSTT=\"fast.tmobile.[c]om\",\"user_name\",\"password\"", "OK", "ERROR", 30000) == 1)
{
// Waits for status IP START
while(sendATcommand2("AT+CIPSTATUS", "START", "", 500) == 0 );
delay(5000);
// Brings Up Wireless Connection
if (sendATcommand2("AT+CIICR", "OK", "ERROR", 30000) == 1)
{
// Waits for status IP GPRSACT
while(sendATcommand2("AT+CIPSTATUS", "GPRSACT", "", 500) == 0 );
delay(5000);
// Gets Local IP Address
if (sendATcommand2("AT+CIFSR", ".", "ERROR", 10000) == 1)
{
// Waits for status IP STATUS
while(sendATcommand2("AT+CIPSTATUS", "IP STATUS", "", 500) == 0 );
delay(5000);
Serial.println("Openning TCP");
// Opens a TCP socket
if (sendATcommand2("AT+CIPSTART=\"TCP\",\"google.[c]om\",\"80\"", "CONNECT OK", "CONNECT FAIL", 30000) == 1)
{
Serial.println("Connected");
// Sends some data to the TCP socket
sprintf(aux_str,"AT+CIPSEND=%d", strlen(ip_data));
if (sendATcommand2(aux_str, ">", "ERROR", 10000) == 1)
{
sendATcommand2(ip_data, "OK", "ERROR", 10000);
}
}
else
{
Serial.println("Error openning the connection");
}
}
else
{
Serial.println("Error getting the IP address");
}
}
else
{
Serial.println("Error bring up wireless connection");
}
}
else
{
Serial.println("Error setting the APN");
}
}
else
{
Serial.println("Error setting the single connection");
}
}
void power_on() {
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand2("AT", "OK", "OK", 2000);
if (answer == 0) {
Serial.println("Powering up...");
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
delay(1000);
digitalWrite(9,HIGH);
delay(2000);
digitalWrite(9,LOW);
delay(3000);
Serial.println("Waiting...");
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand2("AT", "OK", "OK", 2000);
mySerial.println(answer);
}
}
}
int8_t sendATcommand2(char* ATcommand, char* expected_answer1, char* expected_answer2, unsigned int timeout){
//Serial.print("Sending: ");
//Serial.println(ATcommand);
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( mySerial.available() > 0) mySerial.read(); // Clean the input buffer
mySerial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(mySerial.available() != 0){
response[x] = mySerial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
else if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
}
// Waits for the asnwer with time out
while((answer == 0) && ((millis() - previous) < timeout));
Serial.print("Received--: ");
Serial.println(response);
return answer;
}
[/code]