Toll-Free Customer Support 24/7

GSM GPRS SIM900A Modem- Arduino Compatible


int8_t answer,val123=0, FLAG=0,kk=0,hht=0;
int onModulePin= 9;


char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0,j=0,ch;
char SMS[300];
char SMS1[12];
char SMS2[200];
char aux_string[30];

void setup(){

pinMode(onModulePin, OUTPUT);
Serial.begin(9600);

power_on();

delay(3000);
while( (sendATcommand("AT+CREG?", "+CREG: 0,1", 500) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 500)) == 0 );

 

delay(5000);
GPRS_connect();

}
void loop(){

/*********************************************************************************************/

delay(2000);
answer = sendATcommand("AT+HTTPPARA=\"URL\",\"www.google.co.in\"", "OK", 9000);
if (answer == 1)
{
// Starts GET action
delay(2000);
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 20000);

if (answer == 1)
{
memset(SMS, '\0', 300);
hht=0;
delay(1000);
answer = sendATcommand("AT+HTTPREAD=0,200","+HTTPREAD:", 10000);
if (answer == 1)
{
answer = 0;
x=0;

while(Serial.available() == 0);
do{
// if there are data in the UART input buffer, reads it and checks for the asnwer
if(Serial.available() > 0){
SMS[x] = Serial.read();
x++;
if (strstr(SMS, "OK") != NULL)
{
answer = 1;
}

}
}while(answer == 0); // Waits for the asnwer with time out

SMS[x] = '\0';
Serial.println(SMS);
}
}
}

delay(5000);

}

void power_on(){

uint8_t answer=0;

// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);

// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}

}

 

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command


x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}

 

int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command


x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.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
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}

void GPRS_connect()
{

sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"www\"", "OK", 2000);

while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
{
delay(5000);
}
delay(1000);
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
delay(1000);
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
}


}

Back to top