cancel
Showing results for 
Search instead for 
Did you mean: 

How to send data structure from MCU to embedded Web Server

AAbde
Associate II

I'm setting up an embedded Web Server on STM32F4 MCU.

My program variables are stored into one big data structure. For instance, if I need to access to the temperature of my board, I'd simply check the value of Variables.Temperatures.BoardTemperature.

I used the ad.cgi example as a base to extract data from my software variable and have them on the web server. Here is an example of how I proceeded :

On a .cgi file I'd call the CGI function :

c g 1 size=’10’ id=’TempValue’ value=’%1d’>  

In my HTTP_Server_CGI.c, I process the data using the netCGI_Script function :

Switch (env[0]) {
 
          case 'g' : 
 
              len = (uint32_t)sprintf(buf, &env[4], Variables.Temperatures.BoardTemperature);
 
}

Finally, I'd use a .cgx file to update this value :

         t <form> 
 
         t <text> 
 
         t <id>TempValue</id>
 
         c <value>TempValue</value>
 
         t </form> 

Finally, using the already written updateForm function, I'd update values of my form every 500 ms.

Altough this method work fine I'm not willing to use it because of the incredible amount of code it generates. I have over 800 variables I need to update constantly. Writing 2400 line of code for simple data transfer seems odd.

I want to fetch the whole Acquisition structure from my server into my web page, and process the incoming data with JavaScript in order to use minimal HTTP requests between the MCU & Web Server. How is this possible ? Can XML handle large data or implementing JSON data structure would fit better ?

3 REPLIES 3

You can use packed structures and pass the whole structure in the original binary form, but it is a hassle (alignment issues, endianness, synchronization issues with every change, etc.).

> I have over 800 variables...

> Can XML handle large data or implementing JSON data structure would fit better ?

JSON is definitely more compact than XML, but maybe you should use some binary data serialization protocol like like BSON.

AAbde
Associate II

I've never done serialization before. In the example above, I always write the data format when fetching data from the server. In this case, how can I store the structure knowing that it is made of various different data types into a single array ?

> how can I store the structure knowing that it is made of various different data types into a single array ?

You create a packed structure type and pass the structure as array of bytes, then you recreate the structure from the bytes on the other side. If you are using the same compiler/architecture on both sides you can even use a regular (not packed) structure. Packed structures are slower and don't work on all architectures (i.e. Cortex-M0). [Click Show More]

/* Sending */
 
struct my_struct {
        uint8_t  field1;
        uint16_t field2;
        uint32_t array1[50];
} __attribute__((packed));
 
struct my_struct my_data = {0};
 
my_data.field1 = 12;
my_data.field2 = 34;
my_data.array1[12] = 34;
 
send_data((unsigned char *)&my_data, sizeof my_data);
/* Receiving */
 
struct my_struct {
        uint8_t  field1;
        uint16_t field2;
        uint32_t array1[50];
} __attribute__((packed));
 
struct my_struct my_data;
 
if (receive_data((unsigned char *)&my_data, sizeof my_data)) {
    printf("%u\n", my_data.field1); /* prints 12 */
}