ESP-IDF: ESP32 as TCP Client

We have seen how to connect to a AP in the previous post, in this post we will hose to open a TCP connection with a server.

Why TCP?

Though we can use HTTP , that approach consumes lot of resources like hosting a presentation layer, for a quick recap, given below is the OSI layer. In case of embedded systems, with constrained environment, using TCP , two layers below the 7 layers will make us power efficient and controlled way of programming the chipset.

We will see how TCP connection can be made using ESP 32.  For development we will use IoT development platform built on top of eclipse platform.

ESP-IDF uses lwIP (lightweight IP) to provide support many protocols .

  • Protocols supported by lwIP
  • ARP (Address Resolution Protocol)
  • IP (Internet Protocol) v4 and v6
  • TCP (Transmission Control Protocol)
  • UDP (User Datagram Protocol)
  • DNS (Domain Name Server)
  • SNMP (Simple Network Management Protocol)
  • DHCP (Dynamic Host Configuration Protocol)
  • ICMP (Internet Control Message Protocol) for network maintenance and debugging
  • IGMP (Internet Group Management Protocol) for multicast traffic management
  • PPP (Point to Point Protocol)
  • PPPoE (Point to Point Protocol over Ethernet)

lwIP provides three types of APIs

  • Raw API
  • netcon API
  • BSD Socket API

 

the first one is aimed at performance and code size , the remaining two are aimed at usability and portability . Raw API doesn’t need a OS while the other two requires a OS

We will use Socket API for this blog , list of methods available in socket API

lwip bsd socket api
lwip bsd socket api

How to send data remote host

In order to  send some data to remote site, we will simulate a  TCP server, running on a remote host and receive data sent by TCP client. Our TCP client runs on ESP-32  and TCP server runs on windows machine (the server is Nodejs based,it can run on any platform with nodejs support) . ESP32 sends a text message  (“HelloTCPServer” ) to server, in response server sends “Hello TCP Client”

ESP32 TCP Client
ESP32 TCP Client

Program :

All the code is available on this github repo. First we connect ESP32 to a WLAN, you can see more about it here. Then on a separate task we will start the TCP client.  Sending a message to server involves following steps

open a socket

    socket(domain, type, protocol)

The first parameter is communication domain for which we are creating the domain. Second parameter is socket type it can be SOCK_STREAM/SOCK_DGRAM/SOCK_SEQPACKET. Third parameter specifies the protocol (if we pass 0, lib will select the appropriate protocol)

Connect to remote host

    connect(socket,address,address_lenght)

to connect to a remote host,  you pass the socket to be used, the address and length of the sockaddr structure to connect method

write to socket

    write(socket,message,message_len)

write method accepts socket, message and length of the message (in the same order). It writes the given message to the socket

read from the socket

    read(socket,buffer,size_of_buffer)

read function, read available data from the socket and returns the number bytes written to buffer

close the socket

    close(socket)

closed the given socket

How to run :

In the tcp_client.c , put your network settings and your machine address where you will be running the TCP server program.

To start the server run the following command

node nodejs_tcp_server.js

you should see the following on your consoles

Nodejs TCP Server
Nodejs TCP Server

On serial monitor, you can see the following..It prints the message received from server

ESP32 TCP Client
ESP32 TCP Client

 

Note : Seems ESP IDF doesn’t support use of lwip RAW API. Please refer this forum post for more details. So that means we can only use the BSD Socket API and netcon API

20 Comments

Add a Comment

Your email address will not be published. Required fields are marked *