This is a sample program using the HttpRequest library.
Sample: main.c
#include <stdio.h>
#include "lib.h"
#include "http_request.h"

//URL of the server
#define SERVER_URLĀ "https://www.opto-support.com/oph5000demo/postsample.php"

char postData[] = {
        "### Posted by OPH ###\r\n"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
        "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\r\n"
        "### Posted by OPH ###\r\n"
};

//Option flag for http
#define HTTP_OPTION      HTTP_FLAG_BYPASSMODE
//#define HTTP_OPTION     0

//Timeout for WLAN connection
#define WLAN_CONNECTION_TIMEOUT     (30000/20)
#define SERVER_RESPONSE_TIMEOUT     (15000/20)

#define HTTP_SUCCESS                        0
#define HTTP_WLAN_TIMEOUT_ERROR             1
#define HTTP_SERVER_CONNECTION_ERROR        2
#define HTTP_SERVER_RESPONSE_ERROR          3
#define HTTP_SERVER_TIMEOUT_ERROR           4
#define HTTP_SYSTEM_ERROR                   (-1)

void appJobA(void);
void appGet(void);
void appPost(void);

#define PHASE_TOP_MENU  1
#define PHASE_JOB_A     2

void main(void)
{
    int appPhase = PHASE_TOP_MENU;
    int keyhit;

    //Log settings
    DBG_HTTP_REQUEST_API = 1;
    //DBG_HTTP_REQUEST_LIB = 1;
    DBG_HTTP_RECEIVED_DATA = 1;
    DBG_HTTP_SENT_DATA = 1;

    OsStatus(ON);
    SetEcho(OFF);


    while(1){
        switch (appPhase){
            case PHASE_TOP_MENU:
                printf("\f    <<Top menu>>\n[1] Start JOB_A\n[*] Power off\n");
                //Disable WLAN at the top menu to save battery.
                SysSetWLANPower(SYS_WLAN_POWER_OFF);
                printf(">WLAN disabled\n");
                while(1){
                    keyhit = getchar();
                    if (keyhit == '1'){
                        appPhase = PHASE_JOB_A;
                        break;
                    }
                    if (keyhit == '.'){
                        PowerOff();
                        continue;
                    }
                    Idle();
                }
                break;
            case PHASE_JOB_A:
                appJobA();
                appPhase = PHASE_TOP_MENU;
                break;
        }
    }
}

void appJobA()
{
    int keyhit;
    int exitFlag = FALSE;

    printf("\f    <<JOB_A>>\n");

    //Enable WLAN
    SysSetWLANPower(SYS_WLAN_POWER_AUTO);
    SysWLANInit(NULL);
    printf(">WLAN enabled\n");

    //Init HttpRequest
    HTTP_InitHttpRequest();

    while (1){
        printf("\n[F1] GET  [F2] POST\n[BS] Exit\n");
        while (1){
            keyhit = getchar();
            switch (keyhit){
                case F1_KEY:
                    //HTTP GET
                    appGet();
                    break;
                case F2_KEY:
                    //HTTP POST
                    appPost();
                    break;
                case BS_KEY:
                    break;
                default:
                    Idle();
                    continue;
            }
            break;
        }
        if (keyhit == BS_KEY){
            break;
        }
    }
    //Deinit HttpRequest
    HTTP_DeinitHttpRequest();
    return;
}


void appGet(void)
{
    int status;
    unsigned int waitStartTick;
    bool abortFlag = false;
    HTTP_REQUEST_HANDLE hRequest;
    char responsePhase;
    unsigned int responseStatus;
    char *content;
    unsigned int content_length;
    char *header;
    unsigned int headerLength;
        
    AutoPowerDown(APD_DISABLE, 0);

    // Repeat HTTP GET until BS_KEY pressed
    while (1){
        printf("\f    <<Http GET>>\n");

        //Check WLAN connection
        waitStartTick = GetTickCount();
        do{
            SysGetWLANConnectStatus(&status);
            if (status == SYS_WLAN_STATUS_CONNECTED)
                break;
            Idle();
        }while (GetTickCount() - waitStartTick < WLAN_CONNECTION_TIMEOUT);
        if (status != SYS_WLAN_STATUS_CONNECTED){
            printf(">WLAN timeout!\n");
            break;
        }
        printf(">WLAN ready\n");

        //Create HTTP GET request and send it to HTTP server
        hRequest = HTTP_CreateRequest(
                        SERVER_URL,
                        HTTP_REQ_GET,
                        NULL,
                        0,
                        "Connection: close\r\n",
                        SERVER_RESPONSE_TIMEOUT,
                        HTTP_OPTION);
        if (hRequest){
            printf(">%s\n>GET in progress...\n", SERVER_URL);
            while (1){
                //Wait for HTTP response
                if (HTTP_GetResponse(hRequest,
                                    &responsePhase,
                                    &responseStatus,
                                    &content,
                                    &content_length,
                                    &header, &headerLength)){
                    switch(responsePhase){
                    case HTTP_REQUEST_CONNECTION_ERROR:
                        printf(">CONNECTION_ERROR!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_SYSTEM_ERROR:
                        printf(">SYSTEM_ERROR!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_TIMEOUT:
                        printf(">TIMEOUT!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_FINISHED:
                        printf(">FINISHED status=%d\n", responseStatus);
                        if (responseStatus == 200){
                            printf(" %d bytes received\n", content_length);
                            if (content_length){
                                SetTextColor(RGB_BLUE);
                                printf("%s\n", content); //Display received data
                                SetTextColor(RGB_BLACK);
                            }
                        }
                        break;
                    default:
                        Idle();
                        continue;
                    }
                }else{
                    printf(">System error!\n");
                    abortFlag = true;
                }
                break;
            }
            HTTP_CloseRequest(hRequest);
        }else{
            printf(">Server failed\n");
            abortFlag = true;
        }
        if (!abortFlag){
            printf("Repeating. [BS] Stop");
            waitStartTick = GetTickCount();
            while (GetTickCount() - waitStartTick < 3000/20){
                if (kbhit()){
                    if (getchar() == BS_KEY){
                        abortFlag = true;
                        break;
                    }
                }
                Idle();
            }
        }
        if (abortFlag){
            break;
        }
    }
    AutoPowerDown(APD_ENABLE, 0);
}

void appPost(void)
{
    int status;
    unsigned int waitStartTick;
    bool abortFlag = false;
    HTTP_REQUEST_HANDLE hRequest;
    char responsePhase;
    unsigned int responseStatus;
    char *content;
    unsigned int content_length;
    char *header;
    unsigned int headerLength;

    AutoPowerDown(APD_DISABLE, 0);

    // Repeat HTTP POST until BS_KEY pressed
    while (1){
        printf("\f    <<Http POST>>\n");

        //Check WLAN connection
        waitStartTick = GetTickCount();
        do{
            SysGetWLANConnectStatus(&status);
            if (status == SYS_WLAN_STATUS_CONNECTED)
                break;
            Idle();
        }while (GetTickCount() - waitStartTick < WLAN_CONNECTION_TIMEOUT);
        if (status != SYS_WLAN_STATUS_CONNECTED){
            printf(">WLAN timeout!\n");
            break;
        }
        printf(">WLAN ready\n");

        //Create HTTP POST request and send it to HTTP server
        hRequest = HTTP_CreateRequest(
                        SERVER_URL,
                        HTTP_REQ_POST,
                        postData,
                        sizeof(postData),
                        "Connection: close\r\n",
                        SERVER_RESPONSE_TIMEOUT,
                        HTTP_OPTION);
        if (hRequest){
            printf(">%s\n>POST in progress...\n", SERVER_URL);
            while (1){
                //Wait for HTTP response
                if (HTTP_GetResponse(hRequest,
                                    &responsePhase,
                                    &responseStatus,
                                    &content,
                                    &content_length,
                                    &header, &headerLength)){
                    switch(responsePhase){
                    case HTTP_REQUEST_CONNECTION_ERROR:
                        printf(">CONNECTION_ERROR!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_SYSTEM_ERROR:
                        printf(">SYSTEM_ERROR!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_TIMEOUT:
                        printf(">TIMEOUT!\n");
                        abortFlag = true;
                        break;
                    case HTTP_REQUEST_FINISHED:
                        printf(">FINISHED status=%d\n", responseStatus);
                        if (responseStatus == 200){
                            printf(" %d bytes received\n", content_length);
                            if (content_length){
                                SetTextColor(RGB_BLUE);
                                printf("%s\n", content); //Display received data
                                SetTextColor(RGB_BLACK);
                            }
                        }
                        break;
                    default:
                        Idle();
                        continue;
                    }
                }else{
                    printf(">System error!\n");
                    abortFlag = true;
                }
                break;
            }
            HTTP_CloseRequest(hRequest);
        }else{
            printf(">Server failed\n");
            abortFlag = true;
        }
        if (!abortFlag){
            printf("Repeating. [BS] Stop");
            waitStartTick = GetTickCount();
            while (GetTickCount() - waitStartTick < 3000/20){
                if (kbhit()){
                    if (getchar() == BS_KEY){
                        abortFlag = true;
                        break;
                    }
                }
                Idle();
            }
        }
        if (abortFlag){
            break;
        }
    }
    AutoPowerDown(APD_ENABLE, 0);
}


Sample: postsample.php
<?php
echo "Requested Method=" . $_SERVER["REQUEST_METHOD"] . "\r\n";
if ($_SERVER["REQUEST_METHOD"] == "GET"){
    $body = "### Data for GET ###\r\nabcdefghijklmnopqrstuvwxyz\r\n";
}else{
    $body = file_get_contents( 'php://input' ); /*Echo back the posted data*/
}
echo htmlspecialchars($body, ENT_QUOTES, "UTF-8");



Last updated: 2021/06/04