Sample code to HTTP using SIM900 and Arduino

Microcontroller Topics

Sample code to HTTP using SIM900 and Arduino

Postby Saman » Mon Feb 03, 2014 5:57 pm

Code: Select all
/*
*  Description: This example shows how to do a GET action to a url.
*  This example shows the AT commands (and the answers of the module) used
*  to work with HTTP. For more information about the AT commands,
*  refer to the AT command manual.
*
*  Copyright (C) 2013 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.

*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.

*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*  Version 0.2
*  Author: Alejandro Gallego
*/


int8_t answer;
int onModulePin= 2;

char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0;


void setup(){

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

    Serial.println("Starting...");
    power_on();

    delay(3000);

    // sets the PIN code
    sendATcommand("AT+CPIN=****", "OK", 2000);

    delay(3000);

    while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);


    sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"APN\",\"APN\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"USER\",\"user_name\"", "OK", 2000);
    sendATcommand("AT+SAPBR=3,1,\"PWD\",\"password\"", "OK", 2000);

    while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
    {
        delay(5000);
    }


}
void loop(){
    // Initializes HTTP service
    answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
    if (answer == 1)
    {
        // Sets CID parameter
        answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
        if (answer == 1)
        {
            // Sets url
            answer = sendATcommand("AT+HTTPPARA=\"URL\",\"www.url.com\"", "OK", 5000);
            if (answer == 1)
            {
                // Starts GET action
                answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
               
                if (answer == 1)
                {
                    x=0;
                    do{
                        sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
                        if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
                        {
                            data_size = 0;
                            while(Serial.available()==0);
                            aux = Serial.read();
                            do{
                                data_size *= 10;
                                data_size += (aux-0x30);
                                while(Serial.available()==0);
                                aux = Serial.read();       
                            }while(aux != 0x0D);

                            Serial.print("Data received: ");
                            Serial.println(data_size);

                            if (data_size > 0)
                            {
                                while(Serial.available() < data_size);
                                Serial.read();

                                for (int y = 0; y < data_size; y++)
                                {
                                    data[x] = Serial.read();
                                    x++;
                                }
                                data[x] = '\0';
                            }
                            else
                            {
                                Serial.println("Download finished");   
                            }
                        }
                        else if (answer == 2)
                        {
                            Serial.println("Error from HTTP");
                        }
                        else
                        {
                            Serial.println("Error getting the url");
                            data_size = 0;
                        }
                       
                        sendATcommand("", "+HTTPACTION:0,200", 20000);
                    }while (data_size > 0);
                   
                    Serial.print("Data received: ");
                    Serial.println(data);
                }
                else
                {
                    Serial.println("Error getting the url");
                }
            }
            else
            {
                Serial.println("Error setting the url");
            }
        }
        else
        {
            Serial.println("Error setting the CID");
        }   
    }
    else
    {
        Serial.println("Error initializating");
    }
   
    sendATcommand("AT+HTTPTERM", "OK", 5000);

    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;
}
User avatar
Saman
Support Team
Support Team
 
Posts: 816
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Re: Sample code to HTTP using SIM900 and Arduino

Postby Saman » Mon Feb 03, 2014 5:59 pm

AT commandResponseDescription
AT+SAPBROKConfigures GPRS profile
AT+HTTPINITOKInitializes HTTP service
AT+HTTPPARAOKConfigures HTTP parameters
AT+HTTPACTION=0OKSets HTTP Method Action , GET in this chase.
AT+HTTPREADReads HTTP data
AT+HTTPTERMOKCloses the opened HTTP session.
User avatar
Saman
Support Team
Support Team
 
Posts: 816
Joined: Fri Jul 31, 2009 5:02 pm
Cash on hand: 152,553.20
Location: Mount Lavinia
Medals: 1
EC_Achievment (1)

Invitations sent: 44
Registered friends: 1
Reputation point: 37
Staff Sergeant

Re: Sample code to HTTP using SIM900 and Arduino

Postby sodiptakarina » Tue Oct 28, 2014 9:59 am

Hallo,

i am interested in your project. Now, i have project to turn a lamp ON or OFF from internet by using GSM/GPRS Shield SIM900 and also Arduino UNO . I am newbie in microcontroller, so I want ask you, could you give me a flowchart or example code to read data from Internet via GPRS, so i can start this project with good step. Thx in advance
User avatar
sodiptakarina
 
Posts: 2
Joined: Tue Oct 28, 2014 8:34 am
Cash on hand: 173.40

Invitations sent: 0
Registered friends: 0
Reputation point: 0
Lance Corporal

Re: Sample code to HTTP using SIM900 and Arduino

Postby SevenZero » Wed Oct 29, 2014 5:25 pm

If you are not too familiar with SIM900, GPRS, etc... it is better you buy the SIM900 based GPRS shield for Arduino. There are Arduino libraries, example codes written for this shield in the internet that you can easily use. Good luck!

http://www.lankatronics.com/development-tools/gsm-gprs/sim900-gsm-gprs-shield-development-board.html
User avatar
SevenZero
Support Team
Support Team
 
Posts: 286
Joined: Sun Nov 01, 2009 3:07 pm
Cash on hand: 201,474.40

Invitations sent: 0
Registered friends: 0
Reputation point: 10
Corporal

Re: Sample code to HTTP using SIM900 and Arduino

Postby sodiptakarina » Fri Oct 31, 2014 12:35 am

thank you, but i have a little problem, could you help me please?
I compile this code to my arduino+gsm sim900 shield, this code works good, but what should I do next, so i can turn the lamp on/off from web?
i hope, you can give me a way to solve this problem . . :)

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
InetGSM inet;
char msg[50];
char inSerial[50];
int i=0;
boolean started=false;
void setup()
{
 Serial.begin(9600);
 Serial.println("GSM Shield testing.");
 if (gsm.begin(2400)){
   Serial.println("\nstatus=READY");
   started=true;  
 }
 else Serial.println("\nstatus=IDLE");
 if(started){
   if (inet.attachGPRS("xxxxx", "", ""))
     Serial.println("status=ATTACHED");
   else Serial.println("status=ERROR");
   delay(1000);
   gsm.SimpleWriteln("AT+CIFSR");
   delay(5000);
   gsm.WhileSimpleRead();  
   inet.httpGET("lxxxx.xxx", 80, "/index.php?lamp=1&statt=0", msg, 50);
 }
};
void loop()
{
 serialswread();
};
void serialswread(){
 gsm.SimpleRead();
}
User avatar
sodiptakarina
 
Posts: 2
Joined: Tue Oct 28, 2014 8:34 am
Cash on hand: 173.40

Invitations sent: 0
Registered friends: 0
Reputation point: 0
Lance Corporal


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 2 guests