Development

Communication / GPRS:

» GPRS 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_Pro module
1 x GPRS/3G antenna
1 x SIM card

Notes

- This example must be mastered by those users who want increase battery life reducing the power consumption of the GPRS_Pro module
- GPRS_Pro module must be powered with a battery
- When GPRS_Pro is in use, interrupts can't be used
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  -------------- GPRS_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.0
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

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

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

int answer;
unsigned int counter = 0;

////////////////////////////////////////////////////////////////////////
#define URL "http://pruebas.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_Pro.set_APN(apn, login, password);
    // And shows them
    GPRS_Pro.show_APN();
    USB.println(F("---******************************************************************************---"));
}


void loop()
{

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

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

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

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

            // 5. configure GPRS connection for HTTP or FTP applications:
            answer = GPRS_Pro.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_Pro.readURL(URL, frame.buffer, frame.length, 1, GET);

                // gets URL from the solicited URL
                if ( answer == 1)
                {
                    USB.println(F("Done"));  
                    USB.println(F("The server has replied with:")); 
                    USB.println(GPRS_Pro.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_Pro.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_Pro module cannot connect to the network"));     
        }
    }
    else
    {
        USB.println(F("GPRS_Pro module not ready"));    
    }

    // 7. powers off the GPRS_Pro module
    GPRS_Pro.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 RTC temperature.
---******************************************************************************---

-------------------------------
APN: airtelwap.es
LOGIN: wap@wap
PASSWORD: wap125
-------------------------------

GPRS_Pro module ready...
Setting PIN code...
PIN code incorrect
GPRS_Pro 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 35 36 38 39 37 36 32 33 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 30 3A 31 33 3A 31 37 23 49 4E 5F 54 45 4D 50 3A 33 31 2E 37 35 23
String: <=>€#356897623#Waspmote_PRO#0#TIME:Tue, 00/01/03, 00:13:17#BAT:31#
===============================
Done
The server has replied with:
method::GET;
frame::3C3D3E80022333353638393736323323576173706D6F74655F50524F23302354494D453A5475652C2030302F30312F30332C2030303A31333A313723494E5F54454D503A33312E373523;
TIME:Tue, 00/01/03, 00 , BAT:31 ,
GPRS_Pro module ready...
Setting PIN code...
PIN code incorrect
GPRS_Pro 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 35 36 38 39 37 36 32 33 23 57 61 73 70 6D 6F 74 65 5F 50 52 4F 23 31 23 54 49 4D 45 3A 54 75 65 2C 20 30 30 2F 30 31 2F 30 33 2C 20 30 30 3A 31 33 3A 35 38 23 49 4E 5F 54 45 4D 50 3A 33 31 2E 37 35 23
String: <=>€#356897623#Waspmote_PRO#1#TIME:Tue, 00/01/03, 00:13:58#BAT:31#
===============================
Done
The server has replied with:
method::GET;
frame::3C3D3E80022333353638393736323323576173706D6F74655F50524F23312354494D453A5475652C2030302F30312F30332C2030303A31333A353823494E5F54454D503A33312E373523;
TIME:Tue, 00/01/03, 00 , BAT:31 ,

Quick Publish: