cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] How to make a REST API for the STM32F lwIP?

angus schmaloer
Associate III
Posted on October 13, 2017 at 14:10

Hello,

I am trying to make a rest api for my stm32f controller. I am using the lwIP library for the Ethernet connection.

I looked at the STMCube applications with the lwIP library, but it works with .(s)html web pages.

i want to make a web interface that can be used for remote IO, settings and reading pins/data.

I want to send the data in JSON strings.

The data of LED 1 has to be set my styping into a web browser: 

http://10.1.0.113/LEDS/1/1

.

Does anyone know how to make REST API's for the stm32?

Greetings,

Angus Schmaloer

#REST # API #STM32 #lwIP

1 ACCEPTED SOLUTION

Accepted Solutions
angus schmaloer
Associate III
Posted on October 27, 2017 at 09:14

Hey all,

I fixed it.

I took some extra time and went digging into the lwIP HTTP library.

There is a (complex) way they process the request.

I copied the files and made my own driver.

My new driver can handle a HTTP server AND RESTAPI's.

I had to change some in the httpd.c for the whole RESTAPI 

processing.

In my copy of httpd_cgi_ssi.c, named restapi_cgi_ssi.c, i added my own way of adding extra restapi pages, based on the way you add cgi pages.

First it looked a bit challenging and weird to rewrite a bit of the lwIP library.

If anyone need to implement a RESTAPI message me.

Greetings,

Angus

View solution in original post

12 REPLIES 12
Joerg Wagner
Senior III
Posted on October 13, 2017 at 18:00

You can use simple string functions to parse. It has nothing in common with *.(s)html

Read the TCP data on port 80 and use i.e.

sprintf(token, 'GET /LEDS/1/1');

if(strncmp(buf, token, strlen(token)) {

   // some action

}

- Joerg -

Posted on October 15, 2017 at 10:09

Hey,

Thanks,

So, i can just use the HAL DMA ETH receive functie with an interrupt on the dma?

And the raw lwIP server example application from the STMCube is verry complex, are REST API's that much simpeler? (Excluding the processing of the data)

Greetings,

Angus

Posted on October 15, 2017 at 13:09

Angus, basically you do not get in touch with DMA functions. It's handled in the HAL and driver layer.

You have the option to use BSD sockets, the NetConn Interface or LwIP RAW functions.

In the CubeF? package are sample applications for each way, with and without FreeRTOS.

Take a look at the documentation of LwIP, regardless which microcontroller or platform you use.

- Joerg -

angus schmaloer
Associate III
Posted on October 17, 2017 at 16:23

hey,

The RAW lwIP application example uses http. i can only send static data 'pages' or with  ssi &cgi, but that also only works with .shtml.

This is my first time ever working with Ethernet and TCP.

Everybody is like read port 80 data, but how?

My internship supervisor only worked on a arduino and he is like: just read the data.

But i really don't know how.

Angus

Posted on October 17, 2017 at 21:35

Okay, you choosed this example. It's well documented. In file httpd_cgi_ssi.c you will see the function http_server_init(void).

In this function a callback handler is set for the receiving TCP data: ADC_Handler and a CGI handler is set.

Each time TCP data on port 80 is sent to the ST board, ADC_Handler is called.

You will find the processing in this function. Just read the comments.

- Joerg -

angus schmaloer
Associate III
Posted on October 27, 2017 at 09:14

Hey all,

I fixed it.

I took some extra time and went digging into the lwIP HTTP library.

There is a (complex) way they process the request.

I copied the files and made my own driver.

My new driver can handle a HTTP server AND RESTAPI's.

I had to change some in the httpd.c for the whole RESTAPI 

processing.

In my copy of httpd_cgi_ssi.c, named restapi_cgi_ssi.c, i added my own way of adding extra restapi pages, based on the way you add cgi pages.

First it looked a bit challenging and weird to rewrite a bit of the lwIP library.

If anyone need to implement a RESTAPI message me.

Greetings,

Angus
Jessy J
Associate II

Hello

I am using lwip with freertos and http server netconn.. I used also the httpd_cgi_ssi.c but the handler of cgi does not seem to be working correctly because when I want to turn off the led via the web page , it is never turning off.. Is theree something else missin g??

Thaank you in advance

> If anyone need to implement a RESTAPI message me.

Please let me know how to get the code to implement REST in STM32

Thanks

LCE
Principal

Find the httpd files within lwip, there the GET method is supported. There are also exmples for SSI / CGI, I think.

It's some work to get it running, but the foundation is there.