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.

CC3200MODLAUNCHXL: How to obtain data from HTTP server code and how to parse JSON object in HTTP server code

Part Number: CC3200MODLAUNCHXL
Other Parts Discussed in Thread: CC3200, CC3200MOD

Hi all,

We have a requirement in project where the CC3200 MOD will be a server and an Android terminal will be talking to it.
The Android terminal would be sending a JSON object which the server(CC3200) has to decode and send response or act to a command in the object (JSON string).

I am aware of the following things present in the SDK.

1. http server - demo
2. http client demo
3. Jasmine json parser.

I believe I have to use http server along with Jasmine parser.
Am I right ?

My queries :

1. In void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pSlHttpServerEvent,
SlHttpServerResponse_t *pSlHttpServerResponse)

and in the SL_NETAPP_HTTPGETTOKENVALUE_EVENT case, I see that pSlHttpServerResponse is being filled with response length and response data.

How is the structure pSlHttpServerResponse sent to the client , does the webserver which called the callback SimpleLinkHttpServerCallback automatically sends ?
If not how is the response sent ?

2. The customer has sent the data fields required in the form of "raml" documents.
Do I have to create a JSON format similar to what is there in http-demo-client like
#define POST_DATA "{\n\"name\":\"xyz\",\n\"address\":\n{\n\"plot#\":12,\n\"street\":\"abc\",\n\"city\":\"ijk\"\n},\n\"age\":30\n}"

Thank you very much.

Regards,
Vamsi.

  • Hi Vamsi,

    Your android app doesn't necessarily have to use HTTP to talk to the CC3200. If you know the IP address of the CC3200, you could just open a TCP socket and send whatever data you want directly to the CC3200 without being constrained by the HTTP server limitations. Of course, if you want to use some sort of web interface interaction on the android side then using HTTP probably makes sense.

    As for your queries:

    1) Once the NWP invokes the HTTP server callback on the host, it waits a couple seconds for the host to fill the pSlhttpServerResponse struct with data. Once the callback returns, it will use that struct to make and send to the client a http response to the get/post request.

    2) I am not familiar with raml documents, but in order for the POST request to be given to the host's http server callback, you need to POST one or more custom tokens. Please look at section 12.5 of the NWP user's guide:
    www.ti.com/.../swru368b.pdf
    for a reference of how you would use the custom tokens to allow for POST requests to trigger host actions. You can also look at the http server callback in the oob example for reference.
    When posting data to the server, you don't need to format your data as JSON object. However, JSON is often used since it's a useful container for string data.

    Let me know if you have other questions on the HTTP server, or if you need more help.

    Regards,
    Michael
  • Hi Michael,

    Thank you for the reply.
    The document was very helpful.

    I used Postman tool to test the POST method with my defined token value and I was able to hit the SimpleLinkHttpServerCallback function.

    The issue is with GET method.
    When I sent GET method from Postman tool with user defined token value, I am receiving a html page in response, but that does not contain my intended value.
    I am testing http server example itself for GET testing.
    What I receive is the content of main.html. But not the string notifying the status of LED1 and LED2

    Not sure if I am missing something.

    Thank you very much.

    Regards,
    Vamsi.
  • Hi Vamsi,

    When you send a GET request, the HTTP server will respond with the resource you request. Thus, when you send a GET request pointing to a specific webpage, the CC3200 will send back the contents of the page you request. In order to use tokens with your HTML page, you need to embed the tokens within the webpage that you serve, so that the CC3200's internal HTTP can substitute that token with the correct data.

    Please reference section 12.4.5 of the NWP user's guide for what I mean by token substitution. You can also look at the webpages bundled with the out of box example, such as cc3200-sdk/example/out_of_box/html/setup.html for an example of how you would use GET tokens.

    Regards,
    Michael
  • Hi Michael,

    Our project contains CC3200MOD as main processor and a secondary processor for business logic

    The system gets commands from Android terminal, which is developed by some third party.

    The commands come in JSON format and will be of the form

    192.168.1.1/CommandX/getValueA/{id}                       id, if applicable

    192.168.1.1/CommandY/setValueB/{id}                       id, if applicable

    Both setting of values and retrieving of values should be possible.

    Our reply also should be in JSON format for the Android device to understand.

    With regards to above,  below are queries -

    1.  Testing has been done for POST method using  Postman tool, during testing one thing is observed with respect to the value being passed

          Only 40 characters are being allowed to be passed as value, anything beyond that are truncated.

          Even the HTTP callback handler is not being called.

          I have checked to see if this is due to Postman truncating the string. But from below post mentions that more than 40 characters are

         handled seamlessly.

         

         Is there any restriction on the length of the string that can be handled by the webserver inside CC3200MOD.

    2. Regarding GET method, the httpserver example itself was used.

        As a response, the server returned 204 and No-content.

        When I tried to print the response text and response in led_demo.html, I see the whole webpage being sent instead of LED1_OFF,LED2_OFF string.

        What could be the reason ?

         Below are the screenshots for issue 2. Also attaching the led_demo.html modified file, (modified part of the code)

    HTML code of led_demo.html

    var HTTPrequest = new XMLHttpRequest();
    function UpdateStatus()
    {
    //var status = "__SL_G_ULD";
    var params = "__SL_G_ULD";
    //if(status.indexOf("LED1_ON") > -1)
    // document.getElementById("LEDno1").checked = true;
    //else
    // document.getElementById("LEDno1").checked = false;
    //if(status.indexOf("LED2_ON") > -1)
    // document.getElementById("LEDno2").checked = true;
    //else
    // document.getElementById("LEDno2").checked = false;

    HTTPrequest.open("GET","", true);
    HTTPrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    HTTPrequest.onreadystatechange = function()
    {
    if (HTTPrequest.readyState==4 && HTTPrequest.status==200)
    {
    console.log("response:" + HTTPrequest.status);                                                      >>>>>>>>>>>>>>>>>>>>>> Response code
    if(HTTPrequest.responseText == null) {
    console.log("Error");
    }
    else{
    console.log(HTTPrequest.responseText);                                                                >>>>>>>>>>>>>>>>>>>>>> Response Text
    }
    }
    }
    HTTPrequest.send(params);
    }

    function ToggleLED(whichOne)

    To iterate, the two issues are 

    1. Token value restriction if any for the webserver

    2. How to read the response of GET request.

     Thank you very much.

    Regards,

    Vamsi.

  • Hi Vamsi,

    1) The max length of the token should be 64 bytes, and not just 40 bytes. That being said, the max amount of data might still be too little for your use case.

    2) The behavior you are seeing is normal. When the internal HTTP server of the CC3200 receives a GET request, it will send back the contents of the GET request target. Furthermore, if the GET request has a HTML page as its target, it will check that HTML page for any custom tokens, and then perform token substitution and replace each token with an internal value or a value provided through the HTTP server callback. You cannot simply send back a single JSON value unfortunately.

    As you can tell, the internal HTTP server of the CC3200 is quite limited. I suggest you take a look at a software HTTP server that runs on your MCU, such as the web server used in the websock_camera example.
    Alternatively, you can also just run a simple TCP socket that your android terminal can connect to, and send back exactly the data that you need.

    Regards,
    Michael