Development

Communication / GPRS:

» GPRS 19d: Reading URL with POST and frame

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

Required Materials

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

Notes

- GPRS_Pro module must be powered with a battery
- When GPRS_Pro is in use, interrupts can't be used
- 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 loop counter (counter) and the RTC temperature (temp).
- The syntax to add parameters is below:
pruebas.libelium.com/test-get-post.php?parameter1=value1&...&last_parameter=last_value
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  -------------- GPRS_19d - Getting URL with POST and fame  -------------- 
 *  
 *  Explanation: This example shows how to read an URL with a POST request 
 *  including a frame into the POST data.
 *  
 *  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;

////////////////////////////////////////////////////////////////////////
#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("POST method to the libelium's test url sending a 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 batery level."));
    USB.println(F("The url to use is: pruebas.libelium.com/getpost_frame_parser.php"));
    USB.println(F("---******************************************************************************---"));

    // 1. 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..."));

                // switch on RTC
                RTC.ON();
                RTC.getTime();

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

                // 6. get URL from the solicited URL
                answer = GPRS_Pro.readURL(URL, frame.buffer, frame.length, 1, POST);

                // check answer
                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

D#
USB port started...
---******************************************************************************---
POST method to the libelium's test url sending a 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.
The url to use is : pruebas.libelium.com/test-get-post.php
---******************************************************************************---

-------------------------------
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...
===============================
Current ASCII Frame:
Length: 58
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 30 2D 33 31 2D 33 34 23 49 4E 5F 54 45 4D 50 3A 33 31 2E 37 35 23
String: <=>€#356897623#Waspmote_PRO#0#TIME:0-31-34#BAT:31#
===============================
Done
The server has replied with:
method::POST;
POST;frame::3C3D3E80022333353638393736323323576173706D6F74655F50524F23302354494D453A302D33312D333423494E5F54454D503A33312E373523;
TIME:0-31-34 , BAT:31 ,
GPRS_Pro module ready...
Setting PIN code...
PIN code incorrect
GPRS_Pro module connected to the network...
Get the URL...
===============================
Current ASCII Frame:
Length: 58
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 30 2D 33 32 2D 31 35 23 49 4E 5F 54 45 4D 50 3A 33 32 2E 30 30 23
String: <=>€#356897623#Waspmote_PRO#1#TIME:0-32-15#BAT:31#
===============================
Done
The server has replied with:
method::POST;
POST;frame::3C3D3E80022333353638393736323323576173706D6F74655F50524F23312354494D453A302D33322D313523494E5F54454D503A33322E303023;
TIME:0-32-15 , BAT:31 ,

Quick Publish: