Perform an HTTP request

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to send an HTTP Request with Flyport GPRS.

FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the code since the pinout is slightly different.

Introduction to data mode

To perfom any type of data communication, APN parameters should be configured. It is suggested to set APN parameters everytime Flyport GPRS resets (or wakes up from hibernation/sleep)
Connection functions (like TCP/HTTP Client connection, FTP Client file exchange, email sending) are available only when APN is set correctly.

NOTE: APN (Access Point Name) parameters are specific for each mobile operator. Please, ask to your SIM Card provider.

How to use it

Create a new project. Fill the variables under the "Setup here your params!" with your own data (apn, server ip, port, etc...)

Compile and download code to the device.
Open the Serial Monitor utility on the IDE to send commands to Flyport. Serial monitor also provides feedback information about operation in progress.

Type the character reported above to start related operation:

Character Operation
0 Starts a GET request to provided Server
1 Starts a POST request to provided Server
2 Starts a POST request adding additional parameters to provided Server


This tutorial shows how to create a firmware with "state machine" execution. The state machine keeps track of your code execution. In this way, if problems occurs, Flyport can automatically start from a certain state. If the connection/data exchange process go wrong for any reason (could be an incoming call or network disconnection etc...) the firmware restarts from APN setup phase.

NOTE: The following example should be configured with your own parameters (APN and Server Info).

Code example

Source code for taskFlyport.c:

#include "taskFlyport.h"
 
TCP_SOCKET sockHttp;
BOOL inExecution = TRUE;
int requestType = -1; // -1=Idle, 0=GET method, 1=HTTP POST without additional params, 2=HTTP POST with additional params
int currentAction = 0;
BYTE regStat = NO_REG;
 
// Setup here your params!
char apnName[] = "my.apn.com";
char httpServName[] = "mysite";
char httpServPort[] = "80";
char requestGetURL[] = "mysite/mypage.html";
char requestURL[] = "mysite";
 
// Application Variables
char inBuff[1500];
char paramsHttp[120];
char data[200];
 
void FlyportTask()
{
    sockHttp.number = INVALID_SOCKET;
 
    vTaskDelay(20);
    _dbgwrite("Flyport Task Started...\r\n");
 
    // Wait for GSM Connection successfull
    _dbgwrite("Waiting network connection...");
    while((LastConnStatus() != REG_SUCCESS) && (LastConnStatus() != ROAMING))
    {
        vTaskDelay(20);
        do
        {
            UpdateConnStatus();
            while(LastExecStat() == OP_EXECUTION)
                vTaskDelay(1);
        }while(LastExecStat() != OP_SUCCESS);
    }
    vTaskDelay(20);
    _dbgwrite("Flyport registered on network!\r\n");
    vTaskDelay(150);
    _dbgwrite("Please use Debug UART and send:\r\n- \'0\' to execute a GET\r\n- \'1\' to execute a standard POST request\r\n- \'2\' to execute a POST with additional params\r\n");
 
    while(1)
    {
        // User input parsing:
        if(UARTBufferSize(1) > 0)
        {
            UARTRead(1, inBuff, 1);
            inBuff[1] = '\0';
            vTaskDelay(20);
            UARTFlush(1);
            currentAction = 0;
            sockHttp.number = INVALID_SOCKET;
 
            switch(inBuff[0])
            {
                case '0': // Execute a GET
                    requestType = 0;
                    break;
 
                case '1': // Execute a standard POST
                    requestType = 1;
                    break;
 
                case '2': // Execute a POST with additional parameters
                    requestType = 2;
                    break;
 
                default:
                    requestType = -1;
                    break;
            }
        }
 
        if(LastExecStat() == OP_TIMEOUT)
        {
            // Something gone wrong! 
            // Restart state machine since GSM Task restarted GPRS Module!
            currentAction = 0;
        }
 
        // Currently no action is in progress and a new Request should be sent to server
        if((LastExecStat() != OP_EXECUTION) && (requestType != -1))
        {
            switch(currentAction)
            {
                case 0: // APN Setup
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        APNConfig(apnName,"","",DYNAMIC_IP,DYNAMIC_IP,DYNAMIC_IP);
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("APN correctly set!\n");
                            currentAction++;
                        }
                        else
                        {
                            _dbgwrite("Problems on APNConfig...\r\n");
                            currentAction = 0;
                        }
                    }
                    break;
 
                case 1: // Check Network connection
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        UpdateConnStatus();
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            if(LastConnStatus() != regStat)
                                regStat = LastConnStatus();
 
                            if((regStat == REG_SUCCESS) || (regStat == ROAMING))
                            {
                                // Start with new action!
                                currentAction++;
                            }
                        }
                        else
                        {
                            _dbgwrite("Problems on UpdateConnStatus...\r\n");
                            currentAction = 0;
                        }
                    }
                    break;
 
 
                case 2: // HTTP Socket open
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        _dbgwrite("Starting HTTP/TCP Connection...\r\n");
                        HTTPOpen(&sockHttp, httpServName, httpServPort);
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("HTTP Socket opened!\n");
                            currentAction++;
                        }
                        else
                        {
                            _dbgwrite("Problems on HTTPOpen...\r\n");
                            currentAction = 0;
                        }
                    }
                    break;    
 
 
                case 3: // HTTP Request send
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        _dbgwrite("Sending request to server...\r\n");
 
                        // Choose the type of request:
                        switch(requestType)
                        {
                            case 0: // GET
                                // Launch the request
                                HTTPRequest(&sockHttp, HTTP_GET, requestGetURL, "", HTTP_NO_PARAM);
                                break;
 
                            case 1: // HTTP POST without additional params
                                // Prepare Data Content:
                                sprintf(data, "This is my standard data!\r\n\r\n");
 
                                // Launch the request
                                HTTPRequest(&sockHttp, HTTP_POST, requestURL, data, HTTP_NO_PARAM);
                                break;
 
                            case 2:    // HTTP POST with additional params
                                // Prepare Data Content:
                                sprintf(data, "This is my data!\r\nAdditional params are included using HTTPRequest function!\r\n\r\n");
 
                                // Fill additional params:
                                sprintf(paramsHttp, "Content-Type: application/json\r\nContent-Length: %d", strlen(data));
 
                                // Launch the request
                                HTTPRequest(&sockHttp, HTTP_POST, requestURL, data, paramsHttp);
                                break;
                        }
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("HTTP Request correctly set!\n");
                            _dbgwrite("wait for a while for response...");
                            vTaskDelay(100);
                            currentAction++;
                        }
                        else
                        {
                            _dbgwrite("Problems on HTTPRequest...\r\n");
                            currentAction = 6;
                        }
                    }
                    break;
 
                case 4: // Update Socket Status
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        _dbgwrite("Updating HTTP Socket...\r\n");
                        HTTPStatus(&sockHttp);
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("Socket Status updated!\n");
                            currentAction++;
                        }
                        else
                        {
                            _dbgwrite("Problems on HTTPStatus...\r\n");
                            currentAction = 6;
                        }
                    }
                    break;
 
                case 5: // Read available response data...
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        _dbgwrite("HTTP Read...\r\n");
                        int lenTemp = sockHttp.rxLen;
                        if(lenTemp == 0)
                            _dbgwrite("No data to Read!\r\n");
                        else
                        {
                            if(lenTemp > 1500)
                                lenTemp = 1500;
                            HTTPReadData(&sockHttp, inBuff, lenTemp);
                            inBuff[lenTemp] = '\0';
                        }
                    }
                    else
                    {
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("[HTTP Data Read]\n");
                            _dbgwrite(inBuff);
                            _dbgwrite("\r\n");
                            currentAction++;
                        }
                        else
                        {
                            _dbgwrite("Problems on HTTPReadData...\r\n");
                            currentAction = 6;
                        }
                    }
                    break;    
 
                case 6: // Close Socket
                    if(inExecution == FALSE)
                    {
                        inExecution = TRUE;
                        _dbgwrite("Closing Socket...\r\n");
                        HTTPClose(&sockHttp);
                    }
                    else
                    {
                        requestType = -1;
                        currentAction = 0;
                        inExecution = FALSE;
                        if(LastExecStat() == OP_SUCCESS)
                        {
                            _dbgwrite("Socket Closed!\n");    
                        }
                        else
                        {
                            _dbgwrite("Problems on HTTPClose...\r\n");
                        }
                    }
                    break;    
 
                default:
                    break;
            }
        }
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox