TCP Client on ESP32 with ESP-IDF programmed in C: Step-by-Step Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay what's going on everyone it's the EV engineer and in this video I have a pretty cool demo where I'm going to be writing a TCP client for the esp32 microcontroller using the ESP IDF so I'm going to be coding this program completely in C code and we're going to do some interesting things such as connect to Google and we'll write some get requests in C using Bare Bones TCP sockets the first thing we're going to do here is create our ESP IDF project and the way we do that is we type IDF dot Pi create project and we'll call this TCP client once this is created we can see that it has generated a folder for us so we can CD into here and there's some more goodies in here we have the cmakelists.txt that's for our build system and we have the main folder which has the main source file and another cmake lists.txt so let's go ahead and let's start programming our TCP client in C so I like to open up another shell in bash so that I can use Linux commands because I'm on a Windows system and this is my windows subsystem for Linux Ubuntu machine so I'm going to CD into our newly created bowler CD into Main and I'm going to be editing this file in neovit so I'm just going to start from a bear uh empty file for now and I'm going to import some headers that we're going to need for this demo and these headers are specifically for the ESP IDF framework um and this one in particular is important the protocol example is common this is what allows us to connect to Wi-Fi and we're going to add some other headers later specifically for sockets but for now I just want to set up our microcontroller to be able to connect to Wi-Fi so we're now going to need our main function which in the ESP IDF framework is called app main because it's not actually the real main it's it's just what free rtos hooks onto so let's call that Main and it needs to be called that and we have some checks here these are straight from the framework not super relevant for sockets but um if you're interested in knowing more about it feel free to go read the documentation but we're just checking the flash we're checking the network interfaces uh we're doing some event loop I don't really even know what this does to be honest it's straight straight from the framework but the important one is ESP air check example connect so this function call will verify that our Wi-Fi connection is good which is going to be a prerequisite for writing any sockets or doing any HTTP requests so let's leave this file for now and we can go back to command prompt let's go back to the main folder and we're just going to do a little test run here we're going to do IDF dot Pi build now this will take a few minutes so while this is running let's go back and make some more edits that we're going to need so I mentioned earlier that we're going to need Wi-Fi so the way we do that in this framework is we're going to need to edit the cmake lists.txt so the cmakelists.txt uh well actually there's two of them and I think I opened the wrong one we don't need to edit this one we need to edit the one in the top level folder so we'll do nvim cmake lists.txt and this is the uh cmake script for our entire project and if you recall I mentioned we're going to need that one protocol examples uh we're gonna need the header file specifically and um all the corresponding library library source files so the way we include that in the project or the way we tell cmake to build that component for us to use is we add this one line which is called set extra component stir we give the framework path examples common components protocol examples common so this is a ESP IDF project just like the one we're making right now but it has tons of common utilities for all sorts of communication protocols such as Wi-Fi so let's save this file and let's go back to our Command shell and it looks like there was an issue yeah so as you can see it couldn't find the protocol examples common uh that's because we just told it right now where it is so we didn't have time to actually make it before I kick off another build though I'm going to input the Wi-Fi parameters for our project that we're going to need so the way we do that is IDF dot Pi menu config and this is going to open up a little GUI for us and now the GUI has popped up now you can see here there is one uh entry called example connection configuration and in here we have the Wi-Fi network and we have the Wi-Fi password now I'm going to input these right now the way you do that is you click enter and then you type in blah blah blah for your credentials obviously I'm not going to show mine right now so I'll see you after you enter yours now after you enter your credentials it's going to uh save whatever you wrote into this SDK config file so let's just build uh one more time and we should expect everything to work because we now have the the path set up for the project protocols common and we just input our Wi-Fi credentials great so it looks like the part that we expected to compile actually worked the only thing it's failing on now is it's saying I don't know what this TCP client function call is and that's because we haven't written it yet so let's go back here and let's go into Main and we will end them our TCP main file and just before I hit enter here I just wanted to point out that you can see this SDK config file these weren't here before uh and until we ran the IDF menu config and then it created all the parameters for our project and it stores them in here so if you're ever curious what all the parameters for your project is you can open up this file and it'll have all the configurations so now let's click enter and you can see here we have the function called to TCP client we haven't implemented it yet so let's do that void TCP client and we will give this a void parameter and Open Bracket close brackets cool so to start what we're going to do is actually create a infinite Loop because we don't really want this TCB client to be exiting we just want it to keep on sending TCP requests so I will open up an infinite Loop for that and the first thing we're going to do now is we're going to create our socket so uh I will paste this code in here so what this is doing is it's creating an ipv4 so IP protocol version 4 TCP socket file descriptor so a socket file descriptor is not a real file but it's treated as a file so when you create a socket file descriptor the operating system allows you to bind your program to this endpoint so from the perspective of your program a socket file descriptor is a file from the perspective of the operating system the socket file descriptor is just the uh the hook that your program uses to ask the operating system for various TCP functionality so that was a very crude explanation but uh that's pretty much all you need to know we need to create something called a socket file script um so before we continue let's actually include all the headers that we're going to need to use various socket functionality so um I have these written out here and I will just paste them right here so sddio obviously for printf uh sysocket.h this is really the main header file that has all the socket functions we have a few other helper libraries such as netdb this is for closing sockets um and then yeah these are just standard utilities so we have our socket file descriptor um now what we need to do is we're going to set up the host parameters now in our case the host is actually just going to be everyone's favorite website google.com because it is a very simple uh demo to set up so google.com we're going to use this struct host entity get host by name and that's from the library that we included above we'll do a quick check for if host is null if it is no good we'll return and uh let's continue so the next thing we're going to do is we're actually going to set up the socket parameters for the host so this here isn't exactly a socket parameter yet it's just this random entity that we've named host entity called google.com what we need now is something called a socket address in data structure so the socket address data structure is all the information needed to set up the TCP socket with the server so what information do we need well we need to tell that which version of um of the Internet Protocol we're going to be using so if you want to use ipv4 what you do is you input AF underscore inet here for the uh sin family and for the sin Port we're going to be using Port 80 because that is the default HTTP port and H this weird function here is called host to network uh s I'm not sure what the S is but what this is doing is it's just making sure that the endess of this integer is correct because network endedness is actually big Indian so if the host system uses little endian it has to swap around the bytes so now finally what we do is we input the IP address which is in this uh struct parameter and uh because we used this Library function up here called host entity we have a bit of a bit of a cryptic syntax here but we have to cast the host address as an in address pointer and then dereference it it's a little cryptic but it is pretty standard uh and it's well documented so now we have the data structure needed for rtcp connection we also have the socket which is the the end point that our program uses so what we need to do is we have to connect the socket address parameters with the socket and then once that is done we can finally use the TCP protocol so the way we do that is through this function called drumroll connect so I wrote a little comment here attempt to establish a connection with the server block until the connection is established or an error occurs if successful open the client file descriptor for reading and writing so um what this is telling us is that this function is blocking so we're just going to wait here until the timeout occurs and we've either connected to the Google server or there was an issue and we're going to be returning and printing an error message um so yeah there's a bit of a Nuance here and I forgot to create this type name uh which I will do right now so there's something called a struct uh sock address so I just wanted to make a quick interlude here if you wanted some more information on what the type depth was for the sock address structure so I have a textbook here which is mainly what I use to learn about sockets and you can see they have the data structure for sockets which we use to define the family the port the address and this is just a bunch of padding so just zeros and then the generic socket address structure now they mentioned down here in this paragraph the connect bind and accept functions require a pointer to protocol specific socket address structures but the problem at the time is that the sockets interface required the generic socket address structure uh but there weren't any void pointers in the C language at the time so what they needed to do is cast all of the protocol specific socket address structures to the generic socket address structures so that is why in my program I also did this and this seems to be now the official way to do it so we need to create this type def up here and we're going to be using this for example in the connect function where we have to cast our address or the the the address of our address as a socket address pointer and we'll be giving that to the connect function and like I said it's going to be connecting the parameters with the socket so if we get past this point the assumption is that we've connected to the server so I'm going to be printing a little message here if you happen to be looking at the console log uh cool so we know at this point that we have successfully connected now what are we going to do we're going to be shooting off some strings to the Google server and uh it just so happens that the string we're going to be shooting off is actually a get request so if you've never seen a get request in the form of a string before it's pretty cool you just write get and then root HTTP 1.1 Bachelor backslash and host google.com and then a bunch of other character turns and new lines so and and we should note that there's no payload in this get request um we're just connecting to the endpoint so now what we can do is we have everything set up to finally send our TCP uh our TCP message which happens to be an HTTP string but the HTTP isn't actually important what you should be focusing on is the TCP part we're sending a TCP string and then what the server decides to do with that string could be any sort of higher level protocol in our case it's http um so let's send our socket string the way you send a TCP string is you use the send function which is defined in the socket Library you give it the file descriptor the message the length of the message and this zero here is for any optional flags that you want to include we don't want to include any so we're going to use a0 so uh now after we've sent our string to the server we're going to want to check if the server responds so let's uh create a a character buffer and let's ask the socket API if anything has been returned so the buffer is here and the way we receive stuff is with the receive socket function we give it the socket file descriptor the buffer and the buffer size and more optional Flags we're going to log the response from the server using the ESP log function and I'm going to be using the free rtos task delay to wait three seconds before we repeat this entire process from scratch um I just realized up here so um I didn't explain iprotocol describes the lower level protocol that you want the TCP socket to use usually people just set this to zero that's just the defaults so we're going to be adding that up here will come here we'll add this we're also going to be including including the tag which is just used for the ESP IDF logging functionality the other thing we should note is that when we are creating the socket file descriptor this sock stream here is what you use to specify that you want a TCP socket rather than a UDP socket because those types of sockets are treated differently so we're going to do one more thing which is actually to properly close the socket because if you keep a socket open and the program ends this is more of an issue on a Linux machine or like a or any desktop machine really is if the program ends but it isn't properly close the socket it could have issues reopening that socket so you always want to make sure that you're doing the appropriate housekeeping required so the way we're going to close our socket is uh we're going to check that it exists first everything's fine and then we're going to be shutting down the socket and closing it so these two functions here are what you need to do to shut down and then close the socket and let's get rid of this extra bracket so what our program is going to do is it's going to send one message close the socket and then it'll come back up and it'll repeat the whole process so let's uh save our file let's go back to the command line and let's build so it looks like our program built without a hitch so now what we're going to do is we're going to type in IDF dot Pi Dash p com3 which is the port that I'm using Flash and then I want to Monitor and I have my esp32 right next to me here we're going to type in uh well we're just going to press enter it's going to flash our device we're going to hold down the boot Button so our program is now flashing on the device and it is now booting up and it is setting up the Wi-Fi connection through this example connect function and we've now started into our program as you can see here it looks like we've successfully gotten a response from the Google server so we connected the TCP sockets and we got HTTP 200 okay from the Google server and at the end of our Loop if you recall we shut down the socket and we restarted the entire uh TCP client and we wait three seconds with the free rtos the vtas delay and we start from scratch so every three seconds we're setting up a new TCP client and sending a new HTTP request and getting a new response so everything looks like it's working as expected so I hope you enjoyed the video we did a pretty interesting demo today uh one might call this an iot project because we are using a microcontroller to connect a TCP socket to Google and this is really the foundation of how the internet works and nowadays you're able to do this kind of code which used to only exist for these Mainframe Linux computers on a small Asic on a microcontroller so what a time to be alive uh if you enjoyed the video please remember to drop a like And subscribe let me know in the comment section if you you enjoyed this type of content so for all my subscribers thank you so much for watching I appreciate you guys and have a nice day
Info
Channel: The EV Engineer
Views: 3,712
Rating: undefined out of 5
Keywords: TCP Client, ESP32, ESP-IDF, C Programming, Embedded Systems, Networking, IoT Development, Berkeley Sockets, Microcontroller Tutorial, Programming ESP32, Internet of Things, Embedded Programming, IoT Connectivity, TCP/IP, ESP32 Development Board, Coding Tutorial, ESP32 Tutorial, Socket Programming, ESP-IDF Framework
Id: sj-IuTQ0fJk
Channel Id: undefined
Length: 20min 44sec (1244 seconds)
Published: Sun Aug 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.