TCP Server on NodeMCU ESPlorer IDE

Introduction

The Transmission Control Protocol (TCP) is a standard transport layer internet protocol which used in establishing and maintaining communication between server and client.

It is widely used in IOT (Internet of Things) embedded applications, where every sensor is connected to a server and we have access to control over the internet.

NodeMCU has TCP/IP APIs available using which we can implement TCP Server/Client protocol on it.

NodeMCU has Wi-Fi functionality available on board. With this Wi-Fi functionality, NodeMCU can connect to any wi-fi network as a client or it can create a network to which other wi-fi enabled devices can connect.

To know more about NodeMCU wi-fi functionality and its modes refer to NodeMCU Wi-Fi with ESPlorer

NodeMCU as TCP Server using Wi-Fi AP mode

NodeMCU wi-fi has Access Point (AP) mode through which it can create Wireless LAN to which any wi-fi enabled device can connect as shown in the below figure.

NodeMCU as TCP Server using Wi-Fi AP mode

NodeMCU as TCP Server using Wi-Fi AP mode

We can set SSID and Password for AP mode which will be used to authenticate other devices while connecting to it. NodeMCU provides network APIs to use it as a TCP server. Refer to NodeMCU Net module to know the functions that can be used to set wi-fi station and access point mode for NodeMCU.

NodeMCU as TCP Server using Wi-Fi STA mode

NodeMCU has Station (STA) mode using which it can connect to the existing wi-fi network and act as a TCP server with an IP address assigned by that network.

NodeMCU as TCP Server using Wi-Fi STA mode

NodeMCU as TCP Server using Wi-Fi STA mode

NodeMCU gets an IP address from the Wi-Fi router to which it is connected. With this IP address, it can act as a TCP server to which any wi-fi device can connect within that network.

Example

Let’s write a Lua script to enable NodeMCU as a TCP server with Wi-Fi STA/Wi-Fi AP mode and control an LED connected at the server-side from the client-side.

Here we have connected LED to the pin no. 2 i.e. D2 pin on the NodeMCU board as shown in the below figure.

NodeMCU LED connection

In Wi-Fi Access Point (AP) mode, NodeMCU creates a server hence we can set its IP address, IP netmask, and IP gateway.

 

Let’s take below SSID, Password to join the network and addresses for AP mode

  • SSID = “NodeMCU”
  • Password = “12345678”
  • IP = "192.168.2.1"
  • Netmask = "255.255.255.0"
  • Gateway = "192.168.2.1"

Refer NodeMCU Wi-Fi with ESPlorer to know the wi-fi APIs used to set the above parameters.

Lua Script for TCP server with wi-fi AP mode

wifi.setmode(wifi.SOFTAP)
config = {}
config.ssid = "NodeMCU"
config.pwd = "12345678"
wifi.ap.config(config)

config_ip = {}
config_ip.ip = "192.168.2.1"
config_ip.netmask = "255.255.255.0"
config_ip.gateway = "192.168.2.1"
wifi.ap.setip(config_ip)


LEDpin = 2
gpio.mode(LEDpin, gpio.OUTPUT)
server = net.createServer(net.TCP, 120)

function receiver(sck, data)
  if string.find(data, "LED ON")  then
   sck:send("\r\nLED ON")
   gpio.write(LEDpin, gpio.HIGH)
  elseif string.find(data, "LED OFF")  then
   sck:send("\r\nLED OFF")
   gpio.write(LEDpin, gpio.LOW)
  elseif string.find(data, "EXIT")  then
   sck:close()
  else
   sck:send("\r\nCommand Not Found...!!!")
  end
end

if server then
  server:listen(80, function(conn)
  conn:on("receive", receiver)
  conn:send("Hello Client\r\n")
  conn:send("1. Send 'LED ON' command to ON LED\r\n")
  conn:send("2. Send 'LED OFF' command to OFF LED\r\n")
  conn:send("3. Send 'EXIT' command to Exit\r\n")
  end)
end

 

Refer to "getting started with NodeMCU using ESPlorer" to know about how to write Lua script for NodeMCU

Now after uploading the above script to NodeMCU, search for wifi network with SSID (mentioned in the script) from wifi enabled device (e.g. mobile or PC). Connect to it with a given password (mentioned in the script).

Download any TCP Client application (e.g. Tera Term for PC), enter the IP address and port (mentioned in the script), and connect. See video demonstration at the end of this document for how to connect with NodeMCU TCP Server from TCP Client application.

We can also make NodeMCU TCP Server inWi-Fi Station (STA) mode, where NodeMCU is connected to the access point (Gateway Point). Through this access point, TCP Clients can connect to the NodeMCU TCP Server. NodeMCU IP addresses can be obtained from the Wi-Fi router (access point). Here only SSID and password of the access point is required for the NodeMCU TCP server to connect with it.

Lua Script for TCP server with wi-fi STA mode

station_cfg={}
station_cfg.ssid="ssid"     -- Enter SSID here
station_cfg.pwd="password"  -- Enter password here


wifi.setmode(wifi.STATION)  -- set wi-fi mode to station
wifi.sta.config(station_cfg)-- set ssid&pwd to config
wifi.sta.connect()          -- connect to router

LEDpin = 2                  -- declare LED pin
gpio.mode(LEDpin, gpio.OUTPUT)
server = net.createServer(net.TCP, 120)-- create TCP server

function receiver(sck, data)-- process callback on recive data from client
  if string.find(data, "LED ON")  then
   sck:send("\r\nLED ON")       
   gpio.write(LEDpin, gpio.HIGH)
  elseif string.find(data, "LED OFF")  then
   sck:send("\r\nLED OFF")
   gpio.write(LEDpin, gpio.LOW)
  elseif string.find(data, "EXIT")  then
   sck:close()
  else
   sck:send("\r\nCommand Not Found...!!!")
  end
end

if server then
  server:listen(80, function(conn)-- listen to the port 80
  conn:on("receive", receiver)
  conn:send("Hello Client\r\n")
  conn:send("1. Send 'LED ON' command to ON LED\r\n")
  conn:send("2. Send 'LED OFF' command to OFF LED\r\n")
  conn:send("3. Send 'EXIT' command to Exit\r\n")
  end)
end

 

Video


Components Used

NodeMCU
NodeMCUNodeMCU
1
ESP12F
ESP12E
1

Downloads

NodeMCU TCP Server Lua scripts Download
Ad