The ESP32 NodeMCU Module is a versatile development board known for its robust capabilities in IoT projects. With built-in Wi-Fi and Bluetooth connectivity, it offers seamless integration into networked environments. Its compact size and powerful features make it a preferred choice for various IoT applications, from home automation to industrial monitoring. here will discuss ESP32 NodeMCU Net Module.
Read more: ESP32 NodeMCU Module – NetContents
Functions
Function | Description |
---|---|
net.createConnection() | Creates a client. |
net.createServer() | Creates a server. |
net.createUDPSocket() | Creates a UDP socket. |
net.multicastJoin() | Joins a multicast group. |
net.multicastLeave() | Leaves a multicast group. |
net.server:close() | Closes the server. |
net.server:listen() | Listens on a port from an IP address. |
net.server:getaddr() | Returns the server’s local address and port. |
net.socket:close() | Closes a socket. |
net.socket:connect() | Connects to a remote server. |
net.socket:dns() | Provides DNS resolution for a hostname. |
net.socket:getpeer() | Retrieves the port and IP of the remote peer. |
net.socket:getaddr() | Retrieves the local port and IP of the socket. |
net.socket:hold() | Throttles data reception by placing a request to block the TCP receive function. |
net.socket:on() | Registers callback functions for specific events. |
net.socket:send() | Sends data to the remote peer. |
net.socket:ttl() | Changes or retrieves the Time-To-Live value on the socket. |
net.socket:unhold() | Unblocks TCP receiving data by revoking a preceding hold(). |
net.udpsocket:close() | Closes a UDP socket. |
net.udpsocket:listen() | Listens on a port from an IP address. |
net.udpsocket:on() | Registers callback functions for specific events. |
net.udpsocket:send() | Sends data to a specific remote peer. |
net.udpsocket:dns() | Provides DNS resolution for a hostname. |
net.udpsocket:getaddr() | Retrieves the local port and IP of the socket. |
net.udpsocket:ttl() | Changes or retrieves the Time-To-Live value on the socket. |
net.dns.getdnsserver() | Gets the IP address of the DNS server used to resolve hostnames. |
net.dns.resolve() | Resolves a hostname to an IP address. |
net.dns.setdnsserver() | Sets the IP of the DNS server used to resolve hostnames. |
Ex : Create a server Connection
The function net.createServer([type[, timeout]])
is used to create a server with an optional timeout parameter. When you specify a timeout value, it determines the duration for which the server will wait for activity from a client before considering it inactive and closing the connection.
For example, if you create a TCP server with a timeout of 30 seconds using
-- 30s time out for a inactive client net.createServer(net.TCP, 30) -- 30s timeout
it means that if a client connects to this server but does not send any data within 30 seconds, the server will automatically close the connection to that client.
This timeout functionality is useful for managing server resources efficiently. It allows servers to automatically clean up inactive connections, preventing them from occupying resources indefinitely. In scenarios where maintaining active connections is crucial, such as in real-time communication applications, setting a timeout ensures that resources are available for active clients and that inactive ones do not unnecessarily consume server resources.
Ex : Receive function
function receiver(sck, data) print(data) sck:close() end
Ex : Web server function
-- server listens on 80, if data received, print data to console and send "hello world" back to caller if sv then sv:listen(80, function(conn) conn:on("receive", receiver) conn:send("Arunworld") end) end
TCP Server Connection(Client)
here the code for ESP32 NodeMCU Net Module example
- Connect the WiFi Station
- Once got IP address then run Server_Run() Function
- Create a connection
- Connect the server
- Send the Data “ArunEwolrd IOT Project” to Server
print("wifi init") wifi.start() wifi.mode(wifi.STATION) --connect to Access Point (DO NOT save config to flash) station_cfg={} station_cfg.ssid="ArunEwolrd" station_cfg.pwd="Arun" wifi.sta.config(station_cfg) wifi.sta.connect() function Server_Run() print("Server Running") srv = net.createConnection(net.TCP, 0) srv:on("receive", function(sck, c) print(c) end) -- Wait for connection before sending. srv:on("connection", function(sck, c) sck:send("ArunEworld IOT Projects") end) srv:connect(80,"192.168.4.1") end --register callback wifi.sta.on("connected", function(ev, info) print("Connected to Router") print("NodeMCU IP Connected ssid", info.ssid, "bssid", info.bssid, "Channel", info.channel, "Auth", info.auth) end) --register callback wifi.sta.on("disconnected", function(ev, info) print("disconnected to Router") print("NodeMCU Disconnected ssid", info.ssid, "bssid", info.bssid, "reason", info.reason) end) --register callback wifi.sta.on("authmode_changed", function(ev, info) print("authmode_changed in Router") print("NodeMCU authmode_changed old_mode", info.old_mode, "new_mode", info.new_mode) end) --register callback wifi.sta.on("got_ip", function(ev, info) print("GOt IP") print("NodeMCU IP config:", info.ip, "netmask", info.netmask, "gw", info.gw) Server_Run() end)