This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/CC3200MOD: Time Getting

Part Number: CC3200MOD
Other Parts Discussed in Thread: CC3200

Tool/software: Code Composer Studio

Hi,

I had  a custom board having cc3200Mod,the device is used to post data in cloud .i did it using http client demo,now i need to get real time using the same code .is there any options to get time from the same http client demo .i noticed a time getting application from the sdk (sntp time),which is an rtos code .is it possible to implement sntp time code in http client demo?.if there any other option please explain.

  • Hi,

    You can add SNTP code into your project with http client. SNTP protocol is very trivial for implementation and it is very easy implement it by yourself.

    Another option is get time from http server. If http server is under your control you can implement on server http GET method which will return current time.

    Jan
  • Hi JAN,
    Thanks for your reply.
    Server is under my control ,could u explain how can i get time from the server.(what are the changes that i need to implement in cc3200mod code)

    Aju

  • Hi Aju,

    It is simple. At your server you create handler which returns actual server time. Inside your CC3220 code you send GET request and by return value you set time inside CC3220.

    Jan
  • Hi Jan,
    Now i'm looking for it but i didn't obtain any thing.here i post my getmethod and read response.if ii have mistakes in my code please explain detailed that how can i solve it or provide me some example code .



    //*****************************************************************************
    //
    //! \brief HTTP GET Demonstration
    //!
    //! \param[in] httpClient - Pointer to http client
    //!
    //! \return 0 on success else error code on failure
    //!
    //*****************************************************************************
    static int HTTPGetMethod(HTTPCli_Handle httpClient)
    {

    long lRetVal = 0;
    /*
    * GET /get.php HTTP/1.1
    * This line specifies an HTTP command, called a method, followed by the address of a
    * document and the version of the HTTP protocol being used. In this case, the request
    * is using the GET method to ask for the index.html document using HTTP 1.1. After
    * this initial line, the request can contain optional header information that gives the server
    * additional data about the request. For example:
    * User-Agent: Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]
    *
    */

    //GET /get.php HTTP/1.1
    // optional header information that gives the server additional data about the request.
    //Accept: image/gif, image/jpeg, text/*, */* <---- "*/*" indicating all media types

    HTTPCli_Field fields[4] = {
    {HTTPCli_FIELD_NAME_HOST, HOST_NAME},
    {HTTPCli_FIELD_NAME_ACCEPT, "*/*"}, //Accept: image/gif, image/jpeg, text/*, */*
    {HTTPCli_FIELD_NAME_CONTENT_LENGTH, "0"},
    {NULL, NULL}
    };
    bool moreFlags;


    /* Set request header fields to be send for HTTP request. */
    HTTPCli_setRequestFields(httpClient, fields);

    /* Send GET method request. */
    /* Here we are setting moreFlags = 0 as there are no more header fields need to send
    at later stage. Please refer HTTP Library API documentaion @ HTTPCli_sendRequest
    for more information.
    */
    moreFlags = 0;

    /*
    * Here we are setting moreFlags = 1 as there are some more header fields need to send
    * other than setted in previous call HTTPCli_setRequestFields() at later stage.
    * Please refer HTTP Library API documentaion @ref HTTPCli_sendRequest for more information.
    */
    //moreFlags = 1;
    lRetVal = HTTPCli_sendRequest(httpClient, HTTPCli_METHOD_GET, GET_REQUEST_URI, moreFlags);
    if(lRetVal < 0)
    {
    UART_PRINT("Failed to send HTTP GET request.\n\r");
    return lRetVal;
    }


    //##################################################################################################################
    //bool lastFlag = 1;
    //char tmpBuf[4];
    //sprintf((char *)tmpBuf, "%d", (sizeof(POST_DATA)-1)); // POST data body

    /*
    * Here we are setting lastFlag = 1 as it is last header field.
    * Please refer HTTP Library API documentaion @ref HTTPCli_sendField for more information.
    */
    //lastFlag = 1;
    //lRetVal = HTTPCli_sendField(httpClient, HTTPCli_FIELD_NAME_CONTENT_LENGTH, (const char *)tmpBuf, lastFlag);
    //if(lRetVal < 0)
    //{
    // UART_PRINT("Failed to send HTTP POST request header.\n\r");
    // return lRetVal;
    //}

    /* Send POST data/body */
    //lRetVal = HTTPCli_sendRequestBody(httpClient, POST_DATA, (sizeof(POST_DATA)-1));
    //if(lRetVal < 0)
    //{
    // UART_PRINT("Failed to send HTTP POST request body.\n\r");
    // return lRetVal;
    //}

    //##################################################################################################################

    lRetVal = readResponse(httpClient); //Currently returns all the text from get.html with text formatting (prints data to a serial port)



    // lRetVal = HTTPCli_sendRequest(httpClient, HTTPCli_METHOD_GET, GET_REQUEST_URI_JSON, moreFlags);
    // if(lRetVal < 0)
    // {
    // UART_PRINT("Failed to send HTTP GET request.\n\r");
    // return lRetVal;
    // }


    // lRetVal = readResponse(httpClient);


    return lRetVal;
    }



    static int readResponse(HTTPCli_Handle httpClient)
    {
    long lRetVal = 0;
    int bytesRead = 0;
    int id = 0;
    unsigned long len = 0;
    int json = 0;
    char *dataBuffer=NULL;
    bool moreFlags = 1;
    const char *ids[4] = {
    HTTPCli_FIELD_NAME_CONTENT_LENGTH,
    HTTPCli_FIELD_NAME_CONNECTION,
    HTTPCli_FIELD_NAME_CONTENT_TYPE,
    NULL
    };

    /* Read HTTP POST request status code */
    lRetVal = HTTPCli_getResponseStatus(httpClient);
    if(lRetVal > 0)
    {
    switch(lRetVal)
    {
    case 200:
    {
    UART_PRINT("HTTP Status 200\n\r");
    /*
    Set response header fields to filter response headers. All
    other than set by this call we be skipped by library.
    */
    HTTPCli_setResponseFields(httpClient, (const char **)ids);
    //UART_PRINT("\n\r ids : %s", ids);

    /* Read filter response header and take appropriate action. */
    /* Note:
    1. id will be same as index of fileds in filter array setted
    in previous HTTPCli_setResponseFields() call.

    2. moreFlags will be set to 1 by HTTPCli_getResponseField(), if field
    value could not be completely read. A subsequent call to
    HTTPCli_getResponseField() will read remaining field value and will
    return HTTPCli_FIELD_ID_DUMMY. Please refer HTTP Client Libary API
    documenation @ref HTTPCli_getResponseField for more information.
    */
    while((id = HTTPCli_getResponseField(httpClient, (char *)g_buff, sizeof(g_buff), &moreFlags))
    != HTTPCli_FIELD_ID_END)
    {
    //(char *)g_buff - value Field value string.

    switch(id)
    {
    case 0: /* HTTPCli_FIELD_NAME_CONTENT_LENGTH */
    {
    len = strtoul((char *)g_buff, NULL, 0);
    UART_PRINT("\n\rHTTPCli_FIELD_NAME_CONTENT_LENGTH\n\r");
    UART_PRINT("\n\rLength: %s", g_buff);
    }
    break;
    case 1: /* HTTPCli_FIELD_NAME_CONNECTION */
    {
    UART_PRINT("\n\rHTTPCli_FIELD_NAME_CONNECTION\n\r");
    UART_PRINT("\n\n\rConnection name: %s", g_buff);
    }
    break;
    case 2: /* HTTPCli_FIELD_NAME_CONTENT_TYPE */
    {

    UART_PRINT("\n\r HTTPCli_FIELD_NAME_CONTENT_TYPE\n\r");
    UART_PRINT("\n\r Content type : %s", g_buff);
    if(!strncmp((const char *)g_buff, "application/json",
    sizeof("application/json")))
    {
    json = 1;
    UART_PRINT("\n\r g_buff == application/json \n\r");
    }
    else
    {
    /* Note:
    Developers are advised to use appropriate
    content handler. In this example all content
    type other than json are treated as plain text.
    */
    json = 0;
    }
    //UART_PRINT(HTTPCli_FIELD_NAME_CONTENT_TYPE);
    //UART_PRINT(" : ");
    //UART_PRINT("\n\r application/json\n\r");
    }
    break;
    default:
    {
    UART_PRINT("\n\r Wrong filter id \n\r");
    lRetVal = -1;
    goto end;
    }
    }
    }
    bytesRead = 0;
    if(len > sizeof(g_buff))
    {
    dataBuffer = (char *) malloc(len);
    if(dataBuffer)
    {
    UART_PRINT("\n\r Failed to allocate memory \n\r");
    lRetVal = -1;
    goto end;
    }
    }
    else
    {
    dataBuffer = (char *)g_buff;
    }

    /* Read response data/body */
    /* Note:
    moreFlag will be set to 1 by HTTPCli_readResponseBody() call, if more
    data is available Or in other words content length > length of buffer.
    The remaining data will be read in subsequent call to HTTPCli_readResponseBody().
    Please refer HTTP Client Libary API documenation @ref HTTPCli_readResponseBody
    for more information

    */
    bytesRead = HTTPCli_readResponseBody(httpClient, (char *)dataBuffer, len, &moreFlags);
    UART_PRINT("\n\n\r Received response body: \n\r %s", dataBuffer);//Print web-site response

    if(bytesRead < 0)
    {
    UART_PRINT("\n\r Failed to received response body\n\r");
    lRetVal = bytesRead;
    goto end;
    }
    else if( bytesRead < len || moreFlags)
    {
    UART_PRINT("\n\r Mismatch in content length and received data length\n\r");
    goto end;
    }
    dataBuffer[bytesRead] = '\0';

    if(json)
    {
    /* Parse JSON data */
    lRetVal = ParseJSONData(dataBuffer);
    if(lRetVal < 0)
    {
    goto end;
    }
    }
    else
    {
    /* treating data as a plain text */
    }

    }
    break;

    case 404:
    UART_PRINT("File not found. \r\n");
    /* Handle response body as per requirement.
    Note:
    Developers are advised to take appopriate action for HTTP
    return status code else flush the response body.
    In this example we are flushing response body in default
    case for all other than 200 HTTP Status code.
    */
    default:
    /* Note:
    Need to flush received buffer explicitly as library will not do
    for next request.Apllication is responsible for reading all the
    data.
    */
    FlushHTTPResponse(httpClient);
    break;
    }
    }
    else
    {
    UART_PRINT("Failed to receive data from server.\r\n");
    goto end;
    }

    lRetVal = 0;

    end:
    if(len > sizeof(g_buff) && (dataBuffer != NULL))
    {
    free(dataBuffer);
    }
    return lRetVal;
    }
  • Hi Aju,

    Http GET request is only a way how obtain information/data from http server. And this information will be current data+time with format as you want. And this depends on your implementation. You need:
    - create http server callback returning time in some format (string, number of seconds, etc.)
    - from CC3200 send post request to your server
    - parse response at CC3200 and set RTC time by API sl_DevSet() if needed

    Jan
  • Hi Jan,
    Can you explain i detail.How to do it???(i'm stuck in here ).My server echo a data but i didn't obtain .only get

    HTTP Post Begin:
    HTTP Status 200

    HTTPCli_FIELD_NAME_CONTENT_TYPE

    Content type : text/html; charset=UTF-8

    Received response body:
    aext/html; charset=UTF-8HTTP Post End:



    in console.


    ( This is the echo data that i implement in server:"Welcome to my PHP data page.
    2018-03-09 17:35:15". I Generate a post request In cc3200Mod to the corresponding page but the data doesn't echo back ).
    Please help me to solve this.
  • Hi Aju,

    Without debugging of your code I am not able answer your question. I am not able to provide you such kind of effort.

    But one question. Are you sure that content of dataBuffer is a null terminated string to be possible print it by UART_PRINT()?

    Communication between CC3200 and your server you can sniff by Wireshark to be sure that is correct.

    Jan
  • Hi Jan,

    i tried the same in "www.cnktechlabs.com/post.php" then i obtain the content and in function bytesRead = HTTPCli_readResponseBody(httpClient, (char *)dataBuffer, len, &moreFlags); the byteRead has 235 value.Whe i tried it in my server i didn't obtain anything and also the byteRead is zero.but i can post data to server ,the problem is to obtain data .How to solve it

    Aju

  • Hi Aju,

    You should use Wireshark to determine how communication with your serve looks. In case of number of bytes is zero, you probably send back response without data from your server.

    Jan
  • Hi Jan,
    can you explain the way to insert SNTP code in http client,what are the functions that i need to include because it's an RTOS code.

    Aju
  • Hi Aju,

    NTP library is located at \source\ti\net\sntp\ of 1.60 SDK. If you need SNTP you can use this library and add it into your project. Only issue which you can have is that this library use SlNetSock abstraction layer.

    Jan