3 - WebSocket server implementation in ESP IDF FreeRTOS for ESP32

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello today we're going to talk about websocket server on the esp32 in the ESP IDF environment like always we will start with the expressive website and expressive documentation the interesting thing is that a websocket server is placed inside the HTTP server as you can see here API reference application protocols HTTP server and websocket server is part of it and we will scroll down a bit and you can see here a very brief explanation about websocket server with the pretty complicated example from my point of view our repositories is inside the Sims iot devices we have a websocket server with actually two files the first one is a HTTP server and the second one is the website websocket server for those of you who are interested in short explanation you can skip to the websocket server but I suggest to start a HTTP server we have specific video about it but here we want to see the difference between the HTTP server and the websocket server so we will open here the HTTP server and here we will open the websocket server starting with HTTP server this is a code and we have here in our editor and inside the HTTP server we have a standard includes Wi-Fi connect Wi-Fi Handler Wi-Fi connectivity and this is the server we are starting from application main which takes us to the HTTP server application start here we have four lines of code which defining the server the default configuration we are starting the server and registering the Handler which is a URI Handler which for Simplicity we started with the get Handler which defined here please note that URI is slash http means that we are going to default a Gateway of our router of our wifi router and we will add HTTP to it our Handler Handler get Handler is sending the message hello from HD from HTTP server this is a simple HTTP server let's see how it works and let's see uh how we can check it we will check it with Postman on our esp32 we installing the server and the postman will be used as a client and we will send a get message to our server on esp32 okay please note the following Wi-Fi is now running and here here we have our Gateway our code is printing the Wi-Fi AP which is the Gateway to our router in order to send the command we will use get option with the same with the same Gateway and we will add HTTP at the end I will press enter now it's connecting hello from HTTP server and we have a get request here you can see it here okay entire get request with nobody with the following message from the server let's go back this is the message that we have received now after we have a basic HTTP server running we can go forward to our websocket server first let's see the differences between HTTP and websocket server here on the HTTP server if we are taking client and the HTTP server we are sending an HTTP request and then we got HTTP response and every time for we save the request and it spawns and so on the other hand for the websocket server we only initially have a handshake which established the connection between the server client and the server and then we have a full duplex where we can send messages freely from one another but here in the inside the esp32 inside this pidf philosophy we are using HTTP protocol to do so but the duplex is a fully made in order to do it We Will We Will sorry we will open our websocket server program here we will copy it this is our program see we will pass the past it here save and run a run the server now we are running the websocket server and meanwhile you can see that the includes and we have it in the additional tag here and the Wi-Fi connection and Wi-Fi Handler are exactly the same the difference begins here and it's a little bit more complicated our main application is Wi-Fi connection we wait for Wi-Fi connection to take place and then we run the websocket application start for a you can see just to see to make it visible here this is our original HTTP server you see uh we have very similar program here we have server null and we have a default configuration for the config HTTP config and the and here we're starting the server with HTTP start with HTTP start and the same as as uh as here this is the same also application we used for uh install initiating our handlers you see that this and these are completely the same the difference began with the handlers here we are sending our message to our initiation to URI Handler which is here which exactly is the same that we are doing here please know that our uh we we add to the Gateway in WC just to make it different but we are using the same HTTP get and we're using the same URL Handler structure but instead initiating get Handler and just print it's a message just send the message via HTTP server which was done by this and function here we are starting the synchronous get Handler a synchronous get Handler is initializing the queue which can save the messages and by that enable us the full duplex connection here we we Define the memory for our we call it the we call it the asynchronous response data structure for our data structure here we resolve some memory defining the parameters and starting the queue this is the main thing we are starting the queue with the following Handler and this Handler is a synchronous response Handler which have three parts we have our data formatted what we want to see we want to we want to send a hello from esp32 websocket server and very important to use this line because the HTTP protocol headers must be exactly like this every change from this in some space or some so on can cause an error anyway here we initialized our data after it we initialize this asynchronous response structure the same structure that you have seen here have seen here and then we are sending the data we're sending first the string the HTTP string the headers and then we are sending the data hello from the server let's see how it works and here we are ready on Port 80. here we will go to our uh you see the same here here and here we have the same Gateway and we added WSS enter okay and we couldn't connect to the server let's see what could be what was wrong one possibility is that we didn't initialize the web socket server inside the compiler okay after starting menuconfig the config options HTTP server websocket server save save okay let's recompile [Music] our code okay it will take a bit here because of the complete recompilation let's see meanwhile there are unexpected response hangxi details okay the Hang Shay handshake wasn't uh sufficient and here we have the old message from the previous uh test um okay uh regarding the response data structure we have two major [Music] parameters here first the server instance which is an HTTP handle and the second the session socket file this disrupter which was a specific number you can see it here okay let's just wait to see it recompile okay okay connecting okay this is our Gateway and okay let's try it once again enter unexpected response oh by the way here you see the number of FD which was assigned to us oh this oh here now it's working we didn't get the get request here okay hello from websocket server and then here we have the command we have the entire entire command with the response body hello from websocket server so our error was that we didn't use the get here in this configuration but now it's working and you see the postman sends the command and got the return statement thank you
Info
Channel: SIMS IOT Devices
Views: 5,584
Rating: undefined out of 5
Keywords:
Id: fmZok-zgXRg
Channel Id: undefined
Length: 15min 49sec (949 seconds)
Published: Sun Sep 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.