ESP8266 NodeMCU TCP Socket Server Arduino Example

In this tutorial we are making ESP8266 NodeMCU as TCP Socket Server for bidirectional communication. Before we start directly on Socket programming let’s have some understanding of what is TCP Socket.

TCP Socket

Socket communication is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server. Low level communication is used in most of the industrial applications and for continuous data transfer.

Whats the difference between HTTP and TCP communication ?

TCP is a transfer protocol. HTTP is built on top of TCP, meaning that every HTTP request uses TCP as its transfer protocol. Other application protocols can use TCP as their transport protocol as well. See the OSI model for more on the theory of network protocols. The basic idea is that TCP (a transfer protocol) is lower-level than HTTP (an application protocol which happens to use TCP).

Every HTTP server you create is necessarily also a TCP server (in the sense that it is a server using TCP as its transfer protocol).

ESP8266 NodeMCU Code for TCP Socket Server

Enter Your WiFi SSID and Password and upload code to NodeMCU

Testing

Upload above code and open serial monitor first to get the IP address of ESP8266. Then Open Telnet program to test the communication.

Step 1: Get IP from serial monitor and Connect to ESP using telnet [192.168.xx.xx IP] [port]

NodeMCU TCP Socket Server

Step 2: Observe serial monitor

Step 3: Send Some data from telnet

Step 4: Send some data from serial monitor

Advantages of using TCP Socket programming

As noted above, the primary purpose of TCP is to provide reliable logical circuit or connection service between pairs of processes. It does not assume reliability from the lower-level protocols (such as IP) so TCP must guarantee this itself.

TCP can be characterized by the following facilities it provides for the applications using it:

Stream Data Transfer
From the application’s viewpoint, TCP transfers a contiguous stream of bytes through the internet. The application does not have to bother with chopping the data into basic blocks or datagrams. TCP does this by grouping the bytes in TCP segments, which are passed to IP for transmission to the destination. Also, TCP itself decides how to segment the data and it may forward the data at its own convenience.

Sometimes, an application needs to be sure that all the data passed to TCP has actually been transmitted to the destination. For that reason, a push function is defined. It will push all remaining TCP segments still in storage to the destination host. The normal close connection function also pushes the data to the destination.

Reliability
TCP assigns a sequence number to each byte transmitted, and expects a positive acknowledgment (ACK) from the receiving TCP. If the ACK is not received within a timeout interval, the data is retransmitted. As the data is transmitted in blocks (TCP segments) only the sequence number of the first data byte in the segment is sent to the destination host.

The receiving TCP uses the sequence numbers to rearrange the segments when they arrive out of order, and to eliminate duplicate segments.

Flow Control
The receiving TCP, when sending an ACK back to the sender, also indicates to the sender the number of bytes it can receive beyond the last received TCP segment, without causing overrun and overflow in its internal buffers. This is sent in the ACK in the form of the highest sequence number it can receive without problems. This mechanism is also referred to as a window-mechanism.

Multiplexing
Is achieved through the use of ports, just as with UDP.

Logical Connections
The reliability and flow control mechanisms described above require that TCP initializes and maintains certain status information for each data stream”. The combination of this status, including sockets, sequence numbers and window sizes, is called a logical connection. Each connection is uniquely identified by the pair of sockets used by the sending and receiving processes.

Full Duplex
TCP provides for concurrent data streams in both directions.

Leave a Reply