Development

Communication / GPRS+GPS:

» GPRS SIM928A 19b: Reading URL with GET and frame

This example shows how to read an URL with a GET request including a frame into the GET request.

Required Materials

1 x Waspmote
1 x Battery
1 x GPRS_SIM928A module
1 x GPRS/3G antenna
1 x SIM card

Notes

- GPRS_SIM928 module must be powered with a battery
- When GPRS_SIM928 is in use, interrupts can't be used
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  -------------- GPRS_SIM928A_19b - Reading URL with GET and frame -------------- 
 *  
 *  Explanation: This example shows how to read an URL with a GET request 
 *  including a frame into the GET request.
 *  
 *  Copyright (C) 2016 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:           1.1
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

#include <WaspFrame.h>
#include "WaspGPRS_SIM928A.h"

char apn[] = "apn";
char login[] = "login";
char password[] = "password";

int answer;
unsigned int counter = 0;

////////////////////////////////////////////////////////////////////////
#define URL "http://test.libelium.com/getpost_frame_parser.php?"
////////////////////////////////////////////////////////////////////////


void setup()
{
    // setup for Serial port over USB:
    USB.ON();
    USB.println(F("USB port started..."));

    USB.println(F("---******************************************************************************---"));
    USB.println(F("GET method to the libelium's test url with frame..."));
    USB.println(F("You can use this php to test the HTTP connection of the module."));
    USB.println(F("The php returns the parameters that the user sends with the URL."));
    USB.println(F("In this case the RTC time and the battery level."));
    USB.println(F("---******************************************************************************---"));

    // Sets operator parameters
    GPRS_SIM928A.set_APN(apn, login, password);
    // And shows them
    GPRS_SIM928A.show_APN();
    USB.println(F("---******************************************************************************---"));
}


void loop()
{

    // 2. activate the GPRS_SIM928A module:
    answer = GPRS_SIM928A.ON(); 
    if ((answer == 1) || (answer == -3))
    { 
        USB.println(F("GPRS_SIM928A module ready..."));

        // 3. set pin code:
        USB.println(F("Setting PIN code..."));
        // **** must be substituted by the SIM code
        if (GPRS_SIM928A.setPIN("****") == 1) 
        {
            USB.println(F("PIN code accepted"));
        }
        else
        {
            USB.println(F("PIN code incorrect"));
        }

        // 4.wait for connection to the network:
        answer = GPRS_SIM928A.check(180);    
        if (answer == 1)
        {

            USB.println(F("GPRS_SIM928A module connected to the network..."));

            // 5. configure GPRS connection for HTTP or FTP applications:
            answer = GPRS_SIM928A.configureGPRS_HTTP_FTP(1);
            if (answer == 1)
            {
                USB.println(F("Get the URL with GET request and frame..."));                

                // switch on RTC
                RTC.ON();

                // Create new frame (ASCII)
                frame.createFrame(ASCII,"Waspmote_PRO"); 
                // set frame fields (Time from RTC)
                frame.addSensor(SENSOR_TIME, RTC.getTime());
                // set frame fields (Battery sensor - uint8_t)
                frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
                // show Frame
                frame.showFrame();                

                // 6. get URL from the solicited URL
                answer = GPRS_SIM928A.readURL(URL, frame.buffer, frame.length, 1, GET);
                
                // check answer
                if ( answer == 1)
                {
                    USB.println(F("Done"));  
                    USB.println(F("The server has replied with:")); 
                    USB.println(GPRS_SIM928A.buffer_GPRS);
                }
                else if (answer < -9)
                {
                    USB.print(F("Failed. Error code: "));
                    USB.println(answer, DEC);
                    USB.print(F("CME error code: "));
                    USB.println(GPRS_SIM928A.CME_CMS_code, DEC);
                }
                else 
                {
                    USB.print(F("Failed. Error code: "));
                    USB.println(answer, DEC);
                }

            }
            else
            {
                USB.println(F("Configuration 1 failed. Error code: "));
                USB.println(answer, DEC);
            }
        }
        else
        {
            USB.println(F("GPRS_SIM928A module cannot connect to the network"));     
        }
    }
    else
    {
        USB.println(F("GPRS_SIM928A module not ready"));    
    }

    // 7. powers off the GPRS_SIM928A module
    GPRS_SIM928A.OFF(); 

    delay(5000);
}





Output

H#
USB port started...
---******************************************************************************---
GET method to the libelium's test url with frame...
You can use this php to test the HTTP connection of the module.
The php returns the parameters that the user sends with the URL.
In this case the RTC time and the battery level.
---******************************************************************************---
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
---******************************************************************************---
GPRS_SIM928A module ready...
Setting PIN code...
PIN code accepted
GPRS_SIM928A module connected to the network...
Get the URL with GET request and frame...
===============================
Current ASCII Frame:
Length: 74
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 32 35 33 39 36 33 32 23 57 61 73 70 6D 6F 74 65 5F 50 52 4F 23 30 23 54 49 4D 45 3A 54 75 65 2C 20 30 30 2F 30 31 2F 30 33 2C 20 30 34 3A 31 34 3A 32 30 23 49 4E 5F 54 45 4D 50 3A 32 39 2E 32 35 23
String: <=>€#382539632#Waspmote_PRO#0#TIME:Tue, 00/01/03, 04:14:20#BAT:29#
===============================
Done
The server has replied with:
method::GET;
frame::3C3D3E80022333383235333936333223576173706D6F74655F50524F23302354494D453A5475652C2030302F30312F30332C2030343A31343A323023494E5F54454D503A32392E323523;
TIME:Tue, 00/01/03, 04 , BAT:29 ,

Quick Publish: