0:09
hello and welcome to controllers tech
0:13
this is another video in the ethernet
0:15
series and this is going to be the last
0:17
video on the raw apis
0:20
after this we will look into the
0:22
ethernet with the rtos
0:24
this video is the continuation from the
0:27
previous one where we sent the data to
0:29
the web server using the ssi
0:32
in this video we will see how to receive
0:35
data from the web server using the cgi
0:38
common gateway interface
0:41
as i mentioned this is the continuation
0:43
from the previous video i will also
0:45
continue where we left off last time
0:48
this here is the project
0:51
and this is how the web server looks
0:54
the ssi page updates every one second
0:57
and we display the values from the
1:00
since we are sending the data from the
1:02
web server to the controller we need to
1:05
create a new web page
1:08
and for this purpose i am going to use
1:20
this looks good enough
1:22
let me change the heading real quick
1:30
it is here is the action which will act
1:32
after the submit button is clicked
1:35
let's keep it unchanged for now
1:40
the f name and l name are the parameters
1:48
here f name is the first parameter and
1:51
john is the first value
1:54
similarly l name is the second parameter
1:57
and do is the second value
2:00
we will talk about these later
2:02
the action for the form i wanted to call
2:08
i will declare this later in the code
2:11
let's copy this and we will create a
2:29
i am calling it cgi form dot html
2:50
you can see the action then the
2:52
parameters and their values
2:55
all right we will come back to this but
2:57
first we need to enable the cgi and to
3:00
do that we will go to the cube mx
3:09
go to l w i p http advance parameters
3:13
and enable the cgi hold style
3:22
that's it click save to generate the
3:30
if we build it we will get our usual
3:32
error about the custom fs data file
3:40
you need to disable the use of custom fs
3:47
the next error will be about the fs data
3:55
let's go to the http folder
4:01
copy the html file here in the fs folder
4:05
generate the fsdata.c file again
4:09
now all the errors should be gone
4:15
all right we are good to go
4:17
i am going to write the functions in the
4:19
same file that we use for the ssi
4:23
last time we used the function set ssi
4:26
handler to assign the ssi handler to the
4:30
we are going to use a similar function
4:32
for the cgi handler also
4:37
here is the set cgi handler function and
4:41
it takes 2 parameters
4:43
the first is the pointer to a-t-c-g-i
4:46
structure and second is the number of
4:48
cgi handlers we are using
4:53
this t-c-g-i structure is defined right
4:56
above and it have two elements the file
4:59
name of the cgi and the handler itself
5:05
this is the function for the cgi handler
5:08
let's copy this into our file
5:26
i am calling it c g i form handler
5:32
the c g i handler takes 3 parameters
5:36
the first parameter i index provides the
5:40
index of the cgi in the cgi array
5:43
for example if you are using more than
5:46
one cgi handlers this parameter will
5:49
give the index of the handler being
5:54
the next is the num parameter and it
5:56
gives the index of the parameter within
6:00
for example in this tutorial i have used
6:03
the f name and l name so this num
6:06
parameter will basically indicate
6:08
whether it's the f name parameter or the
6:10
lname parameter that called the handler
6:16
then comes the pc parameter and pc value
6:20
they basically are the parameter and its
6:23
for example here in this case they will
6:26
be f name and john or l name and do
6:30
one last thing about this handler is
6:32
that it should return the pointer to the
6:34
html page basically the cgi page
6:38
let's write the rest of the code now
6:47
first of all we will define a structure
6:49
with the file name and the handler
6:58
i am calling it form cgi
7:03
the file name will be same as we
7:05
declared in the action of the html file
7:10
and handler is the cgi handler
7:15
now we will write the c g i handler
7:28
since we are only using one handler the
7:34
we have to write ay for loop for the
7:36
number of parameters we have in our html
7:41
as i mentioned we have 2 parameters f
7:59
if the f name parameter is detected we
8:02
will copy the value into our name array
8:23
and similarly if the parameters is l
8:26
name we will concatenate the value to
8:45
this way we will get the name as first
8:50
at the end of this handler we need to
8:52
return the pointer to the html file
8:56
this is the name of the cgi file that we
9:02
now we will set the cgi handlers
9:12
the first parameter is the pointer to
9:19
we will pass the address of the form cgi
9:24
and the number of handlers are 1 in our
9:27
that's it since we are already calling
9:29
the http server in it in our main file
9:32
we don't need to do anything else
9:35
let's build and test the code
9:38
i have added the name in the live
9:48
now we will go to the cgi form dot html
9:58
type in the first and last name
10:02
you can see the name has been stored in
10:06
let's try something else
10:15
the data did get stored but we have the
10:17
leftover from previous data
10:20
we need to clean the buffer before
10:22
storing the new data
10:24
before copying the first name we will
10:58
this time we got the data and there is
11:00
no leftover from the last time
11:03
we can still access the s s i page from
11:06
the previous tutorial
11:12
it keeps refreshing in the background
11:15
in the meanwhile we can transfer data
11:17
from the web server to our buffer
11:34
now let's say you want to use another
11:36
section for the data
11:38
for example some buttons to control the
11:42
to do that we will create another cgi
11:47
i am calling it cgi led handler
11:50
we will create another tcgi structure
11:56
we need to change the handler also
11:59
let's create an array of the cgi
12:06
before setting the cgi handler we need
12:09
to assign the tcgi structures to the
12:21
and now we will pass the structure array
12:24
to the set handler function
12:28
also we are using 2 handlers now
12:31
finally we will write the cgi led
12:48
since the l-e-d handler is the second
12:51
element in the array the i index will be
12:55
after this you do the rest of the
12:56
processing in the similar way
12:59
first check for the parameter and then
13:04
based on that control the leds
13:07
this is it for the video
13:09
i hope you understood the usage of cgi
13:12
in the http web server
13:15
this is the last video using the raw api
13:18
and the next video on the ethernet will
13:22
i will just cover the basic
13:24
initialization using the rtos and the
13:30
you can download the code from the link
13:34
leave comments in case of any doubt
13:37
keep watching and have a nice day ahead